-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration_test.go
181 lines (152 loc) · 4.33 KB
/
integration_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
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
170
171
172
173
174
175
176
177
178
179
180
181
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/FACT-Finder/perfably/api"
"github.com/FACT-Finder/perfably/auth"
"github.com/FACT-Finder/perfably/config"
"github.com/FACT-Finder/perfably/state"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestIntegration(t *testing.T) {
workDir := filepath.Join(getCurrentPath(), "work")
scenarioDir := filepath.Join(getCurrentPath(), "scenario")
update := os.Getenv("UPDATE") == "true"
scenarioFiles, err := ioutil.ReadDir(scenarioDir)
require.NoError(t, err)
for _, file := range scenarioFiles {
if file.IsDir() {
continue
}
t.Run(file.Name(), func(t *testing.T) {
clearDir(workDir)
scenarioFile := filepath.Join(scenarioDir, file.Name())
body, err := ioutil.ReadFile(scenarioFile)
require.NoError(t, err)
var test Test
err = yaml.Unmarshal(body, &test)
require.NoError(t, err)
users, err := auth.Parse(workDir)
require.NoError(t, err)
for name, pass := range test.Users {
_, err := users.CreateWithPW(name, pass, 4)
require.NoError(t, err)
}
s, err := state.ReadState(&test.Config, workDir)
defer func() {
s.Close()
}()
require.NoError(t, err)
handler := api.New(&test.Config, s, users)
for i, step := range test.Steps {
t.Run(fmt.Sprintf("%d_%s", i, step.Name), func(t *testing.T) {
if step.Restart {
users, err = auth.Parse(workDir)
require.NoError(t, err)
s.Close()
s, err = state.ReadState(&test.Config, workDir)
require.NoError(t, err)
handler = api.New(&test.Config, s, users)
return
}
if step.HTTP != "" {
parts := strings.Split(step.HTTP, " ")
var body io.Reader
if step.RequestBody != "" {
b, err := json.Marshal(step.RequestBody)
require.NoError(t, err)
body = bytes.NewReader(b)
}
req, err := http.NewRequest(parts[0], parts[1], body)
require.NoError(t, err)
if step.Auth != "" {
userPW := strings.Split(step.Auth, ":")
req.SetBasicAuth(userPW[0], userPW[1])
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if update {
step.Status = recorder.Code
step.ResponseBody = ""
if recorder.Body.Len() != 0 {
actual := prettyPrint(t, recorder.Body.Bytes())
step.ResponseBody = string(actual)
}
}
require.Equal(t, step.Status, recorder.Code)
if step.ResponseBody != "" {
actual := prettyPrint(t, recorder.Body.Bytes())
require.Equal(t, step.ResponseBody, string(actual))
}
}
if step.FileContent != nil && len(step.FileContent) > 0 {
for file, content := range step.FileContent {
actual, err := ioutil.ReadFile(filepath.Join(workDir, file))
require.NoError(t, err)
if update {
step.FileContent[file] = string(actual)
}
require.Equal(t, content, string(actual))
}
}
})
}
if update {
newBytes, err := yaml.Marshal(test)
require.NoError(t, err)
err = ioutil.WriteFile(scenarioFile, newBytes, 0o644)
require.NoError(t, err)
}
})
}
clearDir(workDir)
}
type Test struct {
Config config.Config `yaml:"config"`
Users map[string]string `yaml:"users"`
Steps []*TestStep `yaml:"steps"`
}
type TestStep struct {
Name string `yaml:"name"`
Restart bool `yaml:"restart,omitempty"`
HTTP string `yaml:"http,omitempty"`
Auth string `yaml:"auth,omitempty"`
RequestBody interface{} `yaml:"request_body,omitempty"`
Status int `yaml:"status,omitempty"`
ResponseBody string `yaml:"response_body,omitempty"`
FileContent map[string]string `yaml:"file_content"`
}
func getCurrentPath() string {
_, filename, _, _ := runtime.Caller(1)
return filepath.Dir(filename)
}
func clearDir(dir string) error {
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
for _, file := range files {
err = os.RemoveAll(file)
if err != nil {
return err
}
}
return nil
}
func prettyPrint(t *testing.T, b []byte) []byte {
var out bytes.Buffer
err := json.Indent(&out, b, "", " ")
require.NoError(t, err)
return out.Bytes()
}