Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyrickard committed Oct 8, 2019
1 parent c780a56 commit 7157b49
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/config/relocation.go → pkg/manifest/relocation.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package manifest

// RelocationMapping represents a runtime provided mapping of a bundle image to a new tag
type RelocationMapping map[string]string
6 changes: 6 additions & 0 deletions pkg/manifest/runtime-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (m *RuntimeManifest) ResolveImages(bun *bundle.Bundle, reloMap RelocationMa
if err != nil {
return errors.Wrap(err, "unable to update image map from bundle.json")
}
m.ImageMap[alias] = manifestImage
}

for alias, reloRef := range reloMap {
Expand All @@ -326,6 +327,7 @@ func (m *RuntimeManifest) ResolveImages(bun *bundle.Bundle, reloMap RelocationMa
if err != nil {
return errors.Wrap(err, "unable to update image map from relocation mapping")
}
m.ImageMap[alias] = manifestImage
}
return nil
}
Expand All @@ -338,8 +340,12 @@ func resolveImage(image *MappedImage, refString string) error {
}
switch v := ref.(type) {
case reference.Canonical:
if tagged, ok := ref.(reference.NamedTagged); ok {
image.Tag = tagged.Tag()
}
image.Repository = v.Name()
image.Digest = v.Digest().String()

case reference.NamedTagged:
image.Tag = v.Tag()
image.Repository = v.Name()
Expand Down
131 changes: 130 additions & 1 deletion pkg/manifest/runtime-manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,141 @@ func TestResolveImage(t *testing.T) {
Tag: "latest",
},
},
{
name: "the one with a hostname",
reference: "deislabs.io/deislabs/porter-hello",
want: MappedImage{
Repository: "deislabs.io/deislabs/porter-hello",
Tag: "latest",
},
},
{
name: "the one with a hostname and port",
reference: "deislabs.io:9090/deislabs/porter-hello:foo",
want: MappedImage{
Repository: "deislabs.io:9090/deislabs/porter-hello",
Tag: "foo",
},
},
{

name: "tagged and digested",
reference: "deislabs/porter-hello:latest@sha256:8b06c3da72dc9fa7002b9bc1f73a7421b4287c9cf0d3b08633287473707f9a63",
want: MappedImage{
Repository: "deislabs/porter-hello",
Tag: "latest",
Digest: "sha256:8b06c3da72dc9fa7002b9bc1f73a7421b4287c9cf0d3b08633287473707f9a63",
},
},
}
for _, test := range tests {
got := &MappedImage{}
resolveImage(got, test.reference)
err := resolveImage(got, test.reference)
assert.NoError(t, err)
assert.Equal(t, test.want.Repository, got.Repository)
assert.Equal(t, test.want.Tag, got.Tag)
assert.Equal(t, test.want.Digest, got.Digest)
}
}

func TestResolveImageErrors(t *testing.T) {
tests := []struct {
name string
reference string
want string
}{
{
name: "no algo digest",
reference: "deislabs/porter-hello@8b06c3da72dc9fa7002b9bc1f73a7421b4287c9cf0d3b08633287473707f9a63",
want: "unable to parse docker image %s: invalid reference format",
},
{
name: "bad digest",
reference: "deislabs/porter-hello@sha256:8b06c3da72dc9fa7002b9bc1f73a7421b4287c9cf0d3b08633287473707f",
want: "unable to parse docker image %s: invalid checksum digest length",
},
{
name: "bad digest algo",
reference: "deislabs/porter-hello@sha356:8b06c3da72dc9fa7002b9bc1f73a7421b4287c9cf0d3b08633287473707f9a63",
want: "unable to parse docker image %s: unsupported digest algorithm",
},
{
name: "malformed tagged ref",
reference: "deislabs/porter-hello@latest",
want: "unable to parse docker image %s: invalid reference format",
},
{
name: "too many ports tagged ref",
reference: "deislabs:8080:8080/porter-hello:latest",
want: "unable to parse docker image %s: invalid reference format",
},
}
for _, test := range tests {
got := &MappedImage{}
err := resolveImage(got, test.reference)
assert.EqualError(t, err, fmt.Sprintf(test.want, test.reference))
}
}

func TestResolveImageWithUpdatedBundle(t *testing.T) {
cxt := context.NewTestContext(t)
m := &Manifest{
ImageMap: map[string]MappedImage{
"machine": MappedImage{
Repository: "deislabs/ghost",
Tag: "latest",
Digest: "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041",
},
},
}

img := bundle.Image{}
img.Image = "blah/ghost:latest"
img.Digest = "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041"
bun := &bundle.Bundle{
Images: map[string]bundle.Image{
"machine": img,
},
}

reloMap := RelocationMapping{}

rm := NewRuntimeManifest(cxt.Context, ActionInstall, m)
err := rm.ResolveImages(bun, reloMap)
assert.NoError(t, err)
mi := rm.ImageMap["machine"]
assert.Equal(t, "blah/ghost", mi.Repository)

}
func TestResolveImageWithRelo(t *testing.T) {
cxt := context.NewTestContext(t)
m := &Manifest{
ImageMap: map[string]MappedImage{
"machine": MappedImage{
Repository: "deislabs/ghost",
Tag: "latest",
Digest: "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041",
},
},
}

img := bundle.Image{}
img.Image = "deislabs/ghost:latest"
img.Digest = "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041"
bun := &bundle.Bundle{
Images: map[string]bundle.Image{
"machine": img,
},
}

reloMap := RelocationMapping{
"machine": "cnabio/ghost:latest",
}

rm := NewRuntimeManifest(cxt.Context, ActionInstall, m)
err := rm.ResolveImages(bun, reloMap)
assert.NoError(t, err)
mi := rm.ImageMap["machine"]
assert.Equal(t, "cnabio/ghost", mi.Repository)

}
2 changes: 1 addition & 1 deletion pkg/porter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (p *Porter) Run(opts RunOptions) error {
if err != nil {
return errors.Wrap(err, "couldn't load runtime bundle.json")
}
var reloMap config.RelocationMapping
var reloMap manifest.RelocationMapping
if _, err := p.FileSystem.Stat("/cnab/app/relocation-mapping.json"); err == nil {
reloBytes, err := p.FileSystem.ReadFile("/cnab/app/relocation-mapping.json")
if err != nil {
Expand Down

0 comments on commit 7157b49

Please sign in to comment.