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