github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/loader_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "runtime" 9 "testing" 10 11 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" 12 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 13 ) 14 15 var ( 16 backupSuffix = ".backup" 17 tmpl = []byte(` 18 clouds: 19 useless_cloud: 20 auth: 21 auth_url: "http://localhost/" 22 password: "some-useless-passw0rd" 23 username: "some-name" 24 `) 25 ) 26 27 // copyFile copies file if it exists 28 func copyFile(t *testing.T, src, dest string) { 29 fileStat, err := os.Stat(src) 30 if err != nil && os.IsNotExist(err) { 31 t.Logf("File %s doesn't exist, skipping", src) 32 return 33 } 34 35 data, err := ioutil.ReadFile(src) 36 th.AssertNoErr(t, err) 37 th.AssertNoErr(t, ioutil.WriteFile(dest, data, fileStat.Mode())) 38 } 39 40 // backupFile creates copy of the file and return path to the copy 41 func backupFiles(t *testing.T, originals ...string) { 42 for _, file := range originals { 43 backupFile := fmt.Sprintf("%s%s", file, backupSuffix) 44 copyFile(t, file, backupFile) 45 } 46 } 47 48 func removeIfExist(t *testing.T, path string) { 49 _, err := os.Stat(path) 50 if err != nil && os.IsNotExist(err) { 51 t.Logf("File %s doesn't exist, skipping", path) 52 return 53 } 54 th.AssertNoErr(t, err) 55 th.AssertNoErr(t, os.Remove(path)) 56 } 57 58 // restoreBackup replaces files with the backups 59 func restoreBackup(t *testing.T, files ...string) { 60 for _, original := range files { 61 backup := fmt.Sprintf("%s%s", original, backupSuffix) 62 removeIfExist(t, original) 63 copyFile(t, backup, original) 64 removeIfExist(t, backup) 65 } 66 } 67 68 func checkLazyness(t *testing.T, env *Env, expected bool) { 69 authUrl0 := "http://url:0" 70 _ = os.Setenv("OS_AUTH_URL", authUrl0) 71 cloud0, err := env.Cloud() 72 th.AssertNoErr(t, err) 73 th.AssertEquals(t, authUrl0, cloud0.AuthInfo.AuthURL) 74 75 authUrl1 := "http://url:1" 76 _ = os.Setenv("OS_AUTH_URL", authUrl1) 77 cloud1, err := env.Cloud() 78 th.AssertNoErr(t, err) 79 80 th.AssertEquals(t, expected, authUrl0 == cloud1.AuthInfo.AuthURL) 81 th.AssertEquals(t, !expected, authUrl1 == cloud1.AuthInfo.AuthURL) 82 } 83 84 func TestLazyEnv(t *testing.T) { 85 t.Run("lazy", func(sub *testing.T) { 86 env := NewEnv("OS_", true) 87 checkLazyness(sub, env, true) 88 }) 89 t.Run("not lazy", func(sub *testing.T) { 90 env := NewEnv("OS_", false) 91 checkLazyness(sub, env, false) 92 }) 93 t.Run("default", func(sub *testing.T) { 94 env := NewEnv("OS_") 95 checkLazyness(sub, env, true) 96 sub.Log("Lazy by default") 97 }) 98 } 99 100 func TestCloudYamlPaths(t *testing.T) { 101 _ = os.Setenv("OS_CLOUD", "useless_cloud") 102 home, _ := os.UserHomeDir() 103 cwd, _ := os.Getwd() 104 105 fileName := "clouds.yaml" 106 currentConfigDir := filepath.Join(cwd, fileName) 107 userConfigDir := filepath.Join(home, ".config/openstack", fileName) 108 unixConfigDir := filepath.Join("/etc/openstack", fileName) 109 files := []string{currentConfigDir, userConfigDir, unixConfigDir} 110 backupFiles(t, currentConfigDir, userConfigDir, unixConfigDir) 111 defer restoreBackup(t, files...) 112 113 for _, fileName := range files { 114 t.Run(fmt.Sprintf("Config at %s", fileName), func(subT *testing.T) { 115 if runtime.GOOS == "windows" && fileName == unixConfigDir { 116 subT.Skip("Skipping on windows") 117 } 118 119 dir := filepath.Dir(fileName) 120 if err := os.MkdirAll(dir, 0755); err != nil { // make sure that dir exists 121 if os.IsPermission(err) { 122 subT.Skip(err.Error()) 123 } 124 th.AssertNoErr(t, err) 125 } 126 127 th.AssertNoErr(subT, ioutil.WriteFile(fileName, tmpl, 0644)) 128 cloud, err := NewEnv("OS_").Cloud() 129 th.AssertNoErr(subT, err) 130 th.AssertEquals(subT, "http://localhost/", cloud.AuthInfo.AuthURL) 131 th.AssertEquals(subT, "some-useless-passw0rd", cloud.AuthInfo.Password) 132 th.AssertEquals(subT, "some-name", cloud.AuthInfo.Username) 133 }) 134 } 135 } 136 137 func TestEmptyClouds(t *testing.T) { 138 _ = os.Setenv("OS_CLOUD", "useless_cloud") 139 _ = os.Setenv("OS_CLIENT_CONFIG_FILE", "./clouds.yaml") 140 cwd, _ := os.Getwd() 141 142 fileName := "clouds.yaml" 143 currentConfigDir := filepath.Join(cwd, fileName) 144 files := []string{currentConfigDir} 145 backupFiles(t, currentConfigDir) 146 defer restoreBackup(t, files...) 147 148 th.AssertNoErr(t, ioutil.WriteFile(fileName, []byte{}, 0644)) 149 _, err := NewEnv("OS_").Cloud() 150 th.AssertNoErr(t, err) 151 } 152 153 func TestCloudName(t *testing.T) { 154 _ = os.Setenv("OS_CLOUD", tools.RandomString("CLD_", 5)) 155 expectedName := tools.RandomString("CLD_SET_", 5) 156 cloud, err := NewEnv("OS").Cloud(expectedName) 157 th.AssertNoErr(t, err) 158 th.AssertEquals(t, expectedName, cloud.Cloud) 159 } 160 161 func TestDefaultVendor(t *testing.T) { 162 cloudName := tools.RandomString("cloud-", 3) 163 _ = os.Setenv("OS_CLOUD", cloudName) 164 165 configPath := "/tmp/gophertest/config" 166 _ = os.MkdirAll(configPath, os.ModePerm) 167 defer os.RemoveAll(configPath) 168 169 clientConfigPath := filepath.Join(configPath, "/clouds.yaml") 170 _ = os.Setenv("OS_CLIENT_CONFIG_FILE", clientConfigPath) 171 172 configTemplate := fmt.Sprintf(` 173 clouds: 174 %s: 175 profile: otc 176 auth: 177 project_name: eu-nl_test 178 `, cloudName) 179 180 th.AssertNoErr(t, ioutil.WriteFile(clientConfigPath, []byte(configTemplate), 0644)) 181 182 cld, err := NewEnv("OS_").Cloud() 183 th.AssertNoErr(t, err) 184 185 th.AssertEquals(t, "https://iam.eu-nl.otc.t-systems.com/v3", cld.AuthInfo.AuthURL) 186 th.AssertEquals(t, "3", cld.IdentityAPIVersion) 187 }