@@ -17,13 +17,11 @@ limitations under the License.
1717package integration
1818
1919import (
20- "os"
21- "os/exec"
2220 "sort"
2321 "testing"
24- "time"
2522
2623 "github.com/containerd/containerd"
24+ "github.com/containerd/containerd/errdefs"
2725 "github.com/stretchr/testify/assert"
2826 "github.com/stretchr/testify/require"
2927 "golang.org/x/net/context"
@@ -127,7 +125,9 @@ func TestContainerdRestart(t *testing.T) {
127125 task , err := cntr .Task (ctx , nil )
128126 require .NoError (t , err )
129127 _ , err = task .Delete (ctx , containerd .WithProcessKill )
130- require .NoError (t , err )
128+ if err != nil {
129+ require .True (t , errdefs .IsNotFound (err ))
130+ }
131131 }
132132 }
133133
@@ -196,149 +196,4 @@ func TestContainerdRestart(t *testing.T) {
196196 }
197197}
198198
199- // Note: The test moves runc binary.
200- // The test requires:
201- // 1) The runtime is runc;
202- // 2) runc is in PATH;
203- func TestUnknownStateAfterContainerdRestart (t * testing.T ) {
204- if * runtimeHandler != "" {
205- t .Skip ("unsupported config: runtime handler is set" )
206- }
207- runcPath , err := exec .LookPath ("runc" )
208- if err != nil {
209- t .Skip ("unsupported config: runc not in PATH" )
210- }
211-
212- sbConfig := PodSandboxConfig ("sandbox" , "sandbox-unknown-state" )
213-
214- const testImage = "busybox"
215- t .Logf ("Pull test image %q" , testImage )
216- img , err := imageService .PullImage (& runtime.ImageSpec {Image : testImage }, nil , sbConfig )
217- require .NoError (t , err )
218- defer func () {
219- assert .NoError (t , imageService .RemoveImage (& runtime.ImageSpec {Image : img }))
220- }()
221-
222- t .Log ("Should not be able to create sandbox without runc" )
223- tmpRuncPath := Randomize (runcPath )
224- require .NoError (t , os .Rename (runcPath , tmpRuncPath ))
225- defer func () {
226- os .Rename (tmpRuncPath , runcPath )
227- }()
228- sb , err := runtimeService .RunPodSandbox (sbConfig , "" )
229- if err == nil {
230- assert .NoError (t , runtimeService .StopPodSandbox (sb ))
231- assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
232- t .Skip ("unsupported config: runc is not being used" )
233- }
234- require .NoError (t , os .Rename (tmpRuncPath , runcPath ))
235-
236- t .Log ("Create a sandbox" )
237- sb , err = runtimeService .RunPodSandbox (sbConfig , "" )
238- require .NoError (t , err )
239- defer func () {
240- // Make sure the sandbox is cleaned up in any case.
241- runtimeService .StopPodSandbox (sb )
242- runtimeService .RemovePodSandbox (sb )
243- }()
244- ps , err := runtimeService .PodSandboxStatus (sb )
245- require .NoError (t , err )
246- assert .Equal (t , runtime .PodSandboxState_SANDBOX_READY , ps .GetState ())
247-
248- t .Log ("Create a container" )
249- cnConfig := ContainerConfig (
250- "container-unknown-state" ,
251- testImage ,
252- WithCommand ("sleep" , "1000" ),
253- )
254- cn , err := runtimeService .CreateContainer (sb , cnConfig , sbConfig )
255- require .NoError (t , err )
256-
257- t .Log ("Start the container" )
258- require .NoError (t , runtimeService .StartContainer (cn ))
259- cs , err := runtimeService .ContainerStatus (cn )
260- require .NoError (t , err )
261- assert .Equal (t , runtime .ContainerState_CONTAINER_RUNNING , cs .GetState ())
262-
263- t .Log ("Move runc binary, so that container/sandbox can't be loaded after restart" )
264- tmpRuncPath = Randomize (runcPath )
265- require .NoError (t , os .Rename (runcPath , tmpRuncPath ))
266- defer func () {
267- os .Rename (tmpRuncPath , runcPath )
268- }()
269-
270- t .Log ("Restart containerd" )
271- RestartContainerd (t )
272-
273- t .Log ("Sandbox should be in NOTREADY state after containerd restart" )
274- ps , err = runtimeService .PodSandboxStatus (sb )
275- require .NoError (t , err )
276- assert .Equal (t , runtime .PodSandboxState_SANDBOX_NOTREADY , ps .GetState ())
277-
278- t .Log ("Container should be in UNKNOWN state after containerd restart" )
279- cs , err = runtimeService .ContainerStatus (cn )
280- require .NoError (t , err )
281- assert .Equal (t , runtime .ContainerState_CONTAINER_UNKNOWN , cs .GetState ())
282-
283- t .Log ("Stop/remove the sandbox should fail for the lack of runc" )
284- assert .Error (t , runtimeService .StopPodSandbox (sb ))
285- assert .Error (t , runtimeService .RemovePodSandbox (sb ))
286-
287- t .Log ("Stop/remove the container should fail for the lack of runc" )
288- assert .Error (t , runtimeService .StopContainer (cn , 10 ))
289- assert .Error (t , runtimeService .RemoveContainer (cn ))
290-
291- t .Log ("Move runc back" )
292- require .NoError (t , os .Rename (tmpRuncPath , runcPath ))
293-
294- t .Log ("Sandbox should still be in NOTREADY state" )
295- ps , err = runtimeService .PodSandboxStatus (sb )
296- require .NoError (t , err )
297- assert .Equal (t , runtime .PodSandboxState_SANDBOX_NOTREADY , ps .GetState ())
298-
299- t .Log ("Container should still be in UNKNOWN state" )
300- cs , err = runtimeService .ContainerStatus (cn )
301- require .NoError (t , err )
302- assert .Equal (t , runtime .ContainerState_CONTAINER_UNKNOWN , cs .GetState ())
303-
304- t .Log ("Sandbox operations which require running state should fail" )
305- _ , err = runtimeService .PortForward (& runtime.PortForwardRequest {
306- PodSandboxId : sb ,
307- Port : []int32 {8080 },
308- })
309- assert .Error (t , err )
310-
311- t .Log ("Container operations which require running state should fail" )
312- assert .Error (t , runtimeService .ReopenContainerLog (cn ))
313- _ , _ , err = runtimeService .ExecSync (cn , []string {"ls" }, 10 * time .Second )
314- assert .Error (t , err )
315- _ , err = runtimeService .Attach (& runtime.AttachRequest {
316- ContainerId : cn ,
317- Stdin : true ,
318- Stdout : true ,
319- Stderr : true ,
320- })
321- assert .Error (t , err )
322-
323- t .Log ("Containerd should still be running now" )
324- _ , err = runtimeService .Status ()
325- require .NoError (t , err )
326-
327- t .Log ("Remove the container should fail in this state" )
328- assert .Error (t , runtimeService .RemoveContainer (cn ))
329-
330- t .Log ("Remove the sandbox should fail in this state" )
331- assert .Error (t , runtimeService .RemovePodSandbox (sb ))
332-
333- t .Log ("Should be able to stop container in this state" )
334- assert .NoError (t , runtimeService .StopContainer (cn , 10 ))
335-
336- t .Log ("Should be able to stop sandbox in this state" )
337- assert .NoError (t , runtimeService .StopPodSandbox (sb ))
338-
339- t .Log ("Should be able to remove container after stop" )
340- assert .NoError (t , runtimeService .RemoveContainer (cn ))
341-
342- t .Log ("Should be able to remove sandbox after stop" )
343- assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
344- }
199+ // TODO: Add back the unknown state test.
0 commit comments