Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of runlabel IMAGE and NAME #10193

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pkg/domain/infra/abi/containers_runlabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,28 @@ func generateRunlabelCommand(runlabel string, img *libimage.Image, inputName str
}

func replaceName(arg, name string) string {
if arg == "NAME" {
return name
}

newarg := strings.ReplaceAll(arg, "$NAME", name)
return strings.ReplaceAll(newarg, "${NAME}", name)
newarg = strings.ReplaceAll(newarg, "${NAME}", name)
if strings.HasSuffix(newarg, "=NAME") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a trailing space after NAME here? If so, we probably want to trim it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No these are arguments, only if the user wrapped something with "foo=NAME " would that happen, and in that case I think we should ignore.

newarg = strings.ReplaceAll(newarg, "=NAME", fmt.Sprintf("=%s", name))
}
return newarg
}

func replaceImage(arg, image string) string {
if arg == "IMAGE" {
return image
}
newarg := strings.ReplaceAll(arg, "$IMAGE", image)
return strings.ReplaceAll(newarg, "${IMAGE}", image)
newarg = strings.ReplaceAll(newarg, "${IMAGE}", image)
if strings.HasSuffix(newarg, "=IMAGE") {
newarg = strings.ReplaceAll(newarg, "=IMAGE", fmt.Sprintf("=%s", image))
}
return newarg
}

// generateCommand takes a label (string) and converts it to an executable command
Expand Down
40 changes: 40 additions & 0 deletions pkg/domain/infra/abi/containers_runlabel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package abi

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReplaceName(t *testing.T) {
tests := [][]string{
{"NAME=$NAME", "test1", "NAME=test1"},
{"NAME=${NAME}", "test2", "NAME=test2"},
{"NAME=NAME", "test3", "NAME=test3"},
{"NAME=NAMEFOO", "test3", "NAME=NAMEFOO"},
{"NAME", "test4", "test4"},
{"FNAME", "test5", "FNAME"},
{"NAME=foo", "test6", "NAME=foo"},
{"This is my NAME", "test7", "This is my NAME"},
}
for _, args := range tests {
val := replaceName(args[0], args[1])
assert.Equal(t, val, args[2])
}
}

func TestReplaceImage(t *testing.T) {
tests := [][]string{
{"IMAGE=$IMAGE", "test1", "IMAGE=test1"},
{"IMAGE=${IMAGE}", "test2", "IMAGE=test2"},
{"IMAGE=IMAGE", "test3", "IMAGE=test3"},
{"IMAGE=IMAGEFOO", "test3", "IMAGE=IMAGEFOO"},
{"IMAGE", "test4", "test4"},
{"FIMAGE", "test5", "FIMAGE"},
{"IMAGE=foo", "test6", "IMAGE=foo"},
}
for _, args := range tests {
val := replaceImage(args[0], args[1])
assert.Equal(t, val, args[2])
}
}