-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
164 lines (142 loc) · 4.07 KB
/
main_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/api"
"github.com/mxab/nacp/admissionctrl"
"github.com/mxab/nacp/admissionctrl/mutator"
"github.com/mxab/nacp/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// rewrite the test above as table driven test
func TestProxyTableDriven(t *testing.T) {
type test struct {
name string
path string
method string
want string
}
wantJob := testutil.ReadJob(t, "job.json")
wantJob.Meta = map[string]string{
"hello": "world",
}
register := &api.JobRegisterRequest{
Job: wantJob,
}
wantRegisterData, err := json.Marshal(register)
if err != nil {
t.Fatal(err)
}
tests := []test{
{
name: "create job",
path: "/v1/jobs",
method: "PUT",
want: string(wantRegisterData),
},
{
name: "update job",
path: "/v1/job/some-job",
method: "PUT",
want: string(wantRegisterData),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
nomadDummy := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Test request parameters
assert.Equal(t, req.Method, tc.method, "Ensure method is set to POST")
assert.Equal(t, req.URL.Path, tc.path, "Ensure path is set")
jsonData, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
json := string(jsonData)
assert.JSONEq(t, tc.want, json, "Body matches")
_, _ = rw.Write([]byte(`OK`))
}))
// Close the server when test finishes
defer nomadDummy.Close()
// Use Client & URL from our local test server
nomad, err := url.Parse(nomadDummy.URL)
if err != nil {
t.Fatal(err)
}
jobHandler := admissionctrl.NewJobHandler(
[]admissionctrl.JobMutator{
&mutator.HelloMutator{},
},
[]admissionctrl.JobValidator{},
hclog.NewNullLogger(),
)
proxy := NewProxyHandler(nomad, jobHandler, hclog.NewNullLogger())
proxyServer := httptest.NewServer(http.HandlerFunc(proxy))
defer proxyServer.Close()
jobRequest := api.JobRegisterRequest{
Job: testutil.ReadJob(t, "job.json"),
}
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(jobRequest)
if err != nil {
t.Fatal(err)
}
res, err := sendPut(t, fmt.Sprintf("%s%s", proxyServer.URL, tc.path), buffer)
assert.NoError(t, err, "No http call error")
assert.Equal(t, 200, res.StatusCode, "OK response is expected")
})
}
}
func TestAdmissionControllerErrors(t *testing.T) {
nomadDummy := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
_, _ = rw.Write([]byte(`you should not see this`))
}))
// Close the server when test finishes
defer nomadDummy.Close()
validator := new(testutil.MockValidator)
validator.On("Validate", mock.Anything).Return([]error{}, fmt.Errorf("some error"))
nomad, err := url.Parse(nomadDummy.URL)
if err != nil {
t.Fatal(err)
}
jobHandler := admissionctrl.NewJobHandler(
[]admissionctrl.JobMutator{},
[]admissionctrl.JobValidator{validator},
hclog.NewNullLogger(),
)
proxy := NewProxyHandler(nomad, jobHandler, hclog.NewNullLogger())
proxyServer := httptest.NewServer(http.HandlerFunc(proxy))
defer proxyServer.Close()
jobRequest := api.JobRegisterRequest{
Job: testutil.ReadJob(t, "job.json"),
}
buffer := new(bytes.Buffer)
err = json.NewEncoder(buffer).Encode(jobRequest)
if err != nil {
t.Fatal(err)
}
res, err := sendPut(t, fmt.Sprintf("%s%s", proxyServer.URL, "/v1/jobs"), buffer)
require.NoError(t, err, "No http call error")
assert.Equal(t, 500, res.StatusCode, "Should return 400")
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
x := string(body)
t.Logf("response: %s", x)
}
func sendPut(t *testing.T, url string, body io.Reader) (*http.Response, error) {
t.Helper()
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return http.DefaultClient.Do(req)
}