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