-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test_utils.go
169 lines (134 loc) · 2.95 KB
/
e2e_test_utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"strings"
"text/template"
"time"
"github.com/Ferlab-Ste-Justine/terracd/fs"
)
type TestConfTemplateSrc struct {
Dir string
}
type TestConfTemplate struct {
TerraformPath string
Command string
MinInterval string
Jitter string
Sources []TestConfTemplateSrc
}
func (tpl *TestConfTemplate) SetTfPath() error {
tfPath, tfPathErr := exec.LookPath("terraform")
if tfPathErr != nil {
return tfPathErr
}
tpl.TerraformPath = tfPath
return nil
}
func (tpl *TestConfTemplate) GenerateConfig() error {
fContent, err := ioutil.ReadFile(path.Join("e2e_test", "config.yml.tpl"))
if err != nil {
return err
}
tmpl, tErr := template.New("template").Parse(string(fContent))
if tErr != nil {
return tErr
}
var b bytes.Buffer
exErr := tmpl.Execute(&b, tpl)
if exErr != nil {
return exErr
}
return os.WriteFile("config.yml", b.Bytes(), 0640)
}
func (tpl *TestConfTemplate) CleanupConfig() error {
return os.Remove("config.yml")
}
type TestHooks struct {
Success time.Duration
Skip time.Duration
Failure time.Duration
}
func getTestHook(filePath string) (time.Duration, error) {
pathExists, pathExistsErr := fs.PathExists(filePath)
if pathExistsErr != nil {
return time.Duration(0), pathExistsErr
}
if !pathExists {
return time.Duration(0), nil
}
fContent, rErr := ioutil.ReadFile(filePath)
if rErr != nil {
return time.Duration(0), rErr
}
i, iErr := strconv.ParseInt(strings.TrimSpace(string(fContent)), 10, 64)
if iErr != nil {
return time.Duration(0), iErr
}
return time.Duration(i*1000000), nil
}
func GetTestHooks() (TestHooks, error) {
f, fErr := getTestHook("failure")
if fErr != nil {
return TestHooks{}, fErr
}
sk, skErr := getTestHook("skip")
if skErr != nil {
return TestHooks{}, skErr
}
su, suErr := getTestHook("success")
if suErr != nil {
return TestHooks{}, suErr
}
return TestHooks{su, sk, f}, nil
}
func CleanupTestHooks() error {
err := fs.EnsureFileNotExists("failure")
if err != nil {
return err
}
err = fs.EnsureFileNotExists("skip")
if err != nil {
return err
}
return fs.EnsureFileNotExists("success")
}
func CleanupTestRuntime() error {
fExists, fExistsErr := fs.PathExists(path.Join("e2e_test", "runtime"))
if fExistsErr != nil {
return fExistsErr
}
if fExists {
return os.RemoveAll(path.Join("e2e_test", "runtime"))
}
return nil
}
func CleanupTestExecution(tpl TestConfTemplate) error {
err := tpl.CleanupConfig()
if err != nil {
return err
}
err = CleanupTestHooks()
if err != nil {
return err
}
return CleanupTestRuntime()
}
func FileHasValue(filePath string, value string) (bool, error) {
pathExists, pathExistsErr := fs.PathExists(filePath)
if pathExistsErr != nil {
return false, pathExistsErr
}
if !pathExists {
return false, nil
}
fContent, rErr := ioutil.ReadFile(filePath)
if rErr != nil {
return false, rErr
}
return string(fContent) == value, nil
}