-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add upload behavior test (happy path)
but still no tests
- Loading branch information
1 parent
5d4199c
commit d1f879a
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |