github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/rpkg/pull/command_test.go (about) 1 // Copyright 2022 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 pull 16 17 import ( 18 "bytes" 19 "strings" 20 "testing" 21 22 "github.com/GoogleContainerTools/kpt/internal/printer" 23 fakeprint "github.com/GoogleContainerTools/kpt/internal/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: kpt.dev/v1 72 kind: Kptfile 73 metadata: 74 name: bar 75 annotations: 76 config.kubernetes.io/local-config: "true" 77 config.kubernetes.io/index: '0' 78 internal.config.kubernetes.io/index: '0' 79 internal.config.kubernetes.io/path: 'Kptfile' 80 config.kubernetes.io/path: 'Kptfile' 81 info: 82 description: sample description 83 - apiVersion: v1 84 kind: ConfigMap 85 metadata: 86 name: game-config 87 namespace: default 88 annotations: 89 config.kubernetes.io/index: '0' 90 internal.config.kubernetes.io/index: '0' 91 internal.config.kubernetes.io/path: 'cm.yaml' 92 config.kubernetes.io/path: 'cm.yaml' 93 data: 94 foo: bar 95 `, 96 }, 97 "package with subdirectory": { 98 resources: map[string]string{ 99 "Kptfile": strings.TrimSpace(` 100 apiVersion: kpt.dev/v1 101 kind: Kptfile 102 metadata: 103 name: bar 104 annotations: 105 config.kubernetes.io/local-config: "true" 106 info: 107 description: sample description 108 `), 109 "sub/cm.yaml": strings.TrimSpace(` 110 apiVersion: v1 111 kind: ConfigMap 112 metadata: 113 name: game-config 114 namespace: default 115 data: 116 foo: bar 117 `), 118 }, 119 output: ` 120 apiVersion: config.kubernetes.io/v1 121 kind: ResourceList 122 items: 123 - apiVersion: kpt.dev/v1 124 kind: Kptfile 125 metadata: 126 name: bar 127 annotations: 128 config.kubernetes.io/local-config: "true" 129 config.kubernetes.io/index: '0' 130 internal.config.kubernetes.io/index: '0' 131 internal.config.kubernetes.io/path: 'Kptfile' 132 config.kubernetes.io/path: 'Kptfile' 133 info: 134 description: sample description 135 - apiVersion: v1 136 kind: ConfigMap 137 metadata: 138 name: game-config 139 namespace: default 140 annotations: 141 config.kubernetes.io/index: '0' 142 internal.config.kubernetes.io/index: '0' 143 internal.config.kubernetes.io/path: 'sub/cm.yaml' 144 config.kubernetes.io/path: 'sub/cm.yaml' 145 data: 146 foo: bar 147 `, 148 }, 149 } 150 151 for tn := range testCases { 152 tc := testCases[tn] 153 t.Run(tn, func(t *testing.T) { 154 c := fake.NewClientBuilder(). 155 WithScheme(scheme). 156 WithObjects(&porchapi.PackageRevisionResources{ 157 ObjectMeta: metav1.ObjectMeta{ 158 Name: pkgRevName, 159 Namespace: "ns", 160 }, 161 Spec: porchapi.PackageRevisionResourcesSpec{ 162 PackageName: "foo", 163 Resources: tc.resources, 164 }, 165 }). 166 Build() 167 output := &bytes.Buffer{} 168 ctx := fakeprint.CtxWithPrinter(output, output) 169 r := &runner{ 170 ctx: ctx, 171 cfg: &genericclioptions.ConfigFlags{ 172 Namespace: &ns, 173 }, 174 client: c, 175 printer: printer.FromContextOrDie(ctx), 176 } 177 cmd := &cobra.Command{} 178 err = r.runE(cmd, []string{pkgRevName}) 179 if err != nil { 180 t.Errorf("unexpected error: %v", err) 181 } 182 if diff := cmp.Diff(strings.TrimSpace(tc.output), strings.TrimSpace(output.String())); diff != "" { 183 t.Errorf("Unexpected result (-want, +got): %s", diff) 184 } 185 }) 186 } 187 }