Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-bob/pkg/proxy/proxy_test.go
2506 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package proxy
6
7
import (
8
"net/url"
9
"testing"
10
)
11
12
func TestRewriteNonDockerAPIURL(t *testing.T) {
13
type input struct {
14
u url.URL
15
fromPrefix string
16
toPrefix string
17
host string
18
}
19
tests := []struct {
20
Name string
21
in input
22
u url.URL
23
}{
24
{
25
Name: "toPrefix is empty",
26
in: input{
27
fromPrefix: "base",
28
toPrefix: "",
29
host: "europe-docker.pkg.dev",
30
u: url.URL{
31
Host: "localhost.com",
32
Path: "/base/artifacts-uploads/namespaces/prince-tf-experiments/repositories/dazzle/uploads/somedata",
33
},
34
},
35
u: url.URL{
36
Host: "europe-docker.pkg.dev",
37
Path: "/artifacts-uploads/namespaces/prince-tf-experiments/repositories/dazzle/uploads/somedata",
38
},
39
},
40
{
41
Name: "fromPrefix is empty",
42
in: input{
43
fromPrefix: "",
44
toPrefix: "base",
45
host: "localhost.com",
46
u: url.URL{
47
Host: "europe-docker.pkg.dev",
48
Path: "/artifacts-uploads/namespaces/prince-tf-experiments/repositories/dazzle/uploads/somedata",
49
},
50
},
51
u: url.URL{
52
Host: "localhost.com",
53
Path: "/base/artifacts-uploads/namespaces/prince-tf-experiments/repositories/dazzle/uploads/somedata",
54
},
55
},
56
{
57
Name: "fromPrefix and toPrefix are not empty",
58
in: input{
59
fromPrefix: "from",
60
toPrefix: "to",
61
host: "localhost.com",
62
u: url.URL{
63
Host: "example.com",
64
Path: "/from/some/random/path",
65
},
66
},
67
u: url.URL{
68
Host: "localhost.com",
69
Path: "/to/some/random/path",
70
},
71
},
72
{
73
Name: "fromPrefix and toPrefix are not empty and origin url is not start with fromPrefix",
74
in: input{
75
fromPrefix: "from",
76
toPrefix: "to",
77
host: "localhost.com",
78
u: url.URL{
79
Host: "example.com",
80
Path: "/other-string/some/random/path",
81
},
82
},
83
u: url.URL{
84
Host: "localhost.com",
85
Path: "/to/other-string/some/random/path",
86
},
87
},
88
{
89
Name: "fromPrefix and toPrefix are empty",
90
in: input{
91
fromPrefix: "",
92
toPrefix: "",
93
host: "localhost.com",
94
u: url.URL{
95
Host: "example.com",
96
Path: "/some/random/path",
97
},
98
},
99
u: url.URL{
100
Host: "localhost.com",
101
Path: "/some/random/path",
102
},
103
},
104
}
105
106
for _, test := range tests {
107
t.Run(test.Name, func(t *testing.T) {
108
rewriteNonDockerAPIURL(&test.in.u, test.in.fromPrefix, test.in.toPrefix, test.in.host)
109
if test.in.u.Path != test.u.Path {
110
t.Errorf("expected path: %s but got %s", test.u.Path, test.in.u.Path)
111
}
112
if test.in.u.Host != test.u.Host {
113
t.Errorf("expected Host: %s but got %s", test.u.Host, test.in.u.Host)
114
}
115
if test.in.u.RawPath != test.u.RawPath {
116
t.Errorf("expected RawPath: %s but got %s", test.u.RawPath, test.in.u.RawPath)
117
}
118
})
119
}
120
121
}
122
123
func TestRewriteDockerAPIURL(t *testing.T) {
124
type input struct {
125
u url.URL
126
fromRepo string
127
toRepo string
128
host string
129
tag string
130
}
131
tests := []struct {
132
Name string
133
in input
134
u url.URL
135
}{
136
{
137
Name: "remote to localhost",
138
in: input{
139
fromRepo: "base-images",
140
toRepo: "base",
141
host: "localhost.com",
142
tag: "",
143
u: url.URL{
144
Host: "prince.azurecr.io",
145
Path: "/v2/base-images/some/random/path",
146
},
147
},
148
u: url.URL{
149
Host: "localhost.com",
150
Path: "/v2/base/some/random/path",
151
},
152
},
153
{
154
Name: "remote to localhost without repo",
155
in: input{
156
fromRepo: "base-images",
157
toRepo: "base",
158
host: "localhost.com",
159
tag: "",
160
u: url.URL{
161
Host: "prince.azurecr.io",
162
Path: "/v2/other-string/some/random/path",
163
},
164
},
165
u: url.URL{
166
Host: "localhost.com",
167
Path: "/v2/base/v2/other-string/some/random/path",
168
},
169
},
170
{
171
Name: "localhost to remote",
172
in: input{
173
fromRepo: "base",
174
toRepo: "base-images",
175
host: "prince.azurecr.io",
176
tag: "",
177
u: url.URL{
178
Host: "localhost.com",
179
Path: "/v2/base/some/random/path",
180
},
181
},
182
u: url.URL{
183
Host: "prince.azurecr.io",
184
Path: "/v2/base-images/some/random/path",
185
},
186
},
187
{
188
Name: "manifest reference update with tag",
189
in: input{
190
fromRepo: "base",
191
toRepo: "base-images",
192
host: "prince.azurecr.io",
193
tag: "tag12345",
194
u: url.URL{
195
Host: "localhost.com",
196
Path: "/v2/base/uploads/manifests/manifest12345",
197
},
198
},
199
u: url.URL{
200
Host: "prince.azurecr.io",
201
Path: "/v2/base-images/uploads/manifests/tag12345",
202
},
203
},
204
}
205
206
for _, test := range tests {
207
t.Run(test.Name, func(t *testing.T) {
208
rewriteDockerAPIURL(&test.in.u, test.in.fromRepo, test.in.toRepo, test.in.host, test.in.tag)
209
if test.in.u.Path != test.u.Path {
210
t.Errorf("expected path: %s but got %s", test.u.Path, test.in.u.Path)
211
}
212
if test.in.u.Host != test.u.Host {
213
t.Errorf("expected Host: %s but got %s", test.u.Host, test.in.u.Host)
214
}
215
if test.in.u.RawPath != test.u.RawPath {
216
t.Errorf("expected RawPath: %s but got %s", test.u.RawPath, test.in.u.RawPath)
217
}
218
})
219
}
220
221
}
222
223