test: do not rely on mocked webserver for happypath test

Signed-off-by: Jesse Simpson <jesse.simpson36@gmail.com>
pull/30807/head
Jesse Simpson 5 months ago
parent c253c2285a
commit ef0dafb720
No known key found for this signature in database
GPG Key ID: 237495C89AB0AAFC

@ -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

@ -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.

@ -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.

@ -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")

Binary file not shown.

@ -1,6 +0,0 @@
apiVersion: v2
name: test
version: 0.1.0
files:
test-0.1.0.tgz: sha256:40ac18ea704177f883dab4a4fe7f8b27de9f79633c64649ddbc3a91011ed7be0
Loading…
Cancel
Save