Skip to content

Commit ed6f399

Browse files
Merge pull request #10193 from rhatdan/runlabel
Fix handling of runlabel IMAGE and NAME
2 parents db48da4 + f8846bd commit ed6f399

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

pkg/domain/infra/abi/containers_runlabel.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,28 @@ func generateRunlabelCommand(runlabel string, img *libimage.Image, inputName str
180180
}
181181

182182
func replaceName(arg, name string) string {
183+
if arg == "NAME" {
184+
return name
185+
}
186+
183187
newarg := strings.ReplaceAll(arg, "$NAME", name)
184-
return strings.ReplaceAll(newarg, "${NAME}", name)
188+
newarg = strings.ReplaceAll(newarg, "${NAME}", name)
189+
if strings.HasSuffix(newarg, "=NAME") {
190+
newarg = strings.ReplaceAll(newarg, "=NAME", fmt.Sprintf("=%s", name))
191+
}
192+
return newarg
185193
}
186194

187195
func replaceImage(arg, image string) string {
196+
if arg == "IMAGE" {
197+
return image
198+
}
188199
newarg := strings.ReplaceAll(arg, "$IMAGE", image)
189-
return strings.ReplaceAll(newarg, "${IMAGE}", image)
200+
newarg = strings.ReplaceAll(newarg, "${IMAGE}", image)
201+
if strings.HasSuffix(newarg, "=IMAGE") {
202+
newarg = strings.ReplaceAll(newarg, "=IMAGE", fmt.Sprintf("=%s", image))
203+
}
204+
return newarg
190205
}
191206

192207
// generateCommand takes a label (string) and converts it to an executable command
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package abi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReplaceName(t *testing.T) {
10+
tests := [][]string{
11+
{"NAME=$NAME", "test1", "NAME=test1"},
12+
{"NAME=${NAME}", "test2", "NAME=test2"},
13+
{"NAME=NAME", "test3", "NAME=test3"},
14+
{"NAME=NAMEFOO", "test3", "NAME=NAMEFOO"},
15+
{"NAME", "test4", "test4"},
16+
{"FNAME", "test5", "FNAME"},
17+
{"NAME=foo", "test6", "NAME=foo"},
18+
{"This is my NAME", "test7", "This is my NAME"},
19+
}
20+
for _, args := range tests {
21+
val := replaceName(args[0], args[1])
22+
assert.Equal(t, val, args[2])
23+
}
24+
}
25+
26+
func TestReplaceImage(t *testing.T) {
27+
tests := [][]string{
28+
{"IMAGE=$IMAGE", "test1", "IMAGE=test1"},
29+
{"IMAGE=${IMAGE}", "test2", "IMAGE=test2"},
30+
{"IMAGE=IMAGE", "test3", "IMAGE=test3"},
31+
{"IMAGE=IMAGEFOO", "test3", "IMAGE=IMAGEFOO"},
32+
{"IMAGE", "test4", "test4"},
33+
{"FIMAGE", "test5", "FIMAGE"},
34+
{"IMAGE=foo", "test6", "IMAGE=foo"},
35+
}
36+
for _, args := range tests {
37+
val := replaceImage(args[0], args[1])
38+
assert.Equal(t, val, args[2])
39+
}
40+
}

0 commit comments

Comments
 (0)