|
| 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 empty", |
| 74 | + in: input{ |
| 75 | + fromPrefix: "", |
| 76 | + toPrefix: "", |
| 77 | + host: "localhost.com", |
| 78 | + u: url.URL{ |
| 79 | + Host: "example.com", |
| 80 | + Path: "/some/random/path", |
| 81 | + }, |
| 82 | + }, |
| 83 | + u: url.URL{ |
| 84 | + Host: "localhost.com", |
| 85 | + Path: "/some/random/path", |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, test := range tests { |
| 91 | + t.Run(test.Name, func(t *testing.T) { |
| 92 | + rewriteNonDockerAPIURL(&test.in.u, test.in.fromPrefix, test.in.toPrefix, test.in.host) |
| 93 | + if test.in.u.Path != test.u.Path { |
| 94 | + t.Errorf("expected path: %s but got %s", test.u.Path, test.in.u.Path) |
| 95 | + } |
| 96 | + if test.in.u.Host != test.u.Host { |
| 97 | + t.Errorf("expected Host: %s but got %s", test.u.Host, test.in.u.Host) |
| 98 | + } |
| 99 | + if test.in.u.RawPath != test.u.RawPath { |
| 100 | + t.Errorf("expected RawPath: %s but got %s", test.u.RawPath, test.in.u.RawPath) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +func TestRewriteDockerAPIURL(t *testing.T) { |
| 108 | + type input struct { |
| 109 | + u url.URL |
| 110 | + fromRepo string |
| 111 | + toRepo string |
| 112 | + host string |
| 113 | + tag string |
| 114 | + } |
| 115 | + tests := []struct { |
| 116 | + Name string |
| 117 | + in input |
| 118 | + u url.URL |
| 119 | + }{ |
| 120 | + { |
| 121 | + Name: "remote to localhost", |
| 122 | + in: input{ |
| 123 | + fromRepo: "base-images", |
| 124 | + toRepo: "base", |
| 125 | + host: "localhost.com", |
| 126 | + tag: "", |
| 127 | + u: url.URL{ |
| 128 | + Host: "prince.azurecr.io", |
| 129 | + Path: "/v2/base-images/some/random/path", |
| 130 | + }, |
| 131 | + }, |
| 132 | + u: url.URL{ |
| 133 | + Host: "localhost.com", |
| 134 | + Path: "/v2/base/some/random/path", |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + Name: "localhost to remote", |
| 139 | + in: input{ |
| 140 | + fromRepo: "base", |
| 141 | + toRepo: "base-images", |
| 142 | + host: "prince.azurecr.io", |
| 143 | + tag: "", |
| 144 | + u: url.URL{ |
| 145 | + Host: "localhost.com", |
| 146 | + Path: "/v2/base/some/random/path", |
| 147 | + }, |
| 148 | + }, |
| 149 | + u: url.URL{ |
| 150 | + Host: "prince.azurecr.io", |
| 151 | + Path: "/v2/base-images/some/random/path", |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + Name: "manifest reference update with tag", |
| 156 | + in: input{ |
| 157 | + fromRepo: "base", |
| 158 | + toRepo: "base-images", |
| 159 | + host: "prince.azurecr.io", |
| 160 | + tag: "tag12345", |
| 161 | + u: url.URL{ |
| 162 | + Host: "localhost.com", |
| 163 | + Path: "/v2/base/uploads/manifests/manifest12345", |
| 164 | + }, |
| 165 | + }, |
| 166 | + u: url.URL{ |
| 167 | + Host: "prince.azurecr.io", |
| 168 | + Path: "/v2/base-images/uploads/manifests/tag12345", |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, test := range tests { |
| 174 | + t.Run(test.Name, func(t *testing.T) { |
| 175 | + rewriteDockerAPIURL(&test.in.u, test.in.fromRepo, test.in.toRepo, test.in.host, test.in.tag) |
| 176 | + if test.in.u.Path != test.u.Path { |
| 177 | + t.Errorf("expected path: %s but got %s", test.u.Path, test.in.u.Path) |
| 178 | + } |
| 179 | + if test.in.u.Host != test.u.Host { |
| 180 | + t.Errorf("expected Host: %s but got %s", test.u.Host, test.in.u.Host) |
| 181 | + } |
| 182 | + if test.in.u.RawPath != test.u.RawPath { |
| 183 | + t.Errorf("expected RawPath: %s but got %s", test.u.RawPath, test.in.u.RawPath) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments