github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/helpers/command_test.go (about) 1 // Copyright (c) 2022, 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 helpers 5 6 import ( 7 "bytes" 8 "fmt" 9 "os" 10 "testing" 11 "time" 12 13 "github.com/spf13/cobra" 14 "github.com/stretchr/testify/assert" 15 "github.com/verrazzano/verrazzano/tools/vz/pkg/constants" 16 testhelpers "github.com/verrazzano/verrazzano/tools/vz/test/helpers" 17 "k8s.io/cli-runtime/pkg/genericclioptions" 18 ) 19 20 const ( 21 testUse = "A sample usage help" 22 testShort = "A sample short help" 23 testLong = "A sample long help" 24 flagNotDefinedErrFmt = "flag accessed but not defined: %s" 25 ) 26 27 // TestNewCommand tests the functionality to create a new command based on the usage, short and log help. 28 func TestNewCommand(t *testing.T) { 29 buf := new(bytes.Buffer) 30 errBuf := new(bytes.Buffer) 31 rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 32 assert.NotNil(t, NewCommand(rc, testUse, testShort, testLong)) 33 } 34 35 // TestGetWaitTimeout tests the functionality that returns the wait timeout if set or returns the default timeout value. 36 func TestGetWaitTimeout(t *testing.T) { 37 // GIVEN a command with no values provided for wait and timeout flags, 38 // WHEN we get the wait timeout value, 39 // THEN an error along with default timeout value of (0) is returned. 40 timeout, err := GetWaitTimeout(getCommandWithoutFlags(), constants.TimeoutFlag) 41 assert.Error(t, err) 42 assert.Contains(t, err.Error(), fmt.Sprintf(flagNotDefinedErrFmt, constants.WaitFlag)) 43 assert.NotNil(t, timeout) 44 assert.Equal(t, time.Duration(0), timeout) 45 46 // GIVEN a command with a value of true for wait and no value provided for timeout flag, 47 // WHEN we get the wait timeout value, 48 // THEN an error along with default timeout value of (0) is returned. 49 cmdWithWaitFlagTrue := getCommandWithoutFlags() 50 cmdWithWaitFlagTrue.PersistentFlags().Bool(constants.WaitFlag, true, "") 51 timeout, err = GetWaitTimeout(cmdWithWaitFlagTrue, constants.TimeoutFlag) 52 assert.Error(t, err) 53 assert.Contains(t, err.Error(), fmt.Sprintf(flagNotDefinedErrFmt, constants.TimeoutFlag)) 54 assert.NotNil(t, timeout) 55 assert.Equal(t, time.Duration(0), timeout) 56 57 // GIVEN a command with a value of false for wait and no value provided for timeout flag, 58 // WHEN we get the wait timeout value, 59 // THEN an error along with default timeout value of (0) is returned. 60 cmdWithWaitFlagFalse := getCommandWithoutFlags() 61 cmdWithWaitFlagFalse.PersistentFlags().Bool(constants.WaitFlag, false, "") 62 timeout, err = GetWaitTimeout(cmdWithWaitFlagFalse, constants.TimeoutFlag) 63 assert.NoError(t, err) 64 assert.NotNil(t, timeout) 65 assert.Equal(t, time.Duration(0), timeout) 66 67 // GIVEN a command with a value of true for wait and value provided for timeout flag, 68 // WHEN we get the wait timeout value, 69 // THEN an error along with default timeout value of (0) is returned. 70 cmdWithWaitFlagAndTimeout := getCommandWithoutFlags() 71 cmdWithWaitFlagAndTimeout.PersistentFlags().Bool(constants.WaitFlag, true, "") 72 cmdWithWaitFlagAndTimeout.PersistentFlags().Duration(constants.TimeoutFlag, time.Duration(1), "") 73 timeout, err = GetWaitTimeout(cmdWithWaitFlagAndTimeout, constants.TimeoutFlag) 74 assert.NoError(t, err) 75 assert.NotNil(t, timeout) 76 assert.Equal(t, time.Duration(1), timeout) 77 } 78 79 // TestGetLogFormat tests the functionality that returns the log format that was set by the user. 80 func TestGetLogFormat(t *testing.T) { 81 // GIVEN a command with no value provided for the log format flag, 82 // WHEN we get the log format, 83 // THEN the default log pattern is returned. 84 logFormat, err := GetLogFormat(getCommandWithoutFlags()) 85 assert.NoError(t, err) 86 assert.NotNil(t, logFormat) 87 assert.Equal(t, LogFormatSimple, logFormat) 88 89 // GIVEN a command with value custom provided for the log format flag, 90 // WHEN we get the log format, 91 // THEN the custom log pattern is returned. 92 cmdWithLogFormat := getCommandWithoutFlags() 93 cmdWithLogFormat.PersistentFlags().String(constants.LogFormatFlag, "custom", "") 94 logFormat, err = GetLogFormat(cmdWithLogFormat) 95 assert.NoError(t, err) 96 assert.NotNil(t, logFormat) 97 assert.Equal(t, LogFormat("custom"), logFormat) 98 } 99 100 // TestGetVersion tests the functionality that returns the right Verrazzano version. 101 func TestGetVersion(t *testing.T) { 102 // Create a fake VZ helper 103 buf := new(bytes.Buffer) 104 errBuf := new(bytes.Buffer) 105 rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 106 107 // GIVEN a command with no values provided for version flags, 108 // WHEN we get the version value, 109 // THEN an error is returned. 110 version, err := GetVersion(getCommandWithoutFlags(), rc) 111 assert.Error(t, err) 112 assert.Contains(t, err.Error(), fmt.Sprintf(flagNotDefinedErrFmt, constants.VersionFlag)) 113 assert.NotNil(t, version) 114 assert.Equal(t, "", version) 115 116 // GIVEN a command with a default values provided for version flags, 117 // WHEN we get the version value, 118 // THEN no error is returned. 119 cmdWithDefaultVZVersion := getCommandWithoutFlags() 120 cmdWithDefaultVZVersion.PersistentFlags().String(constants.VersionFlag, constants.VersionFlagDefault, "") 121 version, err = GetVersion(cmdWithDefaultVZVersion, rc) 122 assert.NoError(t, err) 123 assert.NotNil(t, version) 124 125 // GIVEN a command with a valid version provided for version flags, 126 // WHEN we get the version value, 127 // THEN no error is returned. 128 cmdWithDefaultVZVersion = getCommandWithoutFlags() 129 cmdWithDefaultVZVersion.PersistentFlags().String(constants.VersionFlag, "v9.9.9", "") 130 version, err = GetVersion(cmdWithDefaultVZVersion, rc) 131 assert.NoError(t, err) 132 assert.Equal(t, "v9.9.9", version) 133 134 // GIVEN a command with a valid version provided for version flags, 135 // WHEN we get the version value, 136 // THEN no error is returned. 137 cmdWithDefaultVZVersion = getCommandWithoutFlags() 138 cmdWithDefaultVZVersion.PersistentFlags().String(constants.VersionFlag, "V9.9.9", "") 139 version, err = GetVersion(cmdWithDefaultVZVersion, rc) 140 assert.NoError(t, err) 141 assert.Equal(t, "v9.9.9", version) 142 143 // GIVEN a command with a valid version provided for version flags, 144 // WHEN we get the version value, 145 // THEN no error is returned. 146 cmdWithDefaultVZVersion = getCommandWithoutFlags() 147 cmdWithDefaultVZVersion.PersistentFlags().String(constants.VersionFlag, "9.9.9", "") 148 version, err = GetVersion(cmdWithDefaultVZVersion, rc) 149 assert.NoError(t, err) 150 assert.Equal(t, "v9.9.9", version) 151 152 // GIVEN a command with an invalid version provided for version flags, 153 // WHEN we get the version value, 154 // THEN an error is returned. 155 cmdWithDefaultVZVersion = getCommandWithoutFlags() 156 cmdWithDefaultVZVersion.PersistentFlags().String(constants.VersionFlag, "invalid", "") 157 _, err = GetVersion(cmdWithDefaultVZVersion, rc) 158 assert.EqualError(t, err, "Invalid version string: invalid (valid format is vn.n.n or n.n.n)") 159 } 160 161 // TestGetVersionWithManifests tests the GetVersion function when the user supplies the manifests flag. 162 func TestGetVersionWithManifests(t *testing.T) { 163 // Create a fake VZ helper 164 buf := new(bytes.Buffer) 165 errBuf := new(bytes.Buffer) 166 rc := testhelpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf}) 167 168 // GIVEN a command with a specific version and a manifests file with a version that does not match, 169 // WHEN we get the version value, 170 // THEN an error is returned. 171 cmd := getCommandWithoutFlags() 172 cmd.PersistentFlags().String(constants.VersionFlag, "", "") 173 cmd.PersistentFlags().Set(constants.VersionFlag, "9.9.9") 174 cmd.PersistentFlags().String(constants.ManifestsFlag, "", "") 175 cmd.PersistentFlags().Set(constants.ManifestsFlag, "../../test/testdata/operator-file-fake.yaml") 176 _, err := GetVersion(cmd, rc) 177 assert.ErrorContains(t, err, "Requested version '9.9.9' does not match manifests version 'v1.5.2'") 178 179 // GIVEN a command with a specific version and a manifests file with a version that matches, 180 // WHEN we get the version value, 181 // THEN no error is returned and the returned version is the version specified in the version flag (prefixed with a "v"). 182 cmd = getCommandWithoutFlags() 183 cmd.PersistentFlags().String(constants.VersionFlag, "", "") 184 cmd.PersistentFlags().Set(constants.VersionFlag, "1.5.2") 185 cmd.PersistentFlags().String(constants.ManifestsFlag, "", "") 186 cmd.PersistentFlags().Set(constants.ManifestsFlag, "../../test/testdata/operator-file-fake.yaml") 187 version, err := GetVersion(cmd, rc) 188 assert.NoError(t, err) 189 assert.Equal(t, "v1.5.2", version) 190 191 // GIVEN a command with a specific version that includes a suffix and a manifests file with a major/minor/patch version that matches, 192 // WHEN we get the version value, 193 // THEN no error is returned and the returned version is the version specified in the version flag (prefixed with a "v"). 194 cmd = getCommandWithoutFlags() 195 cmd.PersistentFlags().String(constants.VersionFlag, "", "") 196 cmd.PersistentFlags().Set(constants.VersionFlag, "1.5.2-1234+xyz") 197 cmd.PersistentFlags().String(constants.ManifestsFlag, "", "") 198 cmd.PersistentFlags().Set(constants.ManifestsFlag, "../../test/testdata/operator-file-fake.yaml") 199 version, err = GetVersion(cmd, rc) 200 assert.NoError(t, err) 201 assert.Equal(t, "v1.5.2-1234+xyz", version) 202 203 // GIVEN the manifests flag has a value and the version flag is not provided, 204 // WHEN we get the version value, 205 // THEN no error is returned and the returned version is the version from the VPO deployment in the manifests. 206 cmd = getCommandWithoutFlags() 207 cmd.PersistentFlags().String(constants.VersionFlag, constants.VersionFlagDefault, "") 208 cmd.PersistentFlags().String(constants.ManifestsFlag, "", "") 209 cmd.PersistentFlags().Set(constants.ManifestsFlag, "../../test/testdata/operator-file-fake.yaml") 210 version, err = GetVersion(cmd, rc) 211 assert.NoError(t, err) 212 assert.Equal(t, "v1.5.2", version) 213 } 214 215 // TestGetOperatorFile tests the functionality to return the right operator file. 216 func TestGetOperatorFile(t *testing.T) { 217 // GIVEN a command with no value provided for the operator file flag, 218 // WHEN we get the operator file, 219 // THEN the default value of operator file is returned. 220 operatorFile, err := getOperatorFileFromFlag(getCommandWithoutFlags()) 221 assert.Error(t, err) 222 assert.Contains(t, err.Error(), fmt.Sprintf(flagNotDefinedErrFmt, constants.ManifestsFlag)) 223 assert.NotNil(t, operatorFile) 224 assert.Equal(t, "", operatorFile) 225 226 // GIVEN a command with no value provided for the operator file flag, 227 // WHEN we get the operator file, 228 // THEN the default value of operator file is returned. 229 cmdWithOperatorFile := getCommandWithoutFlags() 230 cmdWithOperatorFile.PersistentFlags().String(constants.ManifestsFlag, "/tmp/operator.yaml", "") 231 operatorFile, err = getOperatorFileFromFlag(cmdWithOperatorFile) 232 assert.NoError(t, err) 233 assert.NotNil(t, operatorFile) 234 assert.Equal(t, "/tmp/operator.yaml", operatorFile) 235 236 } 237 238 // getCommandWithoutFlags creates a dummy test command with no flags 239 func getCommandWithoutFlags() *cobra.Command { 240 return &cobra.Command{ 241 Use: testUse, 242 Short: testShort, 243 Long: testLong, 244 } 245 }