-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_helpers.go
154 lines (130 loc) · 4.04 KB
/
test_helpers.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
package base
import (
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"testing"
"time"
log "github.com/iter8-tools/iter8/base/log"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
const (
// ExperimentFile is the name of the experiment file
ExperimentFile = "experiment.yaml"
// ExperimentTemplateFile is the name of the template that will produce the experiment file
ExperimentTemplateFile = "experiment.tpl"
)
// mockDriver is a mock driver that can be used to run experiments
type mockDriver struct {
*Experiment
}
// Read an experiment
func (m *mockDriver) Read() (*Experiment, error) {
return m.Experiment, nil
}
// Write an experiment
func (m *mockDriver) Write(exp *Experiment) error {
m.Experiment = exp
// get URL of metrics server from environment variable
metricsServerURL, ok := os.LookupEnv(MetricsServerURL)
if !ok {
errorMessage := "could not look up METRICS_SERVER_URL environment variable"
log.Logger.Error(errorMessage)
return fmt.Errorf(errorMessage)
}
err := PutExperimentResultToMetricsService(metricsServerURL, exp.Metadata.Namespace, exp.Metadata.Name, exp.Result)
if err != nil {
return err
}
return nil
}
// GetRevision gets experiment revision
func (m *mockDriver) GetRevision() int {
return 0
}
// CreateExperimentYaml creates an experiment.yaml file from a template and a URL
func CreateExperimentYaml(t *testing.T, template string, url string, output string) {
values := struct {
URL string
}{
URL: url,
}
byteArray, err := os.ReadFile(filepath.Clean(template))
assert.NoError(t, err)
tpl, err := CreateTemplate(string(byteArray))
assert.NoError(t, err)
var buf bytes.Buffer
err = tpl.Execute(&buf, values)
assert.NoError(t, err)
err = os.WriteFile(output, buf.Bytes(), 0600)
assert.NoError(t, err)
}
// GetTrackingHandler creates a handler for fhttp.DynamicHTTPServer that sets a variable to true
// This can be used to verify that the handler was called.
func GetTrackingHandler(breadcrumb *bool) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
*breadcrumb = true
time.Sleep(100 * time.Millisecond)
w.WriteHeader(200)
}
}
// StartHTTPMock activates and cleanups httpmock
func StartHTTPMock(t *testing.T) {
httpmock.Activate()
t.Cleanup(httpmock.DeactivateAndReset)
httpmock.RegisterNoResponder(httpmock.InitialTransport.RoundTrip)
}
// MetricsServerCallback is a callback function for when the particular metrics server endpoint
// is called
type MetricsServerCallback func(req *http.Request)
// MockMetricsServerInput is the input for MockMetricsServer()
// allows the user to provide callbacks when particular endpoints are called
type MockMetricsServerInput struct {
MetricsServerURL string
// PUT /testResult
ExperimentResultCallback MetricsServerCallback
// GET /grpcDashboard
GRPCDashboardCallback MetricsServerCallback
// GET /httpDashboard
HTTPDashboardCallback MetricsServerCallback
}
// MockMetricsServer is a mock metrics server
// use the callback functions in the MockMetricsServerInput to test if those endpoints are called
func MockMetricsServer(input MockMetricsServerInput) {
// PUT /testResult
httpmock.RegisterResponder(
http.MethodPut,
input.MetricsServerURL+TestResultPath,
func(req *http.Request) (*http.Response, error) {
if input.ExperimentResultCallback != nil {
input.ExperimentResultCallback(req)
}
return httpmock.NewStringResponse(200, "success"), nil
},
)
// GET /httpDashboard
httpmock.RegisterResponder(
http.MethodGet,
input.MetricsServerURL+HTTPDashboardPath,
func(req *http.Request) (*http.Response, error) {
if input.HTTPDashboardCallback != nil {
input.HTTPDashboardCallback(req)
}
return httpmock.NewStringResponse(200, "success"), nil
},
)
// GET /grpcDashboard
httpmock.RegisterResponder(
http.MethodGet,
input.MetricsServerURL+GRPCDashboardPath,
func(req *http.Request) (*http.Response, error) {
if input.GRPCDashboardCallback != nil {
input.GRPCDashboardCallback(req)
}
return httpmock.NewStringResponse(200, "success"), nil
},
)
}