forked from scaleway/scaleway-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_legacy_test.go
104 lines (91 loc) · 2.38 KB
/
config_legacy_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package scw
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
)
// TestMigrateLegacyConfig tests legacy config properly migrate to V2 config
func TestMigrateLegacyConfig(t *testing.T) {
tests := []struct {
name string
env map[string]string
files map[string]string
isMigrated bool
expectedFiles map[string]string
}{
{
name: "No config path",
isMigrated: false,
files: map[string]string{
".scwrc": v1ValidConfigFile,
},
},
{
name: "Default config path",
isMigrated: true,
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".scwrc": v1ValidConfigFile,
},
expectedFiles: map[string]string{
".config/scw/config.yaml": v2FromV1ConfigFile,
},
},
{
name: "V2 config already exist",
isMigrated: false,
env: map[string]string{
"HOME": "{HOME}",
},
files: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
".scwrc": v1ValidConfigFile,
},
expectedFiles: map[string]string{
".config/scw/config.yaml": v2SimpleValidConfigFile,
},
},
}
// create home dir
dir := initEnv(t)
// delete home dir and reset env variables
defer resetEnv(t, os.Environ(), dir)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// set up env and config file(s)
setEnv(t, test.env, test.files, dir)
// remove config file(s)
defer cleanEnv(t, test.files, dir)
defer cleanEnv(t, test.expectedFiles, dir)
isMigrated, err := MigrateLegacyConfig()
testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, test.isMigrated, isMigrated)
// test expected files
for fileName, expectedContent := range test.expectedFiles {
content, err := ioutil.ReadFile(filepath.Join(dir, fileName))
testhelpers.AssertNoError(t, err)
testhelpers.Equals(t, expectedContent, string(content))
}
})
}
}
// v1 config
var (
v1ValidOrganizationID = "29aa5db6-1d6d-404e-890d-f896913f9ec1"
v1ValidToken = "a057b0c1-eb47-4bf8-a589-72c1f2029515"
v1Version = "1.19"
v1ValidConfigFile = `{
"organization":"` + v1ValidOrganizationID + `",
"token":"` + v1ValidToken + `",
"version":"` + v1Version + `"
}`
v1InvalidConfigFile = `
"organization":"` + v1ValidOrganizationID + `",
"token":"` + v1ValidToken + `",
"version":"` + v1Version + `"
`
)