2017-12-19 18:36:50 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2023-07-19 08:34:07 -04:00
|
|
|
"fmt"
|
2017-12-19 18:36:50 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-12-19 18:36:50 -05:00
|
|
|
)
|
|
|
|
|
2022-04-29 13:26:50 -04:00
|
|
|
func waitFn(cid string) (<-chan container.WaitResponse, <-chan error) {
|
|
|
|
resC := make(chan container.WaitResponse)
|
2017-12-19 18:36:50 -05:00
|
|
|
errC := make(chan error, 1)
|
2022-04-29 13:26:50 -04:00
|
|
|
var res container.WaitResponse
|
2017-12-19 18:36:50 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(cid, "exit-code-42"):
|
|
|
|
res.StatusCode = 42
|
|
|
|
resC <- res
|
|
|
|
case strings.Contains(cid, "non-existent"):
|
2023-07-19 08:34:07 -04:00
|
|
|
err := fmt.Errorf("no such container: %v", cid)
|
2017-12-19 18:36:50 -05:00
|
|
|
errC <- err
|
|
|
|
case strings.Contains(cid, "wait-error"):
|
2022-04-29 13:26:50 -04:00
|
|
|
res.Error = &container.WaitExitError{Message: "removal failed"}
|
2017-12-19 18:36:50 -05:00
|
|
|
resC <- res
|
|
|
|
default:
|
|
|
|
// normal exit
|
|
|
|
resC <- res
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resC, errC
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitExitOrRemoved(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
cid string
|
|
|
|
exitCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cid: "normal-container",
|
|
|
|
exitCode: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "give-me-exit-code-42",
|
|
|
|
exitCode: 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "i-want-a-wait-error",
|
|
|
|
exitCode: 125,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "non-existent-container-id",
|
|
|
|
exitCode: 125,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-19 08:34:07 -04:00
|
|
|
client := &fakeClient{waitFunc: waitFn, Version: api.DefaultVersion}
|
2017-12-19 18:36:50 -05:00
|
|
|
for _, testcase := range testcases {
|
|
|
|
statusC := waitExitOrRemoved(context.Background(), client, testcase.cid, true)
|
|
|
|
exitCode := <-statusC
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.exitCode, exitCode))
|
2017-12-19 18:36:50 -05:00
|
|
|
}
|
|
|
|
}
|