github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/diff/pkgdiff_test.go (about) 1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package diff 16 17 import ( 18 "testing" 19 20 "github.com/GoogleContainerTools/kpt/internal/testutil" 21 "github.com/GoogleContainerTools/kpt/internal/testutil/pkgbuilder" 22 "github.com/stretchr/testify/assert" 23 "sigs.k8s.io/kustomize/kyaml/sets" 24 ) 25 26 func TestPkgDiff(t *testing.T) { 27 testCases := []struct { 28 name string 29 pkg1 *pkgbuilder.RootPkg 30 pkg2 *pkgbuilder.RootPkg 31 diff sets.String 32 }{ 33 { 34 name: "equal packages doesn't have a diff", 35 pkg1: pkgbuilder.NewRootPkg(). 36 WithKptfile(pkgbuilder.NewKptfile()). 37 WithResource(pkgbuilder.DeploymentResource), 38 pkg2: pkgbuilder.NewRootPkg(). 39 WithKptfile(pkgbuilder.NewKptfile()). 40 WithResource(pkgbuilder.DeploymentResource), 41 diff: toStringSet(), 42 }, 43 { 44 name: "different files between packages", 45 pkg1: pkgbuilder.NewRootPkg(). 46 WithKptfile(). 47 WithResource(pkgbuilder.DeploymentResource), 48 pkg2: pkgbuilder.NewRootPkg(). 49 WithKptfile(). 50 WithResource(pkgbuilder.ConfigMapResource), 51 diff: toStringSet("configmap.yaml", "deployment.yaml"), 52 }, 53 { 54 name: "different upstream in Kptfile is not a diff", 55 pkg1: pkgbuilder.NewRootPkg(). 56 WithKptfile(pkgbuilder.NewKptfile(). 57 WithUpstream("github.com/GoogleContainerTools/kpt", "/", "master", "resource-merge")). 58 WithResource(pkgbuilder.DeploymentResource), 59 pkg2: pkgbuilder.NewRootPkg(). 60 WithKptfile(pkgbuilder.NewKptfile(). 61 WithUpstream("github.com/GoogleContainerTools/kpt", "/", "kpt/v1", "resource-merge")). 62 WithResource(pkgbuilder.DeploymentResource), 63 diff: toStringSet(), 64 }, 65 { 66 name: "different pipelines in Kptfile is a diff", 67 pkg1: pkgbuilder.NewRootPkg(). 68 WithKptfile(pkgbuilder.NewKptfile(). 69 WithPipeline(pkgbuilder.NewFunction("gcr.io/kpt-dev/foo:latest"))). 70 WithResource(pkgbuilder.DeploymentResource), 71 pkg2: pkgbuilder.NewRootPkg(). 72 WithKptfile(pkgbuilder.NewKptfile(). 73 WithPipeline(pkgbuilder.NewFunction("gcr.io/kpt-dev/buzz:latest"))). 74 WithResource(pkgbuilder.DeploymentResource), 75 diff: toStringSet("Kptfile"), 76 }, 77 { 78 name: "subpackages are not included in no diff", 79 pkg1: pkgbuilder.NewRootPkg(). 80 WithKptfile(). 81 WithResource(pkgbuilder.DeploymentResource). 82 WithSubPackages( 83 pkgbuilder.NewSubPkg("subpackage"). 84 WithKptfile(pkgbuilder.NewKptfile()). 85 WithResource(pkgbuilder.DeploymentResource), 86 ), 87 pkg2: pkgbuilder.NewRootPkg(). 88 WithKptfile(). 89 WithResource(pkgbuilder.DeploymentResource). 90 WithSubPackages( 91 pkgbuilder.NewSubPkg("subpackage"). 92 WithKptfile(). 93 WithResource(pkgbuilder.ConfigMapResource), 94 ), 95 diff: toStringSet(), 96 }, 97 { 98 name: "subpackages are not included in root package diff", 99 pkg1: pkgbuilder.NewRootPkg(). 100 WithKptfile(). 101 WithResource(pkgbuilder.ConfigMapResource). 102 WithSubPackages( 103 pkgbuilder.NewSubPkg("subpackage"). 104 WithKptfile(pkgbuilder.NewKptfile()). 105 WithResource(pkgbuilder.DeploymentResource), 106 ), 107 pkg2: pkgbuilder.NewRootPkg(). 108 WithKptfile(). 109 WithResource(pkgbuilder.DeploymentResource). 110 WithSubPackages( 111 pkgbuilder.NewSubPkg("subpackage"). 112 WithKptfile(). 113 WithResource(pkgbuilder.ConfigMapResource), 114 ), 115 diff: toStringSet("deployment.yaml", "configmap.yaml"), 116 }, 117 { 118 name: "package doesn't exist", 119 pkg1: nil, 120 pkg2: pkgbuilder.NewRootPkg(). 121 WithKptfile(pkgbuilder.NewKptfile()), 122 diff: toStringSet("Kptfile"), 123 }, 124 { 125 name: "package doesn't have a kptfile", 126 pkg1: pkgbuilder.NewRootPkg(), 127 pkg2: pkgbuilder.NewRootPkg(). 128 WithKptfile(). 129 WithResource(pkgbuilder.DeploymentResource). 130 WithSubPackages( 131 pkgbuilder.NewSubPkg("subpackage"). 132 WithKptfile(). 133 WithResource(pkgbuilder.ConfigMapResource), 134 ), 135 diff: toStringSet("Kptfile", "deployment.yaml"), 136 }, 137 } 138 139 for i := range testCases { 140 test := testCases[i] 141 t.Run(test.name, func(t *testing.T) { 142 pkg1Dir := test.pkg1.ExpandPkg(t, testutil.EmptyReposInfo) 143 pkg2Dir := test.pkg2.ExpandPkg(t, testutil.EmptyReposInfo) 144 diff, err := PkgDiff(pkg1Dir, pkg2Dir) 145 if !assert.NoError(t, err) { 146 t.FailNow() 147 } 148 149 assert.Equal(t, 0, len(diff.SymmetricDifference(test.diff))) 150 }) 151 } 152 } 153 154 func toStringSet(files ...string) sets.String { 155 s := sets.String{} 156 for _, f := range files { 157 s.Insert(f) 158 } 159 return s 160 }