github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/utils/config/config_test.go (about) 1 package config_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "time" 9 10 . "code.cloudfoundry.org/cli/utils/config" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/ginkgo/extensions/table" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Config", func() { 18 var homeDir string 19 20 BeforeEach(func() { 21 var err error 22 homeDir, err = ioutil.TempDir("", "cli-config-tests") 23 Expect(err).NotTo(HaveOccurred()) 24 25 os.Setenv("CF_HOME", homeDir) 26 }) 27 28 AfterEach(func() { 29 if homeDir != "" { 30 os.RemoveAll(homeDir) 31 os.Unsetenv("CF_HOME") 32 } 33 }) 34 35 Context("when there isn't a config set", func() { 36 var ( 37 oldLang string 38 oldLCAll string 39 ) 40 41 BeforeEach(func() { 42 oldLang = os.Getenv("LANG") 43 oldLCAll = os.Getenv("LC_ALL") 44 os.Unsetenv("LANG") 45 os.Unsetenv("LC_ALL") 46 }) 47 48 It("returns a default config", func() { 49 defer os.Setenv("LANG", oldLang) 50 defer os.Setenv("LC_ALL", oldLCAll) 51 52 config, err := LoadConfig() 53 Expect(err).ToNot(HaveOccurred()) 54 55 Expect(config).ToNot(BeNil()) 56 Expect(config.Target()).To(Equal(DefaultTarget)) 57 Expect(config.ColorEnabled()).To(Equal(ColorEnabled)) 58 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 59 Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout)) 60 Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout)) 61 Expect(config.Locale()).To(BeEmpty()) 62 63 pluginConfig := config.PluginConfig() 64 Expect(pluginConfig).To(BeEmpty()) 65 }) 66 }) 67 68 DescribeTable("when the plugin config exists", 69 func(setup func() (string, string)) { 70 location, CFPluginHome := setup() 71 if CFPluginHome != "" { 72 os.Setenv("CF_PLUGIN_HOME", CFPluginHome) 73 defer os.Unsetenv("CF_PLUGIN_HOME") 74 } 75 76 rawConfig := ` 77 { 78 "Plugins": { 79 "Diego-Enabler": { 80 "Location": "~/.cf/plugins/diego-enabler_darwin_amd64", 81 "Version": { 82 "Major": 1, 83 "Minor": 0, 84 "Build": 1 85 }, 86 "Commands": [ 87 { 88 "Name": "enable-diego", 89 "Alias": "", 90 "HelpText": "enable Diego support for an app", 91 "UsageDetails": { 92 "Usage": "cf enable-diego APP_NAME", 93 "Options": null 94 } 95 }, 96 { 97 "Name": "disable-diego", 98 "Alias": "", 99 "HelpText": "disable Diego support for an app", 100 "UsageDetails": { 101 "Usage": "cf disable-diego APP_NAME", 102 "Options": null 103 } 104 } 105 ] 106 } 107 } 108 }` 109 setPluginConfig(location, rawConfig) 110 config, err := LoadConfig() 111 Expect(err).ToNot(HaveOccurred()) 112 Expect(config).ToNot(BeNil()) 113 114 plugins := config.PluginConfig() 115 Expect(plugins).ToNot(BeEmpty()) 116 117 plugin := plugins["Diego-Enabler"] 118 Expect(plugin.Location).To(Equal("~/.cf/plugins/diego-enabler_darwin_amd64")) 119 Expect(plugin.Version.Major).To(Equal(1)) 120 Expect(plugin.Commands).To(HaveLen(2)) 121 Expect(plugin.Commands).To(ContainElement( 122 PluginCommand{ 123 Name: "enable-diego", 124 Alias: "", 125 HelpText: "enable Diego support for an app", 126 UsageDetails: PluginUsageDetails{ 127 Usage: "cf enable-diego APP_NAME", 128 }, 129 }, 130 )) 131 }, 132 133 Entry("standard location", func() (string, string) { 134 return filepath.Join(homeDir, ".cf", "plugins"), "" 135 }), 136 137 Entry("non-standard location", func() (string, string) { 138 return filepath.Join(homeDir, "foo", ".cf", "plugins"), filepath.Join(homeDir, "foo") 139 }), 140 ) 141 142 Describe("Getter Functions", func() { 143 DescribeTable("ColorEnabled", 144 func(configVal string, envVal string, expected ColorSetting) { 145 rawConfig := fmt.Sprintf(`{"ColorEnabled":"%s"}`, configVal) 146 setConfig(homeDir, rawConfig) 147 148 defer os.Unsetenv("CF_COLOR") 149 if envVal == "" { 150 os.Unsetenv("CF_COLOR") 151 } else { 152 os.Setenv("CF_COLOR", envVal) 153 } 154 155 config, err := LoadConfig() 156 Expect(err).ToNot(HaveOccurred()) 157 Expect(config).ToNot(BeNil()) 158 159 Expect(config.ColorEnabled()).To(Equal(expected)) 160 }, 161 Entry("config=true env=true enabled", "true", "true", ColorEnabled), 162 Entry("config=true env=false disabled", "true", "false", ColorDisbled), 163 Entry("config=false env=true enabled", "false", "true", ColorEnabled), 164 Entry("config=false env=false disabled", "false", "false", ColorDisbled), 165 166 Entry("config=unset env=false disabled", "", "false", ColorDisbled), 167 Entry("config=unset env=true enabled", "", "true", ColorEnabled), 168 Entry("config=false env=unset disabled", "false", "", ColorDisbled), 169 Entry("config=true env=unset disabled", "true", "", ColorEnabled), 170 171 Entry("config=unset env=unset falls back to default", "", "", ColorEnabled), 172 ) 173 174 Describe("Target", func() { 175 var config *Config 176 177 BeforeEach(func() { 178 rawConfig := `{ "Target":"https://api.foo.com" }` 179 setConfig(homeDir, rawConfig) 180 181 var err error 182 config, err = LoadConfig() 183 Expect(err).ToNot(HaveOccurred()) 184 Expect(config).ToNot(BeNil()) 185 }) 186 187 It("returns fields directly from config", func() { 188 Expect(config.Target()).To(Equal("https://api.foo.com")) 189 }) 190 }) 191 192 DescribeTable("Locale", 193 func(langVal string, lcAllVall string, configVal string, expected string) { 194 rawConfig := fmt.Sprintf(`{"Locale":"%s"}`, configVal) 195 setConfig(homeDir, rawConfig) 196 197 defer os.Unsetenv("LANG") 198 if langVal == "" { 199 os.Unsetenv("LANG") 200 } else { 201 os.Setenv("LANG", langVal) 202 } 203 204 defer os.Unsetenv("LC_ALL") 205 if lcAllVall == "" { 206 os.Unsetenv("LC_ALL") 207 } else { 208 os.Setenv("LC_ALL", lcAllVall) 209 } 210 211 config, err := LoadConfig() 212 Expect(err).ToNot(HaveOccurred()) 213 Expect(config).ToNot(BeNil()) 214 215 Expect(config.Locale()).To(Equal(expected)) 216 }, 217 218 Entry("LANG=ko-KO.UTF-8 LC_ALL=empty config=empty ko-KO", "ko-KO.UTF-8", "", "", "ko-KO"), 219 Entry("LANG=ko-KO.UTF-8 LC_ALL=fr_FR.UTF-8 config=empty fr-FR", "ko-KO.UTF-8", "fr_FR.UTF-8", "", "fr-FR"), 220 Entry("LANG=ko-KO.UTF-8 LC_ALL=fr_FR.UTF-8 config=pt-BR pt-BR", "ko-KO.UTF-8", "fr_FR.UTF-8", "pt-BR", "pt-BR"), 221 222 Entry("config=empty LANG=empty LC_ALL=empty default", "", "", "", DefaultLocale), 223 ) 224 225 Describe("BinaryName", func() { 226 It("returns the name used to invoke", func() { 227 config, err := LoadConfig() 228 Expect(err).ToNot(HaveOccurred()) 229 Expect(config).ToNot(BeNil()) 230 231 // Ginkgo will uses a config file as the first test argument, so that 232 // will be considered the binary name 233 Expect(config.BinaryName()).To(MatchRegexp("config\\.test$")) 234 }) 235 }) 236 237 Context("when there are environment variables", func() { 238 var ( 239 originalCFStagingTimeout string 240 originalCFStartupTimeout string 241 originalHTTPSProxy string 242 243 config *Config 244 ) 245 246 BeforeEach(func() { 247 originalCFStagingTimeout = os.Getenv("CF_STAGING_TIMEOUT") 248 originalCFStartupTimeout = os.Getenv("CF_STARTUP_TIMEOUT") 249 originalHTTPSProxy = os.Getenv("https_proxy") 250 os.Setenv("CF_STAGING_TIMEOUT", "8675") 251 os.Setenv("CF_STARTUP_TIMEOUT", "309") 252 os.Setenv("https_proxy", "proxy.com") 253 254 var err error 255 config, err = LoadConfig() 256 Expect(err).ToNot(HaveOccurred()) 257 Expect(config).ToNot(BeNil()) 258 }) 259 260 AfterEach(func() { 261 os.Setenv("CF_STAGING_TIMEOUT", originalCFStagingTimeout) 262 os.Setenv("CF_STARTUP_TIMEOUT", originalCFStartupTimeout) 263 os.Setenv("https_proxy", originalHTTPSProxy) 264 }) 265 266 It("overrides specific config values", func() { 267 Expect(config.StagingTimeout()).To(Equal(time.Duration(8675) * time.Minute)) 268 Expect(config.StartupTimeout()).To(Equal(time.Duration(309) * time.Minute)) 269 Expect(config.HTTPSProxy()).To(Equal("proxy.com")) 270 }) 271 }) 272 }) 273 })