Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vfs_test.go « zip « vfs « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dff2ff4337d3e454b9bb5afc8d76d76081126bde (plain)
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
package zip

import (
	"context"
	"io/ioutil"
	"testing"
	"time"

	"github.com/prometheus/client_golang/prometheus/testutil"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/httprange"
	"gitlab.com/gitlab-org/gitlab-pages/internal/vfs"
	"gitlab.com/gitlab-org/gitlab-pages/metrics"
)

func TestVFSRoot(t *testing.T) {
	url, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil)
	defer cleanup()

	tests := map[string]struct {
		path           string
		expectedErrMsg string
	}{
		"zip_file_exists": {
			path: "/public.zip",
		},
		"zip_file_does_not_exist": {
			path:           "/unknown",
			expectedErrMsg: vfs.ErrNotExist{Inner: httprange.ErrNotFound}.Error(),
		},
		"invalid_url": {
			path:           "/%",
			expectedErrMsg: "invalid URL",
		},
	}

	vfs := New()

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			root, err := vfs.Root(context.Background(), url+tt.path)
			if tt.expectedErrMsg != "" {
				require.Error(t, err)
				require.Contains(t, err.Error(), tt.expectedErrMsg)
				return
			}

			require.NoError(t, err)
			require.IsType(t, &zipArchive{}, root)

			f, err := root.Open(context.Background(), "index.html")
			require.NoError(t, err)

			content, err := ioutil.ReadAll(f)
			require.NoError(t, err)
			require.Equal(t, "zip.gitlab.io/project/index.html\n", string(content))

			fi, err := root.Lstat(context.Background(), "index.html")
			require.NoError(t, err)
			require.Equal(t, "index.html", fi.Name())

			link, err := root.Readlink(context.Background(), "symlink.html")
			require.NoError(t, err)
			require.Equal(t, "subdir/linked.html", link)
		})
	}
}

func TestVFSFindOrOpenArchiveConcurrentAccess(t *testing.T) {
	testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil)
	defer cleanup()

	path := testServerURL + "/public.zip"

	vfs := New().(*zipVFS)
	root, err := vfs.Root(context.Background(), path)
	require.NoError(t, err)

	done := make(chan struct{})
	defer close(done)

	// Try to hit a condition between the invocation
	// of cache.GetWithExpiration and cache.Add
	go func() {
		for {
			select {
			case <-done:
				return

			default:
				vfs.cache.Flush()
				vfs.cache.SetDefault(path, root)
			}
		}
	}()

	require.Eventually(t, func() bool {
		_, err := vfs.findOrOpenArchive(context.Background(), path, path)
		return err == errAlreadyCached
	}, time.Second, time.Nanosecond)
}

func TestVFSFindOrOpenArchiveRefresh(t *testing.T) {
	testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public.zip", nil)
	defer cleanup()

	// It should be large enough to not have flaky executions
	const expiryInterval = 10 * time.Millisecond

	tests := map[string]struct {
		path               string
		expirationInterval time.Duration
		refreshInterval    time.Duration

		expectNewArchive       bool
		expectOpenError        bool
		expectArchiveRefreshed bool
	}{
		"after cache expiry of successful open a new archive is returned": {
			path:               "/public.zip",
			expirationInterval: expiryInterval,
			expectNewArchive:   true,
			expectOpenError:    false,
		},
		"after cache expiry of errored open a new archive is returned": {
			path:               "/unknown.zip",
			expirationInterval: expiryInterval,
			expectNewArchive:   true,
			expectOpenError:    true,
		},
		"subsequent open during refresh interval does refresh archive": {
			path:                   "/public.zip",
			expirationInterval:     time.Second,
			refreshInterval:        time.Second, // refresh always
			expectNewArchive:       false,
			expectOpenError:        false,
			expectArchiveRefreshed: true,
		},
		"subsequent open before refresh interval does not refresh archive": {
			path:                   "/public.zip",
			expirationInterval:     time.Second,
			refreshInterval:        time.Millisecond, // very short interval should not refresh
			expectNewArchive:       false,
			expectOpenError:        false,
			expectArchiveRefreshed: false,
		},
		"subsequent open of errored archive during refresh interval does not refresh": {
			path:                   "/unknown.zip",
			expirationInterval:     time.Second,
			refreshInterval:        time.Second, // refresh always (if not error)
			expectNewArchive:       false,
			expectOpenError:        true,
			expectArchiveRefreshed: false,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			withExpectedArchiveCount(t, 1, func(t *testing.T) {
				vfs := New(
					WithCacheExpirationInterval(test.expirationInterval),
					WithCacheRefreshInterval(test.refreshInterval),
				).(*zipVFS)

				path := testServerURL + test.path

				// create a new archive and increase counters
				archive1, err1 := vfs.findOrOpenArchive(context.Background(), path, path)
				if test.expectOpenError {
					require.Error(t, err1)
					require.Nil(t, archive1)
				} else {
					require.NoError(t, err1)
				}

				item1, exp1, found := vfs.cache.GetWithExpiration(path)
				require.True(t, found)

				// give some time to for timeouts to fire
				time.Sleep(expiryInterval)

				if test.expectNewArchive {
					// should return a new archive
					archive2, err2 := vfs.findOrOpenArchive(context.Background(), path, path)
					if test.expectOpenError {
						require.Error(t, err2)
						require.Nil(t, archive2)
					} else {
						require.NoError(t, err2)
						require.NotEqual(t, archive1, archive2, "a new archive should be returned")
					}
					return
				}

				// should return exactly the same archive
				archive2, err2 := vfs.findOrOpenArchive(context.Background(), path, path)
				require.Equal(t, archive1, archive2, "same archive is returned")
				require.Equal(t, err1, err2, "same error for the same archive")

				item2, exp2, found := vfs.cache.GetWithExpiration(path)
				require.True(t, found)
				require.Equal(t, item1, item2, "same item is returned")

				if test.expectArchiveRefreshed {
					require.Greater(t, exp2.UnixNano(), exp1.UnixNano(), "archive should be refreshed")
				} else {
					require.Equal(t, exp1.UnixNano(), exp2.UnixNano(), "archive has not been refreshed")
				}
			})
		})
	}
}

func withExpectedArchiveCount(t *testing.T, archiveCount int, fn func(t *testing.T)) {
	t.Helper()

	archivesMetric := metrics.ZipCachedEntries.WithLabelValues("archive")
	archivesCount := testutil.ToFloat64(archivesMetric)

	fn(t)

	archivesCountEnd := testutil.ToFloat64(archivesMetric)
	require.Equal(t, float64(archiveCount), archivesCountEnd-archivesCount, "exact number of archives is cached")
}