github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/config_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	helpers "code.cloudfoundry.org/cli/integration/helpers"
     8  	"code.cloudfoundry.org/cli/util/configv3"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("Config", func() {
    16  	var configDir string
    17  
    18  	BeforeEach(func() {
    19  		configDir = filepath.Join(homeDir, ".cf")
    20  	})
    21  
    22  	Describe("Empty Config File", func() {
    23  		BeforeEach(func() {
    24  			helpers.SetConfigContent(configDir, "")
    25  		})
    26  
    27  		It("displays json warning for a refactored command", func() {
    28  			session := helpers.CF("api")
    29  			Eventually(session.Err).Should(Say("Warning: Error read/writing config: unexpected end of JSON input for %s\n", helpers.ConvertPathToRegularExpression(filepath.Join(configDir, "config.json"))))
    30  			Eventually(session).Should(Exit())
    31  		})
    32  
    33  		It("displays json warning for an unrefactored command", func() {
    34  			session := helpers.CF("curl", "/v2/info")
    35  			Eventually(session.Err).Should(Say("Warning: Error read/writing config: unexpected end of JSON input for %s\n", helpers.ConvertPathToRegularExpression(filepath.Join(configDir, "config.json"))))
    36  			Eventually(session).Should(Exit())
    37  		})
    38  	})
    39  
    40  	Describe("Lingering Config Temp Files", func() {
    41  		Context("when lingering tmp files exist from previous failed attempts to write the config", func() {
    42  			BeforeEach(func() {
    43  				for i := 0; i < 3; i++ {
    44  					tmpFile, err := ioutil.TempFile(configDir, "temp-config")
    45  					Expect(err).ToNot(HaveOccurred())
    46  					tmpFile.Close()
    47  				}
    48  			})
    49  
    50  			It("removes those temp files on `logout`", func() {
    51  				Eventually(helpers.CF("logout")).Should(Exit(0))
    52  
    53  				oldTempFileNames, err := filepath.Glob(filepath.Join(configDir, "temp-config?*"))
    54  				Expect(err).ToNot(HaveOccurred())
    55  				Expect(oldTempFileNames).To(BeEmpty())
    56  			})
    57  
    58  			It("removes those temp files on `login`", func() {
    59  				Eventually(helpers.CF("login")).Should(Exit(1))
    60  
    61  				oldTempFileNames, err := filepath.Glob(filepath.Join(configDir, "temp-config?*"))
    62  				Expect(err).ToNot(HaveOccurred())
    63  				Expect(oldTempFileNames).To(BeEmpty())
    64  			})
    65  
    66  			It("removes those temp files on `auth`", func() {
    67  				helpers.LoginCF()
    68  
    69  				oldTempFileNames, err := filepath.Glob(filepath.Join(configDir, "temp-config?*"))
    70  				Expect(err).ToNot(HaveOccurred())
    71  				Expect(oldTempFileNames).To(BeEmpty())
    72  			})
    73  
    74  			It("removes those temp files on `oauth-token`", func() {
    75  				Eventually(helpers.CF("oauth-token")).Should(Exit(1))
    76  
    77  				oldTempFileNames, err := filepath.Glob(filepath.Join(configDir, "temp-config?*"))
    78  				Expect(err).ToNot(HaveOccurred())
    79  				Expect(oldTempFileNames).To(BeEmpty())
    80  			})
    81  		})
    82  	})
    83  
    84  	Describe("Enable Color", func() {
    85  		Context("when color is enabled", func() {
    86  			It("prints colors", func() {
    87  				session := helpers.CFWithEnv(map[string]string{"CF_COLOR": "true"}, "help")
    88  				Eventually(session).Should(Say("\x1b\\[1m"))
    89  				Eventually(session).Should(Exit())
    90  			})
    91  		})
    92  
    93  		Context("when color is disabled", func() {
    94  			It("does not print colors", func() {
    95  				session := helpers.CFWithEnv(map[string]string{"CF_COLOR": "false"}, "help")
    96  				Consistently(session).ShouldNot(Say("\x1b\\[1m"))
    97  				Eventually(session).Should(Exit(0))
    98  			})
    99  		})
   100  	})
   101  
   102  	Describe("Dial Timeout", func() {
   103  		Context("when the dial timeout is set", func() {
   104  			BeforeEach(func() {
   105  				config, err := configv3.LoadConfig()
   106  				Expect(err).ToNot(HaveOccurred())
   107  
   108  				config.ConfigFile.Target = "http://1.2.3.4"
   109  
   110  				err = configv3.WriteConfig(config)
   111  				Expect(err).ToNot(HaveOccurred())
   112  			})
   113  
   114  			It("times out connection attempts after the dial timeout has passed", func() {
   115  				session := helpers.CFWithEnv(map[string]string{"CF_DIAL_TIMEOUT": "1"}, "unbind-service", "banana", "pants")
   116  				Eventually(session.Err).Should(Say("dial tcp 1.2.3.4:80: i/o timeout"))
   117  				Eventually(session).Should(Exit())
   118  			})
   119  		})
   120  	})
   121  })