File: cache_archiver_integration_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (292 lines) | stat: -rw-r--r-- 8,102 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
//go:build integration
// +build integration

package helpers_test

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"testing"
	"time"

	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	"gocloud.dev/blob"
	"gocloud.dev/blob/fileblob"

	"gitlab.com/gitlab-org/gitlab-runner/commands/helpers"
	"gitlab.com/gitlab-org/gitlab-runner/commands/helpers/archive"
	testHelpers "gitlab.com/gitlab-org/gitlab-runner/helpers"
)

const cacheArchiverArchive = "archive.zip"
const cacheArchiverTestArchivedFile = "archive_file"
const cacheExtractorTestArchivedFile = "archive_file"

func TestCacheArchiverIsUpToDate(t *testing.T) {
	helpers.OnEachZipArchiver(t, func(t *testing.T) {
		writeTestFile(t, cacheArchiverTestArchivedFile)
		defer os.Remove(cacheArchiverTestArchivedFile)

		defer os.Remove(cacheArchiverArchive)
		cmd := helpers.NewCacheArchiverCommandForTest(cacheArchiverArchive, []string{cacheArchiverTestArchivedFile})
		cmd.Execute(nil)
		fi, _ := os.Stat(cacheArchiverArchive)
		cmd.Execute(nil)
		fi2, _ := os.Stat(cacheArchiverArchive)
		assert.Equal(t, fi.ModTime(), fi2.ModTime(), "archive is up to date")

		// We need to wait one second, since the FS doesn't save milliseconds
		time.Sleep(time.Second)

		err := os.Chtimes(cacheArchiverTestArchivedFile, time.Now(), time.Now())
		assert.NoError(t, err)

		cmd.Execute(nil)
		fi3, _ := os.Stat(cacheArchiverArchive)
		assert.NotEqual(t, fi.ModTime(), fi3.ModTime(), "archive should get updated")
	})
}

func TestCacheArchiverForIfNoFileDefined(t *testing.T) {
	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	cmd := helpers.CacheArchiverCommand{}
	assert.Panics(t, func() {
		cmd.Execute(nil)
	})
}

func TestCacheArchiverRemoteServerNotFound(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
	defer ts.Close()

	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File:    cacheArchiverArchive,
		URL:     ts.URL + "/invalid-file.zip",
		Timeout: 0,
	}
	assert.Panics(t, func() {
		cmd.Execute(nil)
	})
}

func TestCacheArchiverRemoteServer(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
	defer ts.Close()

	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File:    cacheArchiverArchive,
		URL:     ts.URL + "/cache.zip",
		Timeout: 0,
	}
	assert.NotPanics(t, func() {
		cmd.Execute(nil)
	})
}

func TestCacheArchiverGoCloudRemoteServer(t *testing.T) {
	mux, bucketDir, cleanup := setupGoCloudFileBucket(t, "testblob")
	defer cleanup()

	objectName := "path/to/cache.zip"

	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File:       cacheArchiverArchive,
		GoCloudURL: fmt.Sprintf("testblob://bucket/" + objectName),
		Timeout:    0,
	}
	helpers.SetCacheArchiverCommandMux(&cmd, mux)
	assert.NotPanics(t, func() {
		cmd.Execute(nil)
	})

	goCloudObjectExists(t, bucketDir, objectName)
}

func TestCacheArchiverRemoteServerWithHeaders(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadWithCustomHeaders))
	defer ts.Close()

	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File:    cacheArchiverArchive,
		URL:     ts.URL + "/cache.zip",
		Headers: []string{"Content-Type: application/zip", "x-ms-blob-type:   BlockBlob "},
		Timeout: 0,
	}
	assert.NotPanics(t, func() {
		cmd.Execute(nil)
	})
}

func TestCacheArchiverRemoteServerTimedOut(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(testCacheUploadHandler))
	defer ts.Close()

	output := logrus.StandardLogger().Out
	var buf bytes.Buffer
	logrus.SetOutput(&buf)
	defer logrus.SetOutput(output)
	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()

	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File: cacheArchiverArchive,
		URL:  ts.URL + "/timeout",
	}
	helpers.SetCacheArchiverCommandClientTimeout(&cmd, 1*time.Millisecond)

	assert.Panics(t, func() {
		cmd.Execute(nil)
	})
	assert.Contains(t, buf.String(), "Client.Timeout")
}

