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