Skip to content

fix(verify): use container name from configuration in verify tests #9753

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

Merged
merged 2 commits into from
Mar 19, 2025
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: 15 additions & 4 deletions pkg/skaffold/verify/docker/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"math"
"path"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -49,6 +50,8 @@ import (
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
)

var validContainerNameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]*$`)

// Verifier verifies deployments using Docker libs/CLI.
type Verifier struct {
logger log.Logger
Expand Down Expand Up @@ -221,7 +224,10 @@ func (v *Verifier) createAndRunContainer(ctx context.Context, out io.Writer, art
if len(tc.Container.Args) != 0 {
containerCfg.Cmd = tc.Container.Args
}
containerName := v.getContainerName(ctx, artifact.ImageName)

// Use container name from test case if available, otherwise derive from image
containerName := v.getContainerName(ctx, artifact.ImageName, tc.Container.Name)

opts := dockerutil.ContainerCreateOpts{
Name: containerName,
Network: v.network,
Expand Down Expand Up @@ -301,9 +307,14 @@ func (v *Verifier) containerConfigFromImage(ctx context.Context, taggedImage str
return config.Config, err
}

func (v *Verifier) getContainerName(ctx context.Context, name string) string {
// this is done to fix the for naming convention of non-skaffold built images which verify supports
name = path.Base(strings.Split(name, ":")[0])
func (v *Verifier) getContainerName(ctx context.Context, imageName string, containerName string) string {
name := containerName

// If no container name is provided, derive it from the image name
if name == "" || !validContainerNameRegex.MatchString(name) {
name = path.Base(strings.Split(imageName, ":")[0])
}

currentName := name

for {
Expand Down
47 changes: 47 additions & 0 deletions pkg/skaffold/verify/docker/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,50 @@ func Test_UseLocalImages(t *testing.T) {
t.CheckDeepEqual(expectedPullImgs, fDockerDaemon.PulledImages)
})
}

func TestGetContainerName(t *testing.T) {
ctx := context.TODO()

tests := []struct {
description string
imageName string
containerName string
expected string
}{
{
description: "container name specified",
imageName: "gcr.io/cloud-builders/gcloud",
containerName: "custom-container",
expected: "custom-container",
},
{
description: "invalid container name specified",
imageName: "gcr.io/cloud-builders/gcloud",
containerName: "gcr.io/cloud-builders/gcloud",
expected: "gcloud",
},
{
description: "container name not specified",
imageName: "gcr.io/cloud-builders/gcloud",
containerName: "",
expected: "gcloud",
},
}

fakeDockerDaemon := &fakeDockerDaemon{
LocalDaemon: docker.NewLocalDaemon(&testutil.FakeAPIClient{}, nil, false, nil),
}

verifier := &Verifier{
client: fakeDockerDaemon,
}

for _, test := range tests {
testutil.Run(
t, test.description, func(t *testutil.T) {
actual := verifier.getContainerName(ctx, test.imageName, test.containerName)
t.CheckDeepEqual(test.expected, actual)
},
)
}
}
Loading