2016-10-04 14:13:01 -04:00
|
|
|
/*
|
2018-08-24 15:03:55 -04:00
|
|
|
Copyright The Helm Authors.
|
2016-10-04 14:13:01 -04:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-24 10:11:54 -05:00
|
|
|
package cmd
|
2016-10-04 14:13:01 -04:00
|
|
|
|
|
|
|
|
import (
|
2018-05-09 11:37:20 -04:00
|
|
|
"fmt"
|
2016-10-04 14:13:01 -04:00
|
|
|
"os"
|
2016-11-11 13:12:57 -05:00
|
|
|
"path/filepath"
|
2016-10-04 14:13:01 -04:00
|
|
|
"testing"
|
|
|
|
|
|
2024-12-26 16:33:51 -05:00
|
|
|
"helm.sh/helm/v4/internal/test/ensure"
|
2025-02-25 15:20:44 -05:00
|
|
|
chart "helm.sh/helm/v4/pkg/chart/v2"
|
|
|
|
|
"helm.sh/helm/v4/pkg/chart/v2/loader"
|
|
|
|
|
chartutil "helm.sh/helm/v4/pkg/chart/v2/util"
|
2024-12-26 16:33:51 -05:00
|
|
|
"helm.sh/helm/v4/pkg/helmpath"
|
2016-10-04 14:13:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCreateCmd(t *testing.T) {
|
2025-07-01 18:29:19 -04:00
|
|
|
t.Chdir(t.TempDir())
|
2023-05-06 06:02:32 -04:00
|
|
|
ensure.HelmHome(t)
|
2018-05-14 12:23:21 -04:00
|
|
|
cname := "testchart"
|
2016-10-04 14:13:01 -04:00
|
|
|
|
|
|
|
|
// Run a create
|
2019-02-08 19:02:57 -05:00
|
|
|
if _, _, err := executeActionCommand("create " + cname); err != nil {
|
2019-08-23 02:31:50 -04:00
|
|
|
t.Fatalf("Failed to run create: %s", err)
|
2016-10-04 14:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that the chart is there
|
|
|
|
|
if fi, err := os.Stat(cname); err != nil {
|
|
|
|
|
t.Fatalf("no chart directory: %s", err)
|
|
|
|
|
} else if !fi.IsDir() {
|
|
|
|
|
t.Fatalf("chart is not directory")
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 14:28:29 -04:00
|
|
|
c, err := loader.LoadDir(cname)
|
2016-10-04 14:13:01 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 14:28:29 -04:00
|
|
|
if c.Name() != cname {
|
|
|
|
|
t.Errorf("Expected %q name, got %q", cname, c.Name())
|
2016-10-04 14:13:01 -04:00
|
|
|
}
|
2019-07-29 13:00:01 -04:00
|
|
|
if c.Metadata.APIVersion != chart.APIVersionV2 {
|
2018-04-19 14:09:42 -04:00
|
|
|
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
|
2016-10-04 14:13:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-11-11 13:12:57 -05:00
|
|
|
|
|
|
|
|
func TestCreateStarterCmd(t *testing.T) {
|
2025-07-02 12:30:59 -04:00
|
|
|
t.Chdir(t.TempDir())
|
2023-05-06 06:02:32 -04:00
|
|
|
ensure.HelmHome(t)
|
2016-11-11 13:12:57 -05:00
|
|
|
cname := "testchart"
|
2019-01-14 03:11:21 -05:00
|
|
|
defer resetEnv()()
|
2016-11-11 13:12:57 -05:00
|
|
|
// Create a starter.
|
2019-08-23 02:31:50 -04:00
|
|
|
starterchart := helmpath.DataPath("starters")
|
2025-04-27 16:22:57 -04:00
|
|
|
os.MkdirAll(starterchart, 0o755)
|
2019-04-15 14:17:19 -04:00
|
|
|
if dest, err := chartutil.Create("starterchart", starterchart); err != nil {
|
2016-11-11 13:12:57 -05:00
|
|
|
t.Fatalf("Could not create chart: %s", err)
|
|
|
|
|
} else {
|
|
|
|
|
t.Logf("Created %s", dest)
|
|
|
|
|
}
|
|
|
|
|
tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl")
|
2025-04-27 16:22:57 -04:00
|
|
|
if err := os.WriteFile(tplpath, []byte("test"), 0o644); err != nil {
|
2016-11-11 13:12:57 -05:00
|
|
|
t.Fatalf("Could not write template: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run a create
|
2019-01-14 03:11:21 -05:00
|
|
|
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=starterchart %s", cname)); err != nil {
|
2016-11-11 13:12:57 -05:00
|
|
|
t.Errorf("Failed to run create: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that the chart is there
|
|
|
|
|
if fi, err := os.Stat(cname); err != nil {
|
|
|
|
|
t.Fatalf("no chart directory: %s", err)
|
|
|
|
|
} else if !fi.IsDir() {
|
|
|
|
|
t.Fatalf("chart is not directory")
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 14:28:29 -04:00
|
|
|
c, err := loader.LoadDir(cname)
|
2016-11-11 13:12:57 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 14:28:29 -04:00
|
|
|
if c.Name() != cname {
|
|
|
|
|
t.Errorf("Expected %q name, got %q", cname, c.Name())
|
2016-11-11 13:12:57 -05:00
|
|
|
}
|
2019-07-29 13:00:01 -04:00
|
|
|
if c.Metadata.APIVersion != chart.APIVersionV2 {
|
2018-04-19 14:09:42 -04:00
|
|
|
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
|
2016-11-11 13:12:57 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-11 15:19:21 -04:00
|
|
|
expectedNumberOfTemplates := 10
|
2019-09-24 13:55:00 -04:00
|
|
|
if l := len(c.Templates); l != expectedNumberOfTemplates {
|
|
|
|
|
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
|
2016-11-11 13:12:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
for _, tpl := range c.Templates {
|
|
|
|
|
if tpl.Name == "templates/foo.tpl" {
|
|
|
|
|
found = true
|
2018-05-14 12:23:21 -04:00
|
|
|
if data := string(tpl.Data); data != "test" {
|
|
|
|
|
t.Errorf("Expected template 'test', got %q", data)
|
2016-11-11 13:12:57 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
t.Error("Did not find foo.tpl")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-16 08:44:57 -04:00
|
|
|
|
|
|
|
|
func TestCreateStarterAbsoluteCmd(t *testing.T) {
|
2025-07-01 18:29:19 -04:00
|
|
|
t.Chdir(t.TempDir())
|
2019-07-16 08:44:57 -04:00
|
|
|
defer resetEnv()()
|
2023-05-06 06:02:32 -04:00
|
|
|
ensure.HelmHome(t)
|
2019-07-16 08:44:57 -04:00
|
|
|
cname := "testchart"
|
|
|
|
|
|
|
|
|
|
// Create a starter.
|
2019-08-23 02:31:50 -04:00
|
|
|
starterchart := helmpath.DataPath("starters")
|
2025-04-27 16:22:57 -04:00
|
|
|
os.MkdirAll(starterchart, 0o755)
|
2019-07-16 08:44:57 -04:00
|
|
|
if dest, err := chartutil.Create("starterchart", starterchart); err != nil {
|
|
|
|
|
t.Fatalf("Could not create chart: %s", err)
|
|
|
|
|
} else {
|
|
|
|
|
t.Logf("Created %s", dest)
|
|
|
|
|
}
|
|
|
|
|
tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl")
|
2025-04-27 16:22:57 -04:00
|
|
|
if err := os.WriteFile(tplpath, []byte("test"), 0o644); err != nil {
|
2019-07-16 08:44:57 -04:00
|
|
|
t.Fatalf("Could not write template: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
starterChartPath := filepath.Join(starterchart, "starterchart")
|
|
|
|
|
|
|
|
|
|
// Run a create
|
2019-01-14 03:11:21 -05:00
|
|
|
if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil {
|
2019-07-16 08:44:57 -04:00
|
|
|
t.Errorf("Failed to run create: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that the chart is there
|
|
|
|
|
if fi, err := os.Stat(cname); err != nil {
|
|
|
|
|
t.Fatalf("no chart directory: %s", err)
|
|
|
|
|
} else if !fi.IsDir() {
|
|
|
|
|
t.Fatalf("chart is not directory")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c, err := loader.LoadDir(cname)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Name() != cname {
|
|
|
|
|
t.Errorf("Expected %q name, got %q", cname, c.Name())
|
|
|
|
|
}
|
2019-07-29 13:00:01 -04:00
|
|
|
if c.Metadata.APIVersion != chart.APIVersionV2 {
|
2019-07-16 08:44:57 -04:00
|
|
|
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 15:19:21 -04:00
|
|
|
expectedNumberOfTemplates := 10
|
2019-09-24 13:55:00 -04:00
|
|
|
if l := len(c.Templates); l != expectedNumberOfTemplates {
|
|
|
|
|
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
|
2019-07-16 08:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found := false
|
|
|
|
|
for _, tpl := range c.Templates {
|
|
|
|
|
if tpl.Name == "templates/foo.tpl" {
|
|
|
|
|
found = true
|
|
|
|
|
if data := string(tpl.Data); data != "test" {
|
|
|
|
|
t.Errorf("Expected template 'test', got %q", data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
t.Error("Did not find foo.tpl")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 03:43:34 -04:00
|
|
|
|
|
|
|
|
func TestCreateFileCompletion(t *testing.T) {
|
|
|
|
|
checkFileCompletion(t, "create", true)
|
|
|
|
|
checkFileCompletion(t, "create myname", false)
|
|
|
|
|
}
|