Skip to content

Commit

Permalink
Add more test coverage for manifest/
Browse files Browse the repository at this point in the history
  • Loading branch information
tianon committed Nov 18, 2022
1 parent 20b5a50 commit 419da33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions manifest/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ GitCommit: d7e2a8d90a9b8f5dfd5bcd428e0c33b68c40cc19
Directory: 1.5
File: Dockerfile.alpine
s390x-File: Dockerfile.alpine.s390x.bad-boy
Builder: buildkit
GitFetch: refs/heads/having-a-good-time
SharedTags: raspbian
GitCommit: deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
Expand Down Expand Up @@ -105,9 +107,11 @@ s390x-File: Dockerfile
// s390x-GitCommit: b6c460e7cd79b595267870a98013ec3078b490df
//
// Tags: 1.5-alpine
// GitFetch: refs/heads/having-a-good-time
// GitCommit: d7e2a8d90a9b8f5dfd5bcd428e0c33b68c40cc19
// Directory: 1.5
// File: Dockerfile.alpine
// Builder: buildkit
// s390x-File: Dockerfile.alpine.s390x.bad-boy
//
// Tags: raspbian-s390x
Expand Down
2 changes: 1 addition & 1 deletion manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Fetch(library, repo string) (string, string, *Manifest2822, error) {

// try file paths
filePaths := []string{}
if filepath.IsAbs(repo) || strings.IndexRune(repo, filepath.Separator) >= 0 || strings.IndexRune(repo, '/') >= 0 {
if filepath.IsAbs(repo) || strings.ContainsRune(repo, filepath.Separator) || strings.ContainsRune(repo, '/') {
filePaths = append(filePaths, repo)
}
if !filepath.IsAbs(repo) {
Expand Down
39 changes: 38 additions & 1 deletion manifest/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package manifest_test

import (
"errors"
"strings"
"testing"

"github.com/docker-library/bashbrew/manifest"
)

func TestFetchErrors(t *testing.T) {
repoName, tagName, _, err := manifest.Fetch("testdata", "bash:69.420")
repoName, tagName, _, err := manifest.Fetch("/dev/null", "testdata/bash:69.420")
if err == nil {
t.Fatalf("expected tag-not-found error, got repoName=%q, tagName=%q instead", repoName, tagName)
}
Expand All @@ -27,4 +28,40 @@ func TestFetchErrors(t *testing.T) {
t.Fatalf("expected manifest-not-found error, got %q instead", err)
}
t.Logf("correct, expected error: %s", err)

repoName, tagName, _, err = manifest.Fetch("/dev/null", "/proc/kmsg")
if err == nil {
t.Fatalf("expected filesystem error, got repoName=%q, tagName=%q instead", repoName, tagName)
}
if !strings.Contains(err.Error(), "permission denied") && !strings.Contains(err.Error(), "not permitted") {
t.Fatalf("expected filesystem error, got %q instead", err)
}
t.Logf("correct, expected error: %s", err)

repoName, tagName, _, err = manifest.Fetch("/dev/null", "./testdata")
if err == nil {
t.Fatalf("expected directory error, got repoName=%q, tagName=%q instead", repoName, tagName)
}
if !strings.Contains(err.Error(), "is a directory") {
t.Fatalf("expected directory error, got %q instead", err)
}
t.Logf("correct, expected error: %s", err)

repoName, tagName, _, err = manifest.Fetch("/dev/null", "https://nonexistent.subdomain.example.com/nonexistent-project:1.2.3")
if err == nil {
t.Fatalf("expected no such host error, got repoName=%q, tagName=%q instead", repoName, tagName)
}
if !strings.Contains(err.Error(), "no such host") {
t.Fatalf("expected no such host error, got %q instead", err)
}
t.Logf("correct, expected error: %s", err)

repoName, tagName, _, err = manifest.Fetch("/dev/null", "https://example.com:1.2.3")
if err == nil {
t.Fatalf("expected parse error, got repoName=%q, tagName=%q instead", repoName, tagName)
}
if !strings.HasPrefix(err.Error(), "Bad line:") {
t.Fatalf("expected parse error, got %q instead", err)
}
t.Logf("correct, expected error: %s", err)
}

0 comments on commit 419da33

Please sign in to comment.