-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #111 from keloyang/attach-restarting-check
Add a restarting check to runAttach
- Loading branch information
Showing
7 changed files
with
141 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package container | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/docker/cli/cli/internal/test" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/pkg/testutil" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func TestNewAttachCommandErrors(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
args []string | ||
expectedError string | ||
containerInspectFunc func(img string) (types.ContainerJSON, error) | ||
}{ | ||
{ | ||
name: "client-error", | ||
args: []string{"5cb5bb5e4a3b"}, | ||
expectedError: "something went wrong", | ||
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { | ||
return types.ContainerJSON{}, errors.Errorf("something went wrong") | ||
}, | ||
}, | ||
{ | ||
name: "client-stopped", | ||
args: []string{"5cb5bb5e4a3b"}, | ||
expectedError: "You cannot attach to a stopped container", | ||
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { | ||
c := types.ContainerJSON{} | ||
c.ContainerJSONBase = &types.ContainerJSONBase{} | ||
c.ContainerJSONBase.State = &types.ContainerState{Running: false} | ||
return c, nil | ||
}, | ||
}, | ||
{ | ||
name: "client-paused", | ||
args: []string{"5cb5bb5e4a3b"}, | ||
expectedError: "You cannot attach to a paused container", | ||
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { | ||
c := types.ContainerJSON{} | ||
c.ContainerJSONBase = &types.ContainerJSONBase{} | ||
c.ContainerJSONBase.State = &types.ContainerState{ | ||
Running: true, | ||
Paused: true, | ||
} | ||
return c, nil | ||
}, | ||
}, | ||
{ | ||
name: "client-restarting", | ||
args: []string{"5cb5bb5e4a3b"}, | ||
expectedError: "You cannot attach to a restarting container", | ||
containerInspectFunc: func(containerID string) (types.ContainerJSON, error) { | ||
c := types.ContainerJSON{} | ||
c.ContainerJSONBase = &types.ContainerJSONBase{} | ||
c.ContainerJSONBase.State = &types.ContainerState{ | ||
Running: true, | ||
Paused: false, | ||
Restarting: true, | ||
} | ||
return c, nil | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
buf := new(bytes.Buffer) | ||
cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf)) | ||
cmd.SetOutput(ioutil.Discard) | ||
cmd.SetArgs(tc.args) | ||
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) | ||
} | ||
} |
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,19 @@ | ||
package container | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type fakeClient struct { | ||
client.Client | ||
containerInspectFunc func(string) (types.ContainerJSON, error) | ||
} | ||
|
||
func (cli *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) { | ||
if cli.containerInspectFunc != nil { | ||
return cli.containerInspectFunc(containerID) | ||
} | ||
return types.ContainerJSON{}, nil | ||
} |
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
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