Skip to content

Commit

Permalink
test: add upload behavior test (happy path)
Browse files Browse the repository at this point in the history
but still no tests
  • Loading branch information
jakobmoellerdev committed Dec 11, 2024
1 parent 5d4199c commit d1f879a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion api/ocm/plugin/ppi/cmds/upload/put/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ func Command(p ppi.Plugin, cmd *cobra.Command, opts *Options) error {
}

h, err := u.Upload(p, opts.ArtifactType, opts.MediaType, opts.Hint, spec, opts.Credentials, os.Stdin)

if err != nil {
return fmt.Errorf("upload failed: %w", err)
}

acc := h()

data, err := json.Marshal(acc)
if err == nil {
cmd.Printf("%s\n", string(data))
}

return err
}
3 changes: 3 additions & 0 deletions cmds/jfrogplugin/uploaders/helm/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (r *ArtifactoryUploadResponse) ToHelmAccessSpec() (ppi.AccessSpec, error) {
}
chart := path.Base(urlp.Path)
chart = strings.TrimSuffix(chart, path.Ext(chart))

// this is needed so that the chart version constructor for OCM is happy
// OCM encodes helm charts with a ":"...
if idx := strings.LastIndex(chart, "-"); idx > 0 {
chart = chart[:idx] + ":" + chart[idx+1:]
}
Expand Down
82 changes: 82 additions & 0 deletions cmds/jfrogplugin/uploaders/helm/upload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package helm

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
neturl "net/url"
"strings"
"testing"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/ocm/extensions/accessmethods/helm"
)

func TestUpload(t *testing.T) {
const artifactory = "https://mocked.artifactory.localhost:9999"
const chartName, chartVersion, repo = "chart", "1.0.0", "repo"
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Fatalf("method is not PUT as expected by JFrog")
}
if user, pass, ok := r.BasicAuth(); !ok {
t.Fatalf("invalid basic auth: %s - %s", user, pass)
}
res := ArtifactoryUploadResponse{
Repo: repo,
DownloadUri: fmt.Sprintf("%s/path/to/%s/%s-%s.tgz", artifactory, repo, chartName, chartVersion),
}
data, err := json.Marshal(res)
if err != nil {
t.Fatalf("failed to marshal response: %v", err)
}
if _, err := w.Write(data); err != nil {
t.Fatalf("failed to write response: %v", err)
}
}))
t.Cleanup(func() {
srv.Close()
})
client := srv.Client()

url, err := neturl.Parse(srv.URL)
if err != nil {
t.Fatalf("unexpected test client URL: %v", err)
}

ctx := context.Background()

data := strings.NewReader("testdata")

accessSpec, err := Upload(ctx, data, client, url, credentials.DirectCredentials{
credentials.ATTR_USERNAME: "foo",
credentials.ATTR_PASSWORD: "bar",
})

if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if typ := accessSpec.GetType(); typ != helm.Type {
t.Fatalf("unexpected type: %v", typ)
}

helmAccessSpec, ok := accessSpec.(*helm.AccessSpec)
if !ok {
t.Fatalf("unexpected cast failure to helm access spec")
}

if specChart := helmAccessSpec.GetChartName(); specChart != chartName {
t.Fatalf("unexpected chart name: %v", specChart)
}
if specVersion := helmAccessSpec.GetVersion(); specVersion != chartVersion {
t.Fatalf("unexpected chart version: %v", specVersion)
}

if helmAccessSpec.HelmRepository != fmt.Sprintf("%s/artifactory/api/helm/%s", artifactory, repo) {
t.Fatalf("expected an injected helm api reference to artifactory")
}

}

0 comments on commit d1f879a

Please sign in to comment.