github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/v7/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 DescribeTable("displays verbose output to terminal", 21 func(env string, configTrace string, flag bool) { 22 tmpDir, err := ioutil.TempDir("", "") 23 defer os.RemoveAll(tmpDir) 24 Expect(err).NotTo(HaveOccurred()) 25 26 helpers.SetupCF(ReadOnlyOrg, ReadOnlySpace) 27 28 // Invalidate the access token to cause a token refresh in order to 29 // test the call to the UAA. 30 helpers.SetConfig(func(config *configv3.Config) { 31 config.ConfigFile.AccessToken = helpers.ExpiredAccessToken() 32 }) 33 34 var envMap map[string]string 35 if env != "" { 36 if string(env[0]) == "/" { 37 env = filepath.Join(tmpDir, env) 38 } 39 envMap = map[string]string{"CF_TRACE": env} 40 } 41 42 command := []string{"run-task", "app", "echo"} 43 44 if flag { 45 command = append(command, "-v") 46 } 47 48 if configTrace != "" { 49 if string(configTrace[0]) == "/" { 50 configTrace = filepath.Join(tmpDir, configTrace) 51 } 52 session := helpers.CF("config", "--trace", configTrace) 53 Eventually(session).Should(Exit(0)) 54 } 55 56 session := helpers.CFWithEnv(envMap, command...) 57 58 Eventually(session).Should(Say("REQUEST:")) 59 Eventually(session).Should(Say("POST /oauth/token")) 60 Eventually(session).Should(Say(`User-Agent: cf/[\w.+-]+ \(go\d+\.\d+(\.\d+)?; %s %s\)`, runtime.GOARCH, runtime.GOOS)) 61 Eventually(session).Should(Say(`\[PRIVATE DATA HIDDEN\]`)) //This is required to test the previous line. If it fails, the previous matcher went too far. 62 Eventually(session).Should(Say("RESPONSE:")) 63 Eventually(session).Should(Say("REQUEST:")) 64 Eventually(session).Should(Say("GET /v3/apps")) 65 Eventually(session).Should(Say(`User-Agent: cf/[\w.+-]+ \(go\d+\.\d+(\.\d+)?; %s %s\)`, runtime.GOARCH, runtime.GOOS)) 66 Eventually(session).Should(Say("RESPONSE:")) 67 Eventually(session).Should(Exit(1)) 68 }, 69 70 Entry("CF_TRACE true: enables verbose", "true", "", false), 71 Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false), 72 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false), 73 74 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true), 75 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true), 76 77 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true), 78 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false), 79 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true), 80 81 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true), 82 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false), 83 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true), 84 ) 85 86 DescribeTable("displays verbose output to multiple files", 87 func(env string, configTrace string, flag bool, location []string) { 88 tmpDir, err := ioutil.TempDir("", "") 89 defer os.RemoveAll(tmpDir) 90 Expect(err).NotTo(HaveOccurred()) 91 92 helpers.SetupCF(ReadOnlyOrg, ReadOnlySpace) 93 94 // Invalidate the access token to cause a token refresh in order to 95 // test the call to the UAA. 96 helpers.SetConfig(func(config *configv3.Config) { 97 config.ConfigFile.AccessToken = helpers.ExpiredAccessToken() 98 }) 99 100 var envMap map[string]string 101 if env != "" { 102 if string(env[0]) == "/" { 103 env = filepath.Join(tmpDir, env) 104 } 105 envMap = map[string]string{"CF_TRACE": env} 106 } 107 108 command := []string{"run-task", "app", "echo"} 109 110 if flag { 111 command = append(command, "-v") 112 } 113 114 if configTrace != "" { 115 if string(configTrace[0]) == "/" { 116 configTrace = filepath.Join(tmpDir, configTrace) 117 } 118 session := helpers.CF("config", "--trace", configTrace) 119 Eventually(session).Should(Exit(0)) 120 } 121 122 session := helpers.CFWithEnv(envMap, command...) 123 Eventually(session).Should(Exit(1)) 124 125 for _, filePath := range location { 126 contents, err := ioutil.ReadFile(tmpDir + filePath) 127 Expect(err).ToNot(HaveOccurred()) 128 129 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 130 Expect(string(contents)).To(MatchRegexp("GET /v3/apps")) 131 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 132 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 133 Expect(string(contents)).To(MatchRegexp("POST /oauth/token")) 134 Expect(string(contents)).To(MatchRegexp("RESPONSE:")) 135 136 stat, err := os.Stat(tmpDir + filePath) 137 Expect(err).ToNot(HaveOccurred()) 138 139 if runtime.GOOS == "windows" { 140 Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String())) 141 } else { 142 Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String())) 143 } 144 } 145 }, 146 147 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false, []string{"/foo"}), 148 149 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", false, []string{"/foo"}), 150 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true, []string{"/foo"}), 151 152 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", false, []string{"/foo"}), 153 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true, []string{"/foo"}), 154 155 Entry("CF_TRACE filepath: enables logging to file", "/foo", "", false, []string{"/foo"}), 156 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true, []string{"/foo"}), 157 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false, []string{"/foo"}), 158 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", false, []string{"/foo", "/bar"}), 159 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true, []string{"/foo", "/bar"}), 160 ) 161 162 Describe("NOAA", func() { 163 var orgName string 164 165 BeforeEach(func() { 166 orgName = helpers.NewOrgName() 167 spaceName := helpers.NewSpaceName() 168 169 helpers.SetupCF(orgName, spaceName) 170 }) 171 172 AfterEach(func() { 173 Eventually(helpers.CF("config", "--trace", "false")).Should(Exit(0)) 174 helpers.QuickDeleteOrg(orgName) 175 }) 176 177 DescribeTable("displays verbose output to terminal", 178 func(env string, configTrace string, flag bool) { 179 tmpDir, err := ioutil.TempDir("", "") 180 defer os.RemoveAll(tmpDir) 181 Expect(err).NotTo(HaveOccurred()) 182 183 appName := helpers.PrefixedRandomName("app") 184 185 helpers.WithHelloWorldApp(func(appDir string) { 186 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 187 }) 188 189 var envMap map[string]string 190 if env != "" { 191 if string(env[0]) == "/" { 192 env = filepath.Join(tmpDir, env) 193 } 194 envMap = map[string]string{"CF_TRACE": env} 195 } 196 197 command := []string{"logs", appName} 198 199 if flag { 200 command = append(command, "-v") 201 } 202 203 if configTrace != "" { 204 if string(configTrace[0]) == "/" { 205 configTrace = filepath.Join(tmpDir, configTrace) 206 } 207 session := helpers.CF("config", "--trace", configTrace) 208 Eventually(session).Should(Exit(0)) 209 } 210 211 session := helpers.CFWithEnv(envMap, command...) 212 213 Eventually(session).Should(Say("REQUEST:")) 214 Eventually(session).Should(Say("POST /oauth/token")) 215 Eventually(session).Should(Say(`\[PRIVATE DATA HIDDEN\]`)) 216 Eventually(session).Should(Say("WEBSOCKET REQUEST:")) 217 Eventually(session).Should(Say(`Authorization: \[PRIVATE DATA HIDDEN\]`)) 218 Eventually(session.Kill()).Should(Exit()) 219 }, 220 221 Entry("CF_TRACE true: enables verbose", "true", "", false), 222 Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false), 223 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false), 224 225 Entry("CF_TRACE false, '-v': enables verbose", "false", "", true), 226 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true), 227 228 Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true), 229 Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false), 230 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true), 231 232 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true), 233 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false), 234 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true), 235 ) 236 237 DescribeTable("displays verbose output to multiple files", 238 func(env string, configTrace string, location []string) { 239 tmpDir, err := ioutil.TempDir("", "") 240 defer os.RemoveAll(tmpDir) 241 Expect(err).NotTo(HaveOccurred()) 242 243 appName := helpers.PrefixedRandomName("app") 244 245 helpers.WithHelloWorldApp(func(appDir string) { 246 Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0)) 247 }) 248 249 var envMap map[string]string 250 if env != "" { 251 if string(env[0]) == "/" { 252 env = filepath.Join(tmpDir, env) 253 } 254 envMap = map[string]string{"CF_TRACE": env} 255 } 256 257 if configTrace != "" { 258 if strings.HasPrefix(configTrace, "/") { 259 configTrace = filepath.Join(tmpDir, configTrace) 260 } 261 session := helpers.CF("config", "--trace", configTrace) 262 Eventually(session).Should(Exit(0)) 263 } 264 265 session := helpers.CFWithEnv(envMap, "logs", "-v", appName) 266 267 Eventually(session).Should(Say("WEBSOCKET RESPONSE")) 268 Eventually(session.Kill()).Should(Exit()) 269 270 for _, filePath := range location { 271 contents, err := ioutil.ReadFile(tmpDir + filePath) 272 Expect(err).ToNot(HaveOccurred()) 273 274 Expect(string(contents)).To(MatchRegexp("REQUEST:")) 275 Expect(string(contents)).To(MatchRegexp("POST /oauth/token")) 276 Expect(string(contents)).To(MatchRegexp(`\[PRIVATE DATA HIDDEN\]`)) 277 Expect(string(contents)).To(MatchRegexp("WEBSOCKET REQUEST:")) 278 Expect(string(contents)).To(MatchRegexp(`Authorization: \[PRIVATE DATA HIDDEN\]`)) 279 280 stat, err := os.Stat(tmpDir + filePath) 281 Expect(err).ToNot(HaveOccurred()) 282 283 if runtime.GOOS == "windows" { 284 Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String())) 285 } else { 286 Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String())) 287 } 288 } 289 }, 290 291 Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", []string{"/foo"}), 292 293 Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", []string{"/foo"}), 294 Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", []string{"/foo"}), 295 296 Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", []string{"/foo"}), 297 Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", []string{"/foo"}), 298 299 Entry("CF_TRACE filepath: enables logging to file", "/foo", "", []string{"/foo"}), 300 Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", []string{"/foo"}), 301 Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", []string{"/foo"}), 302 Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}), 303 Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}), 304 ) 305 }) 306 })