@@ -20,53 +20,205 @@ import (
2020 "testing"
2121
2222 "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"
2325)
2426
2527func 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 ()
4329
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 ("network" , "create" , data .Identifier ())
35+ helpers .Ensure ("volume" , "create" , data .Identifier ())
36+ data .Set ("identifier" , data .Identifier ())
37+ },
38+ Cleanup : func (data test.Data , helpers test.Helpers ) {
39+ helpers .Anyhow ("network" , "rm" , data .Identifier ())
40+ helpers .Anyhow ("volume" , "rm" , data .Identifier ())
41+ },
42+ SubTests : []* test.Case {
43+ {
44+ Description : "--cgroup-manager" ,
45+ Command : test .RunCommand ("__complete" , "--cgroup-manager" , "" ),
46+ Expected : test .Expects (0 , nil , test .Contains ("cgroupfs\n " )),
47+ },
48+ {
49+ Description : "--snapshotter" ,
50+ Command : test .RunCommand ("__complete" , "--snapshotter" , "" ),
51+ Expected : test .Expects (0 , nil , test .Contains ("native\n " )),
52+ },
53+ {
54+ Description : "empty" ,
55+ Command : test .RunCommand ("__complete" , "" ),
56+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
57+ },
58+ {
59+ Description : "run -" ,
60+ Command : test .RunCommand ("__complete" , "run" , "-" ),
61+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
62+ },
63+ {
64+ Description : "run --n" ,
65+ Command : test .RunCommand ("__complete" , "run" , "--n" ),
66+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
67+ },
68+ {
69+ Description : "run --ne" ,
70+ Command : test .RunCommand ("__complete" , "run" , "--ne" ),
71+ Expected : test .Expects (0 , nil , test .Contains ("--network\t " )),
72+ },
73+ {
74+ Description : "run --net" ,
75+ Command : test .RunCommand ("__complete" , "run" , "--net" , "" ),
76+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
77+ return & test.Expected {
78+ Output : test .All (
79+ test .Contains ("host\n " ),
80+ test .Contains (data .Get ("identifier" )+ "\n " ),
81+ ),
82+ }
83+ },
84+ },
85+ {
86+ Description : "run -it --net" ,
87+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--net" , "" ),
88+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
89+ return & test.Expected {
90+ Output : test .All (
91+ test .Contains ("host\n " ),
92+ test .Contains (data .Get ("identifier" )+ "\n " ),
93+ ),
94+ }
95+ },
96+ },
97+ {
98+ Description : "run -ti --rm --net" ,
99+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--rm" , "--net" , "" ),
100+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
101+ return & test.Expected {
102+ Output : test .All (
103+ test .Contains ("host\n " ),
104+ test .Contains (data .Get ("identifier" )+ "\n " ),
105+ ),
106+ }
107+ },
108+ },
109+ {
110+ Description : "run --restart" ,
111+ Command : test .RunCommand ("__complete" , "run" , "--restart" , "" ),
112+ Expected : test .Expects (0 , nil , test .Contains ("always\n " )),
113+ },
114+ {
115+ Description : "network --rm" ,
116+ Command : test .RunCommand ("__complete" , "network" , "rm" , "" ),
117+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
118+ return & test.Expected {
119+ Output : test .All (
120+ test .DoesNotContain ("host\n " ),
121+ test .Contains (data .Get ("identifier" )+ "\n " ),
122+ ),
123+ }
124+ },
125+ },
126+ {
127+ Description : "run --cap-add" ,
128+ Command : test .RunCommand ("__complete" , "run" , "--cap-add" , "" ),
129+ Expected : test .Expects (0 , nil , test .All (
130+ test .Contains ("sys_admin\n " ),
131+ test .DoesNotContain ("CAP_SYS_ADMIN\n " ),
132+ )),
133+ },
134+ {
135+ Description : "volume inspect" ,
136+ Command : test .RunCommand ("__complete" , "volume" , "inspect" , "" ),
137+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
138+ return & test.Expected {
139+ Output : test .Contains (data .Get ("identifier" ) + "\n " ),
140+ }
141+ },
142+ },
143+ {
144+ Description : "volume rm" ,
145+ Command : test .RunCommand ("__complete" , "volume" , "rm" , "" ),
146+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
147+ return & test.Expected {
148+ Output : test .Contains (data .Get ("identifier" ) + "\n " ),
149+ }
150+ },
151+ },
152+ {
153+ Description : "no namespace --cgroup-manager" ,
154+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
155+ cmd := helpers .Command ()
156+ cmd .Clear ()
157+ cmd .WithBinary ("nerdctl" )
158+ cmd .WithArgs ("__complete" , "--cgroup-manager" , "" )
159+ return cmd
160+ },
161+ Expected : test .Expects (0 , nil , test .Contains ("cgroupfs\n " )),
162+ },
163+ {
164+ Description : "no namespace empty" ,
165+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
166+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).WithArgs ("__complete" , "" )
167+ },
168+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
169+ },
170+ {
171+ Description : "namespace space empty" ,
172+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
173+ // mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
174+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).
175+ WithArgs ("__complete" , "--namespace" , testutil .Namespace , "" )
176+ },
177+ Expected : test .Expects (0 , nil , test .Contains ("run\t " )),
178+ },
179+ },
180+ }
49181
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 )
182+ testCase . Run ( t )
183+ }
184+
185+ // Test is privatized so that we can guarantee the image will not get rmi-ed
186+ func TestCompletionPrivate ( t * testing. T ) {
187+ nerdtest . Setup ( )
56188
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 )
189+ testCase := & test.Case {
190+ Description : "With an image" ,
191+ Require : test .Require (nerdtest .Private , test .Not (nerdtest .Docker )),
192+ Setup : func (data test.Data , helpers test.Helpers ) {
193+ helpers .Ensure ("pull" , testutil .AlpineImage )
194+ },
195+ SubTests : []* test.Case {
196+ {
197+ Description : "run -i" ,
198+ Command : test .RunCommand ("__complete" , "run" , "-i" , "" ),
199+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
200+ },
201+ {
202+ Description : "run -it" ,
203+ Command : test .RunCommand ("__complete" , "run" , "-it" , "" ),
204+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
205+ },
206+ {
207+ Description : "run -it --rm" ,
208+ Command : test .RunCommand ("__complete" , "run" , "-it" , "--rm" , "" ),
209+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage )),
210+ },
211+ {
212+ Description : "namespace run -i" ,
213+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
214+ // mind {"--namespace=nerdctl-test"} vs {"--namespace", "nerdctl-test"}
215+ return helpers .Command ().Clear ().WithBinary ("nerdctl" ).
216+ WithArgs ("__complete" , "--namespace" , testutil .Namespace , "run" , "-i" , "" )
217+ },
218+ Expected : test .Expects (0 , nil , test .Contains (testutil .AlpineImage + "\n " )),
219+ },
220+ },
221+ }
63222
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 )
223+ testCase .Run (t )
72224}
0 commit comments