github.com/loafoe/cli@v7.1.0+incompatible/util/configv3/env_test.go (about)

     1  package configv3_test
     2  
     3  import (
     4  	"time"
     5  
     6  	. "code.cloudfoundry.org/cli/util/configv3"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/ginkgo/extensions/table"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Config", func() {
    14  	var (
    15  		config *Config
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		config = &Config{}
    20  	})
    21  
    22  	When("there are environment variables set", func() {
    23  		BeforeEach(func() {
    24  			config.ENV = EnvOverride{
    25  				CFDialTimeout:    "1234",
    26  				CFPassword:       "I am password.",
    27  				CFStagingTimeout: "8675",
    28  				CFStartupTimeout: "309",
    29  				CFUsername:       "i-R-user",
    30  				DockerPassword:   "banana",
    31  				HTTPSProxy:       "proxy.com",
    32  			}
    33  		})
    34  
    35  		It("overrides specific config values using those variables", func() {
    36  			Expect(config.CFUsername()).To(Equal("i-R-user"))
    37  			Expect(config.CFPassword()).To(Equal("I am password."))
    38  			Expect(config.DialTimeout()).To(Equal(1234 * time.Second))
    39  			Expect(config.DockerPassword()).To(Equal("banana"))
    40  			Expect(config.HTTPSProxy()).To(Equal("proxy.com"))
    41  			Expect(config.StagingTimeout()).To(Equal(time.Duration(8675) * time.Minute))
    42  			Expect(config.StartupTimeout()).To(Equal(time.Duration(309) * time.Minute))
    43  		})
    44  	})
    45  
    46  	Describe("BinaryName", func() {
    47  		BeforeEach(func() {
    48  			config.ENV.BinaryName = "potatoman"
    49  		})
    50  
    51  		It("returns the name used to invoke", func() {
    52  			Expect(config.BinaryName()).To(Equal("potatoman"))
    53  		})
    54  	})
    55  
    56  	Describe("BinaryVersion", func() {
    57  		It("returns back version.BinaryVersion", func() {
    58  			conf := Config{}
    59  			Expect(conf.BinaryVersion()).To(Equal("0.0.0-unknown-version"))
    60  		})
    61  	})
    62  
    63  	Describe("DialTimeout", func() {
    64  		When("no DialTimeout is set in the env", func() {
    65  			BeforeEach(func() {
    66  				config.ENV.CFDialTimeout = ""
    67  			})
    68  
    69  			It("uses the default dial timeout", func() {
    70  				Expect(config.DialTimeout()).To(Equal(DefaultDialTimeout))
    71  			})
    72  		})
    73  	})
    74  
    75  	DescribeTable("Experimental",
    76  		func(envVal string, expected bool) {
    77  			config.ENV.Experimental = envVal
    78  			Expect(config.Experimental()).To(Equal(expected))
    79  		},
    80  
    81  		Entry("uses default value of false if environment value is not set", "", false),
    82  		Entry("uses environment value if a valid environment value is set", "true", true),
    83  		Entry("uses default value of false if an invalid environment value is set", "something-invalid", false),
    84  	)
    85  
    86  	DescribeTable("LogLevel",
    87  		func(envVal string, expectedLevel int) {
    88  			config := Config{ENV: EnvOverride{CFLogLevel: envVal}}
    89  			Expect(config.LogLevel()).To(Equal(expectedLevel))
    90  		},
    91  
    92  		Entry("Default to 0", "", 0),
    93  		Entry("panic returns 0", "panic", 0),
    94  		Entry("fatal returns 1", "fatal", 1),
    95  		Entry("error returns 2", "error", 2),
    96  		Entry("warn returns 3", "warn", 3),
    97  		Entry("info returns 4", "info", 4),
    98  		Entry("debug returns 5", "debug", 5),
    99  		Entry("dEbUg returns 5", "dEbUg", 5),
   100  	)
   101  
   102  	Describe("StagingTimeout", func() {
   103  		When("no StagingTimeout is set in the env", func() {
   104  			BeforeEach(func() {
   105  				config.ENV.CFStagingTimeout = ""
   106  			})
   107  
   108  			It("uses the default staging timeout", func() {
   109  				Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout))
   110  			})
   111  		})
   112  	})
   113  
   114  	Describe("StartupTimeout", func() {
   115  		When("no StartupTimeout is set in the env", func() {
   116  			BeforeEach(func() {
   117  				config.ENV.CFStartupTimeout = ""
   118  			})
   119  
   120  			It("uses the default startup timeout", func() {
   121  				Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout))
   122  			})
   123  		})
   124  	})
   125  })