github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/container/utils_test.go (about) 1 package container 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/docker/api" 10 "github.com/docker/docker/api/types/container" 11 "github.com/pkg/errors" 12 "gotest.tools/v3/assert" 13 is "gotest.tools/v3/assert/cmp" 14 ) 15 16 func waitFn(cid string) (<-chan container.ContainerWaitOKBody, <-chan error) { 17 resC := make(chan container.ContainerWaitOKBody) 18 errC := make(chan error, 1) 19 var res container.ContainerWaitOKBody 20 21 go func() { 22 switch { 23 case strings.Contains(cid, "exit-code-42"): 24 res.StatusCode = 42 25 resC <- res 26 case strings.Contains(cid, "non-existent"): 27 err := errors.Errorf("No such container: %v", cid) 28 errC <- err 29 case strings.Contains(cid, "wait-error"): 30 res.Error = &container.ContainerWaitOKBodyError{Message: "removal failed"} 31 resC <- res 32 default: 33 // normal exit 34 resC <- res 35 } 36 }() 37 38 return resC, errC 39 } 40 41 func TestWaitExitOrRemoved(t *testing.T) { 42 testcases := []struct { 43 cid string 44 exitCode int 45 }{ 46 { 47 cid: "normal-container", 48 exitCode: 0, 49 }, 50 { 51 cid: "give-me-exit-code-42", 52 exitCode: 42, 53 }, 54 { 55 cid: "i-want-a-wait-error", 56 exitCode: 125, 57 }, 58 { 59 cid: "non-existent-container-id", 60 exitCode: 125, 61 }, 62 } 63 64 client := test.NewFakeCli(&fakeClient{waitFunc: waitFn, Version: api.DefaultVersion}) 65 for _, testcase := range testcases { 66 statusC := waitExitOrRemoved(context.Background(), client, testcase.cid, true) 67 exitCode := <-statusC 68 assert.Check(t, is.Equal(testcase.exitCode, exitCode)) 69 } 70 }