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) {
|
2024-10-13 12:48:47 -04:00
|
|
|
tests := []struct {
|
2017-12-19 18:36:50 -05:00
|
|
|
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}
|
2024-10-13 12:48:47 -04:00
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.cid, func(t *testing.T) {
|
|
|
|
statusC := waitExitOrRemoved(context.Background(), client, tc.cid, true)
|
|
|
|
exitCode := <-statusC
|
|
|
|
assert.Check(t, is.Equal(tc.exitCode, exitCode))
|
|
|
|
})
|
2017-12-19 18:36:50 -05:00
|
|
|
}
|
|
|
|
}
|