This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #273 from adshmh/add-unit-tests-to-task-package
add unit tests to task package
- Loading branch information
Showing
9 changed files
with
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package task | ||
|
||
import ( | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/swarm" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type fakeClient struct { | ||
client.APIClient | ||
nodeInspectWithRaw func(ref string) (swarm.Node, []byte, error) | ||
serviceInspectWithRaw func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) | ||
} | ||
|
||
func (cli *fakeClient) NodeInspectWithRaw(ctx context.Context, ref string) (swarm.Node, []byte, error) { | ||
if cli.nodeInspectWithRaw != nil { | ||
return cli.nodeInspectWithRaw(ref) | ||
} | ||
return swarm.Node{}, nil, nil | ||
} | ||
|
||
func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) { | ||
if cli.serviceInspectWithRaw != nil { | ||
return cli.serviceInspectWithRaw(ref, options) | ||
} | ||
return swarm.Service{}, nil, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package task | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
|
||
"github.com/docker/cli/cli/command/formatter" | ||
"github.com/docker/cli/cli/command/idresolver" | ||
"github.com/docker/cli/cli/internal/test" | ||
"golang.org/x/net/context" | ||
// Import builders to get the builder function as package function | ||
. "github.com/docker/cli/cli/internal/test/builders" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/swarm" | ||
"github.com/docker/docker/pkg/testutil" | ||
"github.com/docker/docker/pkg/testutil/golden" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTaskPrintWithQuietOption(t *testing.T) { | ||
quiet := true | ||
trunc := false | ||
noResolve := true | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task(TaskID("id-foo")), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey) | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-quiet-option.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} | ||
|
||
func TestTaskPrintWithNoTruncOption(t *testing.T) { | ||
quiet := false | ||
trunc := false | ||
noResolve := true | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .ID }}") | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-no-trunc-option.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} | ||
|
||
func TestTaskPrintWithGlobalService(t *testing.T) { | ||
quiet := false | ||
trunc := false | ||
noResolve := true | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}") | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-global-service.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} | ||
|
||
func TestTaskPrintWithReplicatedService(t *testing.T) { | ||
quiet := false | ||
trunc := false | ||
noResolve := true | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}") | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-replicated-service.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} | ||
|
||
func TestTaskPrintWithIndentation(t *testing.T) { | ||
quiet := false | ||
trunc := false | ||
noResolve := false | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{ | ||
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) { | ||
return *Service(ServiceName("service-name-foo")), nil, nil | ||
}, | ||
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { | ||
return *Node(NodeName("node-name-bar")), nil, nil | ||
}, | ||
} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task( | ||
TaskID("id-foo"), | ||
TaskServiceID("service-id-foo"), | ||
TaskNodeID("id-node"), | ||
WithTaskSpec(TaskImage("myimage:mytag")), | ||
TaskDesiredState(swarm.TaskStateReady), | ||
WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))), | ||
), | ||
*Task( | ||
TaskID("id-bar"), | ||
TaskServiceID("service-id-foo"), | ||
TaskNodeID("id-node"), | ||
WithTaskSpec(TaskImage("myimage:mytag")), | ||
TaskDesiredState(swarm.TaskStateReady), | ||
WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))), | ||
), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey) | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-indentation.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} | ||
|
||
func TestTaskPrintWithResolution(t *testing.T) { | ||
quiet := false | ||
trunc := false | ||
noResolve := false | ||
buf := new(bytes.Buffer) | ||
apiClient := &fakeClient{ | ||
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) { | ||
return *Service(ServiceName("service-name-foo")), nil, nil | ||
}, | ||
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { | ||
return *Node(NodeName("node-name-bar")), nil, nil | ||
}, | ||
} | ||
cli := test.NewFakeCli(apiClient, buf) | ||
tasks := []swarm.Task{ | ||
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)), | ||
} | ||
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }} {{ .Node }}") | ||
assert.NoError(t, err) | ||
actual := buf.String() | ||
expected := golden.Get(t, []byte(actual), "task-print-with-resolution.golden") | ||
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) | ||
} |
1 change: 1 addition & 0 deletions
1
cli/command/task/testdata/task-print-with-global-service.golden
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 @@ | ||
service-id-foo.node-id-bar |
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,3 @@ | ||
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS | ||
id-foo service-name-foo.1 myimage:mytag node-name-bar Ready Failed 2 hours ago | ||
id-bar \_ service-name-foo.1 myimage:mytag node-name-bar Ready Failed 2 hours ago |
1 change: 1 addition & 0 deletions
1
cli/command/task/testdata/task-print-with-no-trunc-option.golden
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 @@ | ||
id-foo-yov6omdek8fg3k5stosyp2m50 |
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 @@ | ||
id-foo |
1 change: 1 addition & 0 deletions
1
cli/command/task/testdata/task-print-with-replicated-service.golden
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 @@ | ||
service-id-foo.1 |
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 @@ | ||
service-name-foo.1 node-name-bar |
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