Skip to content

Commit

Permalink
Compilers image tests (#45)
Browse files Browse the repository at this point in the history
* Tests must fail the build - fix a false-positive condition
* Add tests to compilers image
* Add script to update all Go toolchain images and run it
* Add helper script to update tester image
* Add optional `-C` flag to chdir - this will enable including
  tests inside the image, so that any downstream images
  can actually re-run the test.
* Relax Go runtime version check

Signed-off-by: Ilya Dmitrichenko <errordeveloper@gmail.com>
  • Loading branch information
errordeveloper authored Jun 23, 2020
1 parent fe87c5f commit d47f6ac
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ifeq ($(PUSH),true)
OUTPUT := "type=registry,push=true"
endif

all-images: lint maker-image update-maker-image compilers-image update-compilers-image bpftool-image iproute2-image llvm-image
all-images: lint maker-image update-maker-image tester-image update-tester-image compilers-image update-compilers-image bpftool-image iproute2-image llvm-image

lint:
scripts/lint.sh
Expand All @@ -25,6 +25,9 @@ lint:
update-alpine-base-image:
scripts/update-alpine-base-image.sh

update-golang-image:
scripts/update-golang-image.sh

maker-image: .buildx_builder
scripts/build-image.sh image-maker images/maker linux/amd64 $(OUTPUT) "$$(cat .buildx_builder)" $(REGISTRIES)

Expand All @@ -34,8 +37,11 @@ update-maker-image:
tester-image: .buildx_builder
TEST=true scripts/build-image.sh image-tester images/tester linux/amd64,linux/arm64 $(OUTPUT) "$$(cat .buildx_builder)" $(REGISTRIES)

update-tester-image:
scripts/update-tester-image.sh $(firstword $(REGISTRIES))

compilers-image: .buildx_builder
scripts/build-image.sh image-compilers images/compilers linux/amd64 $(OUTPUT) "$$(cat .buildx_builder)" $(REGISTRIES)
TEST=true scripts/build-image.sh image-compilers images/compilers linux/amd64 $(OUTPUT) "$$(cat .buildx_builder)" $(REGISTRIES)

update-compilers-image:
scripts/update-compilers-image.sh $(firstword $(REGISTRIES))
Expand Down
10 changes: 10 additions & 0 deletions images/compilers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

ARG UBUNTU_IMAGE=docker.io/library/ubuntu:20.04@sha256:8bce67040cd0ae39e0beb55bcb976a824d9966d2ac8d2e4bf6119b45505cee64
ARG TESTER_IMAGE=docker.io/cilium/image-tester:a99a94da5d1c0ff1c0c2411f32e25219553a9c9f@sha256:cb808315bc067e9e2e5f12bd7ab589a029eba35eff227ac0f332a2e0ad46738a

FROM ${UBUNTU_IMAGE} as builder

Expand All @@ -10,3 +11,12 @@ RUN /tmp/install-deps.sh

COPY install-bazel.sh /tmp/install-bazel.sh
RUN /tmp/install-bazel.sh

FROM ${CST_IMAGE} as test
COPY --from=builder / /
COPY test /test
RUN /test/bin/cst

FROM scratch
LABEL maintainer="maintainer@cilium.io"
COPY --from=builder / /
97 changes: 97 additions & 0 deletions images/compilers/test/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
schemaVersion: "2.0.0"

commandTests:
- name: "gcc command is in path"
command: "which"
args: ["gcc"]
expectedOutput: ["/usr/bin/gcc"]
- name: "gcc version"
command: "gcc"
args: ["-v"]
expectedError:
- 'Target:\ x86_64-linux-gnu'
- 'gcc\ version\ 9\.3\.0'
- name: "aarch64-linux-gnu-gcc command is in path"
command: "which"
args: ["aarch64-linux-gnu-gcc"]
expectedOutput: ["/usr/bin/aarch64-linux-gnu-gcc"]
- name: "aarch64-linux-gnu-gcc version"
command: "aarch64-linux-gnu-gcc"
args: ["-v"]
expectedError:
- 'Target:\ aarch64-linux-gnu'
- 'gcc\ version\ 9\.3\.0'

- name: "which bazel-2.0.0-linux-x86_64"
command: "which"
args: ["bazel-2.0.0-linux-x86_64"]
expectedOutput: ["/usr/local/bin/bazel-2.0.0-linux-x86_64"]
- name: "which bazel-2.0.0-linux-x86_64 version"
command: "bazel-2.0.0-linux-x86_64"
args: ["version"]
expectedOutput: ['Build\ label:\ 2\.0\.0']
- name: "bazel 2.0.0 version"
command: "bazel"
envVars: [{ key: "USE_BAZEL_VERSION", value: "2.0.0" }]
args: ["version"]
expectedOutput: ['Build\ label:\ 2\.0\.0']

- name: "which bazel-2.2.0-linux-x86_64"
command: "which"
args: ["bazel-2.2.0-linux-x86_64"]
expectedOutput: ["/usr/local/bin/bazel-2.2.0-linux-x86_64"]
- name: "bazel-2.2.0-linux-x86_64 version"
command: "bazel-2.2.0-linux-x86_64"
args: ["version"]
expectedOutput: ['Build\ label:\ 2\.2\.0']
- name: "bazel 2.2.0 version"
command: "bazel"
envVars: [{ key: "USE_BAZEL_VERSION", value: "2.2.0" }]
args: ["version"]
expectedOutput: ['Build\ label:\ 2\.2\.0']

- name: "which bazel-3.1.0-linux-x86_64"
command: "which"
args: ["bazel-3.1.0-linux-x86_64"]
expectedOutput: ["/usr/local/bin/bazel-3.1.0-linux-x86_64"]
- name: "bazel-3.1.0-linux-x86_64 version"
command: "bazel-3.1.0-linux-x86_64"
args: ["version"]
expectedOutput: ['Build\ label:\ 3\.1\.0']
- name: "bazel 3.1.0 version"
command: "bazel"
envVars: [{ key: "USE_BAZEL_VERSION", value: "3.1.0" }]
args: ["version"]
expectedOutput: ['Build\ label:\ 3\.1\.0']

- name: "which bazel-3.2.0-linux-x86_64"
command: "which"
args: ["bazel-3.2.0-linux-x86_64"]
expectedOutput: ["/usr/local/bin/bazel-3.2.0-linux-x86_64"]
- name: "bazel-3.2.0-linux-x86_64 version"
command: "bazel-3.2.0-linux-x86_64"
args: ["version"]
expectedOutput: ['Build\ label:\ 3\.2\.0']
- name: "bazel 3.2.0 version"
command: "bazel"
envVars: [{ key: "USE_BAZEL_VERSION", value: "3.2.0" }]
args: ["version"]
expectedOutput: ['Build\ label:\ 3\.2\.0']

- name: "bazel version"
command: "bazel"
args: ["version"]
expectedOutput: ['Build\ label:\ 3\.2\.0']

fileExistenceTests:
- name: '/usr/local/bin/bazel'
path: '/usr/local/bin/bazel'
shouldExist: true
permissions: '-rwxr-xr-x'

fileContentTests:
- name: '/usr/local/bin/bazel'
path: '/usr/local/bin/bazel'
expectedContents:
- '#!/bin/bash'
- '# Copyright 2019 The Bazel Authors. All rights reserved.'
2 changes: 1 addition & 1 deletion images/maker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ARG DOCKER_IMAGE=docker.io/library/docker:19.03.8-dind@sha256:841b5eb000551dc3c30a46386ab4bfed5839ec9592c88e961236b25194ce88b9
ARG CRANE_IMAGE=gcr.io/go-containerregistry/crane:latest@sha256:88335131ccc1f0687226245f68b0b328864026bc6504e97f4e1c130b5c766420
ARG ALPINE_BASE_IMAGE=docker.io/library/alpine:3.11@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54
ARG GOLANG_IMAGE=docker.io/library/golang:1.14.3@sha256:1e36f8e9ac49d5ee6d72e969382a698614551a59f4533d5d61590e3deeb543a7
ARG GOLANG_IMAGE=docker.io/library/golang:1.14@sha256:ede9a57fa6d862ab87f5abcea707c3d55e445ff01d806334a1cb7aae45ec73bb

FROM ${DOCKER_IMAGE} as docker-dist
FROM ${CRANE_IMAGE} as crane-dist
Expand Down
2 changes: 1 addition & 1 deletion images/tester/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright 2020 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

ARG GOLANG_IMAGE=docker.io/library/golang:1.14.3@sha256:1e36f8e9ac49d5ee6d72e969382a698614551a59f4533d5d61590e3deeb543a7
ARG GOLANG_IMAGE=docker.io/library/golang:1.14@sha256:ede9a57fa6d862ab87f5abcea707c3d55e445ff01d806334a1cb7aae45ec73bb
ARG ALPINE_BASE_IMAGE=docker.io/library/alpine:3.11@sha256:9a839e63dad54c3a6d1834e29692c8492d93f90c59c978c1ed79109ea4fb9a54

FROM --platform=linux/amd64 ${GOLANG_IMAGE} as go-builder
Expand Down
7 changes: 6 additions & 1 deletion images/tester/cst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
configFile = "/test/spec.yaml"
configFile = "spec.yaml"
)

