github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/service/ps_test.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/internal/test"
     8  	"github.com/docker/cli/opts"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/filters"
    11  	"github.com/docker/docker/api/types/swarm"
    12  	"github.com/google/go-cmp/cmp"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func TestCreateFilter(t *testing.T) {
    18  	client := &fakeClient{
    19  		serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    20  			return []swarm.Service{
    21  				{ID: "idmatch"},
    22  				{ID: "idprefixmatch"},
    23  				newService("cccccccc", "namematch"),
    24  				newService("01010101", "notfoundprefix"),
    25  			}, nil
    26  		},
    27  	}
    28  
    29  	filter := opts.NewFilterOpt()
    30  	assert.NilError(t, filter.Set("node=somenode"))
    31  	options := psOptions{
    32  		services: []string{"idmatch", "idprefix", "namematch", "notfound"},
    33  		filter:   filter,
    34  	}
    35  
    36  	actual, notfound, err := createFilter(context.Background(), client, options)
    37  	assert.NilError(t, err)
    38  	assert.Check(t, is.DeepEqual(notfound, []string{"no such service: notfound"}))
    39  
    40  	expected := filters.NewArgs(
    41  		filters.Arg("service", "idmatch"),
    42  		filters.Arg("service", "idprefixmatch"),
    43  		filters.Arg("service", "cccccccc"),
    44  		filters.Arg("node", "somenode"),
    45  	)
    46  	assert.DeepEqual(t, expected, actual, cmpFilters)
    47  }
    48  
    49  func TestCreateFilterWithAmbiguousIDPrefixError(t *testing.T) {
    50  	client := &fakeClient{
    51  		serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    52  			return []swarm.Service{
    53  				{ID: "aaaone"},
    54  				{ID: "aaatwo"},
    55  			}, nil
    56  		},
    57  	}
    58  	options := psOptions{
    59  		services: []string{"aaa"},
    60  		filter:   opts.NewFilterOpt(),
    61  	}
    62  	_, _, err := createFilter(context.Background(), client, options)
    63  	assert.Error(t, err, "multiple services found with provided prefix: aaa")
    64  }
    65  
    66  func TestCreateFilterNoneFound(t *testing.T) {
    67  	client := &fakeClient{}
    68  	options := psOptions{
    69  		services: []string{"foo", "notfound"},
    70  		filter:   opts.NewFilterOpt(),
    71  	}
    72  	_, _, err := createFilter(context.Background(), client, options)
    73  	assert.Error(t, err, "no such service: foo\nno such service: notfound")
    74  }
    75  
    76  func TestRunPSWarnsOnNotFound(t *testing.T) {
    77  	client := &fakeClient{
    78  		serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    79  			return []swarm.Service{
    80  				{ID: "foo"},
    81  			}, nil
    82  		},
    83  	}
    84  
    85  	cli := test.NewFakeCli(client)
    86  	options := psOptions{
    87  		services: []string{"foo", "bar"},
    88  		filter:   opts.NewFilterOpt(),
    89  		format:   "{{.ID}}",
    90  	}
    91  	err := runPS(cli, options)
    92  	assert.Error(t, err, "no such service: bar")
    93  }
    94  
    95  func TestRunPSQuiet(t *testing.T) {
    96  	client := &fakeClient{
    97  		serviceListFunc: func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
    98  			return []swarm.Service{{ID: "foo"}}, nil
    99  		},
   100  		taskListFunc: func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
   101  			return []swarm.Task{{ID: "sxabyp0obqokwekpun4rjo0b3"}}, nil
   102  		},
   103  	}
   104  
   105  	cli := test.NewFakeCli(client)
   106  	err := runPS(cli, psOptions{services: []string{"foo"}, quiet: true, filter: opts.NewFilterOpt()})
   107  	assert.NilError(t, err)
   108  	assert.Check(t, is.Equal("sxabyp0obqokwekpun4rjo0b3\n", cli.OutBuffer().String()))
   109  }
   110  
   111  func TestUpdateNodeFilter(t *testing.T) {
   112  	selfNodeID := "foofoo"
   113  	filter := filters.NewArgs(
   114  		filters.Arg("node", "one"),
   115  		filters.Arg("node", "two"),
   116  		filters.Arg("node", "self"),
   117  	)
   118  
   119  	client := &fakeClient{
   120  		infoFunc: func(_ context.Context) (types.Info, error) {
   121  			return types.Info{Swarm: swarm.Info{NodeID: selfNodeID}}, nil
   122  		},
   123  	}
   124  
   125  	updateNodeFilter(context.Background(), client, filter)
   126  
   127  	expected := filters.NewArgs(
   128  		filters.Arg("node", "one"),
   129  		filters.Arg("node", "two"),
   130  		filters.Arg("node", selfNodeID),
   131  	)
   132  	assert.DeepEqual(t, expected, filter, cmpFilters)
   133  }
   134  
   135  var cmpFilters = cmp.AllowUnexported(filters.Args{})