github.com/argoproj/argo-cd@v1.8.7/util/helm/helm_test.go (about)

     1  package helm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/argoproj/gitops-engine/pkg/utils/kube"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	appsv1 "k8s.io/api/apps/v1"
    12  	apiv1 "k8s.io/api/core/v1"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  	"k8s.io/apimachinery/pkg/runtime"
    15  )
    16  
    17  func template(h Helm, opts *TemplateOpts) ([]*unstructured.Unstructured, error) {
    18  	out, err := h.Template(opts)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	return kube.SplitYAML([]byte(out))
    23  }
    24  
    25  func TestHelmTemplateParams(t *testing.T) {
    26  	h, err := NewHelmApp("./testdata/minio", []HelmRepository{}, false, "")
    27  	assert.NoError(t, err)
    28  	opts := TemplateOpts{
    29  		Name: "test",
    30  		Set: map[string]string{
    31  			"service.type": "LoadBalancer",
    32  			"service.port": "1234",
    33  		},
    34  		SetString: map[string]string{
    35  			"service.annotations.prometheus\\.io/scrape": "true",
    36  		},
    37  	}
    38  	objs, err := template(h, &opts)
    39  	assert.Nil(t, err)
    40  	assert.Equal(t, 5, len(objs))
    41  
    42  	for _, obj := range objs {
    43  		if obj.GetKind() == "Service" && obj.GetName() == "test-minio" {
    44  			var svc apiv1.Service
    45  			err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &svc)
    46  			assert.Nil(t, err)
    47  			assert.Equal(t, apiv1.ServiceTypeLoadBalancer, svc.Spec.Type)
    48  			assert.Equal(t, int32(1234), svc.Spec.Ports[0].TargetPort.IntVal)
    49  			assert.Equal(t, "true", svc.ObjectMeta.Annotations["prometheus.io/scrape"])
    50  		}
    51  	}
    52  }
    53  
    54  func TestHelmTemplateValues(t *testing.T) {
    55  	h, err := NewHelmApp("./testdata/redis", []HelmRepository{}, false, "")
    56  	assert.NoError(t, err)
    57  	opts := TemplateOpts{
    58  		Name:   "test",
    59  		Values: []string{"values-production.yaml"},
    60  	}
    61  	objs, err := template(h, &opts)
    62  	assert.Nil(t, err)
    63  	assert.Equal(t, 8, len(objs))
    64  
    65  	for _, obj := range objs {
    66  		if obj.GetKind() == "Deployment" && obj.GetName() == "test-redis-slave" {
    67  			var dep appsv1.Deployment
    68  			err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &dep)
    69  			assert.Nil(t, err)
    70  			assert.Equal(t, int32(3), *dep.Spec.Replicas)
    71  		}
    72  	}
    73  }
    74  
    75  func TestHelmGetParams(t *testing.T) {
    76  	h, err := NewHelmApp("./testdata/redis", nil, false, "")
    77  	assert.NoError(t, err)
    78  	params, err := h.GetParameters([]string{})
    79  	assert.Nil(t, err)
    80  
    81  	slaveCountParam := params["cluster.slaveCount"]
    82  	assert.Equal(t, slaveCountParam, "1")
    83  }
    84  
    85  func TestHelmGetParamsValueFiles(t *testing.T) {
    86  	h, err := NewHelmApp("./testdata/redis", nil, false, "")
    87  	assert.NoError(t, err)
    88  	params, err := h.GetParameters([]string{"values-production.yaml"})
    89  	assert.Nil(t, err)
    90  
    91  	slaveCountParam := params["cluster.slaveCount"]
    92  	assert.Equal(t, slaveCountParam, "3")
    93  }
    94  
    95  func TestHelmDependencyBuild(t *testing.T) {
    96  	testCases := map[string]string{"Helm": "dependency", "Helm2": "helm2-dependency"}
    97  	helmRepos := []HelmRepository{{Name: "bitnami", Repo: "https://charts.bitnami.com/bitnami"}}
    98  	for name := range testCases {
    99  		t.Run(name, func(t *testing.T) {
   100  			chart := testCases[name]
   101  			clean := func() {
   102  				_ = os.RemoveAll(fmt.Sprintf("./testdata/%s/charts", chart))
   103  				_ = os.RemoveAll(fmt.Sprintf("./testdata/%s/Chart.lock", chart))
   104  			}
   105  			clean()
   106  			defer clean()
   107  			h, err := NewHelmApp(fmt.Sprintf("./testdata/%s", chart), helmRepos, false, "")
   108  			assert.NoError(t, err)
   109  			err = h.Init()
   110  			assert.NoError(t, err)
   111  			_, err = h.Template(&TemplateOpts{Name: "wordpress"})
   112  			assert.Error(t, err)
   113  			err = h.DependencyBuild()
   114  			assert.NoError(t, err)
   115  			_, err = h.Template(&TemplateOpts{Name: "wordpress"})
   116  			assert.NoError(t, err)
   117  		})
   118  	}
   119  }
   120  
   121  func TestHelmTemplateReleaseNameOverwrite(t *testing.T) {
   122  	h, err := NewHelmApp("./testdata/redis", nil, false, "")
   123  	assert.NoError(t, err)
   124  
   125  	objs, err := template(h, &TemplateOpts{Name: "my-release"})
   126  	assert.Nil(t, err)
   127  	assert.Equal(t, 5, len(objs))
   128  
   129  	for _, obj := range objs {
   130  		if obj.GetKind() == "StatefulSet" {
   131  			var stateful appsv1.StatefulSet
   132  			err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &stateful)
   133  			assert.Nil(t, err)
   134  			assert.Equal(t, "my-release-redis-master", stateful.ObjectMeta.Name)
   135  		}
   136  	}
   137  }
   138  
   139  func TestHelmTemplateReleaseName(t *testing.T) {
   140  	h, err := NewHelmApp("./testdata/redis", nil, false, "")
   141  	assert.NoError(t, err)
   142  	objs, err := template(h, &TemplateOpts{Name: "test"})
   143  	assert.Nil(t, err)
   144  	assert.Equal(t, 5, len(objs))
   145  
   146  	for _, obj := range objs {
   147  		if obj.GetKind() == "StatefulSet" {
   148  			var stateful appsv1.StatefulSet
   149  			err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &stateful)
   150  			assert.Nil(t, err)
   151  			assert.Equal(t, "test-redis-master", stateful.ObjectMeta.Name)
   152  		}
   153  	}
   154  }
   155  
   156  func TestHelmArgCleaner(t *testing.T) {
   157  	for input, expected := range map[string]string{
   158  		`val`:        `val`,
   159  		`bar`:        `bar`,
   160  		`not, clean`: `not\, clean`,
   161  		`a\,b,c`:     `a\,b\,c`,
   162  	} {
   163  		cleaned := cleanSetParameters(input)
   164  		assert.Equal(t, expected, cleaned)
   165  	}
   166  }
   167  
   168  func TestVersion(t *testing.T) {
   169  	ver, err := Version(false)
   170  	assert.NoError(t, err)
   171  	assert.NotEmpty(t, ver)
   172  }
   173  
   174  func Test_flatVals(t *testing.T) {
   175  	t.Run("Map", func(t *testing.T) {
   176  		output := map[string]string{}
   177  
   178  		flatVals(map[string]interface{}{"foo": map[string]interface{}{"bar": "baz"}}, output)
   179  
   180  		assert.Equal(t, map[string]string{"foo.bar": "baz"}, output)
   181  	})
   182  	t.Run("Array", func(t *testing.T) {
   183  		output := map[string]string{}
   184  
   185  		flatVals(map[string]interface{}{"foo": []interface{}{"bar"}}, output)
   186  
   187  		assert.Equal(t, map[string]string{"foo[0]": "bar"}, output)
   188  	})
   189  	t.Run("Val", func(t *testing.T) {
   190  		output := map[string]string{}
   191  
   192  		flatVals(map[string]interface{}{"foo": 1}, output)
   193  
   194  		assert.Equal(t, map[string]string{"foo": "1"}, output)
   195  	})
   196  }
   197  
   198  func TestAPIVersions(t *testing.T) {
   199  	h, err := NewHelmApp("./testdata/api-versions", nil, false, "")
   200  	if !assert.NoError(t, err) {
   201  		return
   202  	}
   203  
   204  	objs, err := template(h, &TemplateOpts{})
   205  	if !assert.NoError(t, err) || !assert.Len(t, objs, 1) {
   206  		return
   207  	}
   208  	assert.Equal(t, objs[0].GetAPIVersion(), "sample/v1")
   209  
   210  	objs, err = template(h, &TemplateOpts{APIVersions: []string{"sample/v2"}})
   211  	if !assert.NoError(t, err) || !assert.Len(t, objs, 1) {
   212  		return
   213  	}
   214  	assert.Equal(t, objs[0].GetAPIVersion(), "sample/v2")
   215  }