Skip to content

Commit

Permalink
Merge pull request #3838 from werf/e2e-build-test
Browse files Browse the repository at this point in the history
tests(e2e): test dockerfile build with all container runtimes
  • Loading branch information
alexey-igrychev committed Oct 19, 2021
2 parents 5da2426 + 7992243 commit 15f32a3
Show file tree
Hide file tree
Showing 35 changed files with 795 additions and 111 deletions.
Expand Up @@ -122,7 +122,7 @@ func NewContainerRegistryPerImplementationData(synchronizedSuiteCallbacksData *S
func setupOptionalLocalContainerRegistry(synchronizedSuiteCallbacksData *SynchronizedSuiteCallbacksData, data *ContainerRegistryPerImplementationData) {
implementationNameForWerf := "default"

localRegistryAddress, localRegistryContainerName := utilsDocker.LocalDockerRegistryRun()
localRegistryAddress, _, localRegistryContainerName := utilsDocker.LocalDockerRegistryRun()
registryAddress := localRegistryAddress

synchronizedSuiteCallbacksData.AppendSynchronizedAfterSuiteAllNodesFunc(func() {
Expand Down
1 change: 1 addition & 0 deletions integration/pkg/utils/docker/container_command.go
Expand Up @@ -44,6 +44,7 @@ func RunSucceedContainerCommandWithStapel(werfBinPath string, projectPath string
"--volumes-from",
container,
"--rm",
"--entrypoint=",
}

dockerOptions = append(dockerOptions, extraDockerOptions...)
Expand Down
13 changes: 9 additions & 4 deletions integration/pkg/utils/docker/docker_registry.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/werf/werf/integration/pkg/utils"
)

func LocalDockerRegistryRun() (string, string) {
func LocalDockerRegistryRun() (string, string, string) {
containerName := fmt.Sprintf("werf_test_docker_registry-%s", utils.GetRandomString(10))
imageName := "flant/werf-test:registry"

Expand All @@ -28,10 +28,15 @@ func LocalDockerRegistryRun() (string, string) {
err := CliRun(dockerCliRunArgs...)
Ω(err).ShouldNot(HaveOccurred(), "docker run "+strings.Join(dockerCliRunArgs, " "))

registry := fmt.Sprintf("localhost:%s", ContainerHostPort(containerName, "5000/tcp"))
registryWithScheme := fmt.Sprintf("http://%s", registry)
inspect := ContainerInspect(containerName)
Ω(inspect.NetworkSettings).ShouldNot(BeNil())
Ω(inspect.NetworkSettings.IPAddress).ShouldNot(BeEmpty())
registryInternalAddress := fmt.Sprintf("%s:%d", inspect.NetworkSettings.IPAddress, 5000)

registryLocalAddress := fmt.Sprintf("localhost:%s", ContainerHostPort(containerName, "5000/tcp"))
registryWithScheme := fmt.Sprintf("http://%s", registryLocalAddress)

utils.WaitTillHostReadyToRespond(registryWithScheme, utils.DefaultWaitTillHostReadyToRespondMaxAttempts)

return registry, containerName
return registryLocalAddress, registryInternalAddress, containerName
}
Expand Up @@ -56,7 +56,7 @@ var _ = SuiteData.AppendSynchronizedBeforeSuiteNode1Func(func() {
})

var _ = SuiteData.AppendSynchronizedBeforeSuiteAllNodesFunc(func(_ []byte) {
SuiteData.Registry, SuiteData.RegistryContainerName = utilsDocker.LocalDockerRegistryRun()
SuiteData.Registry, _, SuiteData.RegistryContainerName = utilsDocker.LocalDockerRegistryRun()
})

var _ = SuiteData.AppendSynchronizedAfterSuiteAllNodesFunc(func() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/build/image.go
Expand Up @@ -143,7 +143,7 @@ func (i *Image) FetchBaseImage(ctx context.Context, c *Conveyor) error {
case ImageFromRegistryAsBaseImage:
containerRuntime := c.ContainerRuntime

if info, err := containerRuntime.GetImageInfo(ctx, i.baseImage.Name()); err != nil {
if info, err := containerRuntime.GetImageInfo(ctx, i.baseImage.Name(), container_runtime.GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to inspect local image %s: %s", i.baseImage.Name(), err)
} else if info != nil {
// TODO: do not use container_runtime.LegacyStageImage for base image
Expand Down Expand Up @@ -174,7 +174,7 @@ func (i *Image) FetchBaseImage(ctx context.Context, c *Conveyor) error {
return err
}

if info, err := containerRuntime.GetImageInfo(ctx, i.baseImage.Name()); err != nil {
if info, err := containerRuntime.GetImageInfo(ctx, i.baseImage.Name(), container_runtime.GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to inspect local image %s: %s", i.baseImage.Name(), err)
} else if info == nil {
return fmt.Errorf("unable to inspect local image %s after successful pull: image is not exists", i.baseImage.Name())
Expand All @@ -186,9 +186,9 @@ func (i *Image) FetchBaseImage(ctx context.Context, c *Conveyor) error {
}
case StageAsBaseImage:
// TODO: check no bug introduced
//if err := c.ContainerRuntime.RefreshImageObject(ctx, &container_runtime.Image{Image: i.baseImage}); err != nil {
// if err := c.ContainerRuntime.RefreshImageObject(ctx, &container_runtime.Image{Image: i.baseImage}); err != nil {
// return err
//}
// }
if err := c.StorageManager.FetchStage(ctx, c.ContainerRuntime, i.stageAsBaseImage); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/stage/dockerfile.go
Expand Up @@ -289,7 +289,7 @@ outerLoop:
}

getBaseImageOnBuildLocally := func() ([]string, error) {
info, err := containerRuntime.GetImageInfo(ctx, resolvedBaseName)
info, err := containerRuntime.GetImageInfo(ctx, resolvedBaseName, container_runtime.GetImageInfoOpts{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -320,7 +320,7 @@ outerLoop:
logboek.Context(ctx).Warn().LogF("WARNING: Could not get base image manifest from local docker and from docker registry: %s\n", getRemotelyErr)
logboek.Context(ctx).Warn().LogLn("WARNING: The base image pulling is necessary for calculating digest of image correctly\n")
if err := logboek.Context(ctx).Default().LogProcess("Pulling base image %s", resolvedBaseName).DoError(func() error {
return containerRuntime.Pull(ctx, resolvedBaseName)
return containerRuntime.Pull(ctx, resolvedBaseName, container_runtime.PullOpts{})
}); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/buildah/docker_with_fuse_buildah.go
Expand Up @@ -141,7 +141,7 @@ func (b *DockerWithFuseBuildah) runBuildah(ctx context.Context, dockerArgs []str

args := []string{"--rm"}
args = append(args, dockerArgs...)
args = append(args, buildahWithFuseDockerArgs(BuildahStorageContainerName)...)
args = append(args, BuildahWithFuseDockerArgs(BuildahStorageContainerName, docker.DockerConfigDir)...)
args = append(args, buildahArgs...)

if debug() {
Expand Down Expand Up @@ -186,13 +186,13 @@ func runStorageContainer(ctx context.Context, name, image string) error {
})
}

func buildahWithFuseDockerArgs(storageContainerName string) []string {
func BuildahWithFuseDockerArgs(storageContainerName, dockerConfigDir string) []string {
return []string{
"--user", "1000",
"--device", "/dev/fuse",
"--security-opt", "seccomp=unconfined",
"--security-opt", "apparmor=unconfined",
"--volume", fmt.Sprintf("%s:%s", docker.DockerConfigDir, "/home/build/.docker"),
"--volume", fmt.Sprintf("%s:%s", dockerConfigDir, "/home/build/.docker"),
"--volumes-from", storageContainerName,
BuildahImage, "buildah",
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/buildah/native_rootless_buildah_linux.go
Expand Up @@ -137,7 +137,8 @@ func (b *NativeRootlessBuildah) Push(ctx context.Context, ref string, opts PushO

func (b *NativeRootlessBuildah) BuildFromDockerfile(ctx context.Context, dockerfile []byte, opts BuildFromDockerfileOpts) (string, error) {
buildOpts := define.BuildOptions{
Isolation: define.IsolationOCIRootless,
Isolation: define.IsolationOCIRootless,
OutputFormat: buildah.Dockerv2ImageManifest,
CommonBuildOpts: &define.CommonBuildOptions{
ShmSize: DefaultShmSize,
},
Expand Down
26 changes: 13 additions & 13 deletions pkg/container_runtime/buildah_runtime.go
Expand Up @@ -22,7 +22,7 @@ func NewBuildahRuntime(buildah buildah.Buildah) *BuildahRuntime {
}

// GetImageInfo returns nil, nil if image not found.
func (runtime *BuildahRuntime) GetImageInfo(ctx context.Context, ref string) (*image.Info, error) {
func (runtime *BuildahRuntime) GetImageInfo(ctx context.Context, ref string, opts GetImageInfoOpts) (*image.Info, error) {
inspect, err := runtime.buildah.Inspect(ctx, ref)
if err != nil {
return nil, fmt.Errorf("error getting buildah inspect of %q: %s", ref, err)
Expand All @@ -47,7 +47,7 @@ func (runtime *BuildahRuntime) GetImageInfo(ctx context.Context, ref string) (*i
}, nil
}

func (runtime *BuildahRuntime) Rmi(ctx context.Context, ref string) error {
func (runtime *BuildahRuntime) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
return runtime.buildah.Rmi(ctx, ref, buildah.RmiOpts{
Force: true,
CommonOpts: buildah.CommonOpts{
Expand All @@ -56,31 +56,31 @@ func (runtime *BuildahRuntime) Rmi(ctx context.Context, ref string) error {
})
}

func (runtime *BuildahRuntime) Pull(ctx context.Context, ref string) error {
func (runtime *BuildahRuntime) Pull(ctx context.Context, ref string, opts PullOpts) error {
return runtime.buildah.Pull(ctx, ref, buildah.PullOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}

func (runtime *BuildahRuntime) Tag(ctx context.Context, ref, newRef string) error {
func (runtime *BuildahRuntime) Tag(ctx context.Context, ref, newRef string, opts TagOpts) error {
return runtime.buildah.Tag(ctx, ref, newRef, buildah.TagOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}

func (runtime *BuildahRuntime) Push(ctx context.Context, ref string) error {
func (runtime *BuildahRuntime) Push(ctx context.Context, ref string, opts PushOpts) error {
return runtime.buildah.Push(ctx, ref, buildah.PushOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
},
})
}

func (runtime *BuildahRuntime) BuildDockerfile(ctx context.Context, dockerfile []byte, opts BuildDockerfileOptions) (string, error) {
func (runtime *BuildahRuntime) BuildDockerfile(ctx context.Context, dockerfile []byte, opts BuildDockerfileOpts) (string, error) {
return runtime.buildah.BuildFromDockerfile(ctx, dockerfile, buildah.BuildFromDockerfileOpts{
CommonOpts: buildah.CommonOpts{
LogWriter: logboek.Context(ctx).OutStream(),
Expand All @@ -90,7 +90,7 @@ func (runtime *BuildahRuntime) BuildDockerfile(ctx context.Context, dockerfile [
}

func (runtime *BuildahRuntime) RefreshImageObject(ctx context.Context, img LegacyImageInterface) error {
if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
Expand All @@ -99,11 +99,11 @@ func (runtime *BuildahRuntime) RefreshImageObject(ctx context.Context, img Legac
}

func (runtime *BuildahRuntime) PullImageFromRegistry(ctx context.Context, img LegacyImageInterface) error {
if err := runtime.Pull(ctx, img.Name()); err != nil {
if err := runtime.Pull(ctx, img.Name(), PullOpts{}); err != nil {
return fmt.Errorf("unable to pull image %s: %s", img.Name(), err)
}

if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to get inspect of image %s: %s", img.Name(), err)
} else {
img.SetInfo(info)
Expand All @@ -114,7 +114,7 @@ func (runtime *BuildahRuntime) PullImageFromRegistry(ctx context.Context, img Le

func (runtime *BuildahRuntime) RenameImage(ctx context.Context, img LegacyImageInterface, newImageName string, removeOldName bool) error {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Tagging image %s by name %s", img.Name(), newImageName)).DoError(func() error {
if err := runtime.Tag(ctx, img.Name(), newImageName); err != nil {
if err := runtime.Tag(ctx, img.Name(), newImageName, TagOpts{}); err != nil {
return fmt.Errorf("unable to tag image %s by name %s: %s", img.Name(), newImageName, err)
}
return nil
Expand All @@ -124,7 +124,7 @@ func (runtime *BuildahRuntime) RenameImage(ctx context.Context, img LegacyImageI

if removeOldName {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Removing old image tag %s", img.Name())).DoError(func() error {
if err := runtime.Rmi(ctx, img.Name()); err != nil {
if err := runtime.Rmi(ctx, img.Name(), RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove image %q: %s", img.Name(), err)
}
return nil
Expand All @@ -135,7 +135,7 @@ func (runtime *BuildahRuntime) RenameImage(ctx context.Context, img LegacyImageI

img.SetName(newImageName)

if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
Expand All @@ -152,7 +152,7 @@ func (runtime *BuildahRuntime) RenameImage(ctx context.Context, img LegacyImageI

func (runtime *BuildahRuntime) RemoveImage(ctx context.Context, img LegacyImageInterface) error {
if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Removing image tag %s", img.Name())).DoError(func() error {
if err := runtime.Rmi(ctx, img.Name()); err != nil {
if err := runtime.Rmi(ctx, img.Name(), RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove image %q: %s", img.Name(), err)
}
return nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/container_runtime/docker_server_runtime.go
Expand Up @@ -20,7 +20,7 @@ func NewDockerServerRuntime() *DockerServerRuntime {
return &DockerServerRuntime{}
}

func (runtime *DockerServerRuntime) BuildDockerfile(ctx context.Context, _ []byte, opts BuildDockerfileOptions) (string, error) {
func (runtime *DockerServerRuntime) BuildDockerfile(ctx context.Context, _ []byte, opts BuildDockerfileOpts) (string, error) {
switch {
case opts.ContextTar == nil:
panic(fmt.Sprintf("ContextTar can't be nil: %+v", opts))
Expand Down Expand Up @@ -66,7 +66,7 @@ func (runtime *DockerServerRuntime) BuildDockerfile(ctx context.Context, _ []byt
return tempID, docker.CliBuild_LiveOutputWithCustomIn(ctx, opts.ContextTar, cliArgs...)
}

func (runtime *DockerServerRuntime) GetImageInfo(ctx context.Context, ref string) (*image.Info, error) {
func (runtime *DockerServerRuntime) GetImageInfo(ctx context.Context, ref string, opts GetImageInfoOpts) (*image.Info, error) {
inspect, err := docker.ImageInspect(ctx, ref)
if client.IsErrNotFound(err) {
return nil, nil
Expand All @@ -85,7 +85,7 @@ func (runtime *DockerServerRuntime) GetImageInspect(ctx context.Context, ref str
}

func (runtime *DockerServerRuntime) RefreshImageObject(ctx context.Context, img LegacyImageInterface) error {
if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (runtime *DockerServerRuntime) RenameImage(ctx context.Context, img LegacyI

img.SetName(newImageName)

if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return err
} else {
img.SetInfo(info)
Expand All @@ -135,7 +135,7 @@ func (runtime *DockerServerRuntime) RenameImage(ctx context.Context, img LegacyI

func (runtime *DockerServerRuntime) RemoveImage(ctx context.Context, img LegacyImageInterface) error {
return logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Removing image tag %s", img.Name())).DoError(func() error {
return runtime.Rmi(ctx, img.Name())
return runtime.Rmi(ctx, img.Name(), RmiOpts{})
})
}

Expand All @@ -144,7 +144,7 @@ func (runtime *DockerServerRuntime) PullImageFromRegistry(ctx context.Context, i
return fmt.Errorf("unable to pull image %s: %s", img.Name(), err)
}

if info, err := runtime.GetImageInfo(ctx, img.Name()); err != nil {
if info, err := runtime.GetImageInfo(ctx, img.Name(), GetImageInfoOpts{}); err != nil {
return fmt.Errorf("unable to get inspect of image %s: %s", img.Name(), err)
} else {
img.SetInfo(info)
Expand All @@ -153,22 +153,22 @@ func (runtime *DockerServerRuntime) PullImageFromRegistry(ctx context.Context, i
return nil
}

func (runtime *DockerServerRuntime) Tag(ctx context.Context, ref, newRef string) error {
func (runtime *DockerServerRuntime) Tag(ctx context.Context, ref, newRef string, opts TagOpts) error {
return docker.CliTag(ctx, ref, newRef)
}

func (runtime *DockerServerRuntime) Push(ctx context.Context, ref string) error {
func (runtime *DockerServerRuntime) Push(ctx context.Context, ref string, opts PushOpts) error {
return docker.CliPushWithRetries(ctx, ref)
}

func (runtime *DockerServerRuntime) Pull(ctx context.Context, ref string) error {
func (runtime *DockerServerRuntime) Pull(ctx context.Context, ref string, opts PullOpts) error {
if err := docker.CliPull(ctx, ref); err != nil {
return fmt.Errorf("unable to pull image %s: %s", ref, err)
}
return nil
}

func (runtime *DockerServerRuntime) Rmi(ctx context.Context, ref string) error {
func (runtime *DockerServerRuntime) Rmi(ctx context.Context, ref string, opts RmiOpts) error {
return docker.CliRmi(ctx, ref, "--force")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/container_runtime/dockerfile_image_builder.go
Expand Up @@ -9,7 +9,7 @@ import (
type DockerfileImageBuilder struct {
ContainerRuntime ContainerRuntime
Dockerfile []byte
BuildDockerfileOptions BuildDockerfileOptions
BuildDockerfileOptions BuildDockerfileOpts
ContextArchivePath string

builtID string
Expand Down Expand Up @@ -50,7 +50,7 @@ func (b *DockerfileImageBuilder) Build(ctx context.Context) error {
}

func (b *DockerfileImageBuilder) Cleanup(ctx context.Context) error {
if err := b.ContainerRuntime.Rmi(ctx, b.builtID); err != nil {
if err := b.ContainerRuntime.Rmi(ctx, b.builtID, RmiOpts{}); err != nil {
return fmt.Errorf("unable to remove built dockerfile image %q: %s", b.builtID, err)
}
return nil
Expand Down

0 comments on commit 15f32a3

Please sign in to comment.