Skip to content

Commit

Permalink
Fix docker tests (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva authored Jan 16, 2022
1 parent 2270655 commit c10d0fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
35 changes: 26 additions & 9 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/jfrog/jfrog-cli-core/v2/xray/commands/scan"

Expand All @@ -16,7 +17,6 @@ import (

"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
clientutils "github.com/jfrog/jfrog-client-go/utils"

gofrogcmd "github.com/jfrog/gofrog/io"
corecontainer "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/container"
Expand All @@ -25,6 +25,7 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -164,6 +165,8 @@ func TestRunPushFatManifestImage(t *testing.T) {
// Setup test env.
workspace, err := filepath.Abs(tests.Out)
assert.NoError(t, err)
assert.NoError(t, fileutils.CreateDirIfNotExist(workspace))

// Build the builder image locally.
builderImageName, err = inttestutils.BuildTestImage(builderImageName, "Dockerfile.Buildx.Fatmanifest", container.DockerClient)
assert.NoError(t, err)
Expand All @@ -174,24 +177,25 @@ func TestRunPushFatManifestImage(t *testing.T) {
assert.NoError(t, gofrogcmd.RunCmd(runCmd))
defer inttestutils.DeleteTestcontainer(t, builderContainerName, container.DockerClient)

// Docker daemon may be lost for the first few seconds, perform 3 retries before failure.
require.True(t, isDaemonRunning(builderContainerName), "docker daemon is not responding in remote container")

// Configure buildx in remote container
execCmd := inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "sh", "script.sh")
require.NoError(t, gofrogcmd.RunCmd(execCmd))

// login to the Artifactory within the container
password := *tests.JfrogPassword
if *tests.JfrogAccessToken != "" {
password = *tests.JfrogAccessToken
}
execCmd := inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "docker", "login", *tests.DockerRepoDomain, "--username", *tests.JfrogUser, "--password", password)
execCmd = inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "docker", "login", *tests.DockerRepoDomain, "--username", *tests.JfrogUser, "--password", password)
err = gofrogcmd.RunCmd(execCmd)
require.NoError(t, err, "fail to login to container registry")

// Build & push the multi platform image to Artifactory
execCmd = inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "/buildx", "build", "--platform", "linux/amd64,linux/arm64,linux/arm/v7", "--tag", path.Join(*tests.DockerRepoDomain, multiArchImageName+multiArchImageTag), "-f", "Dockerfile.Fatmanifest", "--metadata-file", "/workspace/"+buildxOutputFile, "--push", ".")
// Docker daemon may be lost for a few seconds, perform 3 retries before failure.
for i := 0; i < 3; i++ {
if err = gofrogcmd.RunCmd(execCmd); err == nil {
break
}
}
require.NoError(t, err, "failed creating and pushing multi platforms image")
require.NoError(t, gofrogcmd.RunCmd(execCmd))

// Run 'build-docker-create' & publish the results to Artifactory.
buildxOutput := filepath.Join(workspace, buildxOutputFile)
Expand Down Expand Up @@ -223,6 +227,19 @@ func TestRunPushFatManifestImage(t *testing.T) {
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, multiArchImageName, buildName, *tests.DockerLocalRepo)
}

// Check if Docker daemon is running on a given container
func isDaemonRunning(containerName string) bool {
execCmd := inttestutils.NewExecDockerImage(container.DockerClient, containerName, "docker", "ps")
for i := 0; i < 3; i++ {
if execCmd.GetCmd().Run() != nil {
time.Sleep(8 * time.Second)
} else {
return true
}
}
return false
}

func TestContainerPushBuildNameNumberFromEnv(t *testing.T) {
containerManagers := initContainerTest(t)
for _, containerManager := range containerManagers {
Expand Down
2 changes: 1 addition & 1 deletion inttestutils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func DeleteTestcontainer(t *testing.T, containerName string, containerManagerTyp
func ContainerTestCleanup(t *testing.T, serverDetails *config.ServerDetails, artHttpDetails httputils.HttpClientDetails, imageName, buildName, repo string) {
// Remove build from Artifactory
DeleteBuild(serverDetails.ArtifactoryUrl, buildName, artHttpDetails)

tests.CleanFileSystem()
// Remove image from Artifactory
deleteSpec := spec.NewBuilder().Pattern(path.Join(repo, imageName)).BuildSpec()
successCount, failCount, err := tests.DeleteFiles(deleteSpec, serverDetails)
Expand Down
5 changes: 1 addition & 4 deletions testdata/docker/Dockerfile.Buildx.Fatmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ COPY --from=docker/buildx-bin /buildx /buildx

FROM docker:dind
COPY --from=builder /buildx /buildx
RUN /buildx create --name mybuilder
RUN /buildx use mybuilder
RUN /buildx inspect --bootstrap
COPY Dockerfile.Fatmanifest .
COPY Dockerfile.Fatmanifest script.sh /
4 changes: 4 additions & 0 deletions testdata/docker/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
./buildx create --name mybuilder
./buildx use mybuilder
./buildx inspect --bootstrap

0 comments on commit c10d0fe

Please sign in to comment.