Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
They increase coverage slightly from 85.7% to 87%.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Oct 10, 2024
1 parent c19f5fb commit f4ae108
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func findArtifactDescriptor(all []ocispec.Descriptor, opts downloadOptions) (oci
}
}
if opts.artifactName == "" {
return ocispec.Descriptor{}, fmt.Errorf("no artifacts found")
return ocispec.Descriptor{}, fmt.Errorf("no artifact descriptors found")
}
return ocispec.Descriptor{}, fmt.Errorf("artifact %q not found", opts.artifactName)
}
Expand Down
22 changes: 12 additions & 10 deletions internal/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package oci_test

import (
"bytes"
"cmp"
"context"
"embed"
"encoding/json"
Expand Down Expand Up @@ -61,15 +62,16 @@ func parseTestsYAML[T any](t *testing.T) map[string]T {

// testFile represents a single test file inside the testdata directory.
type testFile struct {
Manifest string `json:"manifest"`
Expected string `json:"expected"`
Error string `json:"error"`
Authenticated bool `json:"authenticated"`
AuthUser string `json:"authUser"`
AuthPass string `json:"authPass"`
Artifacts map[string]string `json:"artifacts"`
ArtifactName string `json:"artifactName"`
PlainHTTP bool `json:"plainHTTP"`
Manifest string `json:"manifest"`
ManifestMediaType string `json:"manifestMediaType"`
Expected string `json:"expected"`
Error string `json:"error"`
Authenticated bool `json:"authenticated"`
AuthUser string `json:"authUser"`
AuthPass string `json:"authPass"`
Artifacts map[string]string `json:"artifacts"`
ArtifactName string `json:"artifactName"`
PlainHTTP bool `json:"plainHTTP"`
}

func TestDownload(t *testing.T) {
Expand Down Expand Up @@ -170,7 +172,7 @@ func startOCIMockServer(t *testing.T, tname string, test testFile) string {

// serve the manifest.
if strings.Contains(r.URL.Path, "/manifests/") {
w.Header().Add("Content-Type", "application/vnd.oci.image.manifest.v1+json")
w.Header().Add("Content-Type", cmp.Or(test.ManifestMediaType, "application/vnd.oci.image.manifest.v1+json"))
_, _ = w.Write([]byte(test.Manifest))
return
}
Expand Down
3 changes: 3 additions & 0 deletions internal/oci/testdata/bad-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
manifest: I am most definitely not an OCI manifest!
error: >-
failed to fetch successors: invalid character 'I' looking for beginning of value
3 changes: 2 additions & 1 deletion internal/oci/testdata/no-artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ manifest: |
}
artifacts:
44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a: "{}"
error: no artifacts found
error: >-
failed to find artifact: no artifact descriptors found
6 changes: 6 additions & 0 deletions internal/oci/testdata/not-a-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
manifest: I am most definitely not an OCI manifest!
manifestMediaType: text/plain
# TODO: This seems like a misleading error message.
# Maybe add an extra check in the code for "this is not a manifest"?
error: >-
failed to find artifact: no artifact descriptors found

0 comments on commit f4ae108

Please sign in to comment.