2017-06-01 12:59:11 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-06-01 12:59:11 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-01 12:59:11 -04:00
|
|
|
"github.com/docker/cli/opts"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2023-07-14 17:42:40 -04:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2017-12-21 16:27:57 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-01 12:59:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateFilter(t *testing.T) {
|
|
|
|
client := &fakeClient{
|
|
|
|
serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
|
|
|
{ID: "idmatch"},
|
|
|
|
{ID: "idprefixmatch"},
|
|
|
|
newService("cccccccc", "namematch"),
|
|
|
|
newService("01010101", "notfoundprefix"),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := opts.NewFilterOpt()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, filter.Set("node=somenode"))
|
2017-06-01 12:59:11 -04:00
|
|
|
options := psOptions{
|
|
|
|
services: []string{"idmatch", "idprefix", "namematch", "notfound"},
|
|
|
|
filter: filter,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, notfound, err := createFilter(context.Background(), client, options)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(notfound, []string{"no such service: notfound"}))
|
2017-06-01 12:59:11 -04:00
|
|
|
|
2017-12-21 16:27:57 -05:00
|
|
|
expected := filters.NewArgs(
|
|
|
|
filters.Arg("service", "idmatch"),
|
|
|
|
filters.Arg("service", "idprefixmatch"),
|
|
|
|
filters.Arg("service", "cccccccc"),
|
|
|
|
filters.Arg("node", "somenode"),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(t, expected, actual, cmpFilters)
|
2017-06-01 12:59:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFilterWithAmbiguousIDPrefixError(t *testing.T) {
|
|
|
|
client := &fakeClient{
|
|
|
|
serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
|
|
|
{ID: "aaaone"},
|
|
|
|
{ID: "aaatwo"},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
options := psOptions{
|
|
|
|
services: []string{"aaa"},
|
|
|
|
filter: opts.NewFilterOpt(),
|
|
|
|
}
|
|
|
|
_, _, err := createFilter(context.Background(), client, options)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "multiple services found with provided prefix: aaa")
|
2017-06-01 12:59:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateFilterNoneFound(t *testing.T) {
|
|
|
|
client := &fakeClient{}
|
|
|
|
options := psOptions{
|
|
|
|
services: []string{"foo", "notfound"},
|
|
|
|
filter: opts.NewFilterOpt(),
|
|
|
|
}
|
|
|
|
_, _, err := createFilter(context.Background(), client, options)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "no such service: foo\nno such service: notfound")
|
2017-06-01 12:59:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunPSWarnsOnNotFound(t *testing.T) {
|
|
|
|
client := &fakeClient{
|
|
|
|
serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
|
|
|
{ID: "foo"},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(client)
|
2017-06-01 12:59:11 -04:00
|
|
|
options := psOptions{
|
|
|
|
services: []string{"foo", "bar"},
|
|
|
|
filter: opts.NewFilterOpt(),
|
|
|
|
format: "{{.ID}}",
|
|
|
|
}
|
2023-09-09 18:27:44 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
err := runPS(ctx, cli, options)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "no such service: bar")
|
2017-06-01 12:59:11 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2017-09-30 16:19:34 -04:00
|
|
|
func TestRunPSQuiet(t *testing.T) {
|
|
|
|
client := &fakeClient{
|
|
|
|
serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{{ID: "foo"}}, nil
|
|
|
|
},
|
|
|
|
taskListFunc: func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{{ID: "sxabyp0obqokwekpun4rjo0b3"}}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(client)
|
2023-09-09 18:27:44 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
err := runPS(ctx, cli, psOptions{services: []string{"foo"}, quiet: true, filter: opts.NewFilterOpt()})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal("sxabyp0obqokwekpun4rjo0b3\n", cli.OutBuffer().String()))
|
2017-09-30 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
2017-05-09 18:35:25 -04:00
|
|
|
func TestUpdateNodeFilter(t *testing.T) {
|
|
|
|
selfNodeID := "foofoo"
|
2017-12-21 16:27:57 -05:00
|
|
|
filter := filters.NewArgs(
|
|
|
|
filters.Arg("node", "one"),
|
|
|
|
filters.Arg("node", "two"),
|
|
|
|
filters.Arg("node", "self"),
|
|
|
|
)
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
client := &fakeClient{
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc: func(_ context.Context) (system.Info, error) {
|
|
|
|
return system.Info{Swarm: swarm.Info{NodeID: selfNodeID}}, nil
|
2017-05-09 18:35:25 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
updateNodeFilter(context.Background(), client, filter)
|
|
|
|
|
2017-12-21 16:27:57 -05:00
|
|
|
expected := filters.NewArgs(
|
|
|
|
filters.Arg("node", "one"),
|
|
|
|
filters.Arg("node", "two"),
|
|
|
|
filters.Arg("node", selfNodeID),
|
|
|
|
)
|
|
|
|
assert.DeepEqual(t, expected, filter, cmpFilters)
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
2017-12-21 16:27:57 -05:00
|
|
|
|
|
|
|
var cmpFilters = cmp.AllowUnexported(filters.Args{})
|