github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/pull/command_test.go (about) 1 // Copyright 2022 The kpt Authors 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 pull 16 17 import ( 18 "bytes" 19 "strings" 20 "testing" 21 22 "github.com/GoogleContainerTools/kpt/pkg/printer" 23 fakeprint "github.com/GoogleContainerTools/kpt/pkg/printer/fake" 24 porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 25 "github.com/google/go-cmp/cmp" 26 "github.com/spf13/cobra" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/cli-runtime/pkg/genericclioptions" 29 "sigs.k8s.io/controller-runtime/pkg/client/fake" 30 ) 31 32 func TestCmd(t *testing.T) { 33 pkgRevName := "repo-fjdos9u2nfe2f32" 34 ns := "ns" 35 36 scheme, err := createScheme() 37 if err != nil { 38 t.Fatalf("error creating scheme: %v", err) 39 } 40 41 testCases := map[string]struct { 42 resources map[string]string 43 output string 44 }{ 45 "simple package": { 46 resources: map[string]string{ 47 "Kptfile": strings.TrimSpace(` 48 apiVersion: kpt.dev/v1 49 kind: Kptfile 50 metadata: 51 name: bar 52 annotations: 53 config.kubernetes.io/local-config: "true" 54 info: 55 description: sample description 56 `), 57 "cm.yaml": strings.TrimSpace(` 58 apiVersion: v1 59 kind: ConfigMap 60 metadata: 61 name: game-config 62 namespace: default 63 data: 64 foo: bar 65 `), 66 }, 67 output: ` 68 apiVersion: config.kubernetes.io/v1 69 kind: ResourceList 70 items: 71 - apiVersion: porch.kpt.dev/v1alpha1 72 kind: KptRevisionMetadata 73 metadata: 74 name: repo-fjdos9u2nfe2f32 75 namespace: ns 76 creationTimestamp: null 77 resourceVersion: "999" 78 annotations: 79 config.kubernetes.io/index: '0' 80 internal.config.kubernetes.io/index: '0' 81 internal.config.kubernetes.io/path: '.KptRevisionMetadata' 82 config.kubernetes.io/path: '.KptRevisionMetadata' 83 - apiVersion: kpt.dev/v1 84 kind: Kptfile 85 metadata: 86 name: bar 87 annotations: 88 config.kubernetes.io/local-config: "true" 89 config.kubernetes.io/index: '0' 90 internal.config.kubernetes.io/index: '0' 91 internal.config.kubernetes.io/path: 'Kptfile' 92 config.kubernetes.io/path: 'Kptfile' 93 info: 94 description: sample description 95 - apiVersion: v1 96 kind: ConfigMap 97 metadata: 98 name: game-config 99 namespace: default 100 annotations: 101 config.kubernetes.io/index: '0' 102 internal.config.kubernetes.io/index: '0' 103 internal.config.kubernetes.io/path: 'cm.yaml' 104 config.kubernetes.io/path: 'cm.yaml' 105 data: 106 foo: bar 107 `, 108 }, 109 "package with subdirectory": { 110 resources: map[string]string{ 111 "Kptfile": strings.TrimSpace(` 112 apiVersion: kpt.dev/v1 113 kind: Kptfile 114 metadata: 115 name: bar 116 annotations: 117 config.kubernetes.io/local-config: "true" 118 info: 119 description: sample description 120 `), 121 "sub/cm.yaml": strings.TrimSpace(` 122 apiVersion: v1 123 kind: ConfigMap 124 metadata: 125 name: game-config 126 namespace: default 127 data: 128 foo: bar 129 `), 130 }, 131 output: ` 132 apiVersion: config.kubernetes.io/v1 133 kind: ResourceList 134 items: 135 - apiVersion: porch.kpt.dev/v1alpha1 136 kind: KptRevisionMetadata 137 metadata: 138 name: repo-fjdos9u2nfe2f32 139 namespace: ns 140 creationTimestamp: null 141 resourceVersion: "999" 142 annotations: 143 config.kubernetes.io/index: '0' 144 internal.config.kubernetes.io/index: '0' 145 internal.config.kubernetes.io/path: '.KptRevisionMetadata' 146 config.kubernetes.io/path: '.KptRevisionMetadata' 147 - apiVersion: kpt.dev/v1 148 kind: Kptfile 149 metadata: 150 name: bar 151 annotations: 152 config.kubernetes.io/local-config: "true" 153 config.kubernetes.io/index: '0' 154 internal.config.kubernetes.io/index: '0' 155 internal.config.kubernetes.io/path: 'Kptfile' 156 config.kubernetes.io/path: 'Kptfile' 157 info: 158 description: sample description 159 - apiVersion: v1 160 kind: ConfigMap 161 metadata: 162 name: game-config 163 namespace: default 164 annotations: 165 config.kubernetes.io/index: '0' 166 internal.config.kubernetes.io/index: '0' 167 internal.config.kubernetes.io/path: 'sub/cm.yaml' 168 config.kubernetes.io/path: 'sub/cm.yaml' 169 data: 170 foo: bar 171 `, 172 }, 173 } 174 175 for tn := range testCases { 176 tc := testCases[tn] 177 t.Run(tn, func(t *testing.T) { 178 c := fake.NewClientBuilder(). 179 WithScheme(scheme). 180 WithObjects(&porchapi.PackageRevisionResources{ 181 ObjectMeta: metav1.ObjectMeta{ 182 Name: pkgRevName, 183 Namespace: "ns", 184 }, 185 Spec: porchapi.PackageRevisionResourcesSpec{ 186 PackageName: "foo", 187 Resources: tc.resources, 188 }, 189 }). 190 Build() 191 output := &bytes.Buffer{} 192 ctx := fakeprint.CtxWithPrinter(output, output) 193 r := &runner{ 194 ctx: ctx, 195 cfg: &genericclioptions.ConfigFlags{ 196 Namespace: &ns, 197 }, 198 client: c, 199 printer: printer.FromContextOrDie(ctx), 200 } 201 cmd := &cobra.Command{} 202 err = r.runE(cmd, []string{pkgRevName}) 203 if err != nil { 204 t.Errorf("unexpected error: %v", err) 205 } 206 if diff := cmp.Diff(strings.TrimSpace(tc.output), strings.TrimSpace(output.String())); diff != "" { 207 t.Errorf("Unexpected result (-want, +got): %s", diff) 208 } 209 }) 210 } 211 }