github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/integration/isolated/verbose_flag_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"runtime"
    10  
    11  	helpers "code.cloudfoundry.org/cli/integration/helpers"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/ginkgo/extensions/table"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  	. "github.com/onsi/gomega/gexec"
    18  )
    19  
    20  var _ = Describe("Verbose", func() {
    21  	Context("v2 legacy", func() {
    22  		DescribeTable("displays verbose output",
    23  			func(command func() *Session) {
    24  				login := exec.Command("cf", "auth", "admin", "admin")
    25  				loginSession, err := Start(login, GinkgoWriter, GinkgoWriter)
    26  				Expect(err).NotTo(HaveOccurred())
    27  				Eventually(loginSession).Should(Exit(0))
    28  
    29  				session := command()
    30  				Eventually(session).Should(Say("REQUEST:"))
    31  				Eventually(session).Should(Say("GET /v2/organizations"))
    32  				Eventually(session).Should(Say("RESPONSE:"))
    33  				Eventually(session).Should(Exit(0))
    34  			},
    35  
    36  			Entry("when the -v option is provided with additional command", func() *Session {
    37  				return helpers.CF("-v", "orgs")
    38  			}),
    39  
    40  			Entry("when the CF_TRACE env variable is set", func() *Session {
    41  				return helpers.CFWithEnv(map[string]string{"CF_TRACE": "true"}, "orgs")
    42  			}),
    43  		)
    44  	})
    45  
    46  	Context("v2 refactor", func() {
    47  		DescribeTable("displays verbose output to terminal",
    48  			func(env string, configTrace string, flag bool) {
    49  				tmpDir, err := ioutil.TempDir("", "")
    50  				defer os.RemoveAll(tmpDir)
    51  				Expect(err).NotTo(HaveOccurred())
    52  
    53  				setupCF(ReadOnlyOrg, ReadOnlySpace)
    54  
    55  				var envMap map[string]string
    56  				if env != "" {
    57  					if string(env[0]) == "/" {
    58  						env = filepath.Join(tmpDir, env)
    59  					}
    60  					envMap = map[string]string{"CF_TRACE": env}
    61  				}
    62  
    63  				// We use 'create-user' because it makes a request via the UAA client
    64  				// and a request via the CC client, testing the logging wrapper in both
    65  				// clients.
    66  				randomUsername := helpers.RandomUsername()
    67  				randomPassword := helpers.RandomPassword()
    68  				command := []string{"create-user", randomUsername, randomPassword}
    69  
    70  				if flag {
    71  					command = append(command, "-v")
    72  				}
    73  
    74  				if configTrace != "" {
    75  					if string(configTrace[0]) == "/" {
    76  						configTrace = filepath.Join(tmpDir, configTrace)
    77  					}
    78  					session := helpers.CF("config", "--trace", configTrace)
    79  					Eventually(session).Should(Exit(0))
    80  				}
    81  
    82  				session := helpers.CFWithEnv(envMap, command...)
    83  
    84  				Eventually(session).Should(Say("REQUEST:"))
    85  				Eventually(session).Should(Say("POST /Users"))
    86  				Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(%s; %s %s\\)", runtime.Version(), runtime.GOARCH, runtime.GOOS))
    87  				Eventually(session).Should(Say("RESPONSE:"))
    88  				Eventually(session).Should(Say("REQUEST:"))
    89  				Eventually(session).Should(Say("POST /v2/users"))
    90  				Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(%s; %s %s\\)", runtime.Version(), runtime.GOARCH, runtime.GOOS))
    91  				Eventually(session).Should(Say("RESPONSE:"))
    92  				Eventually(session).Should(Exit(0))
    93  			},
    94  
    95  			Entry("CF_TRACE true: enables verbose", "true", "", false),
    96  			Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false),
    97  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo/bar", false),
    98  
    99  			Entry("CF_TRACE false, '-v': enables verbose", "false", "", true),
   100  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo/bar", true),
   101  
   102  			Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true),
   103  			Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false),
   104  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo/bar", true),
   105  
   106  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo/bar", "", true),
   107  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo/bar", "true", false),
   108  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo/bar", "/baz", true),
   109  		)
   110  
   111  		DescribeTable("displays verbose output to multiple files",
   112  			func(env string, configTrace string, flag bool, location []string) {
   113  				tmpDir, err := ioutil.TempDir("", "")
   114  				defer os.RemoveAll(tmpDir)
   115  				Expect(err).NotTo(HaveOccurred())
   116  
   117  				setupCF(ReadOnlyOrg, ReadOnlySpace)
   118  
   119  				var envMap map[string]string
   120  				if env != "" {
   121  					if string(env[0]) == "/" {
   122  						env = filepath.Join(tmpDir, env)
   123  					}
   124  					envMap = map[string]string{"CF_TRACE": env}
   125  				}
   126  
   127  				// We use 'create-user' because it makes a request via the UAA client
   128  				// and a request via the CC client, testing the logging wrapper in both
   129  				// clients.
   130  				randomUsername := helpers.RandomUsername()
   131  				randomPassword := helpers.RandomPassword()
   132  				command := []string{"create-user", randomUsername, randomPassword}
   133  
   134  				if flag {
   135  					command = append(command, "-v")
   136  				}
   137  
   138  				if configTrace != "" {
   139  					if string(configTrace[0]) == "/" {
   140  						configTrace = filepath.Join(tmpDir, configTrace)
   141  					}
   142  					session := helpers.CF("config", "--trace", configTrace)
   143  					Eventually(session).Should(Exit(0))
   144  				}
   145  
   146  				session := helpers.CFWithEnv(envMap, command...)
   147  				Eventually(session).Should(Exit(0))
   148  
   149  				for _, filePath := range location {
   150  					contents, err := ioutil.ReadFile(tmpDir + filePath)
   151  					Expect(err).ToNot(HaveOccurred())
   152  
   153  					Expect(string(contents)).To(MatchRegexp("REQUEST:"))
   154  					Expect(string(contents)).To(MatchRegexp("POST /Users"))
   155  					Expect(string(contents)).To(MatchRegexp("RESPONSE:"))
   156  					Expect(string(contents)).To(MatchRegexp("REQUEST:"))
   157  					Expect(string(contents)).To(MatchRegexp("POST /v2/users"))
   158  					Expect(string(contents)).To(MatchRegexp("RESPONSE:"))
   159  
   160  					stat, err := os.Stat(tmpDir + filePath)
   161  					Expect(err).ToNot(HaveOccurred())
   162  
   163  					if runtime.GOOS == "windows" {
   164  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String()))
   165  					} else {
   166  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String()))
   167  					}
   168  				}
   169  			},
   170  
   171  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false, []string{"/foo"}),
   172  
   173  			Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", false, []string{"/foo"}),
   174  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true, []string{"/foo"}),
   175  
   176  			Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", false, []string{"/foo"}),
   177  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true, []string{"/foo"}),
   178  
   179  			Entry("CF_TRACE filepath: enables logging to file", "/foo", "", false, []string{"/foo"}),
   180  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true, []string{"/foo"}),
   181  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false, []string{"/foo"}),
   182  			Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", false, []string{"/foo", "/bar"}),
   183  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true, []string{"/foo", "/bar"}),
   184  		)
   185  	})
   186  
   187  	Context("v3", func() {
   188  		DescribeTable("displays verbose output to terminal",
   189  			func(env string, configTrace string, flag bool) {
   190  				tmpDir, err := ioutil.TempDir("", "")
   191  				defer os.RemoveAll(tmpDir)
   192  				Expect(err).NotTo(HaveOccurred())
   193  
   194  				setupCF(ReadOnlyOrg, ReadOnlySpace)
   195  
   196  				// Invalidate the access token to cause a token refresh in order to
   197  				// test the call to the UAA.
   198  				helpers.SetConfig(func(config *configv3.Config) {
   199  					config.ConfigFile.AccessToken = fmt.Sprintf("%sfoo", config.ConfigFile.AccessToken)
   200  				})
   201  
   202  				var envMap map[string]string
   203  				if env != "" {
   204  					if string(env[0]) == "/" {
   205  						env = filepath.Join(tmpDir, env)
   206  					}
   207  					envMap = map[string]string{"CF_TRACE": env}
   208  				}
   209  
   210  				command := []string{"run-task", "app", "echo"}
   211  
   212  				if flag {
   213  					command = append(command, "-v")
   214  				}
   215  
   216  				if configTrace != "" {
   217  					if string(configTrace[0]) == "/" {
   218  						configTrace = filepath.Join(tmpDir, configTrace)
   219  					}
   220  					session := helpers.CF("config", "--trace", configTrace)
   221  					Eventually(session).Should(Exit(0))
   222  				}
   223  
   224  				session := helpers.CFWithEnv(envMap, command...)
   225  
   226  				Eventually(session).Should(Say("REQUEST:"))
   227  				Eventually(session).Should(Say("GET /v3/apps"))
   228  				Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(%s; %s %s\\)", runtime.Version(), runtime.GOARCH, runtime.GOOS))
   229  				Eventually(session).Should(Say("RESPONSE:"))
   230  				Eventually(session).Should(Say("REQUEST:"))
   231  				Eventually(session).Should(Say("POST /oauth/token"))
   232  				Eventually(session).Should(Say("User-Agent: cf/[\\w.+-]+ \\(%s; %s %s\\)", runtime.Version(), runtime.GOARCH, runtime.GOOS))
   233  				Eventually(session).Should(Say("\\[PRIVATE DATA HIDDEN\\]")) //This is required to test the previous line. If it fails, the previous matcher went too far.
   234  				Eventually(session).Should(Say("RESPONSE:"))
   235  				Eventually(session).Should(Exit(1))
   236  			},
   237  
   238  			Entry("CF_TRACE true: enables verbose", "true", "", false),
   239  			Entry("CF_Trace true, config trace false: enables verbose", "true", "false", false),
   240  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo/bar", false),
   241  
   242  			Entry("CF_TRACE false, '-v': enables verbose", "false", "", true),
   243  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo/bar", true),
   244  
   245  			Entry("CF_TRACE empty:, '-v': enables verbose", "", "", true),
   246  			Entry("CF_TRACE empty, config trace true: enables verbose", "", "true", false),
   247  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo/bar", true),
   248  
   249  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo/bar", "", true),
   250  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo/bar", "true", false),
   251  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo/bar", "/baz", true),
   252  		)
   253  
   254  		DescribeTable("displays verbose output to multiple files",
   255  			func(env string, configTrace string, flag bool, location []string) {
   256  				tmpDir, err := ioutil.TempDir("", "")
   257  				defer os.RemoveAll(tmpDir)
   258  				Expect(err).NotTo(HaveOccurred())
   259  
   260  				setupCF(ReadOnlyOrg, ReadOnlySpace)
   261  
   262  				// Invalidate the access token to cause a token refresh in order to
   263  				// test the call to the UAA.
   264  				helpers.SetConfig(func(config *configv3.Config) {
   265  					config.ConfigFile.AccessToken = fmt.Sprintf("%sfoo", config.ConfigFile.AccessToken)
   266  				})
   267  
   268  				var envMap map[string]string
   269  				if env != "" {
   270  					if string(env[0]) == "/" {
   271  						env = filepath.Join(tmpDir, env)
   272  					}
   273  					envMap = map[string]string{"CF_TRACE": env}
   274  				}
   275  
   276  				command := []string{"run-task", "app", "echo"}
   277  
   278  				if flag {
   279  					command = append(command, "-v")
   280  				}
   281  
   282  				if configTrace != "" {
   283  					if string(configTrace[0]) == "/" {
   284  						configTrace = filepath.Join(tmpDir, configTrace)
   285  					}
   286  					session := helpers.CF("config", "--trace", configTrace)
   287  					Eventually(session).Should(Exit(0))
   288  				}
   289  
   290  				session := helpers.CFWithEnv(envMap, command...)
   291  				Eventually(session).Should(Exit(1))
   292  
   293  				for _, filePath := range location {
   294  					contents, err := ioutil.ReadFile(tmpDir + filePath)
   295  					Expect(err).ToNot(HaveOccurred())
   296  
   297  					Expect(string(contents)).To(MatchRegexp("REQUEST:"))
   298  					Expect(string(contents)).To(MatchRegexp("GET /v3/apps"))
   299  					Expect(string(contents)).To(MatchRegexp("RESPONSE:"))
   300  					Expect(string(contents)).To(MatchRegexp("REQUEST:"))
   301  					Expect(string(contents)).To(MatchRegexp("POST /oauth/token"))
   302  					Expect(string(contents)).To(MatchRegexp("RESPONSE:"))
   303  
   304  					stat, err := os.Stat(tmpDir + filePath)
   305  					Expect(err).ToNot(HaveOccurred())
   306  
   307  					if runtime.GOOS == "windows" {
   308  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0666).String()))
   309  					} else {
   310  						Expect(stat.Mode().String()).To(Equal(os.FileMode(0600).String()))
   311  					}
   312  				}
   313  			},
   314  
   315  			Entry("CF_Trace true, config trace file path: enables verbose AND logging to file", "true", "/foo", false, []string{"/foo"}),
   316  
   317  			Entry("CF_TRACE false, config trace file path: enables logging to file", "false", "/foo", false, []string{"/foo"}),
   318  			Entry("CF_TRACE false, config trace file path, '-v': enables verbose AND logging to file", "false", "/foo", true, []string{"/foo"}),
   319  
   320  			Entry("CF_TRACE empty, config trace file path: enables logging to file", "", "/foo", false, []string{"/foo"}),
   321  			Entry("CF_TRACE empty, config trace file path, '-v': enables verbose AND logging to file", "", "/foo", true, []string{"/foo"}),
   322  
   323  			Entry("CF_TRACE filepath: enables logging to file", "/foo", "", false, []string{"/foo"}),
   324  			Entry("CF_TRACE filepath, '-v': enables logging to file", "/foo", "", true, []string{"/foo"}),
   325  			Entry("CF_TRACE filepath, config trace true: enables verbose AND logging to file", "/foo", "true", false, []string{"/foo"}),
   326  			Entry("CF_TRACE filepath, config trace filepath: enables logging to file for BOTH paths", "/foo", "/bar", false, []string{"/foo", "/bar"}),
   327  			Entry("CF_TRACE filepath, config trace filepath, '-v': enables verbose AND logging to file for BOTH paths", "/foo", "/bar", true, []string{"/foo", "/bar"}),
   328  		)
   329  	})
   330  })