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