github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cloud/amazon/testutils/session_test_utils.go (about) 1 package testutils 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "runtime" 8 9 "github.com/olli-ai/jx/v2/pkg/cloud/amazon/session" 10 ) 11 12 // SwitchAWSHome creates a dummy .aws dir for testing 13 func SwitchAWSHome() (string, error) { 14 oldHome := session.UserHomeDir() 15 newHome, err := ioutil.TempDir("", "common_test") 16 SetUserHomeDir(newHome) 17 awsHome := path.Join(newHome, ".aws") 18 err = os.MkdirAll(awsHome, 0777) 19 if err != nil { 20 return oldHome, err 21 } 22 23 awsConfigPath := path.Join(awsHome, "config") 24 if err := ioutil.WriteFile(awsConfigPath, []byte(`[profile foo] 25 region = bar 26 [profile baz] 27 region = qux`), 0600); err != nil { 28 panic(err) 29 } 30 31 return oldHome, nil 32 } 33 34 func SetUserHomeDir(newHome string) { 35 if runtime.GOOS == "windows" { 36 os.Setenv("USERPROFILE", newHome) //nolint:errcheck 37 } 38 // *nix 39 os.Setenv("HOME", newHome) //nolint:errcheck 40 } 41 42 func RestoreHome(oldHome string) { 43 os.Setenv("HOME", oldHome) //nolint:errcheck 44 } 45 46 func ConfigureEnv(region string, defaultRegion string, profile string) { 47 os.Setenv("AWS_REGION", region) //nolint:errcheck 48 os.Setenv("AWS_DEFAULT_REGION", defaultRegion) //nolint:errcheck 49 os.Setenv("AWS_PROFILE", profile) //nolint:errcheck 50 }