github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd 18 19 import ( 20 "fmt" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "helm.sh/helm/v3/pkg/chart" 26 "helm.sh/helm/v3/pkg/chart/loader" 27 "helm.sh/helm/v3/pkg/chartutil" 28 "helm.sh/helm/v3/pkg/helmpath" 29 ) 30 31 func TestCreateCmd(t *testing.T) { 32 // defer ensure.HelmHome(t)() 33 cname := "testchart" 34 // dir := ensure.TempDir(t) 35 // defer testChdir(t, dir)() 36 37 // Run a create 38 if _, _, err := executeActionCommand("create " + cname); err != nil { 39 t.Fatalf("Failed to run create: %s", err) 40 } 41 42 // Test that the chart is there 43 if fi, err := os.Stat(cname); err != nil { 44 t.Fatalf("no chart directory: %s", err) 45 } else if !fi.IsDir() { 46 t.Fatalf("chart is not directory") 47 } 48 49 c, err := loader.LoadDir(cname) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if c.Name() != cname { 55 t.Errorf("Expected %q name, got %q", cname, c.Name()) 56 } 57 if c.Metadata.APIVersion != chart.APIVersionV2 { 58 t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) 59 } 60 } 61 62 func TestCreateStarterCmd(t *testing.T) { 63 // defer ensure.HelmHome(t)() 64 cname := "testchart" 65 defer resetEnv()() 66 os.MkdirAll(helmpath.CachePath(), 0755) 67 defer testChdir(t, helmpath.CachePath())() 68 69 // Create a starter. 70 starterchart := helmpath.DataPath("starters") 71 os.MkdirAll(starterchart, 0755) 72 if dest, err := chartutil.Create("starterchart", starterchart); err != nil { 73 t.Fatalf("Could not create chart: %s", err) 74 } else { 75 t.Logf("Created %s", dest) 76 } 77 tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl") 78 if err := os.WriteFile(tplpath, []byte("test"), 0644); err != nil { 79 t.Fatalf("Could not write template: %s", err) 80 } 81 82 // Run a create 83 if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname)); err != nil { 84 t.Errorf("Failed to run create: %s", err) 85 return 86 } 87 88 // Test that the chart is there 89 if fi, err := os.Stat(cname); err != nil { 90 t.Fatalf("no chart directory: %s", err) 91 } else if !fi.IsDir() { 92 t.Fatalf("chart is not directory") 93 } 94 95 c, err := loader.LoadDir(cname) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 if c.Name() != cname { 101 t.Errorf("Expected %q name, got %q", cname, c.Name()) 102 } 103 if c.Metadata.APIVersion != chart.APIVersionV2 { 104 t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) 105 } 106 107 expectedNumberOfTemplates := 9 108 if l := len(c.Templates); l != expectedNumberOfTemplates { 109 t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) 110 } 111 112 found := false 113 for _, tpl := range c.Templates { 114 if tpl.Name == "templates/foo.tpl" { 115 found = true 116 if data := string(tpl.Data); data != "test" { 117 t.Errorf("Expected template 'test', got %q", data) 118 } 119 } 120 } 121 if !found { 122 t.Error("Did not find foo.tpl") 123 } 124 125 } 126 127 func TestCreateStarterAbsoluteCmd(t *testing.T) { 128 defer resetEnv()() 129 // defer ensure.HelmHome(t)() 130 cname := "testchart" 131 132 // Create a starter. 133 starterchart := helmpath.DataPath("starters") 134 os.MkdirAll(starterchart, 0755) 135 if dest, err := chartutil.Create("starterchart", starterchart); err != nil { 136 t.Fatalf("Could not create chart: %s", err) 137 } else { 138 t.Logf("Created %s", dest) 139 } 140 tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl") 141 if err := os.WriteFile(tplpath, []byte("test"), 0644); err != nil { 142 t.Fatalf("Could not write template: %s", err) 143 } 144 145 os.MkdirAll(helmpath.CachePath(), 0755) 146 defer testChdir(t, helmpath.CachePath())() 147 148 starterChartPath := filepath.Join(starterchart, "starterchart") 149 150 // Run a create 151 if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil { 152 t.Errorf("Failed to run create: %s", err) 153 return 154 } 155 156 // Test that the chart is there 157 if fi, err := os.Stat(cname); err != nil { 158 t.Fatalf("no chart directory: %s", err) 159 } else if !fi.IsDir() { 160 t.Fatalf("chart is not directory") 161 } 162 163 c, err := loader.LoadDir(cname) 164 if err != nil { 165 t.Fatal(err) 166 } 167 168 if c.Name() != cname { 169 t.Errorf("Expected %q name, got %q", cname, c.Name()) 170 } 171 if c.Metadata.APIVersion != chart.APIVersionV2 { 172 t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) 173 } 174 175 expectedNumberOfTemplates := 9 176 if l := len(c.Templates); l != expectedNumberOfTemplates { 177 t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) 178 } 179 180 found := false 181 for _, tpl := range c.Templates { 182 if tpl.Name == "templates/foo.tpl" { 183 found = true 184 if data := string(tpl.Data); data != "test" { 185 t.Errorf("Expected template 'test', got %q", data) 186 } 187 } 188 } 189 if !found { 190 t.Error("Did not find foo.tpl") 191 } 192 } 193 194 func TestCreateFileCompletion(t *testing.T) { 195 checkFileCompletion(t, "create", true) 196 checkFileCompletion(t, "create myname", false) 197 }