|
| 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 TestRewriteURL(t *testing.T) { |
| 13 | + type input struct { |
| 14 | + proxy *Proxy |
| 15 | + u url.URL |
| 16 | + fromRepo string |
| 17 | + toRepo string |
| 18 | + host string |
| 19 | + tag string |
| 20 | + } |
| 21 | + tests := []struct { |
| 22 | + Name string |
| 23 | + in input |
| 24 | + u url.URL |
| 25 | + }{ |
| 26 | + { |
| 27 | + Name: "updates alias reference when it is cross blob mount", |
| 28 | + in: input{ |
| 29 | + proxy: &Proxy{ |
| 30 | + Host: url.URL{Host: "localhost:8080", Scheme: "http"}, |
| 31 | + Aliases: map[string]Repo{ |
| 32 | + "base": { |
| 33 | + Repo: "/gitpod/base-images", |
| 34 | + }, |
| 35 | + "target": { |
| 36 | + Repo: "/gitpod/workspace-images", |
| 37 | + }, |
| 38 | + }, |
| 39 | + proxies: nil, |
| 40 | + }, |
| 41 | + fromRepo: "base", |
| 42 | + toRepo: "/gitpod/base-images", |
| 43 | + host: "registry.gitlab.com", |
| 44 | + tag: "tag12345", |
| 45 | + u: url.URL{ |
| 46 | + Host: "localhost.com", |
| 47 | + RawQuery: "/mounts/uploads/?mount=sha:12345?from=base", |
| 48 | + }, |
| 49 | + }, |
| 50 | + u: url.URL{ |
| 51 | + Host: "registry.gitlab.com", |
| 52 | + RawQuery: "/mounts/uploads/?mount=sha:12345?from=/gitpod/base-images", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "does not update alias reference when it is not cross blob mount", |
| 57 | + in: input{ |
| 58 | + proxy: &Proxy{ |
| 59 | + Host: url.URL{Host: "localhost:8080", Scheme: "http"}, |
| 60 | + Aliases: map[string]Repo{ |
| 61 | + "base": { |
| 62 | + Repo: "/gitpod/base-images", |
| 63 | + }, |
| 64 | + "target": { |
| 65 | + Repo: "/gitpod/workspace-images", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + fromRepo: "base", |
| 70 | + toRepo: "/gitpod/base-images", |
| 71 | + host: "registry.gitlab.com", |
| 72 | + tag: "tag12345", |
| 73 | + u: url.URL{ |
| 74 | + Host: "localhost.com", |
| 75 | + RawQuery: "/blobs/sha:12345base", |
| 76 | + }, |
| 77 | + }, |
| 78 | + u: url.URL{ |
| 79 | + Host: "registry.gitlab.com", |
| 80 | + RawQuery: "/blobs/sha:12345base", |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, test := range tests { |
| 86 | + t.Run(test.Name, func(t *testing.T) { |
| 87 | + test.in.proxy.rewriteURL(&test.in.u, test.in.fromRepo, test.in.toRepo, test.in.host, test.in.tag) |
| 88 | + if test.in.u.RawQuery != test.u.RawQuery { |
| 89 | + t.Errorf("expected raw query: %s but got %s", test.u.RawQuery, test.in.u.RawQuery) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments