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

     1  /*
     2  Copyright 2023 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  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    24  	"github.com/GoogleContainerTools/skaffold/v2/testutil"
    25  )
    26  
    27  func TestPostDeployHooksNamespaces(t *testing.T) {
    28  	MarkIntegrationTest(t, CanRunWithoutGcp)
    29  
    30  	tests := []struct {
    31  		description  string
    32  		dir          string
    33  		configFile   string
    34  		resourceFile string
    35  	}{
    36  		{
    37  			description: "set SKAFFOLD_NAMESPACES from manifests with kubectl deployer",
    38  			configFile: `apiVersion: skaffold/v4beta6
    39  kind: Config
    40  manifests:
    41    rawYaml:
    42    - pod.yaml
    43  
    44  deploy:
    45    kubectl:
    46      hooks:
    47        after:
    48          - host:
    49              command: ["sh", "-c", "echo $SKAFFOLD_NAMESPACES"]
    50  `,
    51  			resourceFile: `apiVersion: v1
    52  kind: Pod
    53  metadata:
    54    name: getting-started
    55    namespace: %v
    56  spec:
    57    containers:
    58    - name: getting-started
    59      image: us-central1-docker.pkg.dev/k8s-skaffold/testing/skaffold-example
    60  `,
    61  		},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		t.Run(test.description, func(t *testing.T) {
    66  			ns, _ := SetupNamespace(t)
    67  			expectedOutput := fmt.Sprintf("Starting post-deploy hooks...\ndefault,%v", ns.Name)
    68  			resourceWithNs := fmt.Sprintf(test.resourceFile, ns.Name)
    69  
    70  			dir := testutil.NewTempDir(t)
    71  			dir.Write("skaffold.yaml", test.configFile)
    72  			dir.Write("pod.yaml", resourceWithNs)
    73  			out, err := skaffold.Run().InDir(dir.Root()).RunWithCombinedOutput(t)
    74  			testutil.CheckError(t, false, err)
    75  			testutil.CheckContains(t, expectedOutput, string(out))
    76  		})
    77  	}
    78  }