github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/util/configv3/env_test.go (about)

     1  package configv3_test
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	. "code.cloudfoundry.org/cli/util/configv3"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Config", func() {
    15  	var (
    16  		config  *Config
    17  		homeDir string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		homeDir = setup()
    22  	})
    23  
    24  	AfterEach(func() {
    25  		teardown(homeDir)
    26  	})
    27  
    28  	Context("when there are environment variables", func() {
    29  		BeforeEach(func() {
    30  			Expect(os.Setenv("CF_DIAL_TIMEOUT", "1234")).ToNot(HaveOccurred())
    31  			Expect(os.Setenv("CF_DOCKER_PASSWORD", "banana")).ToNot(HaveOccurred())
    32  			Expect(os.Setenv("CF_PASSWORD", "I am password.")).ToNot(HaveOccurred())
    33  			Expect(os.Setenv("CF_STAGING_TIMEOUT", "8675")).ToNot(HaveOccurred())
    34  			Expect(os.Setenv("CF_STARTUP_TIMEOUT", "309")).ToNot(HaveOccurred())
    35  			Expect(os.Setenv("CF_USERNAME", "i-R-user")).ToNot(HaveOccurred())
    36  			Expect(os.Setenv("https_proxy", "proxy.com")).ToNot(HaveOccurred())
    37  
    38  			var err error
    39  			config, err = LoadConfig()
    40  			Expect(err).ToNot(HaveOccurred())
    41  			Expect(config).ToNot(BeNil())
    42  		})
    43  
    44  		AfterEach(func() {
    45  			Expect(os.Unsetenv("CF_DIAL_TIMEOUT")).ToNot(HaveOccurred())
    46  			Expect(os.Unsetenv("CF_DOCKER_PASSWORD")).ToNot(HaveOccurred())
    47  			Expect(os.Unsetenv("CF_PASSWORD")).ToNot(HaveOccurred())
    48  			Expect(os.Unsetenv("CF_STAGING_TIMEOUT")).ToNot(HaveOccurred())
    49  			Expect(os.Unsetenv("CF_STARTUP_TIMEOUT")).ToNot(HaveOccurred())
    50  			Expect(os.Unsetenv("CF_USERNAME")).ToNot(HaveOccurred())
    51  			Expect(os.Unsetenv("https_proxy")).ToNot(HaveOccurred())
    52  		})
    53  
    54  		It("overrides specific config values", func() {
    55  			Expect(config.CFUsername()).To(Equal("i-R-user"))
    56  			Expect(config.CFPassword()).To(Equal("I am password."))
    57  			Expect(config.DialTimeout()).To(Equal(1234 * time.Second))
    58  			Expect(config.DockerPassword()).To(Equal("banana"))
    59  			Expect(config.HTTPSProxy()).To(Equal("proxy.com"))
    60  			Expect(config.StagingTimeout()).To(Equal(time.Duration(8675) * time.Minute))
    61  			Expect(config.StartupTimeout()).To(Equal(time.Duration(309) * time.Minute))
    62  		})
    63  	})
    64  
    65  	Describe("BinaryName", func() {
    66  		It("returns the name used to invoke", func() {
    67  			config, err := LoadConfig()
    68  			Expect(err).ToNot(HaveOccurred())
    69  			Expect(config).ToNot(BeNil())
    70  
    71  			// Ginkgo will uses a config file as the first test argument, so that
    72  			// will be considered the binary name
    73  			Expect(config.BinaryName()).To(Equal("configv3.test"))
    74  		})
    75  	})
    76  
    77  	Describe("BinaryVersion", func() {
    78  		It("returns back version.BinaryVersion", func() {
    79  			conf := Config{}
    80  			Expect(conf.BinaryVersion()).To(Equal("0.0.0-unknown-version"))
    81  		})
    82  	})
    83  
    84  	DescribeTable("Experimental",
    85  		func(envVal string, expected bool) {
    86  			setConfig(homeDir, `{}`)
    87  
    88  			defer os.Unsetenv("CF_CLI_EXPERIMENTAL")
    89  			Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred())
    90  			if envVal != "" {
    91  				Expect(os.Setenv("CF_CLI_EXPERIMENTAL", envVal)).ToNot(HaveOccurred())
    92  			}
    93  
    94  			config, err := LoadConfig()
    95  			Expect(err).ToNot(HaveOccurred())
    96  			Expect(config).ToNot(BeNil())
    97  
    98  			Expect(config.Experimental()).To(Equal(expected))
    99  		},
   100  
   101  		Entry("uses default value of false if environment value is not set", "", false),
   102  		Entry("uses environment value if a valid environment value is set", "true", true),
   103  		Entry("uses default value of false if an invalid environment value is set", "something-invalid", false),
   104  	)
   105  
   106  	DescribeTable("LogLevel",
   107  		func(envVal string, expectedLevel int) {
   108  			config := Config{ENV: EnvOverride{CFLogLevel: envVal}}
   109  			Expect(config.LogLevel()).To(Equal(expectedLevel))
   110  		},
   111  
   112  		Entry("Default to 0", "", 0),
   113  		Entry("panic returns 0", "panic", 0),
   114  		Entry("fatal returns 1", "fatal", 1),
   115  		Entry("error returns 2", "error", 2),
   116  		Entry("warn returns 3", "warn", 3),
   117  		Entry("info returns 4", "info", 4),
   118  		Entry("debug returns 5", "debug", 5),
   119  		Entry("dEbUg returns 5", "dEbUg", 5),
   120  	)
   121  })