github.com/arunkumar7540/cli@v6.45.0+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("GET /v2/info"))
   215  				Eventually(session).Should(Say("WEBSOCKET REQUEST:"))
   216  				Eventually(session).Should(Say(`Authorization: \[PRIVATE DATA HIDDEN\]`))
   217  				Eventually(session.Kill()).Should(Exit())
   218  			},
   219  
   220  			Entry("CF_TRACE true: enables verbose", "true", "", false),
   221  			Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false),
   222  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false),
   223  
   224  			Entry("CF_TRACE false, '-v': enables verbose", "false", "", true),
   225  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true),
   226  
   227  			Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true),
   228  			Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false),
   229  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true),
   230  
   231  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true),
   232  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false),
   233  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true),
   234  		)
   235  
   236  		DescribeTable("displays verbose output to multiple files",
   237  			func(env string, configTrace string, location []string) {
   238  				tmpDir, err := ioutil.TempDir("", "")
   239  				defer os.RemoveAll(tmpDir)
   240  				Expect(err).NotTo(HaveOccurred())
   241  
   242  				appName := helpers.PrefixedRandomName("app")
   243  
   244  				helpers.WithHelloWorldApp(func(appDir string) {
   245  					Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "--no-route")).Should(Exit(0))
   246  				})
   247  
   248  				var envMap map[string]string
   249  				if env != "" {
   250  					if string(env[0]) == "/" {
   251  						env = filepath.Join(tmpDir, env)
   252  					}
   253  					envMap = map[string]string{"CF_TRACE": env}
   254  				}
   255  
   256  				if configTrace != "" {
   257  					if strings.HasPrefix(configTrace, "/") {
   258  						configTrace = filepath.Join(tmpDir, configTrace)
   259  					}
   260  					session := helpers.CF("config", "--trace", configTrace)
   261  					Eventually(session).Should(Exit(0))
   262  				}
   263  
   264  				session := helpers.CFWithEnv(envMap, "logs", "-v", appName)
   265  
   266  				Eventually(session).Should(Say("WEBSOCKET RESPONSE"))
   267  				Eventually(session.Kill()).Should(Exit())
   268  
   269  				for _, filePath := range location {
   270  					contents, err := ioutil.ReadFile(tmpDir + filePath)
   271  					Expect(err).ToNot(HaveOccurred())
   272  
   273  					Expect(string(contents)).To(MatchRegexp("REQUEST:"))
   274  					Expect(string(contents)).To(MatchRegexp("GET /v2/info"))
   275  					Expect(string(contents)).To(MatchRegexp("WEBSOCKET REQUEST:"))
   276  					Expect(string(contents)).To(MatchRegexp(`Authorization: \[PRIVATE DATA HIDDEN\]`))
   277  
   278  					stat, err := os.Stat(tmpDir + filePath)
   279  					Expect(err).ToNot(HaveOccurred())
   280  
   281  					if runtime.GOOS == "windows" {
   282  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String()))
   283  					} else {
   284  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String()))
   285  					}
   286  				}
   287  			},
   288  
   289  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", []string{"/foo"}),
   290  
   291  			Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", []string{"/foo"}),
   292  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", []string{"/foo"}),
   293  
   294  			Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", []string{"/foo"}),
   295  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", []string{"/foo"}),
   296  
   297  			Entry("CF_TRACE filepath: enables logging to file", "/foo", "", []string{"/foo"}),
   298  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", []string{"/foo"}),
   299  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", []string{"/foo"}),
   300  			Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}),
   301  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", []string{"/foo", "/bar"}),
   302  		)
   303  	})
   304  })