Skip to content

Commit 8c5e85d

Browse files
committed
cli/command/swarm: minor cleanups: use Println, rename vars
- use Println to print newline instead of custom format - use apiClient instead of client for the API client to prevent shadowing imports. - use dockerCLI with Go's standard camelCase casing. - suppress some errors to make my IDE and linters happier - fix some tests to work with "go test -update" - rewrite TestSwarmInit to use sub-tests Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 925b8fe commit 8c5e85d

14 files changed

Lines changed: 92 additions & 67 deletions

cli/command/swarm/ca_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ func TestDisplayTrustRootInvalidFlags(t *testing.T) {
143143
}, nil
144144
},
145145
}))
146-
assert.Check(t, cmd.Flags().Parse(testCase.args))
146+
cmd.SetArgs([]string{})
147147
cmd.SetOut(io.Discard)
148148
cmd.SetErr(io.Discard)
149+
assert.Check(t, cmd.Flags().Parse(testCase.args))
149150
assert.ErrorContains(t, cmd.Execute(), testCase.errorMsg)
150151
}
151152
}

cli/command/swarm/init.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func newInitCommand(dockerCli command.Cli) *cobra.Command {
6565
return cmd
6666
}
6767

68-
func runInit(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts initOptions) error {
69-
client := dockerCli.Client()
68+
func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts initOptions) error {
69+
apiClient := dockerCLI.Client()
7070

7171
defaultAddrPool := make([]string, 0, len(opts.defaultAddrPools))
7272
for _, p := range opts.defaultAddrPools {
@@ -93,28 +93,28 @@ func runInit(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, o
9393
}
9494
}
9595

96-
nodeID, err := client.SwarmInit(ctx, req)
96+
nodeID, err := apiClient.SwarmInit(ctx, req)
9797
if err != nil {
9898
if strings.Contains(err.Error(), "could not choose an IP address to advertise") || strings.Contains(err.Error(), "could not find the system's IP address") {
9999
return errors.New(err.Error() + " - specify one with --advertise-addr")
100100
}
101101
return err
102102
}
103103

104-
fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
104+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
105105

106-
if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
106+
if err := printJoinCommand(ctx, dockerCLI, nodeID, true, false); err != nil {
107107
return err
108108
}
109109

110-
fmt.Fprint(dockerCli.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.\n\n")
110+
_, _ = fmt.Fprintln(dockerCLI.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.")
111111

112112
if req.AutoLockManagers {
113-
unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
113+
unlockKeyResp, err := apiClient.SwarmGetUnlockKey(ctx)
114114
if err != nil {
115115
return errors.Wrap(err, "could not fetch unlock key")
116116
}
117-
printUnlockCommand(dockerCli.Out(), unlockKeyResp.UnlockKey)
117+
printUnlockCommand(dockerCLI.Out(), unlockKeyResp.UnlockKey)
118118
}
119119

120120
return nil

cli/command/swarm/init_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
7171
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
7272
nodeInspectFunc: tc.nodeInspectFunc,
7373
}))
74-
for key, value := range tc.flags {
75-
cmd.Flags().Set(key, value)
76-
}
74+
cmd.SetArgs([]string{})
7775
cmd.SetOut(io.Discard)
7876
cmd.SetErr(io.Discard)
77+
for k, v := range tc.flags {
78+
assert.Check(t, cmd.Flags().Set(k, v))
79+
}
7980
assert.Error(t, cmd.Execute(), tc.expectedError)
8081
})
8182
}
@@ -112,17 +113,22 @@ func TestSwarmInit(t *testing.T) {
112113
},
113114
}
114115
for _, tc := range testCases {
115-
cli := test.NewFakeCli(&fakeClient{
116-
swarmInitFunc: tc.swarmInitFunc,
117-
swarmInspectFunc: tc.swarmInspectFunc,
118-
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
119-
nodeInspectFunc: tc.nodeInspectFunc,
116+
t.Run(tc.name, func(t *testing.T) {
117+
cli := test.NewFakeCli(&fakeClient{
118+
swarmInitFunc: tc.swarmInitFunc,
119+
swarmInspectFunc: tc.swarmInspectFunc,
120+
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
121+
nodeInspectFunc: tc.nodeInspectFunc,
122+
})
123+
cmd := newInitCommand(cli)
124+
cmd.SetArgs([]string{})
125+
cmd.SetOut(io.Discard)
126+
cmd.SetErr(io.Discard)
127+
for k, v := range tc.flags {
128+
assert.Check(t, cmd.Flags().Set(k, v))
129+
}
130+
assert.NilError(t, cmd.Execute())
131+
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name))
120132
})
121-
cmd := newInitCommand(cli)
122-
for key, value := range tc.flags {
123-
cmd.Flags().Set(key, value)
124-
}
125-
assert.NilError(t, cmd.Execute())
126-
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name))
127133
}
128134
}

