@@ -9,10 +9,12 @@ import (
99 "testing"
1010 "time"
1111
12+ "github.com/GoogleContainerTools/kpt/pkg/kptfile"
1213 "github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
1314 "github.com/stretchr/testify/assert"
1415 "k8s.io/cli-runtime/pkg/genericclioptions"
1516 cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
17+ "sigs.k8s.io/kustomize/kyaml/yaml"
1618)
1719
1820var (
@@ -202,3 +204,67 @@ func TestKptInitOptions_updateKptfile(t *testing.T) {
202204 })
203205 }
204206}
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