func TestCacheArchiverRemoteServerFailOnInvalidServer(t *testing.T) {
	removeHook := testHelpers.MakeFatalToPanic()
	defer removeHook()
	defer os.Remove(cacheArchiverArchive)
	cmd := helpers.CacheArchiverCommand{
		File:    cacheArchiverArchive,
		URL:     "http://localhost:65333/cache.zip",
		Timeout: 0,
	}
	assert.Panics(t, func() {
		cmd.Execute(nil)
	})

	_, err := os.Stat(cacheExtractorTestArchivedFile)
	assert.Error(t, err)
}

func TestCacheArchiverCompressionLevel(t *testing.T) {
	writeTestFile(t, cacheArchiverTestArchivedFile)
	defer os.Remove(cacheArchiverTestArchivedFile)

	for _, expectedLevel := range []string{"fastest", "fast", "default", "slow", "slowest"} {
		t.Run(expectedLevel, func(t *testing.T) {
			mockArchiver := new(archive.MockArchiver)
			defer mockArchiver.AssertExpectations(t)

			archive.Register(
				"zip",
				func(w io.Writer, dir string, level archive.CompressionLevel) (archive.Archiver, error) {
					assert.Equal(t, helpers.GetCompressionLevel(expectedLevel), level)
					return mockArchiver, nil
				},
				nil,
			)

			mockArchiver.On("Archive", mock.Anything, mock.Anything).Return(nil)

			defer os.Remove(cacheArchiverArchive)
			cmd := helpers.NewCacheArchiverCommandForTest(cacheArchiverArchive, []string{cacheArchiverTestArchivedFile})
			cmd.CompressionLevel = expectedLevel
			cmd.Execute(nil)
		})
	}
}

type dirOpener struct {
	tmpDir string
}

func (o *dirOpener) OpenBucketURL(_ context.Context, u *url.URL) (*blob.Bucket, error) {
	return fileblob.OpenBucket(o.tmpDir, nil)
}

func setupGoCloudFileBucket(t *testing.T, scheme string) (m *blob.URLMux, bucketDir string, cleanup func()) {
	tmpDir, err := ioutil.TempDir("", "test-bucket")
	require.NoError(t, err)

	mux := new(blob.URLMux)
	fake := &dirOpener{tmpDir: tmpDir}
	mux.RegisterBucket(scheme, fake)
	cleanup = func() {
		os.RemoveAll(tmpDir)
	}

	return mux, tmpDir, cleanup
}

func goCloudObjectExists(t *testing.T, bucketDir string, objectName string) {
	bucket, err := fileblob.OpenBucket(bucketDir, nil)
	require.NoError(t, err)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	exists, err := bucket.Exists(ctx, objectName)
	require.NoError(t, err)
	assert.True(t, exists)
}

func testCacheBaseUploadHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPut {
		http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	if r.URL.Path != "/cache.zip" {
		if r.URL.Path == "/timeout" {
			time.Sleep(50 * time.Millisecond)
		}
		http.NotFound(w, r)
		return
	}
}

func testCacheUploadHandler(w http.ResponseWriter, r *http.Request) {
	testCacheBaseUploadHandler(w, r)

	if r.Header.Get("Content-Type") != "application/octet-stream" {
		http.Error(w, "500 Wrong Content-Type header", http.StatusInternalServerError)
		return
	}
	if r.Header.Get("Last-Modified") == "" {
		http.Error(w, "500 Missing Last-Modified header", http.StatusInternalServerError)
		return
	}
}

func testCacheUploadWithCustomHeaders(w http.ResponseWriter, r *http.Request) {
	testCacheBaseUploadHandler(w, r)

	if r.Header.Get("Content-Type") != "application/zip" {
		http.Error(w, "500 Wrong Content-Type header", http.StatusInternalServerError)
	}

	if r.Header.Get("x-ms-blob-type") != "BlockBlob" {
		http.Error(w, "500 Wrong x-ms-blob-type header", http.StatusInternalServerError)
	}

	if r.Header.Get("Last-Modified") != "" {
		http.Error(w, "500 Unexpected Last-Modified header included", http.StatusInternalServerError)
	}
}

func writeTestFile(t *testing.T, fileName string) {
	err := ioutil.WriteFile(fileName, nil, 0600)
	require.NoError(t, err, "Writing file:", fileName)
}