-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
plan_test.go
69 lines (60 loc) · 2.34 KB
/
plan_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
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func getDir(t *testing.T) string {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %v", err)
}
dir := filepath.Join(cwd, "../../test", "unit-test")
return dir
}
func TestLoadPlan(t *testing.T) {
// set env var for test
testConnectionString := "test-connetion-string"
testPlanName := "mongo-test"
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.Replace(testPlanName, "-", "_", -1)), "AZURE", "CONNECTIONSTRING"), testConnectionString)
dir := getDir(t)
plan, err := LoadPlan(dir, testPlanName)
if err != nil {
t.Errorf("LoadPlan(%q, %q) returned error: %v", dir, testPlanName, err)
}
if plan.Name != testPlanName {
t.Errorf("LoadPlan(%q, %q) returned plan with wrong name: got %q, want %q", dir, testPlanName, plan.Name, testPlanName)
}
if plan.Azure.ConnectionString != testConnectionString {
t.Errorf("LoadPlan(%q, %q) returned plan with wrong azure connection string: got %q, want %q, viper failed to load env", dir, testPlanName, plan.Azure.ConnectionString, "test-connetion-string")
}
}
func TestLoadPlans(t *testing.T) {
// set env var for test
testConnectionString := "test-connetion-string"
testPlanName := "mongo-test"
targetPassword := "test-password"
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.ReplaceAll(testPlanName, "-", "_")), "AZURE", "CONNECTIONSTRING"), testConnectionString)
os.Setenv(fmt.Sprintf("%s__%s_%s", strings.ToUpper(strings.ReplaceAll(testPlanName, "-", "_")), "TARGET", "PASSWORD"), targetPassword)
dir := getDir(t)
plans, err := LoadPlans(dir)
if err != nil {
t.Errorf("LoadPlans(%q) returned error: %v", dir, err)
}
if len(plans) == 0 {
t.Errorf("LoadPlans(%q) returned empty list of plans", dir)
}
// verify plan.name = mongo-test has a azure connection string set as test-connetion-string
for _, plan := range plans {
if plan.Name == testPlanName {
if plan.Target.Password != targetPassword {
t.Errorf("LoadPlans(%q) returned plan with wrong target password: got %q, want %q", dir, plan.Target.Password, targetPassword)
}
if plan.Azure.ConnectionString != testConnectionString {
t.Errorf("LoadPlans(%q) returned plan with wrong azure connection string: got %q, want %q", dir, plan.Azure.ConnectionString, "test-connetion-string")
}
}
}
}