Skip to content

Commit

Permalink
fix: support running multiple verify tests with same image (GoogleCon…
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle authored Jul 14, 2022
1 parent 010a891 commit dfefaaa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
20 changes: 15 additions & 5 deletions integration/testdata/verify-succeed/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
apiVersion: skaffold/v3alpha1
kind: Config
verify:
- name: hello-world
- name: hello-world-1
container:
name: docker.io/hello-world:latest
image: hello-world
- name: alpine
name: hello-world-1
image: docker.io/hello-world:latest
- name: hello-world-2
container:
name: alpine
name: hello-world-2
image: docker.io/hello-world:latest
- name: alpine-1
container:
name: alpine-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo $FOO; sleep 10; echo bye"]
- name: alpine-2
container:
name: alpine-2
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo $FOO; sleep 10; echo bye"]
2 changes: 2 additions & 0 deletions integration/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func TestVerifyPassingTestsWithEnvVar(t *testing.T) {
testutil.CheckError(t, false, err)
testutil.CheckContains(t, "Hello from Docker!", logs)
testutil.CheckContains(t, "foo-var", logs)
testutil.CheckContains(t, "alpine-1", logs)
testutil.CheckContains(t, "alpine-2", logs)

// verify logs are in the event output as well
b, err := os.ReadFile(logFile + ".v2")
Expand Down
28 changes: 18 additions & 10 deletions pkg/skaffold/verify/docker/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/fatih/semgroup"
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
Expand Down Expand Up @@ -126,17 +127,20 @@ func (v *Verifier) Verify(ctx context.Context, out io.Writer, allbuilds []graph.
s := semgroup.NewGroup(context.Background(), maxWorkers)

for _, tc := range v.cfg {
var nb graph.Artifact
var na graph.Artifact
foundArtifact := false
testCase := tc
for _, b := range allbuilds {
if tc.Container.Image == b.ImageName {
foundArtifact = true
nb = graph.Artifact{
na = graph.Artifact{
ImageName: tc.Container.Image,
Tag: b.Tag,
}
builds = append(builds, nb)
builds = append(builds, graph.Artifact{
ImageName: tc.Container.Image,
Tag: tc.Name,
})
break
}
}
Expand All @@ -145,14 +149,17 @@ func (v *Verifier) Verify(ctx context.Context, out io.Writer, allbuilds []graph.
if err != nil {
return err
}
nb = graph.Artifact{
na = graph.Artifact{
ImageName: tc.Container.Image,
Tag: tc.Container.Image,
}
builds = append(builds, nb)
builds = append(builds, graph.Artifact{
ImageName: tc.Container.Image,
Tag: tc.Name,
})
}
s.Go(func() error {
return v.createAndRunContainer(ctx, out, nb, *testCase)
return v.createAndRunContainer(ctx, out, na, *testCase)
})
}
v.TrackBuildArtifacts(builds)
Expand Down Expand Up @@ -208,7 +215,10 @@ func (v *Verifier) createAndRunContainer(ctx context.Context, out io.Writer, art
eventV2.VerifyFailed(tc.Name, err)
return errors.Wrap(err, "creating container in local docker")
}
v.TrackContainerFromBuild(artifact, tracker.Container{Name: containerName, ID: id})
v.TrackContainerFromBuild(graph.Artifact{
ImageName: opts.VerifyTestName,
Tag: opts.VerifyTestName,
}, tracker.Container{Name: containerName, ID: id})

var containerErr error
select {
Expand Down Expand Up @@ -252,13 +262,11 @@ func (v *Verifier) getContainerName(ctx context.Context, name string) string {
name = path.Base(strings.Split(name, ":")[0])
currentName := name

counter := 1
for {
if !v.client.ContainerExists(ctx, currentName) {
break
}
currentName = fmt.Sprintf("%s-%d", name, counter)
counter++
currentName = fmt.Sprintf("%s-%s", name, uuid.New().String()[0:8])
}

if currentName != name {
Expand Down

0 comments on commit dfefaaa

Please sign in to comment.