github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/deploy/kustomize_test.go (about)

     1  /*
     2  Copyright 2020 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 deploy
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  var (
    28  	baseDeployment = `apiVersion: v1
    29  kind: Pod
    30  metadata:
    31  	name: getting-started
    32  spec:
    33  	containers:
    34  	- name: getting-started
    35  	image: skaffold-example`
    36  
    37  	baseKustomization = `apiVersion: kustomize.config.k8s.io/v1beta1
    38  kind: Kustomization
    39  
    40  resources:
    41  	- deployment.yaml`
    42  
    43  	overlayDeployment = `apiVersion: apps/v1
    44  kind: Deployment
    45  metadata:
    46  	name: skaffold-kustomize
    47  	labels:
    48  		env: overlay`
    49  
    50  	overlayKustomization = `apiVersion: kustomize.config.k8s.io/v1beta1
    51  kind: Kustomization
    52  
    53  nameSuffix: -overlay
    54  
    55  patchesStrategicMerge:
    56  - deployment.yaml
    57  
    58  resources:
    59  - ../../base`
    60  )
    61  
    62  type overlay struct {
    63  	name          string
    64  	deployment    string
    65  	kustomization string
    66  }
    67  
    68  func TestGenerateKustomizePipeline(t *testing.T) {
    69  	tests := []struct {
    70  		description       string
    71  		base              string
    72  		baseKustomization string
    73  		overlays          []overlay
    74  		expectedConfig    latest.SkaffoldConfig
    75  	}{
    76  		{
    77  			description:       "single overlay",
    78  			base:              baseDeployment,
    79  			baseKustomization: baseKustomization,
    80  			overlays:          []overlay{{"dev", overlayDeployment, overlayKustomization}},
    81  			expectedConfig: latest.SkaffoldConfig{
    82  				Pipeline: latest.Pipeline{
    83  					Deploy: latest.DeployConfig{
    84  						DeployType: latest.DeployType{
    85  							KustomizeDeploy: &latest.KustomizeDeploy{
    86  								KustomizePaths: []string{filepath.Join("overlays", "dev")},
    87  							},
    88  						},
    89  					},
    90  				},
    91  			},
    92  		},
    93  		{
    94  			description:       "three overlays",
    95  			base:              baseDeployment,
    96  			baseKustomization: baseKustomization,
    97  			overlays: []overlay{
    98  				{"foo", overlayDeployment, overlayKustomization},
    99  				{"bar", overlayDeployment, overlayKustomization},
   100  				{"baz", overlayDeployment, overlayKustomization},
   101  			},
   102  			expectedConfig: latest.SkaffoldConfig{
   103  				Pipeline: latest.Pipeline{
   104  					Deploy: latest.DeployConfig{
   105  						DeployType: latest.DeployType{
   106  							KustomizeDeploy: &latest.KustomizeDeploy{
   107  								KustomizePaths: []string{filepath.Join("overlays", "foo")},
   108  							},
   109  						},
   110  					},
   111  				},
   112  				Profiles: []latest.Profile{
   113  					{
   114  						Name: "bar",
   115  						Pipeline: latest.Pipeline{
   116  							Deploy: latest.DeployConfig{
   117  								DeployType: latest.DeployType{
   118  									KustomizeDeploy: &latest.KustomizeDeploy{
   119  										KustomizePaths: []string{filepath.Join("overlays", "bar")},
   120  									},
   121  								},
   122  							},
   123  						},
   124  					},
   125  					{
   126  						Name: "baz",
   127  						Pipeline: latest.Pipeline{
   128  							Deploy: latest.DeployConfig{
   129  								DeployType: latest.DeployType{
   130  									KustomizeDeploy: &latest.KustomizeDeploy{
   131  										KustomizePaths: []string{filepath.Join("overlays", "baz")},
   132  									},
   133  								},
   134  							},
   135  						},
   136  					},
   137  				},
   138  			},
   139  		},
   140  	}
   141  
   142  	for _, test := range tests {
   143  		testutil.Run(t, test.description, func(tt *testutil.T) {
   144  			tmpDir := testutil.NewTempDir(t)
   145  
   146  			var overlays []string
   147  			manifests := []string{filepath.Join("base", "deployment.yaml")}
   148  
   149  			tmpDir.Write(filepath.Join("base", "deployment.yaml"), test.base)
   150  			tmpDir.Write(filepath.Join("base", "kustomization.yaml"), test.baseKustomization)
   151  			for _, o := range test.overlays {
   152  				overlays = append(overlays, filepath.Join("overlays", o.name))
   153  				manifests = append(manifests, filepath.Join("overlays", o.name, "deployment.yaml"))
   154  				tmpDir.Write(filepath.Join("overlays", o.name, "deployment.yaml"), o.deployment)
   155  				tmpDir.Write(filepath.Join("overlays", o.name, "kustomization.yaml"), o.kustomization)
   156  			}
   157  
   158  			k := newKustomizeInitializer("", []string{test.base}, overlays, manifests)
   159  
   160  			deployConfig, profiles := k.DeployConfig()
   161  			testutil.CheckDeepEqual(t, test.expectedConfig.Pipeline.Deploy, deployConfig)
   162  			testutil.CheckDeepEqual(t, test.expectedConfig.Profiles, profiles)
   163  		})
   164  	}
   165  }