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