Skip to content

Commit

Permalink
fixup: Switch back to 3 images
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed May 2, 2024
1 parent bbd2ecf commit ef81f95
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 145 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check image build
- name: Check image build for avalanchego test setup
shell: bash
run: bash -x scripts/tests.build_antithesis_avalanchego_image.sh
run: bash -x scripts/tests.build_antithesis_images.sh
env:
TEST_SETUP: avalanchego
govulncheck:
runs-on: ubuntu-latest
name: govulncheck
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/publish_antithesis_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
password: ${{ secrets.ANTITHESIS_GAR_JSON_KEY }}

- name: Build images for avalanchego test setup
run: bash -x ./scripts/build_antithesis_avalanchego_image.sh
run: bash -x ./scripts/build_antithesis_images.sh
env:
IMAGE_PREFIX: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}
TAG: latest
TEST_SETUP: avalanchego
56 changes: 0 additions & 56 deletions scripts/build_antithesis_avalanchego_image.sh

This file was deleted.

66 changes: 66 additions & 0 deletions scripts/build_antithesis_images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

set -euo pipefail

# Builds docker images for antithesis testing.

# e.g.,
# ./scripts/build_antithesis_images.sh # Build local images
# IMAGE_PREFIX=<registry>/<repo> TAG=latest ./scripts/build_antithesis_images.sh # Specify a prefix to enable image push and use a specific tag

# Directory above this script
AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

# Specifying an image prefix will ensure the image is pushed after build
IMAGE_PREFIX="${IMAGE_PREFIX:-}"

TAG="${TAG:-}"
if [[ -z "${TAG}" ]]; then
# Default to tagging with the commit hash
source "${AVALANCHE_PATH}"/scripts/constants.sh
TAG="${commit_hash}"
fi

# The dockerfiles don't specify the golang version to minimize the changes required to bump
# the version. Instead, the golang version is provided as an argument.
GO_VERSION="$(go list -m -f '{{.GoVersion}}')"

function build_images {
local test_setup=$1
local uninstrumented_node_dockerfile=$2

# Define image names
local base_image_name="antithesis-${test_setup}"
if [[ -n "${IMAGE_PREFIX}" ]]; then
base_image_name="${IMAGE_PREFIX}/${base_image_name}"
fi
local node_image_name="${base_image_name}-node:${TAG}"
local workload_image_name="${base_image_name}-workload:${TAG}"
local config_image_name="${base_image_name}-config:${TAG}"

# Define dockerfiles
local base_dockerfile="${AVALANCHE_PATH}/tests/antithesis/${test_setup}/Dockerfile"
local node_dockerfile="${base_dockerfile}.node"
if [[ "$(go env GOARCH)" == "arm64" ]]; then
# Antithesis instrumentation is only supported on amd64. On apple silicon (arm64), the
# uninstrumented Dockerfile will be used to build the node image to enable local test
# development.
node_dockerfile="${uninstrumented_node_dockerfile}"
fi

# Define default build command
local docker_cmd="docker buildx build --build-arg GO_VERSION=${GO_VERSION}"

# Build node image first to allow the config and workload image builds to use it.
${docker_cmd} -t "${node_image_name}" -f "${node_dockerfile}" "${AVALANCHE_PATH}"
${docker_cmd} --build-arg NODE_IMAGE="${node_image_name}" -t "${workload_image_name}" -f "${base_dockerfile}.workload" "${AVALANCHE_PATH}"
${docker_cmd} --build-arg IMAGE_TAG="${TAG}" -t "${config_image_name}" -f "${base_dockerfile}.config" "${AVALANCHE_PATH}"
}

TEST_SETUP="${TEST_SETUP:-}"
if [[ "${TEST_SETUP}" == "avalanchego" ]]; then
build_images avalanchego "${AVALANCHE_PATH}/Dockerfile"
else
echo "TEST_SETUP must be set. Valid values are 'avalanchego'"
exit 255
fi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -euo pipefail

