github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/configv3/load_config_test.go (about) 1 package configv3_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 "code.cloudfoundry.org/cli/command/translatableerror" 10 "code.cloudfoundry.org/cli/integration/helpers" 11 "code.cloudfoundry.org/cli/util/configv3" 12 . "code.cloudfoundry.org/cli/util/configv3" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Config", func() { 19 var ( 20 oldLang string 21 oldLCAll string 22 oldCfExperimental string 23 homeDir string 24 ) 25 26 BeforeEach(func() { 27 homeDir = setup() 28 29 oldLang = os.Getenv("LANG") 30 oldLCAll = os.Getenv("LC_ALL") 31 // specifically for when we run unit tests locally 32 // we save and unset this variable in case it's present 33 // since we want to load a default config 34 oldCfExperimental = os.Getenv("CF_CLI_EXPERIMENTAL") 35 Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred()) 36 Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred()) 37 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).To(Succeed()) 38 }) 39 40 AfterEach(func() { 41 os.Setenv("LANG", oldLang) 42 os.Setenv("LC_ALL", oldLCAll) 43 os.Setenv("CF_CLI_EXPERIMENTAL", oldCfExperimental) 44 teardown(homeDir) 45 }) 46 47 Describe("LoadConfig", func() { 48 var ( 49 config *Config 50 loadErr error 51 inFlags []FlagOverride 52 ) 53 54 BeforeEach(func() { 55 inFlags = []FlagOverride{} 56 }) 57 58 JustBeforeEach(func() { 59 config, loadErr = LoadConfig(inFlags...) 60 }) 61 62 When("there are old temp-config* files lingering from previous failed attempts to write the config", func() { 63 BeforeEach(func() { 64 configDir := filepath.Join(homeDir, ".cf") 65 Expect(os.MkdirAll(configDir, 0777)).To(Succeed()) 66 for i := 0; i < 3; i++ { 67 tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config") 68 Expect(fileErr).ToNot(HaveOccurred()) 69 tmpFile.Close() 70 } 71 }) 72 73 It("removes the lingering temp-config* files", func() { 74 Expect(loadErr).ToNot(HaveOccurred()) 75 76 oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*")) 77 Expect(configErr).ToNot(HaveOccurred()) 78 Expect(oldTempFileNames).To(BeEmpty()) 79 }) 80 }) 81 82 When("there isn't a config set", func() { 83 It("returns a default config", func() { 84 Expect(loadErr).ToNot(HaveOccurred()) 85 86 Expect(config).ToNot(BeNil()) 87 Expect(config.ConfigFile).To(Equal( 88 JSONConfig{ 89 ColorEnabled: DefaultColorEnabled, 90 ConfigVersion: configv3.CurrentConfigVersion, 91 SSHOAuthClient: DefaultSSHOAuthClient, 92 UAAOAuthClient: DefaultUAAOAuthClient, 93 UAAOAuthClientSecret: DefaultUAAOAuthClientSecret, 94 PluginRepositories: []PluginRepository{{ 95 Name: DefaultPluginRepoName, 96 URL: DefaultPluginRepoURL, 97 }}, 98 Target: DefaultTarget, 99 }, 100 )) 101 Expect(config.ENV).To(Equal( 102 EnvOverride{ 103 BinaryName: "configv3.test", // Ginkgo will uses a config file as the first test argument, so that will be considered the binary name 104 }, 105 )) 106 Expect(config.Flags).To(Equal(FlagOverride{})) 107 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 108 109 pluginConfig := config.Plugins() 110 Expect(pluginConfig).To(BeEmpty()) 111 112 // test the plugins map is initialized 113 config.AddPlugin(Plugin{}) 114 }) 115 }) 116 117 When("there is a config set", func() { 118 Context("but it is empty", func() { 119 BeforeEach(func() { 120 setConfig(homeDir, "") 121 }) 122 123 It("returns the default config and an EmptyConfigError", func() { 124 Expect(loadErr).To(Equal(translatableerror.EmptyConfigError{FilePath: filepath.Join(homeDir, ".cf", "config.json")})) 125 Expect(config).ToNot(BeNil()) 126 Expect(config).ToNot(BeNil()) 127 Expect(config.ConfigFile).To(Equal( 128 JSONConfig{ 129 ColorEnabled: DefaultColorEnabled, 130 ConfigVersion: configv3.CurrentConfigVersion, 131 SSHOAuthClient: DefaultSSHOAuthClient, 132 UAAOAuthClient: DefaultUAAOAuthClient, 133 UAAOAuthClientSecret: DefaultUAAOAuthClientSecret, 134 PluginRepositories: []PluginRepository{{ 135 Name: DefaultPluginRepoName, 136 URL: DefaultPluginRepoURL, 137 }}, 138 Target: DefaultTarget, 139 }, 140 )) 141 Expect(config.ENV).To(Equal( 142 EnvOverride{ 143 BinaryName: "configv3.test", // Ginkgo will uses a config file as the first test argument, so that will be considered the binary name 144 }, 145 )) 146 Expect(config.Flags).To(Equal(FlagOverride{})) 147 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 148 149 pluginConfig := config.Plugins() 150 Expect(pluginConfig).To(BeEmpty()) 151 }) 152 }) 153 154 When("UAAOAuthClient is not present", func() { 155 BeforeEach(func() { 156 setConfig(homeDir, `{}`) 157 }) 158 159 It("sets UAAOAuthClient to the default", func() { 160 Expect(loadErr).ToNot(HaveOccurred()) 161 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 162 }) 163 164 It("sets UAAOAuthClientSecret to the default", func() { 165 Expect(loadErr).ToNot(HaveOccurred()) 166 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 167 }) 168 }) 169 170 When("UAAOAuthClient is empty", func() { 171 BeforeEach(func() { 172 rawConfig := fmt.Sprintf(` 173 { 174 "UAAOAuthClient": "", 175 "ConfigVersion": %d 176 }`, configv3.CurrentConfigVersion+1) 177 setConfig(homeDir, rawConfig) 178 }) 179 180 It("sets UAAOAuthClient to the default", func() { 181 Expect(loadErr).ToNot(HaveOccurred()) 182 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 183 }) 184 185 It("sets UAAOAuthClientSecret to the default", func() { 186 Expect(loadErr).ToNot(HaveOccurred()) 187 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 188 }) 189 }) 190 191 When("Version checking", func() { 192 When("the Config Version is < the current version", func() { 193 It("clears the config", func() { 194 rawConfig := fmt.Sprintf(` 195 { 196 "AccessToken": "bearer shazbat!", 197 "ConfigVersion": %d 198 }`, configv3.CurrentConfigVersion-1) 199 setConfig(homeDir, rawConfig) 200 config := helpers.GetConfig() 201 Expect(loadErr).ToNot(HaveOccurred()) 202 Expect(config.ConfigFile.ConfigVersion).To(Equal(configv3.CurrentConfigVersion)) 203 Expect(config.ConfigFile.AccessToken).To(Equal("")) 204 }) 205 }) 206 207 When("the Config Version is = the current version", func() { 208 It("clears the config", func() { 209 rawConfig := fmt.Sprintf(` 210 { 211 "AccessToken": "bearer shazbat!", 212 "ConfigVersion": %d 213 }`, configv3.CurrentConfigVersion) 214 setConfig(homeDir, rawConfig) 215 config := helpers.GetConfig() 216 Expect(loadErr).ToNot(HaveOccurred()) 217 Expect(config.ConfigFile.ConfigVersion).To(Equal(configv3.CurrentConfigVersion)) 218 Expect(config.ConfigFile.AccessToken).To(Equal("bearer shazbat!")) 219 }) 220 }) 221 222 When("the Config Version is > the current version", func() { 223 It("clears the config", func() { 224 rawConfig := fmt.Sprintf(` 225 { 226 "AccessToken": "bearer shazbat!", 227 "ConfigVersion": %d 228 }`, configv3.CurrentConfigVersion+1) 229 setConfig(homeDir, rawConfig) 230 config := helpers.GetConfig() 231 Expect(loadErr).ToNot(HaveOccurred()) 232 Expect(config.ConfigFile.ConfigVersion).To(Equal(configv3.CurrentConfigVersion)) 233 Expect(config.ConfigFile.AccessToken).To(Equal("")) 234 }) 235 }) 236 }) 237 }) 238 239 When("passed flag overrides", func() { 240 BeforeEach(func() { 241 inFlags = append(inFlags, FlagOverride{Verbose: true}, FlagOverride{Verbose: false}) 242 }) 243 244 It("stores the first set of flag overrides on the config", func() { 245 Expect(config.Flags).To(Equal(FlagOverride{Verbose: true})) 246 }) 247 }) 248 }) 249 })