2017-07-20 04:32:51 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-07-20 04:32:51 -04:00
|
|
|
"fmt"
|
2022-02-25 08:30:56 -05:00
|
|
|
"io"
|
2017-07-20 04:32:51 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-07-20 04:32:51 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-07-20 04:32:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRollback(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
serviceUpdateFunc func(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error)
|
|
|
|
expectedDockerCliErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "rollback-service",
|
|
|
|
args: []string{"service-id"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rollback-service-with-warnings",
|
|
|
|
args: []string{"service-id"},
|
|
|
|
serviceUpdateFunc: func(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
|
|
|
|
response := types.ServiceUpdateResponse{}
|
|
|
|
|
|
|
|
response.Warnings = []string{
|
|
|
|
"- warning 1",
|
|
|
|
"- warning 2",
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
},
|
|
|
|
expectedDockerCliErr: "- warning 1\n- warning 2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
serviceUpdateFunc: tc.serviceUpdateFunc,
|
|
|
|
})
|
|
|
|
cmd := newRollbackCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2022-02-25 08:30:56 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(strings.TrimSpace(cli.ErrBuffer().String()), tc.expectedDockerCliErr))
|
2017-07-20 04:32:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRollbackWithErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
serviceInspectWithRawFunc func(ctx context.Context, serviceID string, options types.ServiceInspectOptions) (swarm.Service, []byte, error)
|
|
|
|
serviceUpdateFunc func(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not-enough-args",
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"service-id-1", "service-id-2"},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service-does-not-exists",
|
|
|
|
args: []string{"service-id"},
|
|
|
|
serviceInspectWithRawFunc: func(ctx context.Context, serviceID string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
|
|
|
return swarm.Service{}, []byte{}, fmt.Errorf("no such services: %s", serviceID)
|
|
|
|
},
|
|
|
|
expectedError: "no such services: service-id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service-update-failed",
|
|
|
|
args: []string{"service-id"},
|
|
|
|
serviceUpdateFunc: func(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (types.ServiceUpdateResponse, error) {
|
|
|
|
return types.ServiceUpdateResponse{}, fmt.Errorf("no such services: %s", serviceID)
|
|
|
|
},
|
|
|
|
expectedError: "no such services: service-id",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newRollbackCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
serviceInspectWithRawFunc: tc.serviceInspectWithRawFunc,
|
|
|
|
serviceUpdateFunc: tc.serviceUpdateFunc,
|
|
|
|
}))
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2022-02-25 08:30:56 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-07-20 04:32:51 -04:00
|
|
|
}
|
|
|
|
}
|