-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2envdir_test.go
125 lines (109 loc) · 2.53 KB
/
json2envdir_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package json2envdir
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/heroku/json2envdir/config"
"github.com/satori/go.uuid"
)
func TestParseNoConfig(t *testing.T) {
testDir, err := ioutil.TempDir("", "json2envdirTest")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(testDir) }()
cfg := config.Config{
Sections: map[string]*config.EnvDirSection{
"cheetah": &config.EnvDirSection{
Path: []string{testDir},
},
},
}
err = Process(cfg, `{
"name": "not-cheetah",
"env": {
"VAR": "{{.UUID}}"
}
}`)
if err != nil {
t.Fatalf("Unexpected failure: %q", err)
}
}
func TestParse(t *testing.T) {
dir1, err := ioutil.TempDir("", "json2envdir1")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir1)
dir2, err := ioutil.TempDir("", "json2envdir2")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir2)
cfg := config.Config{
Sections: map[string]*config.EnvDirSection{
"myapp": &config.EnvDirSection{
Path: []string{dir1, dir2},
},
},
}
err = Process(cfg, `{
"name": "myapp",
"env": {
"MYVAR": "123",
"MYVAR_TMPL": "{{.UUID}}",
"MYVAR_HEX": "{{.HEX 32}}"
}
}`)
if err != nil {
t.Fatalf("Unexpected error in Parse: %s", err)
}
b, err := ioutil.ReadFile(filepath.Join(dir1, "MYVAR"))
if err != nil {
t.Fatal(err)
}
if string(b) != "123" {
t.Fatalf("Value of $MYVAR in dir1 should equal to 123, but it's %s", b)
}
b, err = ioutil.ReadFile(filepath.Join(dir1, "MYVAR_TMPL"))
if err != nil {
t.Fatal(err)
}
uuid1, err := uuid.FromString(string(b))
if err != nil {
t.Fatalf("Value of $MYVAR_TMPL in dir1 should contain a UUID")
}
hex1, err := ioutil.ReadFile(filepath.Join(dir1, "MYVAR_HEX"))
if err != nil {
t.Fatal(err)
}
if len(hex1) != 64 {
t.Fatalf("Length of $MYVAR_HEX in dir1 should be 64, got: %d", len(hex1))
}
b, err = ioutil.ReadFile(filepath.Join(dir2, "MYVAR"))
if err != nil {
t.Fatal(err)
}
if string(b) != "123" {
t.Fatalf("Value of $MYVAR in dir2 should equal to 123, but it's %s", b)
}
b, err = ioutil.ReadFile(filepath.Join(dir2, "MYVAR_TMPL"))
if err != nil {
t.Fatal(err)
}
uuid2, err := uuid.FromString(string(b))
if err != nil {
t.Fatalf("Value of $MYVAR_TMPL in dir2 should contain a UUID")
}
hex2, err := ioutil.ReadFile(filepath.Join(dir2, "MYVAR_HEX"))
if err != nil {
t.Fatal(err)
}
if len(hex2) != 64 {
t.Fatalf("Length of $MYVAR_HEX in dir1 should be 64, got: %d", len(hex1))
}
if uuid1 != uuid2 {
t.Fatalf("uuid1 should equal to uuid2: uuid1=%s uuid2=%s", uuid1, uuid2)
}
}