# Validates the construction of the antithesis test image for avalanchego by:
# Validates the construction of the antithesis images for a test setup specified by TEST_SETUP.
#
# 1. Building the antithesis test image
# 2. Extracting the docker compose configuration from the image
Expand All @@ -12,17 +12,18 @@ set -euo pipefail

AVALANCHE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )

# Discover the tag that will be used for the image
# Discover the default tag that will be used for the image
source "${AVALANCHE_PATH}"/scripts/constants.sh
export TAG="${commit_hash}"

# Build the image
bash -x "${AVALANCHE_PATH}"/scripts/build_antithesis_avalanchego_image.sh
# Build the images for the specified test setup
export TEST_SETUP="${TEST_SETUP:-}"
bash -x "${AVALANCHE_PATH}"/scripts/build_antithesis_images.sh

# Create a container from the test image to extract compose configuration from
IMAGE_NAME=antithesis-avalanchego
# Create a container from the config image to extract compose configuration from
IMAGE_NAME="antithesis-${TEST_SETUP}-config"
CONTAINER_NAME="tmp-${IMAGE_NAME}"
docker create --name "${CONTAINER_NAME}" "${IMAGE_NAME}:${TAG}"
docker create --name "${CONTAINER_NAME}" "${IMAGE_NAME}:${TAG}" /bin/true

# Create a temporary directory to write the compose configuration to
TMPDIR="$(mktemp -d)"
Expand Down
47 changes: 18 additions & 29 deletions tests/antithesis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,26 @@ to take full advantage of the supported traceability. Since the
Antithesis Go SDK only supports the amd64/x86_64 architecture as of this
writing, running of instrumented binaries on Macs (arm64) is not possible
without emulation (which would be very slow). To support test development
on Macs, a local build will not be instrumented. This suggested a 2-stage
build:

- build a node image (instrumented or not)
- build a test image based on the node image

Having 2 stages allows reusing a non-instrumented Dockerfile for local
development without having to duplicate workload configuration across
Dockerfiles for both instrumented and non-instrumented builds.

This means there are 2 images produced by image build: a node image, and
a test image. The test image is based on the node image, and only the
test image is intended to be published for use in antithesis testing.
on Macs, a local build will not be instrumented.

## Defining a new test setup

When defining a new test setup - whether in the avalanchego repo or for a
VM in another repo - following the example of an existing test setup is
suggested. The following table enumerates the files defined for existing
test setups:

| Filename | Purpose |
|:-----------------------------------------------------------|:-------------------------------------------------------|
| scripts/build_antithesis_[test setup]_image.sh | Builds the test image to deploy to antithesis |
| scripts/build_antithesis_[test setup]_workload.sh | Builds the workload binary |
| scripts/tests.build_antithesis_[test setup]_image.sh | Validates test image build |
| tests/antithesis/[test setup]/main.go | The entrypoint for the workload binary |
| tests/antithesis/[test setup]/Dockerfile.instrumented-node | Defines how to build the instrumented node image |
| tests/antithesis/[test setup]/Dockerfile | Defines how to build the test image |
| tests/antithesis/[test setup]/gencomposeconfig/main.go | Generates the compose configuration for the test setup |
When defining a new test setup - whether in the avalanchego repo or
for a VM in another repo - following the example of an existing test
setup is suggested. The following table enumerates the files defining
a test setup:

| Filename | Purpose |
|:-------------------------------------------------------|:-------------------------------------------------------|
| scripts/build_antithesis_images.sh | Builds the test images to deploy to antithesis |
| scripts/build_antithesis_[test setup]_workload.sh | Builds the workload binary |
| scripts/tests.build_antithesis_images.sh | Validates the build of the test images |
| tests/antithesis/[test setup]/main.go | The entrypoint for the workload binary |
| tests/antithesis/[test setup]/Dockerfile.config | Defines how to build the config image |
| tests/antithesis/[test setup]/Dockerfile.node | Defines how to build the instrumented node image |
| tests/antithesis/[test setup]/Dockerfile.workload | Defines how to build the workload image |
| tests/antithesis/[test setup]/gencomposeconfig/main.go | Generates the compose configuration for the test setup |

