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