Skip to content

Commit a8d90e2

Browse files
committed
fix(verify): use container name from configuration in verify tests
Signed-off-by: Suleiman Dibirov <[email protected]>
1 parent d5f6acc commit a8d90e2

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

pkg/skaffold/verify/docker/verify.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ func (v *Verifier) createAndRunContainer(ctx context.Context, out io.Writer, art
221221
if len(tc.Container.Args) != 0 {
222222
containerCfg.Cmd = tc.Container.Args
223223
}
224-
containerName := v.getContainerName(ctx, artifact.ImageName)
224+
225+
// Use container name from test case if available, otherwise derive from image
226+
containerName := v.getContainerName(ctx, artifact.ImageName, tc.Container.Name)
227+
225228
opts := dockerutil.ContainerCreateOpts{
226229
Name: containerName,
227230
Network: v.network,
@@ -301,9 +304,14 @@ func (v *Verifier) containerConfigFromImage(ctx context.Context, taggedImage str
301304
return config.Config, err
302305
}
303306

304-
func (v *Verifier) getContainerName(ctx context.Context, name string) string {
305-
// this is done to fix the for naming convention of non-skaffold built images which verify supports
306-
name = path.Base(strings.Split(name, ":")[0])
307+
func (v *Verifier) getContainerName(ctx context.Context, imageName string, containerName string) string {
308+
name := containerName
309+
310+
// If no container name is provided, derive it from the image name
311+
if name == "" {
312+
name = path.Base(strings.Split(imageName, ":")[0])
313+
}
314+
307315
currentName := name
308316

309317
for {

pkg/skaffold/verify/docker/verify_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,44 @@ func Test_UseLocalImages(t *testing.T) {
149149
t.CheckDeepEqual(expectedPullImgs, fDockerDaemon.PulledImages)
150150
})
151151
}
152+
153+
func TestGetContainerName(t *testing.T) {
154+
ctx := context.TODO()
155+
156+
tests := []struct {
157+
description string
158+
imageName string
159+
containerName string
160+
expected string
161+
}{
162+
{
163+
description: "container name specified",
164+
imageName: "gcr.io/cloud-builders/gcloud",
165+
containerName: "custom-container",
166+
expected: "custom-container",
167+
},
168+
{
169+
description: "container name not specified",
170+
imageName: "gcr.io/cloud-builders/gcloud",
171+
containerName: "",
172+
expected: "gcloud",
173+
},
174+
}
175+
176+
fakeDockerDaemon := &fakeDockerDaemon{
177+
LocalDaemon: docker.NewLocalDaemon(&testutil.FakeAPIClient{}, nil, false, nil),
178+
}
179+
180+
verifier := &Verifier{
181+
client: fakeDockerDaemon,
182+
}
183+
184+
for _, test := range tests {
185+
testutil.Run(
186+
t, test.description, func(t *testutil.T) {
187+
actual := verifier.getContainerName(ctx, test.imageName, test.containerName)
188+
t.CheckDeepEqual(test.expected, actual)
189+
},
190+
)
191+
}
192+
}

0 commit comments

Comments
 (0)