From ef0dafb72020200087d4b519d945414f0034455d Mon Sep 17 00:00:00 2001 From: Jesse Simpson Date: Mon, 12 May 2025 16:51:33 -0400 Subject: [PATCH] test: do not rely on mocked webserver for happypath test Signed-off-by: Jesse Simpson --- go.mod | 1 + pkg/pusher/pusher.go | 4 + pkg/uploader/chart_uploader.go | 2 +- pkg/uploader/chart_uploader_test.go | 93 ++++++++-------------- pkg/uploader/testdata/test-0.1.0.tgz | Bin 1262 -> 0 bytes pkg/uploader/testdata/test-0.1.0.tgz.prov | 6 -- 6 files changed, 39 insertions(+), 67 deletions(-) delete mode 100644 pkg/uploader/testdata/test-0.1.0.tgz delete mode 100644 pkg/uploader/testdata/test-0.1.0.tgz.prov diff --git a/go.mod b/go.mod index c08dabc3b..5237f3eda 100644 --- a/go.mod +++ b/go.mod @@ -136,6 +136,7 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/cast v1.7.0 // indirect github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/xlab/treeprint v1.2.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect diff --git a/pkg/pusher/pusher.go b/pkg/pusher/pusher.go index e3c767be9..2dfeb2933 100644 --- a/pkg/pusher/pusher.go +++ b/pkg/pusher/pusher.go @@ -93,6 +93,10 @@ func (p Provider) Provides(scheme string) bool { // Providers is a collection of Provider objects. type Providers []Provider +type ProvidersInterface interface { + ByScheme(scheme string) (Pusher, error) +} + // ByScheme returns a Provider that handles the given scheme. // // If no provider handles this scheme, this will return an error. diff --git a/pkg/uploader/chart_uploader.go b/pkg/uploader/chart_uploader.go index b3d612e38..7ecd2e232 100644 --- a/pkg/uploader/chart_uploader.go +++ b/pkg/uploader/chart_uploader.go @@ -29,7 +29,7 @@ type ChartUploader struct { // Out is the location to write warning and info messages. Out io.Writer // Pusher collection for the operation - Pushers pusher.Providers + Pushers pusher.ProvidersInterface // Options provide parameters to be passed along to the Pusher being initialized. Options []pusher.Option // RegistryClient is a client for interacting with registries. diff --git a/pkg/uploader/chart_uploader_test.go b/pkg/uploader/chart_uploader_test.go index b17ce4c65..692b7d452 100644 --- a/pkg/uploader/chart_uploader_test.go +++ b/pkg/uploader/chart_uploader_test.go @@ -17,77 +17,50 @@ limitations under the License. package uploader import ( - "crypto/sha256" "fmt" - "helm.sh/helm/v4/pkg/cli" - "helm.sh/helm/v4/pkg/pusher" - "helm.sh/helm/v4/pkg/registry" - "net/http" - "net/http/httptest" - "net/url" - "strconv" "strings" "testing" + + "github.com/stretchr/testify/mock" + + "helm.sh/helm/v4/pkg/cli" + "helm.sh/helm/v4/pkg/pusher" ) -func TestChartUploader_UploadTo_Happy(t *testing.T) { - shasum := "" - var content []byte - contentSize := 0 - // no significant meaning behind this uuid, just needs to be a uuid - uploadSessionId := "c6ce3ba4-788f-4e10-93ed-ff77d35c6851" - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == "HEAD" { - w.WriteHeader(http.StatusNotFound) - } else if r.Method == "POST" && r.URL.Path == "/v2/test/blobs/uploads/" { - w.Header().Set("Location", "/v2/test/blobs/uploads/"+uploadSessionId) - w.WriteHeader(http.StatusAccepted) - } else if r.Method == "PUT" && r.URL.Path == "/v2/test/blobs/uploads/"+uploadSessionId { - w.Header().Set("Location", "/v2/test/blobs/sha256:irrelevant") - w.WriteHeader(http.StatusCreated) - } else if r.Method == "PUT" && strings.HasPrefix(r.URL.Path, "/v2/test/manifests/sha256:") { - content = make([]byte, r.ContentLength) - r.Body.Read(content) - h := sha256.New() - h.Write(content) - shasumBuilder := strings.Builder{} - fmt.Fprintf(&shasumBuilder, "%x", h.Sum(nil)) - shasum = shasumBuilder.String() - contentSize = len(content) - w.Header().Set("Location", "/v2/test/manifests/sha256:"+shasum) - w.WriteHeader(http.StatusCreated) - } else if r.Method == "GET" && r.URL.Path == "/v2/test/manifests/sha256:"+shasum { - w.Header().Set("Content-Length", strconv.Itoa(contentSize)) - w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") - w.Header().Set("Docker-Content-Digest", "sha256:"+shasum) - _, err := fmt.Fprint(w, string(content)) - if err != nil { - t.Errorf("%s", err) - } - } else if r.Method == "PUT" && r.URL.Path == "/v2/test/manifests/0.1.0" { - w.Header().Set("Docker-Content-Digest", "sha256:"+shasum) - w.Header().Set("Content-Length", strconv.Itoa(contentSize)) - w.Header().Set("Location", "/v2/test/manifests/sha256:"+shasum) - w.WriteHeader(http.StatusCreated) - } - })) - defer srv.Close() +type MockedPusher struct { + mock.Mock +} - envSettings := cli.EnvSettings{} - pushers := pusher.All(&envSettings) +func (m *MockedPusher) Push(chartRef string, url string, _ ...pusher.Option) error { + m.Called(chartRef, url) + return nil +} - u, _ := url.ParseRequestURI(srv.URL) - ociReplacedUrl := strings.Replace(u.String(), "http", "oci", 1) +type MockedProviders struct { + mock.Mock +} + +func (m *MockedProviders) ByScheme(string) (pusher.Pusher, error) { + args := m.Called() + mockedPusher := args.Get(0).(pusher.Pusher) + return mockedPusher, nil +} + +func TestChartUploader_UploadTo_Happy(t *testing.T) { + mockedPusher := new(MockedPusher) + mockedPusher.On("Push").Return(nil) + + mockedProviders := new(MockedProviders) + mockedProviders.On("ByScheme").Return(mockedPusher, nil) - srvClient := srv.Client() - client, _ := registry.NewClient(registry.ClientOptHTTPClient(srvClient)) uploader := ChartUploader{ - Pushers: pushers, - RegistryClient: client, - Options: []pusher.Option{pusher.WithPlainHTTP(true)}, + Pushers: mockedProviders, } - err := uploader.UploadTo("testdata/test-0.1.0.tgz", ociReplacedUrl+"") + mockedPusher.On("Push", "testdata/test-0.1.0.tgz", "oci://test").Return(nil) + err := uploader.UploadTo("testdata/test-0.1.0.tgz", "oci://test") + mockedPusher.AssertCalled(t, "Push", "testdata/test-0.1.0.tgz", "oci://test") + if err != nil { fmt.Println(err) t.Errorf("Expected push to succeed but got error") diff --git a/pkg/uploader/testdata/test-0.1.0.tgz b/pkg/uploader/testdata/test-0.1.0.tgz deleted file mode 100644 index 933e2c816a516e4601d0516e91e23282da58e5fb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1262 zcmVDc zVQyr3R8em|NM&qo0PI)WZrer^_O+g3e&tpnDcOkw{xkvCDUcQ^k^-%PT#m?*IL#iI z-C5a&?RPJBsgvCV3EDbAP|ppQvpeVcmO~*g`B{ffvMbc;;323eisJdvQF<0dasOEq z&!z{{qvPXev*W{J`uw1n&Zf_c11KKEn|ln$e^YY zVpEJ(8+MURvtsnE9etz+p@jbxszCn1E`ZPA|L|xwyN~~)+2JGpzXl15q7q(_QWh?( zQ|$#dGa7=ImPRcKm2lcScrk%lF?%**BhHramXsnR+@{b?_mep1^{_l2KsAf;54k#BsxvBxM3Z zy0;EI3!sh+G(s7?w1yg>0E*}2>~=pX-53Q=x+j-GMY=vELumH&YaVO@dC97rbPc~? zaO|O1*pNd3Qp5`~Ehw@X{2DmrXlskS#Na*lOi{B!-B{O?gT6yY;_yz`N#zB5o9;CF!qDfU^F_w^{YUpK2?5w_&uHLXa&tjfFDuw_-9ni-%{ zT)(!C^xEgvsynUFSmUV?l0Xiy<(_@RW|{JkRk`AQMW>2WZ|qW|hiX#*$3FP2M3qMu z;$p6B^ZrAyLUq-Dp(}s&1kQPdI(X2yG?>AeLmv~S8E~B#z~~*NaxDX;jN#Bi9j>^Is)CDv4GuJVk6T=<`*yN{2gW6o$epuz*;jk6HebvLd>b;|Sz~czuNjEs$ zO#$ILX>Qr~3a?5?;r&1HF1VGhgu|lfF9%LXyzHce7i-RKvXRtQc{DLvSS#d8IJ5ng zi-*(K3+C7Yw}h~;E`IqmH1P*$0za81`;QH}Y5t3sl0qS$$Jc*-4*cBz-*It&{yQp; zk00m1uR)z?wH{dh_wvozZ|~mz2$7cn8yBF^EKkN{#sliB2-oceph+;H$CI3lgDjrD z7;iVzY*}AlLv}v=U?c|I-d6drwYx^ldvK&q#$K)z`Ho1&>#t$rWR3Sby}ohW3wYO@ zjE8mhhacBfKD?~`%y*7faV*=4chisi)J?ZX`nsCeKU~4yxnU|!2j}(vMx2flyWfak zUxV0psMYpNv=29N*wb$vE%*|~H)FT~!=@!8a0IuvJKKB8hZ`)a?>Av>VyW_>PT#tN Y9_f)D>3gL=0{{U3|HlH~$^aGs0GR-8$^ZZW diff --git a/pkg/uploader/testdata/test-0.1.0.tgz.prov b/pkg/uploader/testdata/test-0.1.0.tgz.prov deleted file mode 100644 index 15b57c8bb..000000000 --- a/pkg/uploader/testdata/test-0.1.0.tgz.prov +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: v2 -name: test -version: 0.1.0 - -files: - test-0.1.0.tgz: sha256:40ac18ea704177f883dab4a4fe7f8b27de9f79633c64649ddbc3a91011ed7be0