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