github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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  	"github.com/codefresh-io/kcfi/pkg/helm-internal/test/ensure"
    27  	"helm.sh/helm/v3/pkg/chart"
    28  	"helm.sh/helm/v3/pkg/chart/loader"
    29  	"helm.sh/helm/v3/pkg/chartutil"
    30  	"helm.sh/helm/v3/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  	expectedNumberOfTemplates := 9
   110  	if l := len(c.Templates); l != expectedNumberOfTemplates {
   111  		t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
   112  	}
   113  
   114  	found := false
   115  	for _, tpl := range c.Templates {
   116  		if tpl.Name == "templates/foo.tpl" {
   117  			found = true
   118  			if data := string(tpl.Data); data != "test" {
   119  				t.Errorf("Expected template 'test', got %q", data)
   120  			}
   121  		}
   122  	}
   123  	if !found {
   124  		t.Error("Did not find foo.tpl")
   125  	}
   126  
   127  }
   128  
   129  func TestCreateStarterAbsoluteCmd(t *testing.T) {
   130  	defer resetEnv()()
   131  	defer ensure.HelmHome(t)()
   132  	cname := "testchart"
   133  
   134  	// Create a starter.
   135  	starterchart := helmpath.DataPath("starters")
   136  	os.MkdirAll(starterchart, 0755)
   137  	if dest, err := chartutil.Create("starterchart", starterchart); err != nil {
   138  		t.Fatalf("Could not create chart: %s", err)
   139  	} else {
   140  		t.Logf("Created %s", dest)
   141  	}
   142  	tplpath := filepath.Join(starterchart, "starterchart", "templates", "foo.tpl")
   143  	if err := ioutil.WriteFile(tplpath, []byte("test"), 0644); err != nil {
   144  		t.Fatalf("Could not write template: %s", err)
   145  	}
   146  
   147  	os.MkdirAll(helmpath.CachePath(), 0755)
   148  	defer testChdir(t, helmpath.CachePath())()
   149  
   150  	starterChartPath := filepath.Join(starterchart, "starterchart")
   151  
   152  	// Run a create
   153  	if _, _, err := executeActionCommand(fmt.Sprintf("create --starter=%s %s", starterChartPath, cname)); err != nil {
   154  		t.Errorf("Failed to run create: %s", err)
   155  		return
   156  	}
   157  
   158  	// Test that the chart is there
   159  	if fi, err := os.Stat(cname); err != nil {
   160  		t.Fatalf("no chart directory: %s", err)
   161  	} else if !fi.IsDir() {
   162  		t.Fatalf("chart is not directory")
   163  	}
   164  
   165  	c, err := loader.LoadDir(cname)
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	if c.Name() != cname {
   171  		t.Errorf("Expected %q name, got %q", cname, c.Name())
   172  	}
   173  	if c.Metadata.APIVersion != chart.APIVersionV2 {
   174  		t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
   175  	}
   176  
   177  	expectedNumberOfTemplates := 9
   178  	if l := len(c.Templates); l != expectedNumberOfTemplates {
   179  		t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
   180  	}
   181  
   182  	found := false
   183  	for _, tpl := range c.Templates {
   184  		if tpl.Name == "templates/foo.tpl" {
   185  			found = true
   186  			if data := string(tpl.Data); data != "test" {
   187  				t.Errorf("Expected template 'test', got %q", data)
   188  			}
   189  		}
   190  	}
   191  	if !found {
   192  		t.Error("Did not find foo.tpl")
   193  	}
   194  }