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