Skip to content

Commit

Permalink
fix: wrong test encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurcgc committed Nov 22, 2023
1 parent dfab1ed commit 25fac64
Showing 1 changed file with 54 additions and 57 deletions.
111 changes: 54 additions & 57 deletions pkg/build/buildkit/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,62 +509,6 @@ ENV MY_ANOTHER_VAR="another var"
_, nerr := dc.ImageRemove(context.TODO(), destImage, dockertypes.ImageRemoveOptions{Force: true})
require.NoError(t, nerr)
}()

t.Run("Job build with Dockerfile", func(t *testing.T) {
destImage := baseRegistry(t, "my-job", "")

dockerfile := `FROM busybox:latest
RUN --mount=type=secret,id=tsuru-job-envvars,target=/var/run/secrets/envs.sh \
. /var/run/secrets/envs.sh \
&& echo ${MY_ENV_VAR} > /tmp/envs \
&& echo ${DATABASE_PASSWORD} >> /tmp/envs
ENV MY_ANOTHER_VAR="another var"
`

req := &pb.BuildRequest{
Kind: pb.BuildKind_BUILD_KIND_APP_BUILD_WITH_CONTAINER_FILE,
Job: &pb.TsuruJob{
Name: "my-job",
EnvVars: map[string]string{
"MY_ENV_VAR": "hello world",
"DATABASE_PASSWORD": "aw3some`p4ss!",
},
},
DestinationImages: []string{destImage},
Containerfile: string(dockerfile),
PushOptions: &pb.PushOptions{
InsecureRegistry: registryHTTP,
},
}

jobFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).Build(context.TODO(), req, os.Stdout)
require.NoError(t, err)
assert.Equal(t, &pb.TsuruConfig{
ImageConfig: &pb.ContainerImageConfig{
Cmd: []string{"sh"},
},
}, jobFiles)

dc := newDockerClient(t)
defer dc.Close()

r, err := dc.ImagePull(context.TODO(), destImage, dockertypes.ImagePullOptions{})
require.NoError(t, err)
defer r.Close()

fmt.Println("Pulling container image", destImage)
_, err = io.Copy(os.Stdout, r)
require.NoError(t, err)

defer func() {
fmt.Printf("Removing container image %s\n", destImage)
_, nerr := dc.ImageRemove(context.TODO(), destImage, dockertypes.ImageRemoveOptions{Force: true})
require.NoError(t, nerr)
}()
})

t.Run("should not store the env vars in the container image manifest", func(t *testing.T) {
is, _, err := dc.ImageInspectWithRaw(context.TODO(), destImage)
require.NoError(t, err)
Expand All @@ -579,7 +523,6 @@ ENV MY_ANOTHER_VAR="another var"
}
}
})

t.Run("should be able to see env vars during the build", func(t *testing.T) {
containerCreateResp, err := dc.ContainerCreate(context.TODO(), &dockertypescontainer.Config{
Image: destImage,
Expand Down Expand Up @@ -623,6 +566,60 @@ ENV MY_ANOTHER_VAR="another var"
assert.Empty(t, stderr.String())
})
})
t.Run("Job build with Dockerfile", func(t *testing.T) {
destImage := baseRegistry(t, "my-job", "")

dockerfile := `FROM busybox:latest
RUN --mount=type=secret,id=tsuru-job-envvars,target=/var/run/secrets/envs.sh \
. /var/run/secrets/envs.sh \
&& echo ${MY_ENV_VAR} > /tmp/envs \
&& echo ${DATABASE_PASSWORD} >> /tmp/envs
ENV MY_ANOTHER_VAR="another var"
`

req := &pb.BuildRequest{
Kind: pb.BuildKind_BUILD_KIND_APP_BUILD_WITH_CONTAINER_FILE,
Job: &pb.TsuruJob{
Name: "my-job",
EnvVars: map[string]string{
"MY_ENV_VAR": "hello world",
"DATABASE_PASSWORD": "aw3some`p4ss!",
},
},
DestinationImages: []string{destImage},
Containerfile: string(dockerfile),
PushOptions: &pb.PushOptions{
InsecureRegistry: registryHTTP,
},
}

jobFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).Build(context.TODO(), req, os.Stdout)
require.NoError(t, err)
assert.Equal(t, &pb.TsuruConfig{
ImageConfig: &pb.ContainerImageConfig{
Cmd: []string{"sh"},
},
}, jobFiles)

dc := newDockerClient(t)
defer dc.Close()

r, err := dc.ImagePull(context.TODO(), destImage, dockertypes.ImagePullOptions{})
require.NoError(t, err)
defer r.Close()

fmt.Println("Pulling container image", destImage)
_, err = io.Copy(os.Stdout, r)
require.NoError(t, err)

defer func() {
fmt.Printf("Removing container image %s\n", destImage)
_, nerr := dc.ImageRemove(context.TODO(), destImage, dockertypes.ImageRemoveOptions{Force: true})
require.NoError(t, nerr)
}()
})

t.Run("neither Procfile nor tsuru.yaml, should use command from image manifest", func(t *testing.T) {
destImage := baseRegistry(t, "my-app", "")
Expand Down

0 comments on commit 25fac64

Please sign in to comment.