/* container-structure-test can be used inside a contaner, however multiple flags have to be set and
Expand All @@ -34,6 +34,7 @@ const (

func main() {
version := flag.Bool("V", false, "print version and exit")
testDir := flag.String("C", "/test", "directory to chdir, and read `spec.yaml`")

flag.Parse()

Expand All @@ -44,6 +45,10 @@ func main() {

color.NoColor = true

if err := os.Chdir(*testDir); err != nil {
fmt.Printf("unable to run tests: %s\n", err)
}

fakeMetadataPath, err := fakeMetadata()
if err != nil {
fmt.Printf("unable to write fake metadata: %s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion images/tester/test/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ commandTests:
command: '/test/bin/cst'
args: ['-V']
expectedOutput:
- 'go1\.14\.3\ linux/(amd64|arm64)'
- 'go1\..*\ linux/(amd64|arm64)'
4 changes: 3 additions & 1 deletion scripts/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ run_buildx() {
build_args+=("${root_dir}")
fi
if [ "${do_test}" = "true" ] ; then
docker buildx build --target=test "${build_args[@]}"
if ! docker buildx build --target=test "${build_args[@]}" ; then
exit 1
fi
fi
docker buildx build --output="${output}" "${tag_args[@]}" "${build_args[@]}"
}
Expand Down
31 changes: 31 additions & 0 deletions scripts/update-golang-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Copyright 2017-2020 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

set -o xtrace
set -o errexit
set -o pipefail
set -o nounset

if [ "$#" -gt 1 ] ; then
echo "$0 supports at most 1 argument"
exit 1
fi

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

root_dir="$(git rev-parse --show-toplevel)"

cd "${root_dir}"

image="${1:-docker.io/library/golang:1.14}"

image_digest="$("${script_dir}/get-image-digest.sh" "${image}")"

# shellcheck disable=SC2207
used_by=($(git grep -l GOLANG_IMAGE= images))

for i in "${used_by[@]}" ; do
sed "s|\(GOLANG_IMAGE=\)docker.io/library/golang:.*\$|\1${image}@${image_digest}|" "${i}" > "${i}.sedtmp" && mv "${i}.sedtmp" "${i}"
done
33 changes: 33 additions & 0 deletions scripts/update-tester-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Copyright 2017-2020 Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

set -o xtrace
set -o errexit
set -o pipefail
set -o nounset

if [ "$#" -ne 1 ] ; then
echo "$0 supports exactly 1 argument"
exit 1
fi

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

root_dir="$(git rev-parse --show-toplevel)"

cd "${root_dir}"

image_name="${1}/image-tester"

image_tag="$(WITHOUT_SUFFIX=1 "${script_dir}/make-image-tag.sh" images/tester)"

image_digest="$("${script_dir}/get-image-digest.sh" "${image_name}:${image_tag}")"

# shellcheck disable=SC2207
used_by=($(git grep -l TESTER_IMAGE= images))

for i in "${used_by[@]}" ; do
sed "s|\(TESTER_IMAGE=${image_name}\):.*\$|\1:${image_tag}@${image_digest}|" "${i}" > "${i}.sedtmp" && mv "${i}.sedtmp" "${i}"
done

0 comments on commit d47f6ac

Please sign in to comment.