@@ -20,53 +20,189 @@ import (
20
20
"testing"
21
21
22
22
"github.com/containerd/nerdctl/v2/pkg/testutil"
23
+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
24
+ "github.com/containerd/nerdctl/v2/pkg/testutil/test"
23
25
)
24
26
25
27
func TestCompletion (t * testing.T ) {
26
- testutil .DockerIncompatible (t )
27
- base := testutil .NewBase (t )
28
- const gsc = "__complete"
29
- // cmd is executed with base.Args={"--namespace=nerdctl-test"}
30
- base .Cmd (gsc , "--cgroup-manager" , "" ).AssertOutContains ("cgroupfs\n " )
31
- base .Cmd (gsc , "--snapshotter" , "" ).AssertOutContains ("native\n " )
32
- base .Cmd (gsc , "" ).AssertOutContains ("run\t " )
33
- base .Cmd (gsc , "run" , "-" ).AssertOutContains ("--network\t " )
34
- base .Cmd (gsc , "run" , "--n" ).AssertOutContains ("--network\t " )
35
- base .Cmd (gsc , "run" , "--ne" ).AssertOutContains ("--network\t " )
36
- base .Cmd (gsc , "run" , "--net" , "" ).AssertOutContains ("host\n " )
37
- base .Cmd (gsc , "run" , "-it" , "--net" , "" ).AssertOutContains ("host\n " )
38
- base .Cmd (gsc , "run" , "-it" , "--rm" , "--net" , "" ).AssertOutContains ("host\n " )
39
- base .Cmd (gsc , "run" , "--restart" , "" ).AssertOutContains ("always\n " )
40
- base .Cmd (gsc , "network" , "rm" , "" ).AssertOutNotContains ("host\n " ) // host is unremovable
41
- base .Cmd (gsc , "run" , "--cap-add" , "" ).AssertOutContains ("sys_admin\n " )
42
- base .Cmd (gsc , "run" , "--cap-add" , "" ).AssertOutNotContains ("CAP_SYS_ADMIN\n " ) // invalid form
28
+ nerdtest .Setup ()
43
29
44
- // Tests with an image
45
- base .Cmd ("pull" , testutil .AlpineImage ).AssertOK ()
46
- base .Cmd (gsc , "run" , "-i" , "" ).AssertOutContains (testutil .AlpineImage )
47
- base .Cmd (gsc , "run" , "-it" , "" ).AssertOutContains (testutil .AlpineImage )
48
- base .Cmd (gsc , "run" , "-it" , "--rm" , "" ).AssertOutContains (testutil .AlpineImage )
30
+ testCase := & test.Case {
31
+ Description : "Base completion" ,
32
+ Require : test .Not (nerdtest .Docker ),
33
+ Setup : func (data test.Data , helpers test.Helpers ) {
34
+ helpers .Ensure ("pull" , testutil .AlpineImage )
35
+ helpers .Ensure ("network" , "create" , data .Identifier ())
36
+ helpers .Ensure ("volume" , "create" , data .Identifier ())
37
+ data .Set ("identifier" , data .Identifier ())
38
+ },
39
+ Cleanup : func (data test.Data , helpers test.Helpers ) {
40
+ helpers .Anyhow ("network" , "rm" , data .Identifier ())
41
+ helpers .Anyhow ("volume" , "rm" , data .Identifier ())
42
+ },
43
+ SubTests : []* test.Case {
44
+ {
45
+ Description : "--cgroup-manager" ,
46
+ Command : test .RunCommand ("__complete" , "--cgroup-manager" , "" ),
47
+ Expected : test .Expects (0 , nil , test .Contains ("cgroupfs\n " )),
48
+ },
49
+ {
50
+ Description : "--snapshotter" ,
51
+ Command : test .RunCommand ("__complete" , "--snapshotter" , "" ),
52
+ Expected : test .Expects (0 , nil , test .Contains ("native\n " )),
53
+ },
54
+ {
55
+ Description : "empty" ,
56
+ Command : test .RunCommand ("__complete" , "" ),
57
+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
58
+ },
59
+ {
60
+ Description : "run -" ,
61
+ Command : test .RunCommand ("__complete" , "run" , "-" ),
62
+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
63
+ },
64
+ {
65
+ Description : "run --n" ,
66
+ Command : test .RunCommand ("__complete" , "run" , "--n" ),
67
+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
68
+ },
69
+ {
70
+ Description : "run --ne" ,
71
+ Command : test .RunCommand ("__complete" , "run" , "--ne" ),
72
+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
73
+ },
74
+ {
75
+ Description : "run --net" ,
76
+ Command : test .RunCommand ("__complete" , "run" , "--net" , "" ),
77
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
78
+ return & test.Expected {
79
+ Output : test .All (
80
+ test .Contains ("host\n " ),
81
+ test .Contains (data .Get ("identifier" )+ "\n " ),
82
+ ),
83
+ }
84
+ },
85
+ },
86
+ {
87
+ Description : "run -it --net" ,
88
+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--net" , "" ),
89
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
90
+ return & test.Expected {
91
+ Output : test .All (
92
+ test .Contains ("host\n " ),
93
+ test .Contains (data .Get ("identifier" )+ "\n " ),
94
+ ),
95
+ }
96
+ },
97
+ },
98
+ {
99
+ Description : "run -ti --rm --net" ,
100
+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--rm" , "--net" , "" ),
101
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
102
+ return & test.Expected {
103
+ Output : test .All (
104
+ test .Contains ("host\n " ),
105
+ test .Contains (data .Get ("identifier" )+ "\n " ),
106
+ ),
107
+ }
108
+ },
109
+ },
110
+ {
111
+ Description : "run --restart" ,
112
+ Command : test .RunCommand ("__complete" , "run" , "--restart" , "" ),
113
+ Expected : test .Expects (0 , nil , test .Contains ("always\n " )),
114
+ },
115
+ {
116
+ Description : "network --rm" ,
117
+ Command : test .RunCommand ("__complete" , "network" , "rm" , "" ),
118
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
119
+ return & test.Expected {
120
+ Output : test .All (
121
+ test .DoesNotContain ("host\n " ),
122
+ test .Contains (data .Get ("identifier" )+ "\n " ),
123
+ ),
124
+ }
125
+ },
126
+ },
127
+ {
128
+ Description : "run --cap-add" ,
129
+ Command : test .RunCommand ("__complete" , "run" , "--cap-add" , "" ),
130
+ Expected : test .Expects (0 , nil , test .All (
131
+ test .Contains ("sys_admin\n " ),
132
+ test .DoesNotContain ("CAP_SYS_ADMIN\n " ),
133
+ )),
134
+ },
135
+ {
136
+ Description : "volume inspect" ,
137
+ Command : test .RunCommand ("__complete" , "volume" , "inspect" , "" ),
138
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
139
+ return & test.Expected {
140
+ Output : test .Contains (data .Get ("identifier" ) + "\n " ),
141
+ }
142
+ },
143
+ },
144
+ {
145
+ Description : "volume rm" ,
146
+ Command : test .RunCommand ("__complete" , "volume" , "rm" , "" ),
147
+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
148
+ return & test.Expected {
149
+ Output : test .Contains (data .Get ("identifier" ) + "\n " ),
150
+ }
151
+ },
152
+ },
153
+ {
154
+ Description : "no namespace --cgroup-manager" ,
155
+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
156
+ cmd := helpers .Command ()
157
+ cmd .Clear ()
158
+ cmd .WithBinary ("nerdctl" )
159
+ cmd .WithArgs ("__complete" , "--cgroup-manager" , "" )
160
+ return cmd
161
+ },
162
+ Expected : test .Expects (0 , nil , test .Contains ("cgroupfs\n " )),
163
+ },
164
+ {
165
+ Description : "no namespace empty" ,
166
+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
167
+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).WithArgs ("__complete" , "" )
168
+ },
169
+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
170
+ },
171
+ {
172
+ Description : "namespace space empty" ,
173
+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
174
+ // mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
175
+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).
176
+ WithArgs ("__complete" , "--namespace" , testutil .Namespace , "" )
177
+ },
178
+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
179
+ },
180
+ {
181
+ Description : "run -i" ,
182
+ Command : test .RunCommand ("__complete" , "run" , "-i" , "" ),
183
+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
184
+ },
185
+ {
186
+ Description : "run -it" ,
187
+ Command : test .RunCommand ("__complete" , "run" , "-it" , "" ),
188
+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
189
+ },
190
+ {
191
+ Description : "run -it --rm" ,
192
+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--rm" , "" ),
193
+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
194
+ },
195
+ {
196
+ Description : "namespace run -i" ,
197
+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
198
+ // mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
199
+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).
200
+ WithArgs ("__complete" , "--namespace" , testutil .Namespace , "run" , "-i" , "" )
201
+ },
202
+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage + "\n " )),
203
+ },
204
+ },
205
+ }
49
206
50
- // Tests with a network
51
- testNetworkName := "nerdctl-test-completion"
52
- defer base .Cmd ("network" , "rm" , testNetworkName ).Run ()
53
- base .Cmd ("network" , "create" , testNetworkName ).AssertOK ()
54
- base .Cmd (gsc , "network" , "rm" , "" ).AssertOutContains (testNetworkName )
55
- base .Cmd (gsc , "run" , "--net" , "" ).AssertOutContains (testNetworkName )
56
-
57
- // Tests with a volume
58
- testVolumekName := "nerdctl-test-completion"
59
- defer base .Cmd ("volume" , "rm" , testVolumekName ).Run ()
60
- base .Cmd ("volume" , "create" , testVolumekName ).AssertOK ()
61
- base .Cmd (gsc , "volume" , "inspect" , "" ).AssertOutContains (testVolumekName )
62
- base .Cmd (gsc , "volume" , "rm" , "" ).AssertOutContains (testVolumekName )
63
-
64
- // Tests with raw base (without Args={"--namespace=nerdctl-test"})
65
- rawBase := testutil .NewBase (t )
66
- rawBase .Args = nil // unset "--namespace=nerdctl-test"
67
- rawBase .Cmd (gsc , "--cgroup-manager" , "" ).AssertOutContains ("cgroupfs\n " )
68
- rawBase .Cmd (gsc , "" ).AssertOutContains ("run\t " )
69
- // mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
70
- rawBase .Cmd (gsc , "--namespace" , testutil .Namespace , "" ).AssertOutContains ("run\t " )
71
- rawBase .Cmd (gsc , "--namespace" , testutil .Namespace , "run" , "-i" , "" ).AssertOutContains (testutil .AlpineImage )
207
+ testCase .Run (t )
72
208
}
0 commit comments