github.com/arunkumar7540/cli@v6.45.0+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 ( 17 oldLang string 18 oldLCAll string 19 oldCfExperimental string 20 oldExperimentalLogin string 21 homeDir string 22 ) 23 24 BeforeEach(func() { 25 homeDir = setup() 26 27 oldLang = os.Getenv("LANG") 28 oldLCAll = os.Getenv("LC_ALL") 29 // specifically for when we run unit tests locally 30 // we save and unset this variable in case it's present 31 // since we want to load a default config 32 oldCfExperimental = os.Getenv("CF_CLI_EXPERIMENTAL") 33 oldExperimentalLogin = os.Getenv("CF_EXPERIMENTAL_LOGIN") 34 Expect(os.Unsetenv("LANG")).ToNot(HaveOccurred()) 35 Expect(os.Unsetenv("LC_ALL")).ToNot(HaveOccurred()) 36 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).To(Succeed()) 37 Expect(os.Unsetenv("CF_EXPERIMENTAL_LOGIN")).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 os.Setenv("CF_EXPERIMENTAL_LOGIN", oldExperimentalLogin) 45 teardown(homeDir) 46 }) 47 48 Describe("LoadConfig", func() { 49 var ( 50 config *Config 51 loadErr error 52 inFlags []FlagOverride 53 ) 54 55 BeforeEach(func() { 56 inFlags = []FlagOverride{} 57 }) 58 59 JustBeforeEach(func() { 60 config, loadErr = LoadConfig(inFlags...) 61 }) 62 63 When("there are old temp-config* files lingering from previous failed attempts to write the config", func() { 64 BeforeEach(func() { 65 configDir := filepath.Join(homeDir, ".cf") 66 Expect(os.MkdirAll(configDir, 0777)).To(Succeed()) 67 for i := 0; i < 3; i++ { 68 tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config") 69 Expect(fileErr).ToNot(HaveOccurred()) 70 tmpFile.Close() 71 } 72 }) 73 74 It("removes the lingering temp-config* files", func() { 75 Expect(loadErr).ToNot(HaveOccurred()) 76 77 oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*")) 78 Expect(configErr).ToNot(HaveOccurred()) 79 Expect(oldTempFileNames).To(BeEmpty()) 80 }) 81 }) 82 83 When("there isn't a config set", func() { 84 It("returns a default config", func() { 85 Expect(loadErr).ToNot(HaveOccurred()) 86 87 Expect(config).ToNot(BeNil()) 88 Expect(config.ConfigFile).To(Equal( 89 JSONConfig{ 90 ColorEnabled: DefaultColorEnabled, 91 ConfigVersion: 3, 92 SSHOAuthClient: DefaultSSHOAuthClient, 93 UAAOAuthClient: DefaultUAAOAuthClient, 94 UAAOAuthClientSecret: DefaultUAAOAuthClientSecret, 95 PluginRepositories: []PluginRepository{{ 96 Name: DefaultPluginRepoName, 97 URL: DefaultPluginRepoURL, 98 }}, 99 Target: DefaultTarget, 100 }, 101 )) 102 Expect(config.ENV).To(Equal( 103 EnvOverride{ 104 BinaryName: "configv3.test", // Ginkgo will uses a config file as the first test argument, so that will be considered the binary name 105 }, 106 )) 107 Expect(config.Flags).To(Equal(FlagOverride{})) 108 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 109 110 pluginConfig := config.Plugins() 111 Expect(pluginConfig).To(BeEmpty()) 112 113 // test the plugins map is initialized 114 config.AddPlugin(Plugin{}) 115 }) 116 }) 117 118 When("there is a config set", func() { 119 Context("but it is empty", func() { 120 BeforeEach(func() { 121 setConfig(homeDir, "") 122 }) 123 124 It("returns the default config and an EmptyConfigError", func() { 125 Expect(loadErr).To(Equal(translatableerror.EmptyConfigError{FilePath: filepath.Join(homeDir, ".cf", "config.json")})) 126 Expect(config).ToNot(BeNil()) 127 Expect(config).ToNot(BeNil()) 128 Expect(config.ConfigFile).To(Equal( 129 JSONConfig{ 130 ColorEnabled: DefaultColorEnabled, 131 ConfigVersion: 3, 132 SSHOAuthClient: DefaultSSHOAuthClient, 133 UAAOAuthClient: DefaultUAAOAuthClient, 134 UAAOAuthClientSecret: DefaultUAAOAuthClientSecret, 135 PluginRepositories: []PluginRepository{{ 136 Name: DefaultPluginRepoName, 137 URL: DefaultPluginRepoURL, 138 }}, 139 Target: DefaultTarget, 140 }, 141 )) 142 Expect(config.ENV).To(Equal( 143 EnvOverride{ 144 BinaryName: "configv3.test", // Ginkgo will uses a config file as the first test argument, so that will be considered the binary name 145 }, 146 )) 147 Expect(config.Flags).To(Equal(FlagOverride{})) 148 Expect(config.PluginHome()).To(Equal(filepath.Join(homeDir, ".cf", "plugins"))) 149 150 pluginConfig := config.Plugins() 151 Expect(pluginConfig).To(BeEmpty()) 152 }) 153 }) 154 155 When("UAAOAuthClient is not present", func() { 156 BeforeEach(func() { 157 setConfig(homeDir, `{}`) 158 }) 159 160 It("sets UAAOAuthClient to the default", func() { 161 Expect(loadErr).ToNot(HaveOccurred()) 162 Expect(config.UAAOAuthClient()).To(Equal(DefaultUAAOAuthClient)) 163 }) 164 165 It("sets UAAOAuthClientSecret to the default", func() { 166 Expect(loadErr).ToNot(HaveOccurred()) 167 Expect(config.UAAOAuthClientSecret()).To(Equal(DefaultUAAOAuthClientSecret)) 168 }) 169 }) 170 171 When("UAAOAuthClient is empty", func() { 172 BeforeEach(func() { 173 rawConfig := ` 174 { 175 "UAAOAuthClient": "" 176 }` 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 192 When("passed flag overrides", func() { 193 BeforeEach(func() { 194 inFlags = append(inFlags, FlagOverride{Verbose: true}, FlagOverride{Verbose: false}) 195 }) 196 197 It("stores the first set of flag overrides on the config", func() { 198 Expect(config.Flags).To(Equal(FlagOverride{Verbose: true})) 199 }) 200 }) 201 202 When("CF_EXPERIMENTAL_LOGIN is set to true", func() { 203 BeforeEach(func() { 204 Expect(os.Setenv("CF_EXPERIMENTAL_LOGIN", "true")).To(Succeed()) 205 }) 206 207 AfterEach(func() { 208 Expect(os.Unsetenv("CF_EXPERIMENTAL_LOGIN")).To(Succeed()) 209 }) 210 211 It("stores that value", func() { 212 Expect(loadErr).ToNot(HaveOccurred()) 213 Expect(config.ExperimentalLogin()).To(BeTrue()) 214 }) 215 }) 216 }) 217 })