File: gob_test.go

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (364 lines) | stat: -rw-r--r-- 10,840 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab/workhorse/internal/api"
	"gitlab.com/gitlab-org/gitlab/workhorse/internal/secret"
	"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
)

type gobAuthServer struct {
	shouldReceiveRequestPath string
	respondWithStatusCode    int
}

type gobUpstreamServer struct {
	shouldReceiveRequestPath string
	shouldBeCalled           bool
	respondWithStatusCode    int
	respondWithBody          string
}

type gobTestCase struct {
	desc   string
	path   string
	method string
	body   string

	shouldRespondWithBody       string
	shouldRespondWithStatusCode int

	authServer gobAuthServer
	upstream   gobUpstreamServer
}

func TestGOBEndpoints(t *testing.T) {
	testCases := [][]gobTestCase{
		genGETTestcases("traces"),
		genGETTestcases("metrics"),
		genGETTestcases("logs"),
		genGETTestcases("analytics"),
		genGETTestcases("services"),

		genPOSTTestcases("traces"),
		genPOSTTestcases("metrics"),
		genPOSTTestcases("logs"),
	}

	for _, signalTestCases := range testCases {
		for _, tc := range signalTestCases {
			t.Run(tc.desc, func(t *testing.T) {
				runTest(t, tc)
			})
		}
	}
}

func genGETTestcases(signal string) []gobTestCase {
	return []gobTestCase{
		{
			desc:   fmt.Sprintf("GET /%s, successful auth, proxies successful upstream", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("GET /%s with multi-digit projectID, successful auth, proxies successful upstream", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/11111/observability/v1/%s", signal),

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/11111/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("GET /%s with url-encode projectID, successful auth, proxies successful upstream", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/diaspora%%2Fdiaspora/observability/v1/%s", signal),

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/diaspora%%2Fdiaspora/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("GET /%s/some/subpath subpath, successful auth, proxies successful upstream", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s/some/subpath", signal),

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s/some/subpath", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("GET /%s, unsuccessful auth, returns auth status code", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),

			shouldRespondWithBody:       "",
			shouldRespondWithStatusCode: 401,

			authServer: gobAuthServer{
				respondWithStatusCode:    401,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				shouldBeCalled: false,
			},
		},
		{
			desc:   fmt.Sprintf("GET /%s, successful auth, correctly proxies upstream failure", signal),
			method: "GET",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),

			shouldRespondWithBody:       "",
			shouldRespondWithStatusCode: 500,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/read/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 500,
				respondWithBody:       "",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
	}
}

func genPOSTTestcases(signal string) []gobTestCase {
	return []gobTestCase{
		{
			desc:   fmt.Sprintf("POST /%s, successful auth, proxies successful upstream", signal),
			method: "POST",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),
			body:   "my posted data",

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/write/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("POST /%s/some/subpath, successful auth, proxies successful upstream", signal),
			method: "POST",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s/some/subpath", signal),
			body:   "my posted data",

			shouldRespondWithBody:       "hello world",
			shouldRespondWithStatusCode: 200,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/write/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 200,
				respondWithBody:       "hello world",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s/some/subpath", signal),
				shouldBeCalled:           true,
			},
		},
		{
			desc:   fmt.Sprintf("POST /%s, unsuccessful auth, returns auth status code", signal),
			method: "POST",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),
			body:   "my posted data",

			shouldRespondWithBody:       "",
			shouldRespondWithStatusCode: 401,

			authServer: gobAuthServer{
				respondWithStatusCode:    401,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/write/%s", signal),
			},

			upstream: gobUpstreamServer{
				shouldBeCalled: false,
			},
		},
		{
			desc:   fmt.Sprintf("POST /%s, successful auth, correctly proxies upstream failure", signal),
			method: "POST",
			path:   fmt.Sprintf("/api/v4/projects/1/observability/v1/%s", signal),
			body:   "my posted data",

			shouldRespondWithBody:       "",
			shouldRespondWithStatusCode: 500,

			authServer: gobAuthServer{
				respondWithStatusCode:    200,
				shouldReceiveRequestPath: fmt.Sprintf("/api/v4/internal/observability/project/1/write/%s", signal),
			},

			upstream: gobUpstreamServer{
				respondWithStatusCode: 500,
				respondWithBody:       "",

				shouldReceiveRequestPath: fmt.Sprintf("/observability/v1/%s", signal),
				shouldBeCalled:           true,
			},
		},
	}
}

func runTest(t *testing.T, tc gobTestCase) {
	gobSettingsHeaders := map[string]string{
		"foo": "bar",
		"baz": "foobar",
	}

	upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if tc.upstream.shouldBeCalled != true {
			assert.Fail(t, "upstream should not be called")
		}

		assert.Equal(t, tc.upstream.shouldReceiveRequestPath, r.URL.Path, "requested upstream endpoint")
		// Assert that upstream received the headers that were returned in the api.Response
		// from the authServer
		for name, value := range gobSettingsHeaders {
			assert.Equal(t, value, r.Header.Get(name), "received correct header")
		}

		defer r.Body.Close()
		b, err := io.ReadAll(r.Body)
		assert.NoError(t, err)
		assert.Equal(t, tc.body, string(b), "received body upstream")

		w.WriteHeader(tc.upstream.respondWithStatusCode)
		_, err = w.Write([]byte(tc.upstream.respondWithBody))
		assert.NoError(t, err, "write auth response")
	}))
	defer upstream.Close()

	authServer := testhelper.TestServerWithHandler(nil, func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, tc.authServer.shouldReceiveRequestPath, r.URL.Path, "requested auth endpoint")
		// Auth request should use the same method as the original Workhorse request
		assert.Equal(t, tc.method, r.Method, "auth request method")

		// return a 204 No Content response if we don't receive the JWT header from Workhorse
		if r.Header.Get(secret.RequestHeader) == "" {
			w.WriteHeader(204)
			return
		}
		w.Header().Set("Content-Type", api.ResponseContentType)

		// Should not receive the body of the original Workhorse request
		defer r.Body.Close()
		b, err := io.ReadAll(r.Body)
		assert.NoError(t, err)
		assert.Empty(t, b)

		data, err := json.Marshal(&api.Response{
			Gob: api.GOBSettings{
				Backend: upstream.URL,
				Headers: gobSettingsHeaders,
			},
		})
		if err != nil {
			w.WriteHeader(503)
			fmt.Fprint(w, err)
			return
		}
		w.WriteHeader(tc.authServer.respondWithStatusCode)
		// Mimic the internal API where response body is only written on success
		if tc.authServer.respondWithStatusCode == 200 {
			w.Write(data)
		}
	})
	defer authServer.Close()

	workhorse := startWorkhorseServer(t, authServer.URL)

	// Do the request
	req, err := http.NewRequest(tc.method, workhorse.URL+tc.path, strings.NewReader(tc.body))
	require.NoError(t, err)

	resp, err := http.DefaultClient.Do(req)
	require.NoError(t, err)
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err)

	assert.Equal(t, tc.shouldRespondWithStatusCode, resp.StatusCode, "response status code")
	assert.Equal(t, tc.shouldRespondWithBody, string(body), "response body")
}