github.com/mgoltzsche/khelm@v1.0.1/cmd/khelm/template_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  	"k8s.io/helm/pkg/repo"
    12  )
    13  
    14  func TestTemplateCommand(t *testing.T) {
    15  	exampleDir := filepath.Join("..", "..", "example")
    16  	for _, c := range []struct {
    17  		name           string
    18  		args           []string
    19  		mustContainObj int
    20  		mustContain    string
    21  	}{
    22  		{
    23  			"chart path only",
    24  			[]string{filepath.Join(exampleDir, "namespace")},
    25  			3, "myconfigb",
    26  		},
    27  		{
    28  			"latest cluster scoped remote chart",
    29  			[]string{"cert-manager", "--repo=https://charts.jetstack.io",
    30  				"--trust-any-repo"},
    31  			-1, "acme.cert-manager.io",
    32  		},
    33  		{
    34  			"remote chart with version",
    35  			[]string{"cert-manager", "--version=0.9.x",
    36  				"--repository=https://charts.jetstack.io",
    37  				"--repo=https://charts.jetstack.io",
    38  				"--trust-any-repo"},
    39  			34, "chart: cainjector-v0.9.1",
    40  		},
    41  		{
    42  			"release name",
    43  			[]string{filepath.Join(exampleDir, "release-name"), "--name=myrelease"},
    44  			1, "myrelease-config",
    45  		},
    46  		{
    47  			"values",
    48  			[]string{filepath.Join(exampleDir, "values-inheritance", "chart"),
    49  				"--values=" + filepath.Join(exampleDir, "values-inheritance", "values.yaml")},
    50  			1, " valueoverwrite: overwritten by file",
    51  		},
    52  		{
    53  			"set",
    54  			[]string{filepath.Join(exampleDir, "values-inheritance", "chart"),
    55  				"--set=example.other1=a,example.overrideValue=explicitly,example.other2=b", "--set=example.other1=x"},
    56  			1, " valueoverwrite: explicitly",
    57  		},
    58  		{
    59  			"set override",
    60  			[]string{filepath.Join(exampleDir, "values-inheritance", "chart"),
    61  				"--values=" + filepath.Join(exampleDir, "values-inheritance", "values.yaml"),
    62  				"--set=example.other1=a,example.overrideValue=explicitly,example.other2=b", "--set=example.other1=x"},
    63  			1, " valueoverwrite: explicitly",
    64  		},
    65  		{
    66  			"apiversions",
    67  			[]string{filepath.Join(exampleDir, "apiversions-condition", "chart"),
    68  				"--api-versions=myfancyapi/v1", "--api-versions=someapi/v1alpha1"},
    69  			1, "fancycr",
    70  		},
    71  		{
    72  			"kubeversion",
    73  			[]string{filepath.Join(exampleDir, "release-name"),
    74  				"--kube-version=1.17"},
    75  			1, "k8sVersion: v1.17.0",
    76  		},
    77  		{
    78  			"namespace",
    79  			[]string{filepath.Join(exampleDir, "namespace"), "--namespace=mynamespace"},
    80  			3, "namespace: mynamespace",
    81  		},
    82  		{
    83  			"force-namespace",
    84  			[]string{filepath.Join(exampleDir, "force-namespace"), "--force-namespace=forced-namespace"},
    85  			5, "namespace: forced-namespace",
    86  		},
    87  		{
    88  			"chart-hooks",
    89  			[]string{filepath.Join(exampleDir, "chart-hooks")},
    90  			10, "helm.sh/hook",
    91  		},
    92  		{
    93  			"chart-hooks-excluded",
    94  			[]string{filepath.Join(exampleDir, "chart-hooks"), "--no-hooks"},
    95  			1, "myvalue",
    96  		},
    97  	} {
    98  		t.Run(c.name, func(t *testing.T) {
    99  			var out bytes.Buffer
   100  			os.Args = append([]string{"testee", "template"}, c.args...)
   101  			err := Execute(nil, &out)
   102  			require.NoError(t, err)
   103  			validateYAML(t, out.Bytes(), c.mustContainObj)
   104  			require.Contains(t, out.String(), c.mustContain, "output of %+v", c.args)
   105  		})
   106  	}
   107  }
   108  
   109  func TestTemplateCommandError(t *testing.T) {
   110  	dir, err := ioutil.TempDir("", "khelm-tpl-test-")
   111  	require.NoError(t, err)
   112  	repoDir := filepath.Join(dir, "repository")
   113  	defer os.RemoveAll(dir)
   114  	os.Setenv("HELM_HOME", dir)
   115  	defer os.Unsetenv("HELM_HOME")
   116  	err = os.Mkdir(repoDir, 0755)
   117  	require.NoError(t, err)
   118  	err = repo.NewRepoFile().WriteFile(filepath.Join(repoDir, "repositories.yaml"), 0644)
   119  	require.NoError(t, err)
   120  	for _, c := range []struct {
   121  		name string
   122  		args []string
   123  	}{
   124  		{
   125  			"reject untrusted repo",
   126  			[]string{"cert-manager", "--repo=https://charts.jetstack.io"},
   127  		},
   128  		{
   129  			"reject cluster scoped resources",
   130  			[]string{"cert-manager", "--repo=https://charts.jetstack.io", "--namespaced-only"},
   131  		},
   132  	} {
   133  		t.Run(c.name, func(t *testing.T) {
   134  			os.Args = append([]string{"testee", "template"}, c.args...)
   135  			err := Execute(nil, &bytes.Buffer{})
   136  			require.Error(t, err)
   137  		})
   138  	}
   139  }