github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/chartutil/create_test.go (about) 1 /* 2 Copyright The Helm 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 chartutil 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/stefanmcshane/helm/pkg/chart" 27 "github.com/stefanmcshane/helm/pkg/chart/loader" 28 ) 29 30 func TestCreate(t *testing.T) { 31 tdir := t.TempDir() 32 33 c, err := Create("foo", tdir) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 dir := filepath.Join(tdir, "foo") 39 40 mychart, err := loader.LoadDir(c) 41 if err != nil { 42 t.Fatalf("Failed to load newly created chart %q: %s", c, err) 43 } 44 45 if mychart.Name() != "foo" { 46 t.Errorf("Expected name to be 'foo', got %q", mychart.Name()) 47 } 48 49 for _, f := range []string{ 50 ChartfileName, 51 DeploymentName, 52 HelpersName, 53 IgnorefileName, 54 NotesName, 55 ServiceAccountName, 56 ServiceName, 57 TemplatesDir, 58 TemplatesTestsDir, 59 TestConnectionName, 60 ValuesfileName, 61 } { 62 if _, err := os.Stat(filepath.Join(dir, f)); err != nil { 63 t.Errorf("Expected %s file: %s", f, err) 64 } 65 } 66 } 67 68 func TestCreateFrom(t *testing.T) { 69 tdir := t.TempDir() 70 71 cf := &chart.Metadata{ 72 APIVersion: chart.APIVersionV1, 73 Name: "foo", 74 Version: "0.1.0", 75 } 76 srcdir := "./testdata/frobnitz/charts/mariner" 77 78 if err := CreateFrom(cf, tdir, srcdir); err != nil { 79 t.Fatal(err) 80 } 81 82 dir := filepath.Join(tdir, "foo") 83 c := filepath.Join(tdir, cf.Name) 84 mychart, err := loader.LoadDir(c) 85 if err != nil { 86 t.Fatalf("Failed to load newly created chart %q: %s", c, err) 87 } 88 89 if mychart.Name() != "foo" { 90 t.Errorf("Expected name to be 'foo', got %q", mychart.Name()) 91 } 92 93 for _, f := range []string{ 94 ChartfileName, 95 ValuesfileName, 96 filepath.Join(TemplatesDir, "placeholder.tpl"), 97 } { 98 if _, err := os.Stat(filepath.Join(dir, f)); err != nil { 99 t.Errorf("Expected %s file: %s", f, err) 100 } 101 102 // Check each file to make sure <CHARTNAME> has been replaced 103 b, err := ioutil.ReadFile(filepath.Join(dir, f)) 104 if err != nil { 105 t.Errorf("Unable to read file %s: %s", f, err) 106 } 107 if bytes.Contains(b, []byte("<CHARTNAME>")) { 108 t.Errorf("File %s contains <CHARTNAME>", f) 109 } 110 } 111 } 112 113 // TestCreate_Overwrite is a regression test for making sure that files are overwritten. 114 func TestCreate_Overwrite(t *testing.T) { 115 tdir := t.TempDir() 116 117 var errlog bytes.Buffer 118 119 if _, err := Create("foo", tdir); err != nil { 120 t.Fatal(err) 121 } 122 123 dir := filepath.Join(tdir, "foo") 124 125 tplname := filepath.Join(dir, "templates/hpa.yaml") 126 writeFile(tplname, []byte("FOO")) 127 128 // Now re-run the create 129 Stderr = &errlog 130 if _, err := Create("foo", tdir); err != nil { 131 t.Fatal(err) 132 } 133 134 data, err := ioutil.ReadFile(tplname) 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 if string(data) == "FOO" { 140 t.Fatal("File that should have been modified was not.") 141 } 142 143 if errlog.Len() == 0 { 144 t.Errorf("Expected warnings about overwriting files.") 145 } 146 } 147 148 func TestValidateChartName(t *testing.T) { 149 for name, shouldPass := range map[string]bool{ 150 "": false, 151 "abcdefghijklmnopqrstuvwxyz-_.": true, 152 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": true, 153 "$hello": false, 154 "HellĂ´": false, 155 "he%%o": false, 156 "he\nllo": false, 157 158 "abcdefghijklmnopqrstuvwxyz-_." + 159 "abcdefghijklmnopqrstuvwxyz-_." + 160 "abcdefghijklmnopqrstuvwxyz-_." + 161 "abcdefghijklmnopqrstuvwxyz-_." + 162 "abcdefghijklmnopqrstuvwxyz-_." + 163 "abcdefghijklmnopqrstuvwxyz-_." + 164 "abcdefghijklmnopqrstuvwxyz-_." + 165 "abcdefghijklmnopqrstuvwxyz-_." + 166 "abcdefghijklmnopqrstuvwxyz-_." + 167 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": false, 168 } { 169 if err := validateChartName(name); (err != nil) == shouldPass { 170 t.Errorf("test for %q failed", name) 171 } 172 } 173 }