@@ -67,9 +67,15 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
6767 podName , namespace := "foo" , "bar"
6868 containerName , image := "sidecar" , "logger"
6969
70+ type podInfo struct {
71+ ContainerId string
72+ SandboxID string
73+ }
74+
7075 const count = 20
7176 configs := make ([]* runtimeapi.ContainerConfig , 0 , count )
7277 sConfigs := make ([]* runtimeapi.PodSandboxConfig , 0 , count )
78+
7379 for i := 0 ; i < count ; i ++ {
7480 s := makeSandboxConfig (fmt .Sprintf ("%s%d" , podName , i ),
7581 fmt .Sprintf ("%s%d" , namespace , i ), fmt .Sprintf ("%d" , i ), 0 )
@@ -80,8 +86,8 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
8086 configs = append (configs , c )
8187 }
8288
83- containerIDs := make (
84- chan string ,
89+ podInfos := make (
90+ chan podInfo ,
8591 len (configs ),
8692 ) // make channel non-blocking to simulate concurrent containers creation
8793
@@ -94,39 +100,64 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
94100
95101 go func () {
96102 creationWg .Wait ()
97- close (containerIDs )
103+ close (podInfos )
98104 }()
99105 for i := range configs {
100106 go func (i int ) {
101107 defer creationWg .Done ()
102- // We don't care about the sandbox id; pass a bogus one.
103- sandboxID := fmt .Sprintf ("sandboxid%d" , i )
108+
109+ runSandboxResp , err := ds .RunPodSandbox (getTestCTX (), & runtimeapi.RunPodSandboxRequest {
110+ Config : sConfigs [i ],
111+ })
112+ if err != nil {
113+ t .Errorf ("RunPodSandbox: %v" , err )
114+ return
115+ }
116+
104117 req := & runtimeapi.CreateContainerRequest {
105- PodSandboxId : sandboxID ,
118+ PodSandboxId : runSandboxResp . PodSandboxId ,
106119 Config : configs [i ],
107120 SandboxConfig : sConfigs [i ],
108121 }
122+
109123 createResp , err := ds .CreateContainer (getTestCTX (), req )
110124 if err != nil {
111125 t .Errorf ("CreateContainer: %v" , err )
112126 return
113127 }
114- containerIDs <- createResp .ContainerId
128+ podInfos <- podInfo {
129+ ContainerId : createResp .ContainerId ,
130+ SandboxID : runSandboxResp .PodSandboxId ,
131+ }
115132 }(i )
116133 }
117134
118- for containerID := range containerIDs {
135+ for pod := range podInfos {
119136 deletionWg .Add (1 )
120- go func (id string ) {
137+ go func (i podInfo ) {
121138 defer deletionWg .Done ()
122139 _ , err := ds .RemoveContainer (
123140 getTestCTX (),
124- & runtimeapi.RemoveContainerRequest {ContainerId : id },
141+ & runtimeapi.RemoveContainerRequest {ContainerId : i . ContainerId },
125142 )
126143 if err != nil {
127144 t .Errorf ("RemoveContainer: %v" , err )
128145 }
129- }(containerID )
146+ _ , err = ds .StopPodSandbox (
147+ getTestCTX (),
148+ & runtimeapi.StopPodSandboxRequest {PodSandboxId : i .SandboxID },
149+ )
150+ if err != nil {
151+ t .Errorf ("StopPodSandbox: %v" , err )
152+ }
153+ _ , err = ds .RemovePodSandbox (
154+ getTestCTX (),
155+ & runtimeapi.RemovePodSandboxRequest {PodSandboxId : i .SandboxID },
156+ )
157+ if err != nil {
158+ t .Errorf ("RemovePodSandbox: %v" , err )
159+ }
160+ }(pod )
130161 }
131162 deletionWg .Wait ()
132163}
@@ -155,10 +186,15 @@ func TestListContainers(t *testing.T) {
155186 state := runtimeapi .ContainerState_CONTAINER_RUNNING
156187 var createdAt int64 = fakeClock .Now ().UnixNano ()
157188 for i := range configs {
158- // We don't care about the sandbox id; pass a bogus one.
159- sandboxID := fmt .Sprintf ("sandboxid%d" , i )
189+ runSandboxResp , err := ds .RunPodSandbox (getTestCTX (), & runtimeapi.RunPodSandboxRequest {
190+ Config : sConfigs [i ],
191+ })
192+ if err != nil {
193+ t .Errorf ("RunPodSandbox: %v" , err )
194+ return
195+ }
160196 req := & runtimeapi.CreateContainerRequest {
161- PodSandboxId : sandboxID ,
197+ PodSandboxId : runSandboxResp . PodSandboxId ,
162198 Config : configs [i ],
163199 SandboxConfig : sConfigs [i ],
164200 }
@@ -174,7 +210,7 @@ func TestListContainers(t *testing.T) {
174210 expected = append ([]* runtimeapi.Container {{
175211 Metadata : configs [i ].Metadata ,
176212 Id : id ,
177- PodSandboxId : sandboxID ,
213+ PodSandboxId : runSandboxResp . PodSandboxId ,
178214 State : state ,
179215 CreatedAt : createdAt ,
180216 Image : configs [i ].Image ,
@@ -226,12 +262,20 @@ func TestContainerStatus(t *testing.T) {
226262
227263 fDocker .InjectImages ([]dockertypes.ImageSummary {{ID : imageName }})
228264
265+ runSandboxResp , err := ds .RunPodSandbox (getTestCTX (), & runtimeapi.RunPodSandboxRequest {
266+ Config : sConfig ,
267+ })
268+ if err != nil {
269+ t .Errorf ("RunPodSandbox: %v" , err )
270+ return
271+ }
272+
229273 // Create the container.
230274 fClock .SetTime (time .Now ().Add (- 1 * time .Hour ))
231275 expected .CreatedAt = fClock .Now ().UnixNano ()
232276
233277 req := & runtimeapi.CreateContainerRequest {
234- PodSandboxId : sandboxID ,
278+ PodSandboxId : runSandboxResp . PodSandboxId ,
235279 Config : config ,
236280 SandboxConfig : sConfig ,
237281 }
@@ -243,7 +287,7 @@ func TestContainerStatus(t *testing.T) {
243287 c , err := fDocker .InspectContainer (id )
244288 require .NoError (t , err )
245289 assert .Equal (t , c .Config .Labels [containerTypeLabelKey ], containerTypeLabelContainer )
246- assert .Equal (t , c .Config .Labels [sandboxIDLabelKey ], sandboxID )
290+ assert .Equal (t , c .Config .Labels [sandboxIDLabelKey ], runSandboxResp . PodSandboxId )
247291
248292 // Set the id manually since we don't know the id until it's created.
249293 expected .Id = id
@@ -309,8 +353,16 @@ func TestContainerLogPath(t *testing.T) {
309353 config := makeContainerConfig (sConfig , "pause" , "iamimage" , 0 , nil , nil )
310354 config .LogPath = containerLogPath
311355
356+ runSandboxResp , err := ds .RunPodSandbox (getTestCTX (), & runtimeapi.RunPodSandboxRequest {
357+ Config : sConfig ,
358+ })
359+ if err != nil {
360+ t .Errorf ("RunPodSandbox: %v" , err )
361+ return
362+ }
363+
312364 req := & runtimeapi.CreateContainerRequest {
313- PodSandboxId : sandboxID ,
365+ PodSandboxId : runSandboxResp . PodSandboxId ,
314366 Config : config ,
315367 SandboxConfig : sConfig ,
316368 }
@@ -371,43 +423,54 @@ func TestContainerCreationConflict(t *testing.T) {
371423 noContainerError := fmt .Errorf ("Error response from daemon: No such container: %s" , containerID )
372424 randomError := fmt .Errorf ("random error" )
373425
426+ // sandBox run called "inspect_image", "pull", "create", "start", "inspect_container",
427+ sandBoxCalls := []string {"inspect_image" , "pull" , "create" , "start" , "inspect_container" }
374428 for desc , test := range map [string ]struct {
375429 createError error
376430 removeError error
377431 expectError error
378432 expectCalls []string
379433 expectFields int
380434 }{
435+ // sandBox run called "inspect_image", "pull", "create", "start", "inspect_container",
381436 "no create error" : {
382- expectCalls : []string {"create" },
437+ expectCalls : append ( sandBoxCalls , []string {"create" }... ) ,
383438 expectFields : 6 ,
384439 },
385440 "random create error" : {
386441 createError : randomError ,
387442 expectError : randomError ,
388- expectCalls : []string {"create" },
443+ expectCalls : append ( sandBoxCalls , []string {"create" }... ) ,
389444 },
390445 "conflict create error with successful remove" : {
391446 createError : conflictError ,
392447 expectError : conflictError ,
393- expectCalls : []string {"create" , "remove" },
448+ expectCalls : append ( sandBoxCalls , []string {"create" , "remove" }... ) ,
394449 },
395450 "conflict create error with random remove error" : {
396451 createError : conflictError ,
397452 removeError : randomError ,
398453 expectError : conflictError ,
399- expectCalls : []string {"create" , "remove" },
454+ expectCalls : append ( sandBoxCalls , []string {"create" , "remove" }... ) ,
400455 },
401456 "conflict create error with no such container remove error" : {
402457 createError : conflictError ,
403458 removeError : noContainerError ,
404- expectCalls : []string {"create" , "remove" , "create" },
459+ expectCalls : append ( sandBoxCalls , []string {"create" , "remove" , "create" }... ) ,
405460 expectFields : 7 ,
406461 },
407462 } {
408463 t .Logf ("TestCase: %s" , desc )
409464 ds , fDocker , _ := newTestDockerService ()
410465
466+ runSandboxResp , err := ds .RunPodSandbox (getTestCTX (), & runtimeapi.RunPodSandboxRequest {
467+ Config : sConfig ,
468+ })
469+ if err != nil {
470+ require .EqualError (t , err , test .expectError .Error ())
471+ continue
472+ }
473+
411474 if test .createError != nil {
412475 fDocker .InjectError ("create" , test .createError )
413476 }
@@ -416,7 +479,7 @@ func TestContainerCreationConflict(t *testing.T) {
416479 }
417480
418481 req := & runtimeapi.CreateContainerRequest {
419- PodSandboxId : sandboxID ,
482+ PodSandboxId : runSandboxResp . PodSandboxId ,
420483 Config : config ,
421484 SandboxConfig : sConfig ,
422485 }
0 commit comments