|
| 1 | +package system |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"errors" |
| 6 | +"fmt" |
| 7 | +"testing" |
| 8 | + |
| 9 | +"github.com/docker/cli/internal/test" |
| 10 | +"github.com/docker/cli/internal/test/builders" |
| 11 | +"github.com/docker/docker/api/types" |
| 12 | +"github.com/docker/docker/api/types/container" |
| 13 | +"github.com/docker/docker/api/types/image" |
| 14 | +"github.com/docker/docker/api/types/network" |
| 15 | +"github.com/docker/docker/api/types/swarm" |
| 16 | +"github.com/docker/docker/api/types/system" |
| 17 | +"github.com/docker/docker/api/types/volume" |
| 18 | +"github.com/spf13/cobra" |
| 19 | +"gotest.tools/v3/assert" |
| 20 | +) |
| 21 | + |
| 22 | +func TestCompleteEventFilter(t *testing.T) { |
| 23 | +tests := []struct { |
| 24 | +client *fakeClient |
| 25 | +toComplete string |
| 26 | +expected []string |
| 27 | +}{ |
| 28 | +{ |
| 29 | +client: &fakeClient{ |
| 30 | +containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { |
| 31 | +return []container.Summary{ |
| 32 | +*builders.Container("c1"), |
| 33 | +*builders.Container("c2"), |
| 34 | +}, nil |
| 35 | +}, |
| 36 | +}, |
| 37 | +toComplete: "container=", |
| 38 | +expected: []string{"container=c1", "container=c2"}, |
| 39 | +}, |
| 40 | +{ |
| 41 | +client: &fakeClient{ |
| 42 | +containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { |
| 43 | +return nil, errors.New("API error") |
| 44 | +}, |
| 45 | +}, |
| 46 | +toComplete: "container=", |
| 47 | +expected: []string{}, |
| 48 | +}, |
| 49 | +{ |
| 50 | +client: &fakeClient{ |
| 51 | +infoFunc: func(ctx context.Context) (system.Info, error) { |
| 52 | +return system.Info{ |
| 53 | +ID: "daemon-id", |
| 54 | +Name: "daemon-name", |
| 55 | +}, nil |
| 56 | +}, |
| 57 | +}, |
| 58 | +toComplete: "daemon=", |
| 59 | +expected: []string{"daemon=daemon-name", "daemon=daemon-id"}, |
| 60 | +}, |
| 61 | +{ |
| 62 | +client: &fakeClient{ |
| 63 | +infoFunc: func(ctx context.Context) (system.Info, error) { |
| 64 | +return system.Info{}, errors.New("API error") |
| 65 | +}, |
| 66 | +}, |
| 67 | +toComplete: "daemon=", |
| 68 | +expected: []string{}, |
| 69 | +}, |
| 70 | +{ |
| 71 | +client: &fakeClient{ |
| 72 | +imageListFunc: func(_ context.Context, _ image.ListOptions) ([]image.Summary, error) { |
| 73 | +return []image.Summary{ |
| 74 | +{RepoTags: []string{"img:1"}}, |
| 75 | +{RepoTags: []string{"img:2"}}, |
| 76 | +}, nil |
| 77 | +}, |
| 78 | +}, |
| 79 | +toComplete: "image=", |
| 80 | +expected: []string{"image=img:1", "image=img:2"}, |
| 81 | +}, |
| 82 | +{ |
| 83 | +client: &fakeClient{ |
| 84 | +imageListFunc: func(_ context.Context, _ image.ListOptions) ([]image.Summary, error) { |
| 85 | +return []image.Summary{}, errors.New("API error") |
| 86 | +}, |
| 87 | +}, |
| 88 | +toComplete: "image=", |
| 89 | +expected: []string{}, |
| 90 | +}, |
| 91 | +{ |
| 92 | +client: &fakeClient{ |
| 93 | +networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { |
| 94 | +return []network.Summary{ |
| 95 | +*builders.NetworkResource(builders.NetworkResourceName("nw1")), |
| 96 | +*builders.NetworkResource(builders.NetworkResourceName("nw2")), |
| 97 | +}, nil |
| 98 | +}, |
| 99 | +}, |
| 100 | +toComplete: "network=", |
| 101 | +expected: []string{"network=nw1", "network=nw2"}, |
| 102 | +}, |
| 103 | +{ |
| 104 | +client: &fakeClient{ |
| 105 | +networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { |
| 106 | +return nil, errors.New("API error") |
| 107 | +}, |
| 108 | +}, |
| 109 | +toComplete: "network=", |
| 110 | +expected: []string{}, |
| 111 | +}, |
| 112 | +{ |
| 113 | +client: &fakeClient{ |
| 114 | +nodeListFunc: func(_ context.Context, _ types.NodeListOptions) ([]swarm.Node, error) { |
| 115 | +return []swarm.Node{ |
| 116 | +*builders.Node(builders.Hostname("n1")), |
| 117 | +}, nil |
| 118 | +}, |
| 119 | +}, |
| 120 | +toComplete: "node=", |
| 121 | +expected: []string{"node=n1"}, |
| 122 | +}, |
| 123 | +{ |
| 124 | +client: &fakeClient{ |
| 125 | +nodeListFunc: func(_ context.Context, _ types.NodeListOptions) ([]swarm.Node, error) { |
| 126 | +return []swarm.Node{}, errors.New("API error") |
| 127 | +}, |
| 128 | +}, |
| 129 | +toComplete: "node=", |
| 130 | +expected: []string{}, |
| 131 | +}, |
| 132 | +{ |
| 133 | +client: &fakeClient{ |
| 134 | +volumeListFunc: func(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) { |
| 135 | +return volume.ListResponse{ |
| 136 | +Volumes: []*volume.Volume{ |
| 137 | +builders.Volume(builders.VolumeName("v1")), |
| 138 | +builders.Volume(builders.VolumeName("v2")), |
| 139 | +}, |
| 140 | +}, nil |
| 141 | +}, |
| 142 | +}, |
| 143 | +toComplete: "volume=", |
| 144 | +expected: []string{"volume=v1", "volume=v2"}, |
| 145 | +}, |
| 146 | +{ |
| 147 | +client: &fakeClient{ |
| 148 | +volumeListFunc: func(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) { |
| 149 | +return volume.ListResponse{}, errors.New("API error") |
| 150 | +}, |
| 151 | +}, |
| 152 | +toComplete: "volume=", |
| 153 | +expected: []string{}, |
| 154 | +}, |
| 155 | +} |
| 156 | + |
| 157 | +for _, tc := range tests { |
| 158 | +cli := test.NewFakeCli(tc.client) |
| 159 | + |
| 160 | +completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, tc.toComplete) |
| 161 | + |
| 162 | +assert.DeepEqual(t, completions, tc.expected) |
| 163 | +assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) |
| 164 | +} |
| 165 | +} |
0 commit comments