github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/integration/isolated/verbose_flag_test.go (about) 1 package isolated 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "runtime" 8 "strings" 9 10 "code.cloudfoundry.org/cli/integration/helpers" 11 "code.cloudfoundry.org/cli/util/configv3" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/ginkgo/extensions/table" 14 . "github.com/onsi/gomega" 15 . "github.com/onsi/gomega/gbytes" 16 . "github.com/onsi/gomega/gexec" 17 ) 18 19 var _ = Describe("Verbose", func() { 20 Context("v2 legacy", func() { 21 DescribeTable("displays verbose output", 22 func(command func() *Session) { 23 helpers.LoginCF() 24 25 session := command() 26 Eventually(session).Should(Say("REQUEST:")) 27 Eventually(session).Should(Say("GET /v2/organizations")) 28 Eventually(session).Should(Say("RESPONSE:")) 29 Eventually(session).Should(Exit(0)) 30 }, 31 32 Entry("when the -v option is provided with additional command", func() *Session { 33 return helpers.CF("-v", "orgs") 34 }), 35 36 Entry("when the CF_TRACE env variable is set", func() *Session { 37 return helpers.CFWithEnv(map[string]string{"CF_TRACE": "true"}, "orgs") 38 }), 39 ) 40 }) 41 42 Context("v2 refactor", func() { 43 DescribeTable("displays verbose output to terminal", 44 func(env string, configTrace string, flag bool) { 45 tmpDir, err := ioutil.TempDir("", "") 46 defer os.RemoveAll(tmpDir) 47 Expect(err).NotTo(HaveOccurred()) 48 49 helpers.LoginCF() 50 helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace) 51 52 var envMap map[string]string 53 if env != "" { 54 if string(env[0]) == "/" { 55 env = filepath.Join(tmpDir, env) 56 } 57 envMap = map[string]string{"CF_TRACE": env} 58 } 59 60 // We use 'create-user' because it makes a request via the UAA client 61 // and a request via the CC client, testing the logging wrapper in both 62 // clients. 63 randomUsername := helpers.NewUsername() 64 randomPassword := helpers.NewPassword() 65 command := []string{"create-user", randomUsername, randomPassword} 66 67 if flag { 68 command = append(command, "-v") 69 } 70 71 if configTrace != "" { 72 if string(configTrace[0]) == "/" { 73 configTrace = filepath.Join(tmpDir, configTrace) 74 } 75 session := helpers.CF("config", "--trace", configTrace) 76 Eventually(session).Should(Exit(0)) 77 } 78 79 session := helpers.CFWithEnv(envMap, command...) 80 81 Eventually(session).Should(Say("REQUEST:")) 82 Eventually(session).Should(Say("GET /v2/info")) 83 Eventually(session).Should(Say("RESPONSE:")) 84 Eventually(session).Should(Say(`"token_endpoint": "http.*"`)) 85 Eventually(session).Should(Say("REQUEST:")) 86 Eventually(session).Should(Say("POST /Users")) 87 Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(go\\d+\\.\\d+(\\.\\d+)?; %s %s\\)", runtime.GOARCH, runtime.GOOS)) 88 Eventually(session).Should(Say("RESPONSE:")) 89 Eventually(session).Should(Say("REQUEST:")) 90 Eventually(session).Should(Say("POST /v2/users")) 91 Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(go\\d+\\.\\d+(\\.\\d+)?; %s %s\\)", runtime.GOARCH, runtime.GOOS)) 92 Eventually(session).Should(Say("RESPONSE:")) 93 Eventually(session).Should(Exit(0)) 94 }, 95 96 Entry("CF_TRACE true: enables verbose", "true", "", false), 97 Entry("CF_TRACE true, config trace false: enables verbose", "true", "false", false), 98 Entry("CF_TRACE true, config trace file path: enables verbose AND logging to file", "true", "/foo", false), 99 100 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true), 101 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true), 102 103 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true), 104 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false), 105 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true), 106 107 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true), 108 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false), 109 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true), 110 ) 111 112 DescribeTable("displays verbose output to multiple files", 113 func(env string, configTrace string, flag bool, location []string) { 114 tmpDir, err := ioutil.TempDir("", "") 115 defer os.RemoveAll(tmpDir) 116 Expect(err).NotTo(HaveOccurred()) 117 118 helpers.LoginCF() 119 helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace) 120 121 var envMap map[string]string 122 if env != "" { 123 if string(env[0]) == "/" { 124 env = filepath.Join(tmpDir, env) 125 } 126 envMap = map[string]string{"CF_TRACE": env} 127 } 128 129 // We use 'create-user' because it makes a request via the UAA client 130 // and a request via the CC client, testing the logging wrapper in both 131 // clients. 132 randomUsername := helpers.NewUsername() 133 randomPassword := helpers.NewPassword() 134 command := []string{"create-user", randomUsername, randomPassword} 135 136 if flag { 137 command = append(command, "-v") 138 } 139 140 if configTrace != "" { 141 if string(configTrace[0]) == "/" { 142 configTrace = filepath.Join(tmpDir, configTrace) 143 } 144 session := helpers.CF("config", "--trace", configTrace) 145 Eventually(session).Should(Exit(0)) 146 } 147 148 session := helpers.CFWithEnv(envMap, command...) 149 Eventually(session).Should(Exit(0)) 150 151 for _, filePath := range location { 152 contents, err := ioutil.ReadFile(tmpDir + filePath) 153 Expect(err).ToNot(HaveOccurred()) 154 155 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 156 Expect(string(contents)).To(MatchRegexp("POST /Users")) 157 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 158 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 159 Expect(string(contents)).To(MatchRegexp("POST /v2/users")) 160 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 161 162 stat, err := os.Stat(tmpDir + filePath) 163 Expect(err).ToNot(HaveOccurred()) 164 165 if runtime.GOOS == "windows" { 166 Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String())) 167 } else { 168 Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String())) 169 } 170 } 171 }, 172 173 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false, []string{"/foo"}), 174 175 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", false, []string{"/foo"}), 176 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true, []string{"/foo"}), 177 178 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", false, []string{"/foo"}), 179 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true, []string{"/foo"}), 180 181 Entry("CF_TRACE filepath: enables logging to file", "/foo", "", false, []string{"/foo"}), 182 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true, []string{"/foo"}), 183 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false, []string{"/foo"}), 184 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", false, []string{"/foo", "/bar"}), 185 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true, []string{"/foo", "/bar"}), 186 ) 187 }) 188 189 Context("v3", func() { 190 DescribeTable("displays verbose output to terminal", 191 func(env string, configTrace string, flag bool) { 192 tmpDir, err := ioutil.TempDir("", "") 193 defer os.RemoveAll(tmpDir) 194 Expect(err).NotTo(HaveOccurred()) 195 196 helpers.SetupCF(ReadOnlyOrg, ReadOnlySpace) 197 198 // Invalidate the access token to cause a token refresh in order to 199 // test the call to the UAA. 200 helpers.SetConfig(func(config *configv3.Config) { 201 config.ConfigFile.AccessToken = helpers.InvalidAccessToken() 202 }) 203 204 var envMap map[string]string 205 if env != "" { 206 if string(env[0]) == "/" { 207 env = filepath.Join(tmpDir, env) 208 } 209 envMap = map[string]string{"CF_TRACE": env} 210 } 211 212 command := []string{"run-task", "app", "echo"} 213 214 if flag { 215 command = append(command, "-v") 216 } 217 218 if configTrace != "" { 219 if string(configTrace[0]) == "/" { 220 configTrace = filepath.Join(tmpDir, configTrace) 221 } 222 session := helpers.CF("config", "--trace", configTrace) 223 Eventually(session).Should(Exit(0)) 224 } 225 226 session := helpers.CFWithEnv(envMap, command...) 227 228 Eventually(session).Should(Say("REQUEST:")) 229 Eventually(session).Should(Say("GET /v3/apps")) 230 Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(go\\d+\\.\\d+(\\.\\d+)?; %s %s\\)", runtime.GOARCH, runtime.GOOS)) 231 Eventually(session).Should(Say("RESPONSE:")) 232 Eventually(session).Should(Say("REQUEST:")) 233 Eventually(session).Should(Say("POST /oauth/token")) 234 Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(go\\d+\\.\\d+(\\.\\d+)?; %s %s\\)", runtime.GOARCH, runtime.GOOS)) 235 Eventually(session).Should(Say("\\[PRIVATE DATA HIDDEN\\]")) //This is required to test the previous line. If it fails, the previous matcher went too far. 236 Eventually(session).Should(Say("RESPONSE:")) 237 Eventually(session).Should(Exit(1)) 238 }, 239 240 Entry("CF_TRACE true: enables verbose", "true", "", false), 241 Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false), 242 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false), 243 244 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true), 245 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true), 246 247 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true), 248 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false), 249 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true), 250 251 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true), 252 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false), 253 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true), 254 ) 255 256 DescribeTable("displays verbose output to multiple files", 257 func(env string, configTrace string, flag bool, location []string) { 258 tmpDir, err := ioutil.TempDir("", "") 259 defer os.RemoveAll(tmpDir) 260 Expect(err).NotTo(HaveOccurred()) 261 262 helpers.SetupCF(ReadOnlyOrg, ReadOnlySpace) 263 264 // Invalidate the access token to cause a token refresh in order to 265 // test the call to the UAA. 266 helpers.SetConfig(func(config *configv3.Config) { 267 config.ConfigFile.AccessToken = helpers.InvalidAccessToken() 268 }) 269 270 var envMap map[string]string 271 if env != "" { 272 if string(env[0]) == "/" { 273 env = filepath.Join(tmpDir, env) 274 } 275 envMap = map[string]string{"CF_TRACE": env} 276 } 277 278 command := []string{"run-task", "app", "echo"} 279 280 if flag { 281 command = append(command, "-v") 282 } 283 284 if configTrace != "" { 285 if string(configTrace[0]) == "/" { 286 configTrace = filepath.Join(tmpDir, configTrace) 287 } 288 session := helpers.CF("config", "--trace", configTrace) 289 Eventually(session).Should(Exit(0)) 290 } 291 292 session := helpers.CFWithEnv(envMap, command...) 293 Eventually(session).Should(Exit(1)) 294 295 for _, filePath := range location { 296 contents, err := ioutil.ReadFile(tmpDir + filePath) 297 Expect(err).ToNot(HaveOccurred()) 298 299 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 300 Expect(string(contents)).To(MatchRegexp("GET /v3/apps")) 301 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 302 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 303 Expect(string(contents)).To(MatchRegexp("POST /oauth/token")) 304 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 305 306 stat, err := os.Stat(tmpDir + filePath) 307 Expect(err).ToNot(HaveOccurred()) 308 309 if runtime.GOOS == "windows" { 310 Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String())) 311 } else { 312 Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String())) 313 } 314 } 315 }, 316 317 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false, []string{"/foo"}), 318 319 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", false, []string{"/foo"}), 320 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true, []string{"/foo"}), 321 322 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", false, []string{"/foo"}), 323 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true, []string{"/foo"}), 324 325 Entry("CF_TRACE filepath: enables logging to file", "/foo", "", false, []string{"/foo"}), 326 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true, []string{"/foo"}), 327 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false, []string{"/foo"}), 328 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", false, []string{"/foo", "/bar"}), 329 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true, []string{"/foo", "/bar"}), 330 ) 331 }) 332 333 Describe("NOAA", func() { 334 var orgName string 335 336 BeforeEach(func() { 337 orgName = helpers.NewOrgName() 338 spaceName := helpers.NewSpaceName() 339 340 helpers.SetupCF(orgName, spaceName) 341 }) 342 343 AfterEach(func() { 344 Eventually(helpers.CF("config", "--trace", "false")).Should(Exit(0)) 345 helpers.QuickDeleteOrg(orgName) 346 }) 347 348 DescribeTable("displays verbose output to terminal", 349 func(env string, configTrace string, flag bool) { 350 tmpDir, err := ioutil.TempDir("", "") 351 defer os.RemoveAll(tmpDir) 352 Expect(err).NotTo(HaveOccurred()) 353 354 appName := helpers.PrefixedRandomName("app") 355 356 helpers.WithHelloWorldApp(func(appDir string) { 357 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 358 }) 359 360 var envMap map[string]string 361 if env != "" { 362 if string(env[0]) == "/" { 363 env = filepath.Join(tmpDir, env) 364 } 365 envMap = map[string]string{"CF_TRACE": env} 366 } 367 368 command := []string{"logs", appName} 369 370 if flag { 371 command = append(command, "-v") 372 } 373 374 if configTrace != "" { 375 if string(configTrace[0]) == "/" { 376 configTrace = filepath.Join(tmpDir, configTrace) 377 } 378 session := helpers.CF("config", "--trace", configTrace) 379 Eventually(session).Should(Exit(0)) 380 } 381 382 session := helpers.CFWithEnv(envMap, command...) 383 384 Eventually(session).Should(Say("REQUEST:")) 385 Eventually(session).Should(Say("POST /oauth/token")) 386 Eventually(session).Should(Say("\\[PRIVATE DATA HIDDEN\\]")) 387 Eventually(session).Should(Say("WEBSOCKET REQUEST:")) 388 Eventually(session).Should(Say("Authorization: \\[PRIVATE DATA HIDDEN\\]")) 389 Eventually(session.Kill()).Should(Exit()) 390 }, 391 392 Entry("CF_TRACE true: enables verbose", "true", "", false), 393 Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false), 394 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false), 395 396 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true), 397 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true), 398 399 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true), 400 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false), 401 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true), 402 403 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true), 404 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false), 405 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true), 406 ) 407 408 DescribeTable("displays verbose output to multiple files", 409 func(env string, configTrace string, location []string) { 410 tmpDir, err := ioutil.TempDir("", "") 411 defer os.RemoveAll(tmpDir) 412 Expect(err).NotTo(HaveOccurred()) 413 414 appName := helpers.PrefixedRandomName("app") 415 416 helpers.WithHelloWorldApp(func(appDir string) { 417 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 418 }) 419 420 var envMap map[string]string 421 if env != "" { 422 if string(env[0]) == "/" { 423 env = filepath.Join(tmpDir, env) 424 } 425 envMap = map[string]string{"CF_TRACE": env} 426 } 427 428 if configTrace != "" { 429 if strings.HasPrefix(configTrace, "/") { 430 configTrace = filepath.Join(tmpDir, configTrace) 431 } 432 session := helpers.CF("config", "--trace", configTrace) 433 Eventually(session).Should(Exit(0)) 434 } 435 436 session := helpers.CFWithEnv(envMap, "logs", "-v", appName) 437 438 Eventually(session).Should(Say("WEBSOCKET RESPONSE")) 439 Eventually(session.Kill()).Should(Exit()) 440 441 for _, filePath := range location { 442 contents, err := ioutil.ReadFile(tmpDir + filePath) 443 Expect(err).ToNot(HaveOccurred()) 444 445 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 446 Expect(string(contents)).To(MatchRegexp("POST /oauth/token")) 447 Expect(string(contents)).To(MatchRegexp("\\[PRIVATE DATA HIDDEN\\]")) 448 Expect(string(contents)).To(MatchRegexp("WEBSOCKET REQUEST:")) 449 Expect(string(contents)).To(MatchRegexp("Authorization: \\[PRIVATE DATA HIDDEN\\]")) 450 451 stat, err := os.Stat(tmpDir + filePath) 452 Expect(err).ToNot(HaveOccurred()) 453 454 if runtime.GOOS == "windows" { 455 Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String())) 456 } else { 457 Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String())) 458 } 459 } 460 }, 461 462 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", []string{"/foo"}), 463 464 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", []string{"/foo"}), 465 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", []string{"/foo"}), 466 467 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", []string{"/foo"}), 468 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", []string{"/foo"}), 469 470 Entry("CF_TRACE filepath: enables logging to file", "/foo", "", []string{"/foo"}), 471 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", []string{"/foo"}), 472 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", []string{"/foo"}), 473 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}), 474 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}), 475 ) 476 }) 477 })