forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request docker#5482 from thaJeztah/restart_stop_unit_tests
cli/command/container: add unit tests for container restart and container stop
- Loading branch information
Showing
4 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"sort" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/docker/cli/internal/test" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/errdefs" | ||
"github.com/pkg/errors" | ||
"gotest.tools/v3/assert" | ||
is "gotest.tools/v3/assert/cmp" | ||
) | ||
|
||
func TestRestart(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
args []string | ||
restarted []string | ||
expectedOpts container.StopOptions | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "without options", | ||
args: []string{"container-1", "container-2"}, | ||
restarted: []string{"container-1", "container-2"}, | ||
}, | ||
{ | ||
name: "with unknown container", | ||
args: []string{"container-1", "nosuchcontainer", "container-2"}, | ||
expectedErr: "no such container", | ||
restarted: []string{"container-1", "container-2"}, | ||
}, | ||
{ | ||
name: "with -t", | ||
args: []string{"-t", "2", "container-1"}, | ||
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)}, | ||
restarted: []string{"container-1"}, | ||
}, | ||
{ | ||
name: "with --time", | ||
args: []string{"--time", "2", "container-1"}, | ||
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)}, | ||
restarted: []string{"container-1"}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
var restarted []string | ||
mutex := new(sync.Mutex) | ||
|
||
cli := test.NewFakeCli(&fakeClient{ | ||
containerRestartFunc: func(ctx context.Context, containerID string, options container.StopOptions) error { | ||
assert.Check(t, is.DeepEqual(options, tc.expectedOpts)) | ||
if containerID == "nosuchcontainer" { | ||
return errdefs.NotFound(errors.New("Error: no such container: " + containerID)) | ||
} | ||
|
||
// TODO(thaJeztah): consider using parallelOperation for restart, similar to "stop" and "remove" | ||
mutex.Lock() | ||
restarted = append(restarted, containerID) | ||
mutex.Unlock() | ||
return nil | ||
}, | ||
Version: "1.36", | ||
}) | ||
cmd := NewRestartCommand(cli) | ||
cmd.SetOut(io.Discard) | ||
cmd.SetErr(io.Discard) | ||
cmd.SetArgs(tc.args) | ||
|
||
err := cmd.Execute() | ||
if tc.expectedErr != "" { | ||
assert.Check(t, is.ErrorContains(err, tc.expectedErr)) | ||
} else { | ||
assert.Check(t, is.Nil(err)) | ||
} | ||
sort.Strings(restarted) | ||
assert.Check(t, is.DeepEqual(restarted, tc.restarted)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"sort" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/docker/cli/internal/test" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/errdefs" | ||
"github.com/pkg/errors" | ||
"gotest.tools/v3/assert" | ||
is "gotest.tools/v3/assert/cmp" | ||
) | ||
|
||
func TestStop(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
args []string | ||
stopped []string | ||
expectedOpts container.StopOptions | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "without options", | ||
args: []string{"container-1", "container-2"}, | ||
stopped: []string{"container-1", "container-2"}, | ||
}, | ||
{ | ||
name: "with unknown container", | ||
args: []string{"container-1", "nosuchcontainer", "container-2"}, | ||
expectedErr: "no such container", | ||
stopped: []string{"container-1", "container-2"}, | ||
}, | ||
{ | ||
name: "with -t", | ||
args: []string{"-t", "2", "container-1"}, | ||
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)}, | ||
stopped: []string{"container-1"}, | ||
}, | ||
{ | ||
name: "with --time", | ||
args: []string{"--time", "2", "container-1"}, | ||
expectedOpts: container.StopOptions{Timeout: func(to int) *int { return &to }(2)}, | ||
stopped: []string{"container-1"}, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
var stopped []string | ||
mutex := new(sync.Mutex) | ||
|
||
cli := test.NewFakeCli(&fakeClient{ | ||
containerStopFunc: func(ctx context.Context, containerID string, options container.StopOptions) error { | ||
assert.Check(t, is.DeepEqual(options, tc.expectedOpts)) | ||
if containerID == "nosuchcontainer" { | ||
return errdefs.NotFound(errors.New("Error: no such container: " + containerID)) | ||
} | ||
|
||
// containerStopFunc is called in parallel for each container | ||
// so append must be synchronized. | ||
mutex.Lock() | ||
stopped = append(stopped, containerID) | ||
mutex.Unlock() | ||
return nil | ||
}, | ||
Version: "1.36", | ||
}) | ||
cmd := NewStopCommand(cli) | ||
cmd.SetOut(io.Discard) | ||
cmd.SetErr(io.Discard) | ||
cmd.SetArgs(tc.args) | ||
|
||
err := cmd.Execute() | ||
if tc.expectedErr != "" { | ||
assert.Check(t, is.ErrorContains(err, tc.expectedErr)) | ||
} else { | ||
assert.Check(t, is.Nil(err)) | ||
} | ||
sort.Strings(stopped) | ||
assert.Check(t, is.DeepEqual(stopped, tc.stopped)) | ||
}) | ||
} | ||
} |