github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/util/configv3/load_config_test.go (about) 1 package configv3_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 8 "code.cloudfoundry.org/cli/command/translatableerror" 9 . "code.cloudfoundry.org/cli/util/configv3" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("Config", func() { 16 var homeDir string 17 18 BeforeEach(func() { 19 homeDir = setup() 20 }) 21 22 AfterEach(func() { 23 teardown(homeDir) 24 }) 25 26 Describe("LoadConfig", func() { 27 Context("when there isn't a config set", func() { 28 var ( 29 oldLang string 30 oldLCAll string 31 ) 32 33 BeforeEach(func() { 34 oldLang = os.Getenv("LANG") 35 oldLCAll = os.Getenv("LC_ALL") 36 Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred()) 37 Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred()) 38 }) 39 40 It("returns a default config", func() { 41 defer os.Setenv("LANG", oldLang) 42 defer os.Setenv("LC_ALL", oldLCAll) 43 44 // specifically for when we run unit tests locally 45 // we save and unset this variable in case it's present 46 // since we want to load a default config 47 envVal := os.Getenv("CF_CLI_EXPERIMENTAL") 48 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred()) 49 50 config, err := LoadConfig() 51 Expect(err).ToNot(HaveOccurred()) 52 53 // then we reset the env variable 54 err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal) 55 Expect(err).ToNot(HaveOccurred()) 56 57 Expect(config).ToNot(BeNil()) 58 Expect(config.Target()).To(Equal(DefaultTarget)) 59 Expect(config.SkipSSLValidation()).To(BeFalse()) 60 Expect(config.ColorEnabled()).To(Equal(ColorEnabled)) 61 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 62 Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout)) 63 Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout)) 64 Expect(config.Locale()).To(BeEmpty()) 65 Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient)) 66 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 67 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 68 Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout)) 69 Expect(config.LogLevel()).To(Equal(0)) 70 Expect(config.DockerPassword()).To(BeEmpty()) 71 72 Expect(config.PluginRepositories()).To(Equal([]PluginRepository{{ 73 Name: "CF-Community", 74 URL: "https://plugins.cloudfoundry.org", 75 }})) 76 Expect(config.Experimental()).To(BeFalse()) 77 78 pluginConfig := config.Plugins() 79 Expect(pluginConfig).To(BeEmpty()) 80 81 trace, location := config.Verbose() 82 Expect(trace).To(BeFalse()) 83 Expect(location).To(BeEmpty()) 84 85 // test the plugins map is initialized 86 config.AddPlugin(Plugin{}) 87 }) 88 }) 89 90 Context("when there is a config set", func() { 91 var ( 92 config *Config 93 err error 94 ) 95 96 Context("but it is empty", func() { 97 var ( 98 oldLang string 99 oldLCAll string 100 ) 101 102 BeforeEach(func() { 103 oldLang = os.Getenv("LANG") 104 oldLCAll = os.Getenv("LC_ALL") 105 Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred()) 106 Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred()) 107 108 setConfig(homeDir, "") 109 }) 110 111 It("returns the default config with a json error", func() { 112 defer os.Setenv("LANG", oldLang) 113 defer os.Setenv("LC_ALL", oldLCAll) 114 115 // specifically for when we run unit tests locally 116 // we save and unset this variable in case it's present 117 // since we want to load a default config 118 envVal := os.Getenv("CF_CLI_EXPERIMENTAL") 119 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred()) 120 121 config, err = LoadConfig() 122 Expect(err).To(Equal(translatableerror.EmptyConfigError{FilePath: filepath.Join(homeDir, ".cf", "config.json")})) 123 124 // then we reset the env variable 125 err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal) 126 Expect(err).ToNot(HaveOccurred()) 127 128 Expect(config).ToNot(BeNil()) 129 Expect(config.Target()).To(Equal(DefaultTarget)) 130 Expect(config.SkipSSLValidation()).To(BeFalse()) 131 Expect(config.ColorEnabled()).To(Equal(ColorEnabled)) 132 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 133 Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout)) 134 Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout)) 135 Expect(config.Locale()).To(BeEmpty()) 136 Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient)) 137 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 138 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 139 Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout)) 140 Expect(config.LogLevel()).To(Equal(0)) 141 Expect(config.DockerPassword()).To(BeEmpty()) 142 143 Expect(config.PluginRepositories()).To(Equal([]PluginRepository{{ 144 Name: "CF-Community", 145 URL: "https://plugins.cloudfoundry.org", 146 }})) 147 Expect(config.Experimental()).To(BeFalse()) 148 149 pluginConfig := config.Plugins() 150 Expect(pluginConfig).To(BeEmpty()) 151 152 trace, location := config.Verbose() 153 Expect(trace).To(BeFalse()) 154 Expect(location).To(BeEmpty()) 155 156 // test the plugins map is initialized 157 config.AddPlugin(Plugin{}) 158 }) 159 }) 160 161 Context("and there are old temp-config* files lingering from previous failed attempts to write the config", func() { 162 var ( 163 oldLang string 164 oldLCAll string 165 ) 166 167 BeforeEach(func() { 168 oldLang = os.Getenv("LANG") 169 oldLCAll = os.Getenv("LC_ALL") 170 Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred()) 171 Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred()) 172 173 setConfig(homeDir, `{}`) 174 configDir := filepath.Join(homeDir, ".cf") 175 for i := 0; i < 3; i++ { 176 tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config") 177 Expect(fileErr).ToNot(HaveOccurred()) 178 tmpFile.Close() 179 } 180 }) 181 182 It("returns the default config and removes the lingering temp-config* files", func() { 183 defer os.Setenv("LANG", oldLang) 184 defer os.Setenv("LC_ALL", oldLCAll) 185 186 // specifically for when we run unit tests locally 187 // we save and unset this variable in case it's present 188 // since we want to load a default config 189 envVal := os.Getenv("CF_CLI_EXPERIMENTAL") 190 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).ToNot(HaveOccurred()) 191 192 config, err = LoadConfig() 193 Expect(err).ToNot(HaveOccurred()) 194 195 oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*")) 196 Expect(configErr).ToNot(HaveOccurred()) 197 Expect(oldTempFileNames).To(BeEmpty()) 198 199 // then we reset the env variable 200 err = os.Setenv("CF_CLI_EXPERIMENTAL", envVal) 201 Expect(err).ToNot(HaveOccurred()) 202 203 Expect(config).ToNot(BeNil()) 204 Expect(config.Target()).To(Equal(DefaultTarget)) 205 Expect(config.SkipSSLValidation()).To(BeFalse()) 206 Expect(config.ColorEnabled()).To(Equal(ColorEnabled)) 207 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 208 Expect(config.StagingTimeout()).To(Equal(DefaultStagingTimeout)) 209 Expect(config.StartupTimeout()).To(Equal(DefaultStartupTimeout)) 210 Expect(config.Locale()).To(BeEmpty()) 211 Expect(config.SSHOAuthClient()).To(Equal(DefaultSSHOAuthClient)) 212 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 213 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 214 Expect(config.OverallPollingTimeout()).To(Equal(DefaultOverallPollingTimeout)) 215 Expect(config.LogLevel()).To(Equal(0)) 216 217 Expect(config.Experimental()).To(BeFalse()) 218 219 pluginConfig := config.Plugins() 220 Expect(pluginConfig).To(BeEmpty()) 221 222 trace, location := config.Verbose() 223 Expect(trace).To(BeFalse()) 224 Expect(location).To(BeEmpty()) 225 226 // test the plugins map is initialized 227 config.AddPlugin(Plugin{}) 228 }) 229 }) 230 231 Context("when UAAOAuthClient is not present", func() { 232 BeforeEach(func() { 233 setConfig(homeDir, `{}`) 234 235 config, err = LoadConfig() 236 Expect(err).ToNot(HaveOccurred()) 237 }) 238 239 It("sets UAAOAuthClient to the default", func() { 240 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 241 }) 242 243 It("sets UAAOAuthClientSecret to the default", func() { 244 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 245 }) 246 }) 247 248 Context("when UAAOAuthClient is empty", func() { 249 BeforeEach(func() { 250 rawConfig := ` 251 { 252 "UAAOAuthClient": "" 253 }` 254 setConfig(homeDir, rawConfig) 255 256 config, err = LoadConfig() 257 Expect(err).ToNot(HaveOccurred()) 258 }) 259 260 It("sets UAAOAuthClient to the default", func() { 261 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 262 }) 263 264 It("sets UAAOAuthClientSecret to the default", func() { 265 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 266 }) 267 }) 268 }) 269 }) 270 })