github.com/verrazzano/verrazzano@v1.7.0/pkg/istio/istioctl_test.go (about) 1 // Copyright (c) 2021, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package istio 5 6 import ( 7 "errors" 8 "github.com/verrazzano/verrazzano/pkg/constants" 9 "github.com/verrazzano/verrazzano/pkg/k8sutil" 10 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 11 "github.com/verrazzano/verrazzano/platform-operator/controllers/verrazzano/component/common" 12 "os/exec" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 // upgradeRunner is used to test istioctl upgrade without actually running an OS exec command 19 type upgradeRunner struct { 20 t *testing.T 21 } 22 23 // installRunner is used to test istioctl install without actually running an OS exec command 24 type installRunner struct { 25 t *testing.T 26 } 27 28 // uninstallRunner is used to test istioctl uninstall without actually running an OS exec command 29 type uninstallRunner struct { 30 t *testing.T 31 } 32 33 // badRunner is used to test istioctl errors without actually running an OS exec command 34 type badRunner struct { 35 t *testing.T 36 } 37 38 // TestUpgrade tests the istioctl upgrade command 39 // GIVEN a set of upgrade parameters 40 // 41 // WHEN I call Upgrade 42 // THEN the istioctl upgrade returns success and the cmd object has correct values 43 func TestUpgrade(t *testing.T) { 44 overrideYaml := "my-override.yaml" 45 46 assert := assert.New(t) 47 SetCmdRunner(upgradeRunner{t: t}) 48 defer SetDefaultRunner() 49 50 stdout, stderr, err := Upgrade(vzlog.DefaultLogger(), overrideYaml) 51 assert.NoError(err, "Upgrade returned an error") 52 assert.Len(stderr, 0, "Upgrade stderr should be empty") 53 assert.NotZero(stdout, "Upgrade stdout should not be empty") 54 } 55 56 // TestUpgradeFail tests the istioctl upgrade command failure condition 57 // GIVEN a set of upgrade parameters and a fake runner that fails 58 // 59 // WHEN I call Upgrade 60 // THEN the istioctl upgrade returns an error 61 func TestUpgradeFail(t *testing.T) { 62 assert := assert.New(t) 63 SetCmdRunner(badRunner{t: t}) 64 defer SetDefaultRunner() 65 66 stdout, stderr, err := Upgrade(vzlog.DefaultLogger(), "", "") 67 assert.Error(err, "Upgrade should have returned an error") 68 assert.Len(stdout, 0, "Upgrade stdout should be empty") 69 assert.NotZero(stderr, "Upgrade stderr should not be empty") 70 } 71 72 // TestInstall tests the istioctl install command 73 // GIVEN a set of upgrade parameters 74 // 75 // WHEN I call Install 76 // THEN the istioctl install returns success and the cmd object has correct values 77 func TestInstall(t *testing.T) { 78 overrideYaml := "my-override.yaml" 79 80 assert := assert.New(t) 81 SetCmdRunner(installRunner{t: t}) 82 defer SetDefaultRunner() 83 84 stdout, stderr, err := Install(vzlog.DefaultLogger(), overrideYaml) 85 assert.NoError(err, "Install returned an error") 86 assert.Len(stderr, 0, "Install stderr should be empty") 87 assert.NotZero(stdout, "Install stdout should not be empty") 88 } 89 90 // TestIsInstalled tests if the component is installed 91 // GIVEN a component 92 // 93 // WHEN I call IsInstalled 94 // THEN true is returned 95 func TestIsInstalled(t *testing.T) { 96 assert := assert.New(t) 97 98 k8sutil.GetCoreV1Func = common.MockGetCoreV1WithNamespace(constants.IstioSystemNamespace) 99 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 100 101 SetCmdRunner(fakeIstioInstalledRunner{}) 102 b, err := IsInstalled(vzlog.DefaultLogger()) 103 assert.NoError(err, "IsInstalled returned an error") 104 assert.True(b, "IsInstalled returned false") 105 } 106 107 // TestUninstall tests the istioctl uninstall command 108 // GIVEN a set of uninstall parameters and a fake uninstall runner 109 // 110 // WHEN I call Uninstall 111 // THEN the istioctl uninstall returns success 112 func TestUninstall(t *testing.T) { 113 114 assert := assert.New(t) 115 SetCmdRunner(uninstallRunner{t: t}) 116 defer SetDefaultRunner() 117 118 stdout, stderr, err := Uninstall(vzlog.DefaultLogger()) 119 assert.NoError(err, "Uninstall returned an error") 120 assert.Len(stderr, 0, "Uninstall stderr should be empty") 121 assert.NotZero(stdout, "Uninstall stdout should not be empty") 122 } 123 124 // TestUninstallFail tests the istioctl uninstall command failure condition 125 // GIVEN a set of uninstall parameters and a fake runner that fails 126 // 127 // WHEN I call Uninstall 128 // THEN the istioctl uninstall returns an error 129 func TestUninstallFail(t *testing.T) { 130 assert := assert.New(t) 131 SetCmdRunner(badRunner{t: t}) 132 defer SetDefaultRunner() 133 134 stdout, stderr, err := Uninstall(vzlog.DefaultLogger()) 135 assert.Error(err, "Uninstall should have returned an error") 136 assert.Len(stdout, 0, "Uninstall stdout should be empty") 137 assert.NotZero(stderr, "Uninstall stderr should not be empty") 138 } 139 140 // fakeIsInstalledRunner overrides the istio run command 141 func (r fakeIstioInstalledRunner) Run(cmd *exec.Cmd) (stdout []byte, stderr []byte, err error) { 142 return []byte("Istio is installed and verified successfully"), []byte(""), nil 143 } 144 145 // Run should assert the command parameters are correct then return a success with stdout contents 146 func (r upgradeRunner) Run(cmd *exec.Cmd) (stdout []byte, stderr []byte, err error) { 147 assert := assert.New(r.t) 148 assert.Contains(cmd.Path, "istioctl", "command should contain istioctl") 149 assert.Contains(cmd.Args[0], "istioctl", "args should contain istioctl") 150 assert.Contains(cmd.Args[1], "install", "args should contain install") 151 assert.Contains(cmd.Args[2], "-y", "args should contain -y") 152 153 return []byte("success"), []byte(""), nil 154 } 155 156 // Run should assert the command parameters are correct then return a success with stdout contents 157 func (r installRunner) Run(cmd *exec.Cmd) (stdout []byte, stderr []byte, err error) { 158 assert := assert.New(r.t) 159 assert.Contains(cmd.Path, "istioctl", "command should contain istioctl") 160 assert.Contains(cmd.Args[0], "istioctl", "args should contain istioctl") 161 assert.Contains(cmd.Args[1], "install", "args should contain install") 162 assert.Contains(cmd.Args[2], "-y", "args should contain -y") 163 164 return []byte("success"), []byte(""), nil 165 } 166 167 // Run should assert the command parameters are correct then return a success with stdout contents 168 func (r uninstallRunner) Run(cmd *exec.Cmd) (stdout []byte, stderr []byte, err error) { 169 assert := assert.New(r.t) 170 assert.Contains(cmd.Path, "istioctl", "command should contain istioctl") 171 assert.Contains(cmd.Args[0], "istioctl", "args should contain istioctl") 172 assert.Contains(cmd.Args[1], "uninstall", "args should contain uninstall") 173 assert.Contains(cmd.Args[2], "--revision", "args should contain --revision") 174 assert.Contains(cmd.Args[3], "default", "args should contain default") 175 assert.Contains(cmd.Args[4], "-y", "args should contain -y") 176 177 return []byte("success"), []byte(""), nil 178 } 179 180 // Run should return an error with stderr contents 181 func (r badRunner) Run(cmd *exec.Cmd) (stdout []byte, stderr []byte, err error) { 182 return []byte(""), []byte("error"), errors.New("error") 183 }