increase test coverage and fix some minor bugs

This commit is contained in:
Simon Aronsson 2020-11-21 14:15:59 +01:00
parent 723d2e9488
commit 2b68874087
No known key found for this signature in database
GPG key ID: 8DA57A5FD341605B
4 changed files with 139 additions and 9 deletions

View file

@ -11,12 +11,9 @@ import (
// BuildManifestURL from raw image data
func BuildManifestURL(image apiTypes.ImageInspect) (string, error) {
parts := strings.Split(image.RepoTags[0], ":")
img := parts[0]
tag := parts[1]
img, tag := extractImageAndTag(image)
hostName, err := ref.ParseNormalizedNamed(img)
fmt.Println(hostName)
if err != nil {
return "", err
}
@ -25,7 +22,7 @@ func BuildManifestURL(image apiTypes.ImageInspect) (string, error) {
if err != nil {
return "", err
}
img = strings.TrimPrefix(img, host)
img = strings.TrimPrefix(img, fmt.Sprintf("%s/", host))
url := url2.URL{
Scheme: "https",
Host: host,
@ -33,3 +30,17 @@ func BuildManifestURL(image apiTypes.ImageInspect) (string, error) {
}
return url.String(), nil
}
func extractImageAndTag(image apiTypes.ImageInspect) (string, string) {
var img string
var tag string
if strings.Contains(image.RepoTags[0], ":") {
parts := strings.Split(image.RepoTags[0], ":")
img = parts[0]
tag = parts[1]
} else {
img = image.RepoTags[0]
tag = "latest"
}
return img, tag
}

View file

@ -0,0 +1,57 @@
package manifest_test
import (
"github.com/containrrr/watchtower/pkg/registry/manifest"
apiTypes "github.com/docker/docker/api/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestManifest(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manifest Suite")
}
var _ = Describe("the manifest module", func() {
When("building a manifest url", func() {
It("should return a valid url given a fully qualified image", func() {
expected := "https://ghcr.io/v2/containrrr/watchtower/manifests/latest"
imageInfo := apiTypes.ImageInspect{
RepoTags: []string {
"ghcr.io/containrrr/watchtower:latest",
},
}
res, err := manifest.BuildManifestURL(imageInfo)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(expected))
})
It("should assume dockerhub for non-qualified images", func() {
expected := "https://index.docker.io/v2/containrrr/watchtower/manifests/latest"
imageInfo := apiTypes.ImageInspect{
RepoTags: []string {
"containrrr/watchtower:latest",
},
}
res, err := manifest.BuildManifestURL(imageInfo)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(expected))
})
It("should assume latest for images that lack an explicit tag", func() {
expected := "https://index.docker.io/v2/containrrr/watchtower/manifests/latest"
imageInfo := apiTypes.ImageInspect{
RepoTags: []string {
"containrrr/watchtower",
},
}
res, err := manifest.BuildManifestURL(imageInfo)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(expected))
})
})
})