-
Notifications
You must be signed in to change notification settings - Fork 2
/
store_test.go
123 lines (107 loc) · 2.36 KB
/
store_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
package goss
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)
const GossTestingEnvOS = "GOSS_TESTING_OS"
func Test_RenderJSON(t *testing.T) {
err := os.Setenv(GossTestingEnvOS, "centos")
if err != nil {
panic(err.Error())
}
defer os.Unsetenv(GossTestingEnvOS)
tmpVars, err := ioutil.TempFile("", "example_tmp_vars_*.yaml")
if err != nil {
panic(err.Error())
}
defer os.Remove(tmpVars.Name())
_, err = tmpVars.WriteString(getExampleVars())
if err != nil {
panic(err.Error())
}
tmpGossfile, err := ioutil.TempFile("", "example_tmp_gossfile_*.yaml")
if err != nil {
panic(err.Error())
}
defer os.Remove(tmpGossfile.Name())
_, err = tmpGossfile.WriteString(getExampleTemplate())
if err != nil {
panic(err.Error())
}
result := RenderJSON(tmpGossfile, tmpVars)
assert.Equal(t, getExpecetd(), result)
}
func getExampleVars() string {
return `
centos:
packages:
kernel:
- "4.9.11-centos"
- "4.9.11-centos2"
debian:
packages:
kernel:
- "4.9.11-debian"
- "4.9.11-debian2"
users:
- user1
- user2
`
}
func getExampleTemplate() string {
return `
package:
# Looping over a variables defined in a vars.yaml using $OS environment variable as a lookup key
{{range $name, $vers := index .Vars .Env.GOSS_TESTING_OS "packages"}}
{{$name}}:
installed: true
versions:
{{range $vers}}
- {{.}}
{{end}}
{{end}}
# This test is only when the OS environment variable matches the pattern
{{if .Env.GOSS_TESTING_OS | regexMatch "[Cc]ent(OS|os)"}}
libselinux:
installed: true
{{end}}
# Loop over users
user:
{{range .Vars.users}}
{{.}}:
exists: true
groups:
- {{.}}
home: /home/{{.}}
shell: /bin/bash
{{end}}
package:
{{if eq .Env.GOSS_TESTING_OS "centos"}}
# This test is only when $OS environment variable is set to "centos"
libselinux:
installed: true
{{end}}
`
}
func getExpecetd() string {
expected := `package:
libselinux:
installed: true
user:
user1:
exists: true
groups:
- user1
home: /home/user1
shell: /bin/bash
user2:
exists: true
groups:
- user2
home: /home/user2
shell: /bin/bash
`
return expected
}