github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/helm_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 integration
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    26  	"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
    27  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    28  )
    29  
    30  func TestHelmDeploy(t *testing.T) {
    31  	MarkIntegrationTest(t, NeedsGcp)
    32  
    33  	ns, client := SetupNamespace(t)
    34  
    35  	// To fix #1823, we make use of env variable templating for release name
    36  	env := []string{fmt.Sprintf("TEST_NS=%s", ns.Name)}
    37  	skaffold.Deploy("--images", "us-central1-docker.pkg.dev/k8s-skaffold/testing/skaffold-helm").InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
    38  
    39  	dep := client.GetDeployment("skaffold-helm-" + ns.Name)
    40  	testutil.CheckDeepEqual(t, dep.Name, dep.ObjectMeta.Labels["release"])
    41  
    42  	skaffold.Delete().InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
    43  }
    44  
    45  func TestHelmDeployWithHook(t *testing.T) {
    46  	MarkIntegrationTest(t, CanRunWithoutGcp)
    47  
    48  	ns, client := SetupNamespace(t)
    49  
    50  	// To fix #1823, we make use of env variable templating for release name
    51  	replicas := 5
    52  	env := []string{fmt.Sprintf("REPLICAS=%d", replicas), fmt.Sprintf("TEST_NS=%s", ns.Name)}
    53  	skaffold.Deploy("--images", "us-central1-docker.pkg.dev/k8s-skaffold/testing/skaffold-helm", "-p", "helm-hook").InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
    54  
    55  	dep := client.GetDeployment("skaffold-helm-" + ns.Name)
    56  	testutil.CheckDeepEqual(t, dep.Spec.Replicas, util.Ptr(int32(replicas)))
    57  
    58  	skaffold.Delete().InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
    59  }
    60  
    61  func TestRunHelmMultiConfig(t *testing.T) {
    62  	var tests = []struct {
    63  		description  string
    64  		dir          string
    65  		args         []string
    66  		deployments  []string
    67  		pods         []string
    68  		env          []string
    69  		targetLogOne string
    70  		targetLogTwo string
    71  	}{
    72  		{
    73  			description:  "helm-multi-config",
    74  			dir:          "testdata/helm-multi-config/skaffold",
    75  			deployments:  []string{"app1", "app2"},
    76  			targetLogOne: "app1",
    77  			targetLogTwo: "app2",
    78  		},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		t.Run(test.description, func(t *testing.T) {
    83  			MarkIntegrationTest(t, CanRunWithoutGcp)
    84  			if test.targetLogOne == "" || test.targetLogTwo == "" {
    85  				t.SkipNow()
    86  			}
    87  			if test.dir == emptydir {
    88  				err := os.MkdirAll(filepath.Join(test.dir, "emptydir"), 0755)
    89  				t.Log("Creating empty directory")
    90  				if err != nil {
    91  					t.Errorf("Error creating empty dir: %s", err)
    92  				}
    93  			}
    94  
    95  			ns, _ := SetupNamespace(t)
    96  
    97  			skaffold.Run(test.args...).InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunOrFailOutput(t)
    98  
    99  			out := skaffold.Run(test.args...).InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunLive(t)
   100  			defer skaffold.Delete().InDir(test.dir).InNs(ns.Name).WithEnv(test.env).Run(t)
   101  
   102  			WaitForLogs(t, out, test.targetLogOne)
   103  			WaitForLogs(t, out, test.targetLogTwo)
   104  		})
   105  	}
   106  }
   107  
   108  func TestRunHelmStatefulSet(t *testing.T) {
   109  	var tests = []struct {
   110  		description string
   111  		dir         string
   112  		args        []string
   113  		pods        []string
   114  		env         []string
   115  		targetLog   string
   116  	}{
   117  		{
   118  			description: "helm-statefulset-v1-schema",
   119  			dir:         "testdata/helm-statefulset-v1-schema",
   120  			targetLog:   "statefulset/skaffold-helm is ready",
   121  		},
   122  	}
   123  
   124  	for _, test := range tests {
   125  		t.Run(test.description, func(t *testing.T) {
   126  			MarkIntegrationTest(t, CanRunWithoutGcp)
   127  			if test.targetLog == "" {
   128  				t.SkipNow()
   129  			}
   130  			if test.dir == emptydir {
   131  				err := os.MkdirAll(filepath.Join(test.dir, "emptydir"), 0755)
   132  				t.Log("Creating empty directory")
   133  				if err != nil {
   134  					t.Errorf("Error creating empty dir: %s", err)
   135  				}
   136  			}
   137  
   138  			ns, _ := SetupNamespace(t)
   139  
   140  			out := skaffold.Run(test.args...).InDir(test.dir).InNs(ns.Name).WithEnv(test.env).RunOrFailOutput(t)
   141  			defer skaffold.Delete().InDir(test.dir).InNs(ns.Name).WithEnv(test.env).Run(t)
   142  
   143  			testutil.CheckContains(t, test.targetLog, string(out))
   144  		})
   145  	}
   146  }
   147  
   148  func TestHelmRenderWithOCIRegistry(t *testing.T) {
   149  	MarkIntegrationTest(t, NeedsGcp)
   150  
   151  	skaffoldConfig := fmt.Sprintf(`apiVersion: skaffold/v4beta1
   152  kind: Config
   153  
   154  deploy:
   155    helm:
   156      releases:
   157      - name: skaffold-helm-chart-oci
   158        remoteChart: oci://%s/skaffold-helm-chart
   159        setValues:
   160          image: skaffold-helm`, skaffold.DefaultRepo)
   161  
   162  	expectedOutput := `---
   163  # Source: skaffold-helm-chart/templates/deployment.yaml
   164  apiVersion: apps/v1
   165  kind: Deployment
   166  metadata:
   167    name: skaffold-helm-chart
   168    labels:
   169      app: skaffold-helm-chart
   170  spec:
   171    selector:
   172      matchLabels:
   173        app: skaffold-helm-chart
   174    replicas: 2
   175    template:
   176      metadata:
   177        labels:
   178          app: skaffold-helm-chart
   179      spec:
   180        containers:
   181        - name: skaffold-helm-chart
   182          image: skaffold-helm
   183  `
   184  
   185  	tmpDir := testutil.NewTempDir(t)
   186  	tmpDir.Write("skaffold.yaml", skaffoldConfig)
   187  	tmpDir.Chdir()
   188  
   189  	skaffold.Render("--output", "rendered.yaml").RunOrFail(t)
   190  	fileContent, err := os.ReadFile("rendered.yaml")
   191  
   192  	testutil.CheckError(t, false, err)
   193  	testutil.CheckDeepEqual(t, expectedOutput, string(fileContent))
   194  }
   195  
   196  func TestHelmDeployWithGlobalFlags(t *testing.T) {
   197  	MarkIntegrationTest(t, CanRunWithoutGcp)
   198  	ns, _ := SetupNamespace(t)
   199  	// To fix #1823, we make use of env variable templating for release name
   200  	env := []string{fmt.Sprintf("TEST_NS=%s", ns.Name)}
   201  	skaffold.Deploy("--images", "us-central1-docker.pkg.dev/k8s-skaffold/testing/skaffold-helm", "-p", "helm-with-global-flags").InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
   202  	skaffold.Delete().InDir("testdata/helm").InNs(ns.Name).WithEnv(env).RunOrFail(t)
   203  }