Skip to content

Commit

Permalink
Merge pull request #61 from buildpacks/jab/remove-label-return-copy-l…
Browse files Browse the repository at this point in the history
…abels

Add #RemoveLabel and ensure #Labels returns a copy
  • Loading branch information
ekcasey authored Aug 31, 2020
2 parents 108a5c5 + 4882147 commit afd98bd
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
11 changes: 10 additions & 1 deletion fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ func (i *Image) Label(key string) (string, error) {
}

func (i *Image) Labels() (map[string]string, error) {
return i.labels, nil
copiedLabels := make(map[string]string)
for i, l := range i.labels {
copiedLabels[i] = l
}
return copiedLabels, nil
}

func (i *Image) OS() (string, error) {
Expand Down Expand Up @@ -108,6 +112,11 @@ func (i *Image) SetLabel(k string, v string) error {
return nil
}

func (i *Image) RemoveLabel(key string) error {
delete(i.labels, key)
return nil
}

func (i *Image) SetEnv(k string, v string) error {
i.env[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 @@ -32,6 +32,7 @@ type Image interface {
Label(string) (string, error)
Labels() (map[string]string, error)
SetLabel(string, string) error
RemoveLabel(string) error
Env(key string) (string, error)
SetEnv(string, string) error
SetEntrypoint(...string) error
Expand Down
11 changes: 10 additions & 1 deletion local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (i *Image) Label(key string) (string, error) {
}

func (i *Image) Labels() (map[string]string, error) {
return i.inspect.Config.Labels, nil
copiedLabels := make(map[string]string)
for i, l := range i.inspect.Config.Labels {
copiedLabels[i] = l
}
return copiedLabels, nil
}

func (i *Image) Env(key string) (string, error) {
Expand Down Expand Up @@ -238,6 +242,11 @@ func (i *Image) SetLabel(key, val string) error {
return nil
}

func (i *Image) RemoveLabel(key string) error {
delete(i.inspect.Config.Labels, key)
return nil
}

func (i *Image) SetEnv(key, val string) error {
ignoreCase := i.inspect.Os == "windows"
for idx, kv := range i.inspect.Config.Env {
Expand Down
54 changes: 54 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,60 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when("#RemoveLabel", func() {
var (
img imgutil.Image
repoName = newTestImageName()
baseImageName = newTestImageName()
)

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

when("image exists", func() {
it("removes matching label on img object", func() {
var err error

baseImage, err := local.NewImage(baseImageName, dockerClient)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.SetLabel("my.custom.label", "old-value"))
h.AssertNil(t, baseImage.Save())

img, err = local.NewImage(repoName, dockerClient, local.FromBaseImage(baseImageName))
h.AssertNil(t, err)

h.AssertNil(t, img.RemoveLabel("my.custom.label"))
h.AssertNil(t, img.Save())

labels, err := img.Labels()
h.AssertNil(t, err)
_, exists := labels["my.custom.label"]
h.AssertEq(t, exists, false)
})

it("saves removal of the label", func() {
var err error

baseImage, err := local.NewImage(baseImageName, dockerClient)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.SetLabel("my.custom.label", "old-value"))
h.AssertNil(t, baseImage.Save())

img, err = local.NewImage(repoName, dockerClient, local.FromBaseImage(baseImageName))
h.AssertNil(t, err)

h.AssertNil(t, img.RemoveLabel("my.custom.label"))
h.AssertNil(t, img.Save())

inspect, _, err := dockerClient.ImageInspectWithRaw(context.TODO(), repoName)
h.AssertNil(t, err)
_, exists := inspect.Config.Labels["my.custom.label"]
h.AssertEq(t, exists, false)
})
})
})

when("#SetEnv", func() {
var repoName = newTestImageName()
var skipCleanup bool
Expand Down
11 changes: 11 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ func (i *Image) SetLabel(key, val string) error {
return err
}

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

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

when("#RemoveLabel", func() {
when("image exists", func() {
var baseImageName = newTestImageName()

it.Before(func() {
baseImage, err := remote.NewImage(baseImageName, authn.DefaultKeychain)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.SetLabel("custom.label", "new-val"))
h.AssertNil(t, baseImage.Save())
})

it("removes label on img object", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(baseImageName))
h.AssertNil(t, err)

h.AssertNil(t, img.RemoveLabel("custom.label"))

labels, err := img.Labels()
h.AssertNil(t, err)
_, exists := labels["my.custom.label"]
h.AssertEq(t, exists, false)
})

it("saves removal of label", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(baseImageName))
h.AssertNil(t, err)

h.AssertNil(t, img.RemoveLabel("custom.label"))
h.AssertNil(t, img.Save())

testImg, err := remote.NewImage(
"test",
authn.DefaultKeychain,
remote.FromBaseImage(repoName),
)
h.AssertNil(t, err)

remoteLabel, err := testImg.Label("custom.label")
h.AssertNil(t, err)
h.AssertEq(t, remoteLabel, "")
})
})
})

when("#SetEnv", func() {
it("sets the environment", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain)
Expand Down

0 comments on commit afd98bd

Please sign in to comment.