Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e0cdeb4c
Commit
e0cdeb4c
authored
Nov 18, 2016
by
Random-Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use netexec container in http lifecycle hook test.
parent
70296869
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
147 additions
and
74 deletions
+147
-74
pods.go
test/e2e/framework/pods.go
+18
-0
lifecycle_hook_test.go
test/e2e_node/lifecycle_hook_test.go
+129
-74
No files found.
test/e2e/framework/pods.go
View file @
e0cdeb4c
...
@@ -18,6 +18,7 @@ package framework
...
@@ -18,6 +18,7 @@ package framework
import
(
import
(
"fmt"
"fmt"
"regexp"
"sync"
"sync"
"time"
"time"
...
@@ -173,3 +174,20 @@ func (c *PodClient) WaitForSuccess(name string, timeout time.Duration) {
...
@@ -173,3 +174,20 @@ func (c *PodClient) WaitForSuccess(name string, timeout time.Duration) {
},
},
))
.
To
(
Succeed
(),
"wait for pod %q to success"
,
name
)
))
.
To
(
Succeed
(),
"wait for pod %q to success"
,
name
)
}
}
// MatchContainerOutput gest output of a container and match expected regexp in the output.
func
(
c
*
PodClient
)
MatchContainerOutput
(
name
string
,
containerName
string
,
expectedRegexp
string
)
error
{
f
:=
c
.
f
output
,
err
:=
GetPodLogs
(
f
.
ClientSet
,
f
.
Namespace
.
Name
,
name
,
containerName
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to get output for container %q of pod %q"
,
containerName
,
name
)
}
regex
,
err
:=
regexp
.
Compile
(
expectedRegexp
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to compile regexp %q: %v"
,
expectedRegexp
,
err
)
}
if
!
regex
.
MatchString
(
output
)
{
return
fmt
.
Errorf
(
"failed to match regexp %q in output %q"
,
expectedRegexp
,
output
)
}
return
nil
}
test/e2e_node/lifecycle_hook_test.go
View file @
e0cdeb4c
...
@@ -26,53 +26,61 @@ import (
...
@@ -26,53 +26,61 @@ import (
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/framework"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
)
)
var
_
=
framework
.
KubeDescribe
(
"Container Lifecycle Hook"
,
func
()
{
var
_
=
framework
.
KubeDescribe
(
"Container Lifecycle Hook"
,
func
()
{
f
:=
framework
.
NewDefaultFramework
(
"container-lifecycle-hook"
)
f
:=
framework
.
NewDefaultFramework
(
"container-lifecycle-hook"
)
var
podClient
*
framework
.
PodClient
var
podClient
*
framework
.
PodClient
var
file
string
const
(
const
podWaitTimeout
=
2
*
time
.
Minute
podCheckInterval
=
1
*
time
.
Second
podWaitTimeout
=
2
*
time
.
Minute
testPodWithHook
:=
func
(
podWithHook
*
api
.
Pod
)
{
postStartWaitTimeout
=
2
*
time
.
Minute
podCheckHook
:=
getLifecycleHookTestPod
(
"pod-check-hook"
,
preStopWaitTimeout
=
30
*
time
.
Second
// Wait until the file is created.
)
[]
string
{
"sh"
,
"-c"
,
fmt
.
Sprintf
(
"while [ ! -e %s ]; do sleep 1; done"
,
file
)},
)
By
(
"create the pod with lifecycle hook"
)
podClient
.
CreateSync
(
podWithHook
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PostStart
!=
nil
{
By
(
"create the hook check pod"
)
podClient
.
Create
(
podCheckHook
)
By
(
"wait for the hook check pod to success"
)
podClient
.
WaitForSuccess
(
podCheckHook
.
Name
,
podWaitTimeout
)
}
By
(
"delete the pod with lifecycle hook"
)
podClient
.
DeleteSync
(
podWithHook
.
Name
,
api
.
NewDeleteOptions
(
15
),
podWaitTimeout
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PreStop
!=
nil
{
By
(
"create the hook check pod"
)
podClient
.
Create
(
podCheckHook
)
By
(
"wait for the prestop check pod to success"
)
podClient
.
WaitForSuccess
(
podCheckHook
.
Name
,
podWaitTimeout
)
}
}
Context
(
"when create a pod with lifecycle hook"
,
func
()
{
Context
(
"when create a pod with lifecycle hook"
,
func
()
{
BeforeEach
(
func
()
{
BeforeEach
(
func
()
{
podClient
=
f
.
PodClient
()
podClient
=
f
.
PodClient
()
file
=
"/tmp/test-"
+
string
(
uuid
.
NewUUID
())
})
AfterEach
(
func
()
{
By
(
"cleanup the temporary file created in the test."
)
cleanupPod
:=
getLifecycleHookTestPod
(
"pod-clean-up"
,
[]
string
{
"rm"
,
file
})
podClient
.
Create
(
cleanupPod
)
podClient
.
WaitForSuccess
(
cleanupPod
.
Name
,
podWaitTimeout
)
})
})
Context
(
"when it is exec hook"
,
func
()
{
Context
(
"when it is exec hook"
,
func
()
{
var
file
string
testPodWithExecHook
:=
func
(
podWithHook
*
api
.
Pod
)
{
podCheckHook
:=
getExecHookTestPod
(
"pod-check-hook"
,
// Wait until the file is created.
[]
string
{
"sh"
,
"-c"
,
fmt
.
Sprintf
(
"while [ ! -e %s ]; do sleep 1; done"
,
file
)},
)
By
(
"create the pod with lifecycle hook"
)
podClient
.
CreateSync
(
podWithHook
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PostStart
!=
nil
{
By
(
"create the hook check pod"
)
podClient
.
Create
(
podCheckHook
)
By
(
"wait for the hook check pod to success"
)
podClient
.
WaitForSuccess
(
podCheckHook
.
Name
,
postStartWaitTimeout
)
}
By
(
"delete the pod with lifecycle hook"
)
podClient
.
DeleteSync
(
podWithHook
.
Name
,
api
.
NewDeleteOptions
(
15
),
podWaitTimeout
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PreStop
!=
nil
{
By
(
"create the hook check pod"
)
podClient
.
Create
(
podCheckHook
)
By
(
"wait for the prestop check pod to success"
)
podClient
.
WaitForSuccess
(
podCheckHook
.
Name
,
preStopWaitTimeout
)
}
}
BeforeEach
(
func
()
{
file
=
"/tmp/test-"
+
string
(
uuid
.
NewUUID
())
})
AfterEach
(
func
()
{
By
(
"cleanup the temporary file created in the test."
)
cleanupPod
:=
getExecHookTestPod
(
"pod-clean-up"
,
[]
string
{
"rm"
,
file
})
podClient
.
Create
(
cleanupPod
)
podClient
.
WaitForSuccess
(
cleanupPod
.
Name
,
podWaitTimeout
)
})
It
(
"should execute poststart exec hook properly [Conformance]"
,
func
()
{
It
(
"should execute poststart exec hook properly [Conformance]"
,
func
()
{
podWithHook
:=
get
Lifecycle
HookTestPod
(
"pod-with-poststart-exec-hook"
,
podWithHook
:=
get
Exec
HookTestPod
(
"pod-with-poststart-exec-hook"
,
// Block forever
// Block forever
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
)
)
...
@@ -81,11 +89,11 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
...
@@ -81,11 +89,11 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
Exec
:
&
api
.
ExecAction
{
Command
:
[]
string
{
"touch"
,
file
}},
Exec
:
&
api
.
ExecAction
{
Command
:
[]
string
{
"touch"
,
file
}},
},
},
}
}
testPodWithHook
(
podWithHook
)
testPodWith
Exec
Hook
(
podWithHook
)
})
})
It
(
"should execute prestop exec hook properly [Conformance]"
,
func
()
{
It
(
"should execute prestop exec hook properly [Conformance]"
,
func
()
{
podWithHook
:=
get
Lifecycle
HookTestPod
(
"pod-with-prestop-exec-hook"
,
podWithHook
:=
get
Exec
HookTestPod
(
"pod-with-prestop-exec-hook"
,
// Block forever
// Block forever
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
)
)
...
@@ -94,64 +102,111 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
...
@@ -94,64 +102,111 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
Exec
:
&
api
.
ExecAction
{
Command
:
[]
string
{
"touch"
,
file
}},
Exec
:
&
api
.
ExecAction
{
Command
:
[]
string
{
"touch"
,
file
}},
},
},
}
}
testPodWithHook
(
podWithHook
)
testPodWith
Exec
Hook
(
podWithHook
)
})
})
})
})
Context
(
"when it is http hook"
,
func
()
{
Context
(
"when it is http hook"
,
func
()
{
var
targetIP
string
var
targetIP
string
BeforeEach
(
func
()
{
podHandleHookRequest
:=
&
api
.
Pod
{
By
(
"cleanup the container to handle the HTTPGet hook request."
)
ObjectMeta
:
api
.
ObjectMeta
{
podHandleHookRequest
:=
getLifecycleHookTestPod
(
"pod-handle-http-request"
,
Name
:
"pod-handle-http-request"
,
[]
string
{
"sh"
,
"-c"
,
},
// Create test file when receive request on 1234.
Spec
:
api
.
PodSpec
{
fmt
.
Sprintf
(
"echo -e
\"
HTTP/1.1 200 OK
\n\"
| nc -l -p 1234; touch %s"
,
file
),
Containers
:
[]
api
.
Container
{
},
{
)
Name
:
"pod-handle-http-request"
,
podHandleHookRequest
.
Spec
.
Containers
[
0
]
.
Ports
=
[]
api
.
ContainerPort
{
Image
:
"gcr.io/google_containers/netexec:1.7"
,
{
Ports
:
[]
api
.
ContainerPort
{
ContainerPort
:
1234
,
{
Protocol
:
api
.
ProtocolTCP
,
ContainerPort
:
8080
,
Protocol
:
api
.
ProtocolTCP
,
},
},
},
},
},
}
},
podHandleHookRequest
=
podClient
.
CreateSync
(
podHandleHookRequest
)
}
targetIP
=
podHandleHookRequest
.
Status
.
PodIP
BeforeEach
(
func
()
{
By
(
"create the container to handle the HTTPGet hook request."
)
newPod
:=
podClient
.
CreateSync
(
podHandleHookRequest
)
targetIP
=
newPod
.
Status
.
PodIP
})
})
testPodWithHttpHook
:=
func
(
podWithHook
*
api
.
Pod
)
{
By
(
"create the pod with lifecycle hook"
)
podClient
.
CreateSync
(
podWithHook
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PostStart
!=
nil
{
By
(
"check poststart hook"
)
Eventually
(
func
()
error
{
return
podClient
.
MatchContainerOutput
(
podHandleHookRequest
.
Name
,
podHandleHookRequest
.
Spec
.
Containers
[
0
]
.
Name
,
`GET /echo\?msg=poststart`
)
},
postStartWaitTimeout
,
podCheckInterval
)
.
Should
(
BeNil
())
}
By
(
"delete the pod with lifecycle hook"
)
podClient
.
DeleteSync
(
podWithHook
.
Name
,
api
.
NewDeleteOptions
(
15
),
podWaitTimeout
)
if
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
.
PreStop
!=
nil
{
By
(
"check prestop hook"
)
Eventually
(
func
()
error
{
return
podClient
.
MatchContainerOutput
(
podHandleHookRequest
.
Name
,
podHandleHookRequest
.
Spec
.
Containers
[
0
]
.
Name
,
`GET /echo\?msg=prestop`
)
},
preStopWaitTimeout
,
podCheckInterval
)
.
Should
(
BeNil
())
}
}
It
(
"should execute poststart http hook properly [Conformance]"
,
func
()
{
It
(
"should execute poststart http hook properly [Conformance]"
,
func
()
{
podWithHook
:=
getLifecycleHookTestPod
(
"pod-with-poststart-http-hook"
,
podWithHook
:=
&
api
.
Pod
{
// Block forever
ObjectMeta
:
api
.
ObjectMeta
{
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
Name
:
"pod-with-poststart-http-hook"
,
)
},
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
=
&
api
.
Lifecycle
{
Spec
:
api
.
PodSpec
{
PostStart
:
&
api
.
Handler
{
Containers
:
[]
api
.
Container
{
HTTPGet
:
&
api
.
HTTPGetAction
{
{
Host
:
targetIP
,
Name
:
"pod-with-poststart-http-hook"
,
Port
:
intstr
.
FromInt
(
1234
),
Image
:
framework
.
GetPauseImageNameForHostArch
(),
Lifecycle
:
&
api
.
Lifecycle
{
PostStart
:
&
api
.
Handler
{
HTTPGet
:
&
api
.
HTTPGetAction
{
Path
:
"/echo?msg=poststart"
,
Host
:
targetIP
,
Port
:
intstr
.
FromInt
(
8080
),
},
},
},
},
},
},
},
},
}
}
testPodWithHook
(
podWithHook
)
testPodWithH
ttpH
ook
(
podWithHook
)
})
})
It
(
"should execute prestop http hook properly [Conformance]"
,
func
()
{
It
(
"should execute prestop http hook properly [Conformance]"
,
func
()
{
podWithHook
:=
getLifecycleHookTestPod
(
"pod-with-prestop-http-hook"
,
podWithHook
:=
&
api
.
Pod
{
// Block forever
ObjectMeta
:
api
.
ObjectMeta
{
[]
string
{
"tail"
,
"-f"
,
"/dev/null"
},
Name
:
"pod-with-prestop-http-hook"
,
)
},
podWithHook
.
Spec
.
Containers
[
0
]
.
Lifecycle
=
&
api
.
Lifecycle
{
Spec
:
api
.
PodSpec
{
PreStop
:
&
api
.
Handler
{
Containers
:
[]
api
.
Container
{
HTTPGet
:
&
api
.
HTTPGetAction
{
{
Host
:
targetIP
,
Name
:
"pod-with-prestop-http-hook"
,
Port
:
intstr
.
FromInt
(
1234
),
Image
:
framework
.
GetPauseImageNameForHostArch
(),
Lifecycle
:
&
api
.
Lifecycle
{
PreStop
:
&
api
.
Handler
{
HTTPGet
:
&
api
.
HTTPGetAction
{
Path
:
"/echo?msg=prestop"
,
Host
:
targetIP
,
Port
:
intstr
.
FromInt
(
8080
),
},
},
},
},
},
},
},
},
}
}
testPodWithHook
(
podWithHook
)
testPodWithH
ttpH
ook
(
podWithHook
)
})
})
})
})
})
})
})
})
func
get
Lifecycle
HookTestPod
(
name
string
,
cmd
[]
string
)
*
api
.
Pod
{
func
get
Exec
HookTestPod
(
name
string
,
cmd
[]
string
)
*
api
.
Pod
{
return
&
api
.
Pod
{
return
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
name
,
Name
:
name
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment