github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/kustomize/patch_test.go (about)

     1  package kustomize
     2  
     3  import (
     4  	"path"
     5  	"testing"
     6  
     7  	"github.com/go-kit/kit/log"
     8  	"github.com/spf13/afero"
     9  	"github.com/spf13/viper"
    10  	"github.com/stretchr/testify/require"
    11  	yaml "gopkg.in/yaml.v3"
    12  	"sigs.k8s.io/kustomize/pkg/gvk"
    13  	kustomizepatch "sigs.k8s.io/kustomize/pkg/patch"
    14  	k8stypes "sigs.k8s.io/kustomize/pkg/types"
    15  
    16  	"github.com/replicatedhq/ship/pkg/api"
    17  	"github.com/replicatedhq/ship/pkg/constants"
    18  	"github.com/replicatedhq/ship/pkg/state"
    19  )
    20  
    21  func TestKustomizer_generateTillerPatches(t *testing.T) {
    22  	type testFile struct {
    23  		path     string
    24  		contents string
    25  	}
    26  	tests := []struct {
    27  		name                string
    28  		step                api.Kustomize
    29  		testFiles           []testFile
    30  		expectKustomization k8stypes.Kustomization
    31  	}{
    32  		{
    33  			name: "yaml with heritage and chart labels",
    34  			step: api.Kustomize{
    35  				Base: "strawberry",
    36  			},
    37  			testFiles: []testFile{
    38  				{
    39  					path: "strawberry/deployment.yaml",
    40  					contents: `apiVersion: apps/v1beta2
    41  kind: Deployment
    42  metadata:
    43    labels:
    44      app: strawberry
    45      heritage: Tiller
    46      chart: strawberry-1.0.0
    47    name: strawberry
    48  `,
    49  				},
    50  			},
    51  			expectKustomization: k8stypes.Kustomization{
    52  				Bases: []string{"../../strawberry"},
    53  				PatchesJson6902: []kustomizepatch.Json6902{
    54  					{
    55  						Path: "chart-patch.json",
    56  						Target: &kustomizepatch.Target{
    57  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
    58  							Name: "strawberry",
    59  						},
    60  					},
    61  					{
    62  						Path: "heritage-patch.json",
    63  						Target: &kustomizepatch.Target{
    64  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
    65  							Name: "strawberry",
    66  						},
    67  					},
    68  				},
    69  			},
    70  		},
    71  		{
    72  			name: "kustomization yaml",
    73  			step: api.Kustomize{
    74  				Base: "strawberry",
    75  			},
    76  			testFiles: []testFile{
    77  				{
    78  					path: "strawberry/kustomization.yaml",
    79  					contents: `apiVersion: apps/v1beta2
    80  bases:
    81  - ../../base
    82  patchesJson6902:
    83  - path: chart-patch.json
    84    target:
    85      group: rbac.authorization.k8s.io
    86      kind: ClusterRole
    87      name: cert-manager-cainjector
    88      version: v1beta1
    89  `,
    90  				},
    91  			},
    92  			expectKustomization: k8stypes.Kustomization{
    93  				Bases:           []string{"../../strawberry"},
    94  				PatchesJson6902: nil,
    95  			},
    96  		},
    97  		{
    98  			name: "both kustomization and relevant yaml with heritage and chart labels",
    99  			step: api.Kustomize{
   100  				Base: "strawberry",
   101  			},
   102  			testFiles: []testFile{
   103  				{
   104  					path: "strawberry/deployment.yaml",
   105  					contents: `apiVersion: apps/v1beta2
   106  kind: Deployment
   107  metadata:
   108    labels:
   109      app: strawberry
   110      heritage: Tiller
   111      chart: strawberry-1.0.0
   112    name: strawberry
   113  `,
   114  				},
   115  				{
   116  					path: "strawberry/kustomization.yaml",
   117  					contents: `apiVersion: apps/v1beta2
   118  bases:
   119  - ../../base
   120  patchesJson6902:
   121  - path: chart-patch.json
   122    target:
   123      group: rbac.authorization.k8s.io
   124      kind: ClusterRole
   125      name: cert-manager-cainjector
   126      version: v1beta1
   127  `,
   128  				},
   129  			},
   130  			expectKustomization: k8stypes.Kustomization{
   131  				Bases: []string{"../../strawberry"},
   132  				PatchesJson6902: []kustomizepatch.Json6902{
   133  					{
   134  						Path: "chart-patch.json",
   135  						Target: &kustomizepatch.Target{
   136  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
   137  							Name: "strawberry",
   138  						},
   139  					},
   140  					{
   141  						Path: "heritage-patch.json",
   142  						Target: &kustomizepatch.Target{
   143  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
   144  							Name: "strawberry",
   145  						},
   146  					},
   147  				},
   148  			},
   149  		},
   150  		{
   151  			name: "yaml with only heritage label",
   152  			step: api.Kustomize{
   153  				Base: "pomegranate",
   154  			},
   155  			testFiles: []testFile{
   156  				{
   157  					path: "pomegranate/deployment.yaml",
   158  					contents: `apiVersion: apps/v1beta2
   159  kind: Deployment
   160  metadata:
   161    labels:
   162      app: pomegranate
   163      heritage: Tiller
   164    name: pomegranate
   165  `,
   166  				},
   167  			},
   168  			expectKustomization: k8stypes.Kustomization{
   169  				Bases: []string{"../../pomegranate"},
   170  				PatchesJson6902: []kustomizepatch.Json6902{
   171  					{
   172  						Path: "heritage-patch.json",
   173  						Target: &kustomizepatch.Target{
   174  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
   175  							Name: "pomegranate",
   176  						},
   177  					},
   178  				},
   179  			},
   180  		},
   181  		{
   182  			name: "yaml with only chart label",
   183  			step: api.Kustomize{
   184  				Base: "apple",
   185  			},
   186  			testFiles: []testFile{
   187  				{
   188  					path: "apple/deployment.yaml",
   189  					contents: `apiVersion: apps/v1beta2
   190  kind: Deployment
   191  metadata:
   192    labels:
   193      app: apple
   194      chart: apple-1.0.0
   195    name: apple
   196  `,
   197  				},
   198  			},
   199  			expectKustomization: k8stypes.Kustomization{
   200  				Bases: []string{"../../apple"},
   201  				PatchesJson6902: []kustomizepatch.Json6902{
   202  					{
   203  						Path: "chart-patch.json",
   204  						Target: &kustomizepatch.Target{
   205  							Gvk:  gvk.Gvk{Group: "apps", Kind: "Deployment", Version: "v1beta2"},
   206  							Name: "apple",
   207  						},
   208  					},
   209  				},
   210  			},
   211  		},
   212  		{
   213  			name: "yaml without heritage and chart labels",
   214  			step: api.Kustomize{
   215  				Base: "banana",
   216  			},
   217  			testFiles: []testFile{
   218  				{
   219  					path: "banana/deployment.yaml",
   220  					contents: `apiVersion: apps/v1beta2
   221  kind: Deployment
   222  metadata:
   223    labels:
   224      app: banana
   225    name: banana
   226  `,
   227  				},
   228  				{
   229  					path: "banana/service.yaml",
   230  					contents: `apiVersion: v1
   231  kind: Service
   232  metadata:
   233    labels:
   234      app: banana
   235    name: banana
   236  `,
   237  				},
   238  			},
   239  			expectKustomization: k8stypes.Kustomization{
   240  				Bases: []string{"../../banana"},
   241  			},
   242  		},
   243  	}
   244  	for _, tt := range tests {
   245  		t.Run(tt.name, func(t *testing.T) {
   246  			req := require.New(t)
   247  
   248  			mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
   249  			for _, testFile := range tt.testFiles {
   250  				err := mockFs.WriteFile(testFile.path, []byte(testFile.contents), 0755)
   251  				req.NoError(err)
   252  			}
   253  
   254  			stateManager, err := state.NewDisposableManager(log.NewNopLogger(), mockFs, viper.New())
   255  			req.NoError(err)
   256  
   257  			err = stateManager.Save(state.State{V1: &state.V1{Kustomize: &state.Kustomize{}}})
   258  			req.NoError(err)
   259  
   260  			l := &Kustomizer{
   261  				Logger: log.NewNopLogger(),
   262  				FS:     mockFs,
   263  				State:  stateManager,
   264  			}
   265  			err = l.generateTillerPatches(tt.step)
   266  			req.NoError(err)
   267  
   268  			kustomizationB, err := mockFs.ReadFile(path.Join(constants.DefaultOverlaysPath, "kustomization.yaml"))
   269  			req.NoError(err)
   270  
   271  			kustomizationYaml := k8stypes.Kustomization{}
   272  			err = yaml.Unmarshal(kustomizationB, &kustomizationYaml)
   273  			req.NoError(err)
   274  
   275  			req.Equal(tt.expectKustomization, kustomizationYaml)
   276  		})
   277  	}
   278  }