Skip to content

Commit

Permalink
Merge pull request #60 from buildpacks/jab/add-labels-to-interface
Browse files Browse the repository at this point in the history
Add #Labels to imgutil.Image
  • Loading branch information
ekcasey authored Aug 28, 2020
2 parents 162b2c7 + 0701952 commit 108a5c5
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
9 changes: 8 additions & 1 deletion fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func NewImage(name, topLayerSha string, identifier imgutil.Identifier) *Image {
return &Image{
labels: map[string]string{},
labels: nil,
env: map[string]string{},
topLayerSha: topLayerSha,
identifier: identifier,
Expand Down Expand Up @@ -67,6 +67,10 @@ func (i *Image) Label(key string) (string, error) {
return i.labels[key], nil
}

func (i *Image) Labels() (map[string]string, error) {
return i.labels, nil
}

func (i *Image) OS() (string, error) {
return i.os, nil
}
Expand Down Expand Up @@ -97,6 +101,9 @@ func (i *Image) Rebase(baseTopLayer string, newBase imgutil.Image) error {
}

func (i *Image) SetLabel(k string, v string) error {
if i.labels == nil {
i.labels = map[string]string{}
}
i.labels[k] = v
return nil
}
Expand Down
1 change: 1 addition & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Image interface {
Name() string
Rename(name string)
Label(string) (string, error)
Labels() (map[string]string, error)
SetLabel(string, string) error
Env(key string) (string, error)
SetEnv(string, string) error
Expand Down
4 changes: 4 additions & 0 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (i *Image) Label(key string) (string, error) {
return labels[key], nil
}

func (i *Image) Labels() (map[string]string, error) {
return i.inspect.Config.Labels, nil
}

func (i *Image) Env(key string) (string, error) {
for _, envVar := range i.inspect.Config.Env {
parts := strings.Split(envVar, "=")
Expand Down
63 changes: 63 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,69 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when("#Labels", func() {
when("image exists with labels", func() {
var repoName = newTestImageName()

it.Before(func() {
existingImage, err := local.NewImage(repoName, dockerClient)
h.AssertNil(t, err)

h.AssertNil(t, existingImage.SetLabel("mykey", "myvalue"))
h.AssertNil(t, existingImage.SetLabel("other", "data"))
h.AssertNil(t, existingImage.Save())
})

it.After(func() {
h.AssertNil(t, h.DockerRmi(dockerClient, repoName))
})

it("returns all the labels", func() {
img, err := local.NewImage(repoName, dockerClient, local.FromBaseImage(repoName))
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, labels["mykey"], "myvalue")
h.AssertEq(t, labels["other"], "data")
})
})

when("image exists with no labels", func() {
var repoName = newTestImageName()

it.Before(func() {
existingImage, err := local.NewImage(repoName, dockerClient)
h.AssertNil(t, err)
h.AssertNil(t, existingImage.Save())
})

it.After(func() {
h.AssertNil(t, h.DockerRmi(dockerClient, repoName))
})

it("returns nil", func() {
img, err := local.NewImage(repoName, dockerClient, local.FromBaseImage(repoName))
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, 0, len(labels))
})
})

when("image NOT exists", func() {
it("returns an empty map", func() {
img, err := local.NewImage(newTestImageName(), dockerClient)
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, 0, len(labels))
})
})
})

when("#Label", func() {
when("image exists", func() {
var repoName = newTestImageName()
Expand Down
8 changes: 8 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func (i *Image) Label(key string) (string, error) {
return labels[key], nil
}

func (i *Image) Labels() (map[string]string, error) {
cfg, err := i.image.ConfigFile()
if err != nil || cfg == nil {
return nil, fmt.Errorf("failed to get config file for image '%s'", i.repoName)
}
return cfg.Config.Labels, nil
}

func (i *Image) Env(key string) (string, error) {
cfg, err := i.image.ConfigFile()
if err != nil || cfg == nil {
Expand Down
55 changes: 55 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,61 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when("#Labels", func() {
when("image exists with labels", func() {
var repoName = newTestImageName()

it.Before(func() {
baseImage, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)

h.AssertNil(t, baseImage.SetLabel("mykey", "myvalue"))
h.AssertNil(t, baseImage.SetLabel("other", "data"))
h.AssertNil(t, baseImage.Save())
})

it("returns all the labels", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(repoName))
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, labels["mykey"], "myvalue")
h.AssertEq(t, labels["other"], "data")
})
})

when("image exists with no labels", func() {
var repoName = newTestImageName()

it.Before(func() {
baseImage, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.Save())
})

it("returns nil", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(repoName))
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, 0, len(labels))
})
})

when("image NOT exists", func() {
it("returns nil", func() {
img, err := remote.NewImage(newTestImageName(), authn.DefaultKeychain)
h.AssertNil(t, err)

labels, err := img.Labels()
h.AssertNil(t, err)
h.AssertEq(t, 0, len(labels))
})
})
})

when("#Label", func() {
when("image exists", func() {
it.Before(func() {
Expand Down

0 comments on commit 108a5c5

Please sign in to comment.