github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/live/destroy/cmddestroy_test.go (about) 1 // Copyright 2021 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 destroy 16 17 import ( 18 "path/filepath" 19 "testing" 20 21 "github.com/GoogleContainerTools/kpt/internal/printer/fake" 22 "github.com/GoogleContainerTools/kpt/internal/testutil" 23 kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" 24 "github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil" 25 "github.com/stretchr/testify/assert" 26 "k8s.io/cli-runtime/pkg/genericclioptions" 27 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 28 "sigs.k8s.io/cli-utils/pkg/common" 29 "sigs.k8s.io/cli-utils/pkg/inventory" 30 ) 31 32 func TestCmd(t *testing.T) { 33 testCases := map[string]struct { 34 args []string 35 namespace string 36 inventory *kptfilev1.Inventory 37 destroyCallbackFunc func(*testing.T, inventory.Info) 38 expectedErrorMsg string 39 }{ 40 "invalid inventory policy": { 41 args: []string{ 42 "--inventory-policy", "noSuchPolicy", 43 }, 44 namespace: "testns", 45 destroyCallbackFunc: func(t *testing.T, _ inventory.Info) { 46 t.FailNow() 47 }, 48 expectedErrorMsg: "inventory policy must be one of strict, adopt", 49 }, 50 "invalid output format": { 51 args: []string{ 52 "--output", "foo", 53 }, 54 namespace: "testns", 55 destroyCallbackFunc: func(t *testing.T, _ inventory.Info) { 56 t.FailNow() 57 }, 58 expectedErrorMsg: "unknown output type \"foo\"", 59 }, 60 "fetches the correct inventory information from the Kptfile": { 61 args: []string{ 62 "--inventory-policy", "adopt", 63 "--output", "events", 64 }, 65 inventory: &kptfilev1.Inventory{ 66 Namespace: "my-ns", 67 Name: "my-name", 68 InventoryID: "my-inv-id", 69 }, 70 namespace: "testns", 71 destroyCallbackFunc: func(t *testing.T, inv inventory.Info) { 72 assert.Equal(t, "my-ns", inv.Namespace()) 73 assert.Equal(t, "my-name", inv.Name()) 74 assert.Equal(t, "my-inv-id", inv.ID()) 75 }, 76 }, 77 } 78 79 for tn, tc := range testCases { 80 t.Run(tn, func(t *testing.T) { 81 tf := cmdtesting.NewTestFactory().WithNamespace(tc.namespace) 82 defer tf.Cleanup() 83 ioStreams, _, _, _ := genericclioptions.NewTestIOStreams() //nolint:dogsled 84 85 w, clean := testutil.SetupWorkspace(t) 86 defer clean() 87 kf := kptfileutil.DefaultKptfile(filepath.Base(w.WorkspaceDirectory)) 88 kf.Inventory = tc.inventory 89 testutil.AddKptfileToWorkspace(t, w, kf) 90 91 revert := testutil.Chdir(t, w.WorkspaceDirectory) 92 defer revert() 93 94 runner := NewRunner(fake.CtxWithDefaultPrinter(), tf, ioStreams) 95 runner.Command.SetArgs(tc.args) 96 runner.destroyRunner = func(_ *Runner, inv inventory.Info, _ common.DryRunStrategy) error { 97 tc.destroyCallbackFunc(t, inv) 98 return nil 99 } 100 err := runner.Command.Execute() 101 102 // Check if there should be an error 103 if tc.expectedErrorMsg != "" { 104 if !assert.Error(t, err) { 105 t.FailNow() 106 } 107 assert.Contains(t, err.Error(), tc.expectedErrorMsg) 108 return 109 } 110 assert.NoError(t, err) 111 }) 112 } 113 }