github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/pkg/init/cmdinit_test.go (about) 1 // Copyright 2019 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package init_test 16 17 import ( 18 "os" 19 "path/filepath" 20 "strings" 21 "testing" 22 23 initialization "github.com/GoogleContainerTools/kpt/commands/pkg/init" 24 "github.com/GoogleContainerTools/kpt/internal/builtins" 25 "github.com/GoogleContainerTools/kpt/internal/testutil" 26 "github.com/GoogleContainerTools/kpt/internal/util/man" 27 "github.com/GoogleContainerTools/kpt/pkg/printer/fake" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestMain(m *testing.M) { 32 os.Exit(testutil.ConfigureTestKptCache(m)) 33 } 34 35 // TestCmd verifies the directory is initialized 36 func TestCmd(t *testing.T) { 37 d := t.TempDir() 38 assert.NoError(t, os.Mkdir(filepath.Join(d, "my-pkg"), 0700)) 39 40 r := initialization.NewRunner(fake.CtxWithDefaultPrinter(), "kpt") 41 r.Command.SetArgs([]string{filepath.Join(d, "my-pkg"), "--description", "my description"}) 42 err := r.Command.Execute() 43 assert.NoError(t, err) 44 45 // verify the contents 46 b, err := os.ReadFile(filepath.Join(d, "my-pkg", "Kptfile")) 47 assert.NoError(t, err) 48 assert.Equal(t, `apiVersion: kpt.dev/v1 49 kind: Kptfile 50 metadata: 51 name: my-pkg 52 annotations: 53 config.kubernetes.io/local-config: "true" 54 info: 55 description: my description 56 `, string(b)) 57 58 b, err = os.ReadFile(filepath.Join(d, "my-pkg", man.ManFilename)) 59 assert.NoError(t, err) 60 assert.Equal(t, strings.ReplaceAll(`# my-pkg 61 62 ## Description 63 my description 64 65 ## Usage 66 67 ### Fetch the package 68 'kpt pkg get REPO_URI[.git]/PKG_PATH[@VERSION] my-pkg' 69 Details: https://kpt.dev/reference/cli/pkg/get/ 70 71 ### View package content 72 'kpt pkg tree my-pkg' 73 Details: https://kpt.dev/reference/cli/pkg/tree/ 74 75 ### Apply the package 76 ''' 77 kpt live init my-pkg 78 kpt live apply my-pkg --reconcile-timeout=2m --output=table 79 ''' 80 Details: https://kpt.dev/reference/cli/live/ 81 `, "'", "`"), string(b)) 82 83 b, err = os.ReadFile(filepath.Join(d, "my-pkg", builtins.PkgContextFile)) 84 assert.NoError(t, err) 85 assert.Equal(t, b, []byte(builtins.AbstractPkgContext())) 86 } 87 88 func TestCmd_currentDir(t *testing.T) { 89 d := t.TempDir() 90 assert.NoError(t, os.Mkdir(filepath.Join(d, "my-pkg"), 0700)) 91 packageDir := filepath.Join(d, "my-pkg") 92 currentDir, err := os.Getwd() 93 assert.NoError(t, err) 94 err = func() error { 95 nestedErr := os.Chdir(packageDir) 96 if nestedErr != nil { 97 return nestedErr 98 } 99 defer func() { 100 deferErr := os.Chdir(currentDir) 101 if deferErr != nil { 102 panic(deferErr) 103 } 104 }() 105 106 r := initialization.NewRunner(fake.CtxWithDefaultPrinter(), "kpt") 107 r.Command.SetArgs([]string{".", "--description", "my description"}) 108 return r.Command.Execute() 109 }() 110 assert.NoError(t, err) 111 112 // verify the contents 113 b, err := os.ReadFile(filepath.Join(packageDir, "Kptfile")) 114 assert.NoError(t, err) 115 assert.Equal(t, `apiVersion: kpt.dev/v1 116 kind: Kptfile 117 metadata: 118 name: my-pkg 119 annotations: 120 config.kubernetes.io/local-config: "true" 121 info: 122 description: my description 123 `, string(b)) 124 } 125 126 func TestCmd_DefaultToCurrentDir(t *testing.T) { 127 d := t.TempDir() 128 assert.NoError(t, os.Mkdir(filepath.Join(d, "my-pkg"), 0700)) 129 packageDir := filepath.Join(d, "my-pkg") 130 currentDir, err := os.Getwd() 131 assert.NoError(t, err) 132 err = func() error { 133 nestedErr := os.Chdir(packageDir) 134 if nestedErr != nil { 135 return nestedErr 136 } 137 defer func() { 138 deferErr := os.Chdir(currentDir) 139 if deferErr != nil { 140 panic(deferErr) 141 } 142 }() 143 144 r := initialization.NewRunner(fake.CtxWithDefaultPrinter(), "kpt") 145 r.Command.SetArgs([]string{"--description", "my description"}) 146 return r.Command.Execute() 147 }() 148 assert.NoError(t, err) 149 150 // verify the contents 151 b, err := os.ReadFile(filepath.Join(packageDir, "Kptfile")) 152 assert.NoError(t, err) 153 assert.Equal(t, `apiVersion: kpt.dev/v1 154 kind: Kptfile 155 metadata: 156 name: my-pkg 157 annotations: 158 config.kubernetes.io/local-config: "true" 159 info: 160 description: my description 161 `, string(b)) 162 } 163 164 // TestCmd_failExists verifies the command throws and error if the directory exists 165 func TestCmd_failNotExists(t *testing.T) { 166 d := t.TempDir() 167 r := initialization.NewRunner(fake.CtxWithDefaultPrinter(), "kpt") 168 r.Command.SetArgs([]string{filepath.Join(d, "my-pkg"), "--description", "my description"}) 169 err := r.Command.Execute() 170 if assert.Error(t, err) { 171 assert.Contains(t, err.Error(), "does not exist") 172 } 173 }