github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/live/apply/cmdapply_test.go (about) 1 // Copyright 2021 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 apply 16 17 import ( 18 "path/filepath" 19 "testing" 20 21 "github.com/GoogleContainerTools/kpt/internal/testutil" 22 kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" 23 "github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil" 24 "github.com/GoogleContainerTools/kpt/pkg/printer/fake" 25 "github.com/stretchr/testify/assert" 26 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 27 "k8s.io/cli-runtime/pkg/genericclioptions" 28 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 29 "sigs.k8s.io/cli-utils/pkg/common" 30 "sigs.k8s.io/cli-utils/pkg/inventory" 31 ) 32 33 func TestCmd(t *testing.T) { 34 testCases := map[string]struct { 35 args []string 36 namespace string 37 inventory *kptfilev1.Inventory 38 applyCallbackFunc func(*testing.T, *Runner, inventory.Info) 39 expectedErrorMsg string 40 }{ 41 "invalid inventory policy": { 42 args: []string{ 43 "--inventory-policy", "noSuchPolicy", 44 }, 45 namespace: "testns", 46 applyCallbackFunc: func(t *testing.T, _ *Runner, _ inventory.Info) { 47 t.FailNow() 48 }, 49 expectedErrorMsg: "inventory policy must be one of strict, adopt", 50 }, 51 "invalid output format": { 52 args: []string{ 53 "--output", "foo", 54 }, 55 namespace: "testns", 56 applyCallbackFunc: func(t *testing.T, _ *Runner, _ inventory.Info) { 57 t.FailNow() 58 }, 59 expectedErrorMsg: "unknown output type \"foo\"", 60 }, 61 "fetches the correct inventory information from the Kptfile": { 62 args: []string{ 63 "--inventory-policy", "adopt", 64 "--output", "events", 65 }, 66 inventory: &kptfilev1.Inventory{ 67 Namespace: "my-ns", 68 Name: "my-name", 69 InventoryID: "my-inv-id", 70 }, 71 namespace: "testns", 72 applyCallbackFunc: func(t *testing.T, _ *Runner, inv inventory.Info) { 73 assert.Equal(t, "my-ns", inv.Namespace()) 74 assert.Equal(t, "my-name", inv.Name()) 75 assert.Equal(t, "my-inv-id", inv.ID()) 76 }, 77 }, 78 "install-resource-group flag defaults to true": { 79 args: []string{}, 80 inventory: &kptfilev1.Inventory{ 81 Namespace: "my-ns", 82 Name: "my-name", 83 InventoryID: "my-inv-id", 84 }, 85 namespace: "testns", 86 applyCallbackFunc: func(t *testing.T, r *Runner, _ inventory.Info) { 87 assert.True(t, r.installCRD) 88 }, 89 }, 90 "install-resource-group flag defaults to false when dry-run": { 91 args: []string{ 92 "--dry-run", 93 }, 94 inventory: &kptfilev1.Inventory{ 95 Namespace: "my-ns", 96 Name: "my-name", 97 InventoryID: "my-inv-id", 98 }, 99 namespace: "testns", 100 applyCallbackFunc: func(t *testing.T, r *Runner, _ inventory.Info) { 101 assert.True(t, r.dryRun) 102 assert.False(t, r.installCRD) 103 }, 104 expectedErrorMsg: "type ResourceGroup not found", 105 }, 106 "install-resource-group flag remains true with dry-run if explicitly set": { 107 args: []string{ 108 "--dry-run", 109 "--install-resource-group", 110 }, 111 inventory: &kptfilev1.Inventory{ 112 Namespace: "my-ns", 113 Name: "my-name", 114 InventoryID: "my-inv-id", 115 }, 116 namespace: "testns", 117 applyCallbackFunc: func(t *testing.T, r *Runner, _ inventory.Info) { 118 assert.True(t, r.dryRun) 119 assert.True(t, r.installCRD) 120 }, 121 }, 122 } 123 124 for tn, tc := range testCases { 125 t.Run(tn, func(t *testing.T) { 126 tf := cmdtesting.NewTestFactory().WithNamespace(tc.namespace) 127 defer tf.Cleanup() 128 ioStreams, _, _, _ := genericclioptions.NewTestIOStreams() //nolint:dogsled 129 130 w, clean := testutil.SetupWorkspace(t) 131 defer clean() 132 kf := kptfileutil.DefaultKptfile(filepath.Base(w.WorkspaceDirectory)) 133 kf.Inventory = tc.inventory 134 testutil.AddKptfileToWorkspace(t, w, kf) 135 136 revert := testutil.Chdir(t, w.WorkspaceDirectory) 137 defer revert() 138 139 runner := NewRunner(fake.CtxWithDefaultPrinter(), tf, ioStreams, false) 140 runner.Command.SetArgs(tc.args) 141 runner.applyRunner = func(_ *Runner, inv inventory.Info, 142 _ []*unstructured.Unstructured, _ common.DryRunStrategy) error { 143 tc.applyCallbackFunc(t, runner, inv) 144 return nil 145 } 146 err := runner.Command.Execute() 147 148 // Check if there should be an error 149 if tc.expectedErrorMsg != "" { 150 if !assert.Error(t, err) { 151 t.FailNow() 152 } 153 assert.Contains(t, err.Error(), tc.expectedErrorMsg) 154 return 155 } 156 assert.NoError(t, err) 157 }) 158 } 159 }