@@ -55,3 +55,44 @@ func TestContainerRestart(t *testing.T) {
5555 require .NoError (t , err )
5656 require .NoError (t , runtimeService .StartContainer (cn ))
5757}
58+
59+ // Test to verify that, after a container fails to start due to a bad command, it can be removed
60+ // and a proper container can be created and started in its stead.
61+ func TestFailedContainerRestart (t * testing.T ) {
62+ t .Logf ("Create a pod config and run sandbox container" )
63+ sb , sbConfig := PodSandboxConfigWithCleanup (t , "sandbox1" , "restart" )
64+
65+ EnsureImageExists (t , pauseImage )
66+
67+ t .Logf ("Create a container config in a pod with a command that fails" )
68+ containerConfig := ContainerConfig (
69+ "container1" ,
70+ pauseImage ,
71+ WithCommand ("something-that-doesnt-exist" ),
72+ WithTestLabels (),
73+ WithTestAnnotations (),
74+ )
75+ cn , err := runtimeService .CreateContainer (sb , containerConfig , sbConfig )
76+ require .NoError (t , err )
77+ defer func () {
78+ assert .NoError (t , runtimeService .RemoveContainer (cn ))
79+ }()
80+ require .Error (t , runtimeService .StartContainer (cn ))
81+ defer func () {
82+ assert .NoError (t , runtimeService .StopContainer (cn , 10 ))
83+ }()
84+
85+ t .Logf ("Create the container with a proper command" )
86+ require .NoError (t , runtimeService .StopContainer (cn , 10 ))
87+ require .NoError (t , runtimeService .RemoveContainer (cn ))
88+
89+ containerConfig = ContainerConfig (
90+ "container1" ,
91+ pauseImage ,
92+ WithTestLabels (),
93+ WithTestAnnotations (),
94+ )
95+ cn , err = runtimeService .CreateContainer (sb , containerConfig , sbConfig )
96+ require .NoError (t , err )
97+ require .NoError (t , runtimeService .StartContainer (cn ))
98+ }
0 commit comments