In addition, github workflows are suggested to ensure
`scripts/tests.build_antithesis_[test setup]_image.sh` runs against PRs
and `scripts/build_antithesis_[test setup]_image.sh` runs against pushes.
`scripts/tests.build_antithesis_images.sh` runs against PRs and
`scripts/build_antithesis_images.sh` runs against pushes.
40 changes: 0 additions & 40 deletions tests/antithesis/avalanchego/Dockerfile

This file was deleted.

29 changes: 29 additions & 0 deletions tests/antithesis/avalanchego/Dockerfile.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The version is supplied as a build argument rather than hard-coded
# to minimize the cost of version changes.
ARG GO_VERSION

# ============= Compilation Stage ================
FROM golang:$GO_VERSION-bullseye AS builder

WORKDIR /build
# Copy and download avalanche dependencies using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .

# IMAGE_TAG should be set to the tag for the images in the generated
# docker compose file.
ARG IMAGE_TAG=latest

# Generate docker compose configuration
RUN TARGET_PATH=./build IMAGE_TAG="$IMAGE_TAG" go run ./tests/antithesis/avalanchego/gencomposeconfig

# ============= Cleanup Stage ================
FROM scratch AS execution

# Copy the docker compose file and volumes into the container
COPY --from=builder /build/build/docker-compose.yml /docker-compose.yml
COPY --from=builder /build/build/volumes /volumes
30 changes: 30 additions & 0 deletions tests/antithesis/avalanchego/Dockerfile.workload
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# The version is supplied as a build argument rather than hard-coded
# to minimize the cost of version changes.
ARG GO_VERSION

# NODE_IMAGE needs to identify an existing node image and should include the tag
ARG NODE_IMAGE

# ============= Compilation Stage ================
FROM golang:$GO_VERSION-bullseye AS builder

WORKDIR /build
# Copy and download avalanche dependencies using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .

# Build the workload
RUN ./scripts/build_antithesis_avalanchego_workload.sh

# ============= Cleanup Stage ================
# Base the workflow on the node image to support bootstrap testing
FROM $NODE_IMAGE AS execution

# Copy the executable into the container
COPY --from=builder /build/build/antithesis-avalanchego-workload /workload

CMD [ "./workload" ]
16 changes: 11 additions & 5 deletions tests/antithesis/avalanchego/gencomposeconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@
package main

import (
"fmt"
"log"
"os"

"github.com/ava-labs/avalanchego/tests/antithesis"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
)

const baseImageName = "antithesis-avalanchego"

// Creates docker-compose.yml and its associated volumes in the target path.
func main() {
targetPath := os.Getenv("TARGET_PATH")
if len(targetPath) == 0 {
log.Fatal("TARGET_PATH environment variable not set")
}

imageName := os.Getenv("IMAGE_NAME")
if len(imageName) == 0 {
log.Fatal("IMAGE_NAME environment variable not set")
imageTag := os.Getenv("IMAGE_TAG")
if len(imageTag) == 0 {
log.Fatal("IMAGE_TAG environment variable not set")
}

nodeImageName := fmt.Sprintf("%s-node:%s", baseImageName, imageTag)
workloadImageName := fmt.Sprintf("%s-workload:%s", baseImageName, imageTag)

network := tmpnet.LocalNetworkOrDie()
err := antithesis.GenerateComposeConfig(network, imageName, targetPath)
err := antithesis.GenerateComposeConfig(network, nodeImageName, workloadImageName, targetPath)
if err != nil {
log.Fatalf("failed to initialize config for docker-compose: %s", err)
log.Fatalf("failed to generate config for docker-compose: %s", err)
}
}
Loading

0 comments on commit ef81f95

Please sign in to comment.