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

     1  package kustomize
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/argoproj/pkg/exec"
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    14  	"github.com/argoproj/argo-cd/util/git"
    15  )
    16  
    17  const kustomization1 = "kustomization_yaml"
    18  const kustomization2a = "kustomization_yml"
    19  const kustomization2b = "Kustomization"
    20  
    21  func testDataDir() (string, error) {
    22  	res, err := ioutil.TempDir("", "kustomize-test")
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  	_, err = exec.RunCommand("cp", exec.CmdOpts{}, "-r", "./testdata/"+kustomization1, filepath.Join(res, "testdata"))
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	return path.Join(res, "testdata"), nil
    31  }
    32  
    33  func TestKustomizeBuild(t *testing.T) {
    34  	appPath, err := testDataDir()
    35  	assert.Nil(t, err)
    36  	namePrefix := "namePrefix-"
    37  	nameSuffix := "-nameSuffix"
    38  	kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "")
    39  	kustomizeSource := v1alpha1.ApplicationSourceKustomize{
    40  		NamePrefix: namePrefix,
    41  		NameSuffix: nameSuffix,
    42  		Images:     v1alpha1.KustomizeImages{"nginx:1.15.5"},
    43  		CommonLabels: map[string]string{
    44  			"app.kubernetes.io/managed-by": "argo-cd",
    45  			"app.kubernetes.io/part-of":    "argo-cd-tests",
    46  		},
    47  		CommonAnnotations: map[string]string{
    48  			"app.kubernetes.io/managed-by": "argo-cd",
    49  			"app.kubernetes.io/part-of":    "argo-cd-tests",
    50  		},
    51  	}
    52  	objs, images, err := kustomize.Build(&kustomizeSource, nil)
    53  	assert.Nil(t, err)
    54  	if err != nil {
    55  		assert.Equal(t, len(objs), 2)
    56  		assert.Equal(t, len(images), 2)
    57  	}
    58  	for _, obj := range objs {
    59  		fmt.Println(obj.GetAnnotations())
    60  		switch obj.GetKind() {
    61  		case "StatefulSet":
    62  			assert.Equal(t, namePrefix+"web"+nameSuffix, obj.GetName())
    63  			assert.Equal(t, map[string]string{
    64  				"app.kubernetes.io/managed-by": "argo-cd",
    65  				"app.kubernetes.io/part-of":    "argo-cd-tests",
    66  			}, obj.GetLabels())
    67  			assert.Equal(t, map[string]string{
    68  				"app.kubernetes.io/managed-by": "argo-cd",
    69  				"app.kubernetes.io/part-of":    "argo-cd-tests",
    70  			}, obj.GetAnnotations())
    71  		case "Deployment":
    72  			assert.Equal(t, namePrefix+"nginx-deployment"+nameSuffix, obj.GetName())
    73  			assert.Equal(t, map[string]string{
    74  				"app":                          "nginx",
    75  				"app.kubernetes.io/managed-by": "argo-cd",
    76  				"app.kubernetes.io/part-of":    "argo-cd-tests",
    77  			}, obj.GetLabels())
    78  			assert.Equal(t, map[string]string{
    79  				"app.kubernetes.io/managed-by": "argo-cd",
    80  				"app.kubernetes.io/part-of":    "argo-cd-tests",
    81  			}, obj.GetAnnotations())
    82  		}
    83  	}
    84  
    85  	for _, image := range images {
    86  		switch image {
    87  		case "nginx":
    88  			assert.Equal(t, "1.15.5", image)
    89  		}
    90  	}
    91  }
    92  
    93  func TestFindKustomization(t *testing.T) {
    94  	testFindKustomization(t, kustomization1, "kustomization.yaml")
    95  	testFindKustomization(t, kustomization2a, "kustomization.yml")
    96  	testFindKustomization(t, kustomization2b, "Kustomization")
    97  }
    98  
    99  func testFindKustomization(t *testing.T, set string, expected string) {
   100  	kustomization, err := (&kustomize{path: "testdata/" + set}).findKustomization()
   101  	assert.Nil(t, err)
   102  	assert.Equal(t, "testdata/"+set+"/"+expected, kustomization)
   103  }
   104  
   105  func TestIsKustomization(t *testing.T) {
   106  	assert.True(t, IsKustomization("kustomization.yaml"))
   107  	assert.True(t, IsKustomization("kustomization.yml"))
   108  	assert.True(t, IsKustomization("Kustomization"))
   109  	assert.False(t, IsKustomization("rubbish.yml"))
   110  }
   111  
   112  func TestParseKustomizeBuildOptions(t *testing.T) {
   113  	built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr")
   114  	assert.Equal(t, []string{"build", "guestbook", "-v", "6", "--logtostderr"}, built)
   115  }
   116  
   117  func TestVersion(t *testing.T) {
   118  	ver, err := Version(false)
   119  	assert.NoError(t, err)
   120  	assert.NotEmpty(t, ver)
   121  }