@@ -9,10 +9,12 @@ import (
9
9
"testing"
10
10
"time"
11
11
12
+ "github.com/GoogleContainerTools/kpt/pkg/kptfile"
12
13
"github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
13
14
"github.com/stretchr/testify/assert"
14
15
"k8s.io/cli-runtime/pkg/genericclioptions"
15
16
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
17
+ "sigs.k8s.io/kustomize/kyaml/yaml"
16
18
)
17
19
18
20
var (
@@ -202,3 +204,67 @@ func TestKptInitOptions_updateKptfile(t *testing.T) {
202
204
})
203
205
}
204
206
}
207
+
208
+ func TestInitCmd (t * testing.T ) {
209
+ testCases := map [string ]struct {
210
+ name string
211
+ namespace string
212
+ id string
213
+ expectedID string
214
+ }{
215
+ "generates an inventory id if one is not provided" : {
216
+ name : "foo" ,
217
+ namespace : "bar" ,
218
+ id : "" ,
219
+ expectedID : "0717f9c46d01349e9d575ab1a5131886fb086a43" ,
220
+ },
221
+ "uses the inventory id if one is provided" : {
222
+ name : "foo" ,
223
+ namespace : "bar" ,
224
+ id : "abc123" ,
225
+ expectedID : "abc123" ,
226
+ },
227
+ }
228
+
229
+ for tn , tc := range testCases {
230
+ t .Run (tn , func (t * testing.T ) {
231
+ tf := cmdtesting .NewTestFactory ().WithNamespace ("test-ns" )
232
+ defer tf .Cleanup ()
233
+ ioStreams , _ , _ , _ := genericclioptions .NewTestIOStreams () //nolint:dogsled
234
+
235
+ dir , err := ioutil .TempDir ("" , "kpt-init-options-test" )
236
+ assert .NoError (t , err )
237
+ kf := kptfile.KptFile {
238
+ ResourceMeta : yaml.ResourceMeta {
239
+ ObjectMeta : yaml.ObjectMeta {
240
+ NameMeta : yaml.NameMeta {
241
+ Name : filepath .Base (dir ),
242
+ },
243
+ },
244
+ TypeMeta : yaml.TypeMeta {
245
+ APIVersion : kptfile .TypeMeta .APIVersion ,
246
+ Kind : kptfile .TypeMeta .Kind },
247
+ },
248
+ }
249
+ err = kptfileutil .WriteFile (dir , kf )
250
+ if ! assert .NoError (t , err ) {
251
+ t .FailNow ()
252
+ }
253
+
254
+ io := NewKptInitOptions (tf , ioStreams )
255
+ io .name = tc .name
256
+ io .namespace = tc .namespace
257
+ io .inventoryID = tc .id
258
+ err = io .Run ([]string {dir })
259
+ if ! assert .NoError (t , err ) {
260
+ t .FailNow ()
261
+ }
262
+
263
+ newKf , err := kptfileutil .ReadFile (dir )
264
+ if ! assert .NoError (t , err ) {
265
+ t .FailNow ()
266
+ }
267
+ assert .Contains (t , newKf .Inventory .InventoryID , tc .expectedID )
268
+ })
269
+ }
270
+ }
0 commit comments