cli/command/swarm/join_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestSwarmJoinErrors(t *testing.T) {
2323
}{
2424
{
2525
name: "not-enough-args",
26+
args: []string{},
2627
expectedError: "requires 1 argument",
2728
},
2829
{

cli/command/swarm/join_token.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,80 +41,79 @@ func newJoinTokenCommand(dockerCli command.Cli) *cobra.Command {
4141
return cmd
4242
}
4343

44-
func runJoinToken(ctx context.Context, dockerCli command.Cli, opts joinTokenOptions) error {
44+
func runJoinToken(ctx context.Context, dockerCLI command.Cli, opts joinTokenOptions) error {
4545
worker := opts.role == "worker"
4646
manager := opts.role == "manager"
4747

4848
if !worker && !manager {
4949
return errors.New("unknown role " + opts.role)
5050
}
5151

52-
client := dockerCli.Client()
52+
apiClient := dockerCLI.Client()
5353

5454
if opts.rotate {
55-
flags := swarm.UpdateFlags{
56-
RotateWorkerToken: worker,
57-
RotateManagerToken: manager,
58-
}
59-
60-
sw, err := client.SwarmInspect(ctx)
55+
sw, err := apiClient.SwarmInspect(ctx)
6156
if err != nil {
6257
return err
6358
}
6459

65-
if err := client.SwarmUpdate(ctx, sw.Version, sw.Spec, flags); err != nil {
60+
err = apiClient.SwarmUpdate(ctx, sw.Version, sw.Spec, swarm.UpdateFlags{
61+
RotateWorkerToken: worker,
62+
RotateManagerToken: manager,
63+
})
64+
if err != nil {
6665
return err
6766
}
6867

6968
if !opts.quiet {
70-
fmt.Fprintf(dockerCli.Out(), "Successfully rotated %s join token.\n\n", opts.role)
69+
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully rotated %s join token.\n\n", opts.role)
7170
}
7271
}
7372

7473
// second SwarmInspect in this function,
7574
// this is necessary since SwarmUpdate after first changes the join tokens
76-
sw, err := client.SwarmInspect(ctx)
75+
sw, err := apiClient.SwarmInspect(ctx)
7776
if err != nil {
7877
return err
7978
}
8079

8180
if opts.quiet && worker {
82-
fmt.Fprintln(dockerCli.Out(), sw.JoinTokens.Worker)
81+
_, _ = fmt.Fprintln(dockerCLI.Out(), sw.JoinTokens.Worker)
8382
return nil
8483
}
8584

8685
if opts.quiet && manager {
87-
fmt.Fprintln(dockerCli.Out(), sw.JoinTokens.Manager)
86+
_, _ = fmt.Fprintln(dockerCLI.Out(), sw.JoinTokens.Manager)
8887
return nil
8988
}
9089

91-
info, err := client.Info(ctx)
90+
info, err := apiClient.Info(ctx)
9291
if err != nil {
9392
return err
9493
}
9594

96-
return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, worker, manager)
95+
return printJoinCommand(ctx, dockerCLI, info.Swarm.NodeID, worker, manager)
9796
}
9897

99-
func printJoinCommand(ctx context.Context, dockerCli command.Cli, nodeID string, worker bool, manager bool) error {
100-
client := dockerCli.Client()
98+
func printJoinCommand(ctx context.Context, dockerCLI command.Cli, nodeID string, worker bool, manager bool) error {
99+
apiClient := dockerCLI.Client()
101100

102-
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
101+
node, _, err := apiClient.NodeInspectWithRaw(ctx, nodeID)
103102
if err != nil {
104103
return err
105104
}
106105

107-
sw, err := client.SwarmInspect(ctx)
106+
sw, err := apiClient.SwarmInspect(ctx)
108107
if err != nil {
109108
return err
110109
}
111110

112111
if node.ManagerStatus != nil {
113112
if worker {
114-
fmt.Fprintf(dockerCli.Out(), "To add a worker to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n", sw.JoinTokens.Worker, node.ManagerStatus.Addr)
113+
_, _ = fmt.Fprintf(dockerCLI.Out(), "To add a worker to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n", sw.JoinTokens.Worker, node.ManagerStatus.Addr)
115114
}
116115
if manager {
117-
fmt.Fprintf(dockerCli.Out(), "To add a manager to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n", sw.JoinTokens.Manager, node.ManagerStatus.Addr)
116+
_, _ = fmt.Fprintf(dockerCLI.Out(), "To add a manager to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n", sw.JoinTokens.Manager, node.ManagerStatus.Addr)
118117
}
119118
}
120119

cli/command/swarm/join_token_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) {
2727
}{
2828
{
2929
name: "not-enough-args",
30+
args: []string{},
3031
expectedError: "requires 1 argument",
3132
},
3233
{

cli/command/swarm/leave_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestSwarmLeaveErrors(t *testing.T) {
2525
},
2626
{
2727
name: "leave-failed",
28+
args: []string{},
2829
swarmLeaveFunc: func() error {
2930
return errors.New("error leaving the swarm")
3031
},
@@ -48,6 +49,9 @@ func TestSwarmLeaveErrors(t *testing.T) {
4849
func TestSwarmLeave(t *testing.T) {
4950
cli := test.NewFakeCli(&fakeClient{})
5051
cmd := newLeaveCommand(cli)
52+
cmd.SetArgs([]string{})
53+
cmd.SetOut(io.Discard)
54+
cmd.SetErr(io.Discard)
5155
assert.NilError(t, cmd.Execute())
5256
assert.Check(t, is.Equal("Node left the swarm.", strings.TrimSpace(cli.OutBuffer().String())))
5357
}

cli/command/swarm/testdata/init-init-autolock.golden

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Swarm initialized: current node (nodeID) is now a manager.
22

33
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
4-
54
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
65
command and provide the following key:
76

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Swarm initialized: current node (nodeID) is now a manager.
22

33
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
4-

cli/command/swarm/testdata/unlockkeys-unlock-key-rotate.golden

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Successfully rotated manager unlock key.
2-
32
To unlock a swarm manager after it restarts, run the `docker swarm unlock`
43
command and provide the following key:
54

0 commit comments

Comments
 (0)