github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/config.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 "code.cloudfoundry.org/cli/util/configv3" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 // TurnOffColors sets CF_COLOR to 'false'. 16 func TurnOffColors() { 17 Expect(os.Setenv("CF_COLOR", "false")).To(Succeed()) 18 } 19 20 // TurnOnExperimental sets CF_CLI_EXPERIMENTAL to 'true'. 21 func TurnOnExperimental() { 22 Expect(os.Setenv("CF_CLI_EXPERIMENTAL", "true")).To(Succeed()) 23 } 24 25 // TurnOffExperimental unsets CF_CLI_EXPERIMENTAL. 26 func TurnOffExperimental() { 27 Expect(os.Unsetenv("CF_CLI_EXPERIMENTAL")).To(Succeed()) 28 } 29 30 // SetHomeDir sets CF_HOME and CF_PLUGIN_HOME to a temp directory and outputs 31 // the created directory through GinkgoWriter. 32 func SetHomeDir() string { 33 var err error 34 homeDir, err := ioutil.TempDir("", "cli-integration-test") 35 Expect(err).NotTo(HaveOccurred()) 36 37 setHomeDirsTo(homeDir, homeDir) 38 return homeDir 39 } 40 41 // WithRandomHomeDir sets CF_HOME and CF_PLUGIN_HOME to a temp directory and outputs 42 // the created directory through GinkgoWriter. Then it executes the provided function 43 // 'action'. Finally, it’s restoring the previous CF_HOME and CF_PLUGIN_HOME. 44 func WithRandomHomeDir(action func()) { 45 oldHomeDir, oldPluginHomeDir := getHomeDirs() 46 homeDir := SetHomeDir() 47 action() 48 setHomeDirsTo(oldHomeDir, oldPluginHomeDir) 49 DestroyHomeDir(homeDir) 50 } 51 52 func getHomeDirs() (string, string) { 53 homeDir := os.Getenv("CF_HOME") 54 pluginHomeDir := os.Getenv("CF_PLUGIN_HOME") 55 return homeDir, pluginHomeDir 56 } 57 58 func setHomeDirsTo(homeDir string, pluginHomeDir string) { 59 GinkgoWriter.Write([]byte(fmt.Sprintln("\nHOME DIR>", homeDir))) 60 61 Expect(os.Setenv("CF_HOME", homeDir)).To(Succeed()) 62 Expect(os.Setenv("CF_PLUGIN_HOME", pluginHomeDir)).To(Succeed()) 63 } 64 65 // SetupSynchronizedSuite runs a setup function in its own CF context, creating 66 // and destroying a home directory around it. 67 func SetupSynchronizedSuite(setup func()) { 68 homeDir := SetHomeDir() 69 SetAPI() 70 LoginCF() 71 setup() 72 DestroyHomeDir(homeDir) 73 } 74 75 // DestroyHomeDir safely removes the given directory checking for errors. 76 func DestroyHomeDir(homeDir string) { 77 if homeDir != "" { 78 Eventually(func() error { return os.RemoveAll(homeDir) }).Should(Succeed()) 79 } 80 } 81 82 // GetConfig loads a CF config JSON file and returns the parsed struct. 83 func GetConfig() *configv3.Config { 84 c, err := configv3.LoadConfig() 85 Expect(err).ToNot(HaveOccurred()) 86 return c 87 } 88 89 // SetConfig allows for a given function to modify a CF config JSON and writes 90 // the result back down to the filesystem. 91 func SetConfig(cb func(conf *configv3.Config)) { 92 config, err := configv3.LoadConfig() 93 Expect(err).ToNot(HaveOccurred()) 94 95 cb(config) 96 97 err = config.WriteConfig() 98 Expect(err).ToNot(HaveOccurred()) 99 } 100 101 // SetConfigContent writes given raw config into given directory as "config.json". 102 func SetConfigContent(dir string, rawConfig string) { 103 err := os.MkdirAll(filepath.Join(dir), 0777) 104 Expect(err).ToNot(HaveOccurred()) 105 err = ioutil.WriteFile(filepath.Join(dir, "config.json"), []byte(rawConfig), 0644) 106 Expect(err).ToNot(HaveOccurred()) 107 } 108 109 // ExpiredAccessToken returns an example expired bearer token. 110 func ExpiredAccessToken() string { 111 return "bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiJiMjZmMTFjMWNhYmI0ZmY0ODhlN2RhYTJkZTQxMTA4NiIsInN1YiI6IjBjZWMwY2E4LTA5MmYtNDkzYy1hYmExLWM4ZTZ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