github.com/crossplane/upjet@v1.3.0/pkg/migration/fork_executor_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package migration 6 7 import ( 8 "testing" 9 10 "github.com/crossplane/crossplane-runtime/pkg/test" 11 "github.com/google/go-cmp/cmp" 12 "github.com/pkg/errors" 13 k8sExec "k8s.io/utils/exec" 14 testingexec "k8s.io/utils/exec/testing" 15 ) 16 17 var ( 18 backupManagedStep = Step{ 19 Name: "backup-managed-resources", 20 Type: StepTypeExec, 21 Exec: &ExecStep{ 22 Command: "sh", 23 Args: []string{"-c", "kubectl get managed -o yaml"}, 24 }, 25 } 26 27 wrongCommand = Step{ 28 Name: "wrong-command", 29 Type: StepTypeExec, 30 Exec: &ExecStep{ 31 Command: "sh", 32 Args: []string{"-c", "nosuchcommand"}, 33 }, 34 } 35 36 wrongStepType = Step{ 37 Name: "wrong-step-type", 38 Type: StepTypeDelete, 39 } 40 ) 41 42 var errorWrongCommand = errors.New("exit status 127") 43 44 func newFakeExec(err error) *testingexec.FakeExec { 45 return &testingexec.FakeExec{ 46 CommandScript: []testingexec.FakeCommandAction{ 47 func(_ string, _ ...string) k8sExec.Cmd { 48 return &testingexec.FakeCmd{ 49 CombinedOutputScript: []testingexec.FakeAction{ 50 func() ([]byte, []byte, error) { 51 return nil, nil, err 52 }, 53 }, 54 } 55 }, 56 }, 57 } 58 } 59 60 func TestForkExecutorStep(t *testing.T) { 61 type args struct { 62 step Step 63 fakeExec *testingexec.FakeExec 64 } 65 type want struct { 66 err error 67 } 68 69 cases := map[string]struct { 70 args 71 want 72 }{ 73 "Success": { 74 args: args{ 75 step: backupManagedStep, 76 fakeExec: &testingexec.FakeExec{DisableScripts: true}, 77 }, 78 want: want{ 79 nil, 80 }, 81 }, 82 "Failure": { 83 args: args{ 84 step: wrongCommand, 85 fakeExec: newFakeExec(errorWrongCommand), 86 }, 87 want: want{ 88 errors.Wrap(errorWrongCommand, `failed to execute the step "wrong-command": failed to execute command`), 89 }, 90 }, 91 "WrongStepType": { 92 args: args{ 93 step: wrongStepType, 94 }, 95 want: want{ 96 errors.Wrap(NewUnsupportedStepTypeError(wrongStepType), `step type should be Exec or step's manualExecution should be non-empty`), 97 }, 98 }, 99 } 100 101 for name, tc := range cases { 102 t.Run(name, func(t *testing.T) { 103 fe := NewForkExecutor(WithExecutor(tc.fakeExec)) 104 err := fe.Step(tc.step, nil) 105 if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { 106 t.Errorf("\n%s\nStep(...): -want error, +got error:\n%s", name, diff) 107 } 108 }) 109 } 110 }