1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package server 18 19import ( 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 "golang.org/x/net/context" 25 26 sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox" 27) 28 29func TestWaitSandboxStop(t *testing.T) { 30 id := "test-id" 31 for desc, test := range map[string]struct { 32 state sandboxstore.State 33 cancel bool 34 timeout time.Duration 35 expectErr bool 36 }{ 37 "should return error if timeout exceeds": { 38 state: sandboxstore.StateReady, 39 timeout: 200 * time.Millisecond, 40 expectErr: true, 41 }, 42 "should return error if context is cancelled": { 43 state: sandboxstore.StateReady, 44 timeout: time.Hour, 45 cancel: true, 46 expectErr: true, 47 }, 48 "should not return error if sandbox is stopped before timeout": { 49 state: sandboxstore.StateNotReady, 50 timeout: time.Hour, 51 expectErr: false, 52 }, 53 } { 54 c := newTestCRIService() 55 sandbox := sandboxstore.NewSandbox( 56 sandboxstore.Metadata{ID: id}, 57 sandboxstore.Status{State: test.state}, 58 ) 59 ctx := context.Background() 60 if test.cancel { 61 cancelledCtx, cancel := context.WithCancel(ctx) 62 cancel() 63 ctx = cancelledCtx 64 } 65 if test.timeout > 0 { 66 timeoutCtx, cancel := context.WithTimeout(ctx, test.timeout) 67 defer cancel() 68 ctx = timeoutCtx 69 } 70 err := c.waitSandboxStop(ctx, sandbox) 71 assert.Equal(t, test.expectErr, err != nil, desc) 72 } 73} 74