github.com/oam-dev/kubevela@v1.9.11/pkg/addon/init_test.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package addon 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestCheckAddonName(t *testing.T) { 28 var err error 29 30 err = CheckAddonName("") 31 assert.ErrorContains(t, err, "should not be empty") 32 33 invalidNames := []string{ 34 "-addon", 35 "addon-", 36 "Caps", 37 "=", 38 ".", 39 } 40 for _, name := range invalidNames { 41 err = CheckAddonName(name) 42 assert.ErrorContains(t, err, "should only") 43 } 44 45 validNames := []string{ 46 "addon-name", 47 "3-addon-name", 48 "addon-name-3", 49 "addon", 50 } 51 for _, name := range validNames { 52 err = CheckAddonName(name) 53 assert.NoError(t, err) 54 } 55 } 56 57 func TestInitCmd_CreateScaffold(t *testing.T) { 58 var err error 59 60 // empty addon name or path 61 cmd := InitCmd{} 62 err = cmd.CreateScaffold() 63 assert.ErrorContains(t, err, "be empty") 64 65 // invalid addon name 66 cmd = InitCmd{ 67 AddonName: "-name", 68 Path: "name", 69 } 70 err = cmd.CreateScaffold() 71 assert.ErrorContains(t, err, "should only") 72 73 // dir already exists 74 cmd = InitCmd{ 75 AddonName: "name", 76 Path: "testdata", 77 } 78 err = cmd.CreateScaffold() 79 assert.ErrorContains(t, err, "cannot create") 80 81 // with helm component 82 cmd = InitCmd{ 83 AddonName: "with-helm", 84 Path: "with-helm", 85 HelmRepoURL: "https://charts.bitnami.com/bitnami", 86 HelmChartVersion: "12.0.0", 87 HelmChartName: "nginx", 88 } 89 err = cmd.CreateScaffold() 90 assert.NoError(t, err) 91 defer os.RemoveAll("with-helm") 92 _, err = os.Stat(filepath.Join("with-helm", ResourcesDirName, "helm.cue")) 93 assert.NoError(t, err) 94 95 // with ref-obj 96 cmd = InitCmd{ 97 AddonName: "with-refobj", 98 Path: "with-refobj", 99 RefObjURLs: []string{"https:"}, 100 } 101 err = cmd.CreateScaffold() 102 assert.ErrorContains(t, err, "not a valid url") 103 cmd.RefObjURLs[0] = "https://some.com" 104 err = cmd.CreateScaffold() 105 assert.NoError(t, err) 106 defer os.RemoveAll("with-refobj") 107 _, err = os.Stat(filepath.Join("with-refobj", ResourcesDirName, "from-url.cue")) 108 assert.NoError(t, err) 109 }