Update swarm command tests to use the new golden

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-16 13:48:31 -04:00
parent 1dd742eac8
commit 3d68aa8416
8 changed files with 40 additions and 55 deletions

View File

@ -1,7 +1,6 @@
package swarm
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
@ -9,8 +8,7 @@ import (
"github.com/docker/cli/cli/internal/test"
"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/gotestyourself/gotestyourself/golden"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
@ -65,14 +63,13 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInitCommand(
test.NewFakeCliWithOutput(&fakeClient{
test.NewFakeCli(&fakeClient{
swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
}))
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
@ -112,20 +109,17 @@ func TestSwarmInit(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInitCommand(
test.NewFakeCliWithOutput(&fakeClient{
swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
cli := test.NewFakeCli(&fakeClient{
swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
nodeInspectFunc: tc.nodeInspectFunc,
})
cmd := newInitCommand(cli)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("init-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name))
}
}

View File

@ -1,7 +1,6 @@
package swarm
import (
"bytes"
"io/ioutil"
"strings"
"testing"
@ -49,12 +48,11 @@ func TestSwarmJoinErrors(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinCommand(
test.NewFakeCliWithOutput(&fakeClient{
test.NewFakeCli(&fakeClient{
swarmJoinFunc: tc.swarmJoinFunc,
infoFunc: tc.infoFunc,
}, buf))
}))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -91,13 +89,12 @@ func TestSwarmJoin(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinCommand(
test.NewFakeCliWithOutput(&fakeClient{
infoFunc: tc.infoFunc,
}, buf))
cli := test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc,
})
cmd := newJoinCommand(cli)
cmd.SetArgs([]string{"remote"})
assert.NoError(t, cmd.Execute())
assert.Equal(t, strings.TrimSpace(buf.String()), tc.expected)
assert.Equal(t, strings.TrimSpace(cli.OutBuffer().String()), tc.expected)
}
}

View File

@ -1,7 +1,6 @@
package swarm
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
@ -13,7 +12,7 @@ import (
// Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert"
)
@ -90,14 +89,13 @@ func TestSwarmJoinTokenErrors(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinTokenCommand(
test.NewFakeCliWithOutput(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc,
swarmUpdateFunc: tc.swarmUpdateFunc,
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
cli := test.NewFakeCli(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc,
swarmUpdateFunc: tc.swarmUpdateFunc,
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
})
cmd := newJoinTokenCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
@ -198,20 +196,17 @@ func TestSwarmJoinToken(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newJoinTokenCommand(
test.NewFakeCliWithOutput(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc,
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
}, buf))
cli := test.NewFakeCli(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc,
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
})
cmd := newJoinTokenCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("jointoken-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("jointoken-%s.golden", tc.name))
}
}

View File

@ -3,3 +3,4 @@ Successfully rotated manager join token.
To add a manager to this swarm, run the following command:
docker swarm join --token manager-join-token 127.0.0.1

View File

@ -1,3 +1,4 @@
To add a manager to this swarm, run the following command:
docker swarm join --token manager-join-token 127.0.0.1

View File

@ -1,3 +1,4 @@
To add a worker to this swarm, run the following command:
docker swarm join --token worker-join-token 127.0.0.1

View File

@ -12,7 +12,7 @@ import (
// Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert"
)
@ -167,8 +167,6 @@ func TestSwarmUnlockKey(t *testing.T) {
cmd.Flags().Set(key, value)
}
assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("unlockkeys-%s.golden", tc.name))
}
}

View File

@ -13,7 +13,7 @@ import (
// Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert"
)
@ -174,8 +174,6 @@ func TestSwarmUpdate(t *testing.T) {
}
cmd.SetOutput(cli.OutBuffer())
assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("update-%s.golden", tc.name))
}
}