From 9fea9efd6137389e19a840767820f59244d33b6d Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Wed, 5 Feb 2025 03:19:03 -0300 Subject: [PATCH] feat(CI): build and run the tests in a container Add an option to run the unit-tests with no setup require. Below are the two [1], [2] options, one with :devel docker tags and one with customizable tags. [1] ``` docker compose -f compose.yaml run unit-test ``` [2] ``` export REPOSITORY=ghcr.io/ export TAG=pr-001 docker compose -f compose.yaml -f compose.override-tags.yaml run unit-test ``` feat(CI): update workflows --- .dockerignore | 1 - .github/license-check/config.json | 3 +- .github/workflows/build.yml | 144 +++++++++----------- Dockerfile | 43 ++++++ Makefile | 2 +- compose.override-tags.yaml | 15 ++ compose.yaml | 34 +++++ internal/advancer/machines/machines_test.go | 2 + pkg/service/address.go | 3 + pkg/service/log.go | 3 + test/dependencies | 3 + test/dependencies.sha256 | 3 + test/devnet/Dockerfile | 9 +- test/devnet/deployment.jq | 5 + test/postgres/drop-test-db.sql | 2 + test/postgres/init-test-db.sql | 5 + 16 files changed, 185 insertions(+), 92 deletions(-) create mode 100644 compose.override-tags.yaml create mode 100644 test/dependencies create mode 100644 test/dependencies.sha256 create mode 100644 test/devnet/deployment.jq create mode 100644 test/postgres/drop-test-db.sql create mode 100755 test/postgres/init-test-db.sql diff --git a/.dockerignore b/.dockerignore index 7f7ce6a87..5e23da31e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,3 @@ -.git* **/target/ .gitignore cartesi-rollups-authority-claimer diff --git a/.github/license-check/config.json b/.github/license-check/config.json index 741739a79..d142a0fad 100644 --- a/.github/license-check/config.json +++ b/.github/license-check/config.json @@ -3,9 +3,10 @@ "include": ["**/*.go", "**/*.rs"], "exclude": [ "cmd/authority-claimer/target/**", + "internal/repository/postgres/db/**", "pkg/contracts/**", - "pkg/readerclient/generated.go", "pkg/inspectclient/generated.go", + "pkg/readerclient/generated.go", "rollups-contracts/**" ], "license": ".github/license-check/header.txt" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 10ef589fc..743d0b1ae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,13 +20,11 @@ permissions: jobs: build-ci-base: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 outputs: output: ${{ steps.export_tag.outputs.image_tag }} steps: - uses: actions/checkout@v4 - with: - submodules: recursive - name: Docker meta id: docker_meta @@ -47,25 +45,53 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - uses: depot/setup-action@v1 - - name: Build and push docker image - id: docker_build - uses: depot/bake-action@v1 + + - name: Build and push devnet image + uses: depot/build-push-action@v1 + with: + file: test/devnet/Dockerfile + context: . + platforms: linux/amd64,linux/arm64 + tags: ghcr.io/cartesi/rollups-node-devnet:${{steps.docker_meta.outputs.version}} + push: true + cache-from: type=gha,scope=debian + cache-to: type=gha,mode=max,scope=debian + project: ${{ vars.DEPOT_PROJECT }} + token: ${{ secrets.DEPOT_TOKEN }} + + - name: Build and push node image + uses: depot/build-push-action@v1 + with: + file: Dockerfile + context: . + platforms: linux/amd64,linux/arm64 + tags: ghcr.io/cartesi/rollups-node:${{steps.docker_meta.outputs.version}} + push: true + cache-from: type=gha,scope=debian + cache-to: type=gha,mode=max,scope=debian + project: ${{ vars.DEPOT_PROJECT }} + token: ${{ secrets.DEPOT_TOKEN }} + + - name: Build and push test image + uses: depot/build-push-action@v1 with: - files: | - ./docker-bake.hcl - ${{ steps.docker_meta.outputs.bake-file }} - ./docker-bake.platforms.hcl - targets: rollups-node-ci + file: Dockerfile + context: . + target: go-tester + platforms: linux/amd64,linux/arm64 + tags: ghcr.io/cartesi/rollups-node-test:${{steps.docker_meta.outputs.version}} push: true + cache-from: type=gha,scope=debian + cache-to: type=gha,mode=max,scope=debian project: ${{ vars.DEPOT_PROJECT }} - workdir: build + token: ${{ secrets.DEPOT_TOKEN }} - name: Export Image Tag id : export_tag run : echo "image_tag=${{steps.docker_meta.outputs.version}}" >> "$GITHUB_OUTPUT" do-basic-checks: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 with: @@ -93,82 +119,37 @@ jobs: docs/*.md check-generated-files: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 container: - image: ghcr.io/cartesi/rollups-node-ci:${{needs.build-ci-base.outputs.output}} + image: ghcr.io/cartesi/rollups-node-test:${{needs.build-ci-base.outputs.output}} needs: - build-ci-base steps: - - uses: actions/checkout@v4 - with: - submodules: recursive - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - - name: Fix VCS Issue - run : git config --global --add safe.directory /__w/rollups-node/rollups-node - - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' - - name: Check auto generated files run: make check-generate - test-go: - runs-on: ubuntu-22.04 - container: - image: ghcr.io/cartesi/rollups-node-ci:${{needs.build-ci-base.outputs.output}} + unit-test: + runs-on: ubuntu-24.04 needs: - build-ci-base steps: - uses: actions/checkout@v4 - with: - submodules: recursive - - uses: depot/setup-action@v1 - - name: Build dependency images - uses: depot/bake-action@v1 - with: - files: | - ./docker-bake.hcl - ./docker-bake.override.hcl - ./docker-bake.platforms.hcl - targets: | - rollups-node-devnet - rollups-node-snapshot - project: ${{ vars.DEPOT_PROJECT }} - workdir: build - load: true - - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' - - - name: Fix VCS Go Linter Issue - run : git config --global --add safe.directory /__w/rollups-node/rollups-node - - - name: Run Go Linter - uses: golangci/golangci-lint-action@v6 - with: - version: v1.58.2 - - - name: Run Go tests + - name: Run unit tests env: - TESTCONTAINERS_RYUK_DISABLED: true - run: go test ./... + REPOSITORY: ghcr.io/ + TAG: ${{needs.build-ci-base.outputs.output}} + CARTESI_POSTGRES_ENDPOINT: "postgres://postgres:password@database:5432/rollupsdb?sslmode=disable" + run: | + docker compose -f compose.yaml -f compose.override-tags.yaml run unit-test build-docker: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 needs: - do-basic-checks - - test-go + - unit-test steps: - uses: actions/checkout@v4 - with: - submodules: recursive - name: Docker meta id: docker_meta @@ -197,22 +178,23 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - uses: depot/setup-action@v1 - - name: Build and push docker image - id: docker_build - uses: depot/bake-action@v1 + + - name: Build and push node image + uses: depot/build-push-action@v1 with: - files: | - ./docker-bake.hcl - ${{ steps.docker_meta.outputs.bake-file }} - ./docker-bake.platforms.hcl - targets: rollups-node - set: rollups-node.args.ROLLUPS_NODE_VERSION=${{ steps.docker_meta.outputs.version }} + file: Dockerfile + context: . + platforms: linux/amd64,linux/arm64 + tags: ghcr.io/cartesi/rollups-node:${{steps.docker_meta.outputs.version}} push: true + cache-from: type=gha,scope=debian + cache-to: type=gha,mode=max,scope=debian project: ${{ vars.DEPOT_PROJECT }} - workdir: build + token: ${{ secrets.DEPOT_TOKEN }} + release: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 needs: - build-docker if: startsWith(github.ref, 'refs/tags/v') diff --git a/Dockerfile b/Dockerfile index 1d2db2731..77a7a4bc0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -87,6 +87,49 @@ COPY --chown=cartesi:cartesi . ${GO_BUILD_PATH}/rollups-node/ RUN cd ${GO_BUILD_PATH}/rollups-node && make build-go +# ============================================================================= +# STAGE: go-tester +# +# Gather the dependencies to run the unit-tests +# ============================================================================= + +FROM go-builder AS go-tester +WORKDIR ${GO_BUILD_PATH}/rollups-node + +# fetch dependencies +RUN < deployment.json + mv /tmp/state.json anvil_state.json +EOF +RUN make echo-dapp + # ============================================================================= # STAGE: rollups-node # diff --git a/Makefile b/Makefile index 4d39ef466..d7423916f 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,7 @@ generate: $(ROLLUPS_CONTRACTS_ABI_BASEDIR)/.stamp ## Generate the file that are check-generate: generate ## Check whether the generated files are in sync @echo "Checking differences on the repository..." - @if git diff --exit-code; then \ + @if git diff --exit-code -- pkg/ internal/; then \ echo "No differences found."; \ else \ echo "ERROR: Differences found in the resulting files."; \ diff --git a/compose.override-tags.yaml b/compose.override-tags.yaml new file mode 100644 index 000000000..6235cefa5 --- /dev/null +++ b/compose.override-tags.yaml @@ -0,0 +1,15 @@ +# better control of tags +# ``` +# export REPOSITORY=ghcr.io/ +# export TAG=pr-001 +# docker compose -f compose.yaml -f compose.unit-test.yaml run unit-test +# ``` +services: + ethereum_provider: + image: ${REPOSITORY}cartesi/rollups-node-devnet:${TAG} + migration: + image: ${REPOSITORY}cartesi/rollups-node:${TAG} + node: + image: ${REPOSITORY}cartesi/rollups-node:${TAG} + unit-test: + image: ${REPOSITORY}cartesi/rollups-node-test:${TAG} diff --git a/compose.yaml b/compose.yaml index 9074a938e..5433eb962 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,6 +11,8 @@ x-env: &env services: ethereum_provider: image: cartesi/rollups-node-devnet:devel + build: + dockerfile: test/devnet/Dockerfile networks: - devnet ports: @@ -46,6 +48,8 @@ services: node: image: cartesi/rollups-node:devel + build: + dockerfile: Dockerfile command: cartesi-rollups-node depends_on: database: @@ -61,6 +65,36 @@ services: environment: <<: *env + # run the unit tests. + # ``` + # docker compose -f compose.yaml run unit-test + # ``` + unit-test: + image: cartesi/rollups-node-test:devel + profiles: [unit-test] + build: + dockerfile: Dockerfile + target: go-tester + entrypoint: ["sh", "-c"] + command: | + ' + set -e + psql $CARTESI_POSTGRES_ENDPOINT -f test/postgres/drop-test-db.sql + psql $CARTESI_POSTGRES_ENDPOINT -f test/postgres/init-test-db.sql + make test + ' + depends_on: + database: + condition: service_healthy + ethereum_provider: + condition: service_healthy + networks: + - devnet + restart: "no" + environment: + <<: *env + CARTESI_TEST_POSTGRES_ENDPOINT: postgres://postgres:password@database:5432/test_rollupsdb?sslmode=disable + volumes: node_data: diff --git a/internal/advancer/machines/machines_test.go b/internal/advancer/machines/machines_test.go index f44979fc3..fe3326b05 100644 --- a/internal/advancer/machines/machines_test.go +++ b/internal/advancer/machines/machines_test.go @@ -1,3 +1,5 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) package machines import ( diff --git a/pkg/service/address.go b/pkg/service/address.go index 7cc6e6da8..7e7b07b05 100644 --- a/pkg/service/address.go +++ b/pkg/service/address.go @@ -1,3 +1,6 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + // Implementation of the pflags Value interface. package service diff --git a/pkg/service/log.go b/pkg/service/log.go index 6445c63ae..af33eed79 100644 --- a/pkg/service/log.go +++ b/pkg/service/log.go @@ -1,3 +1,6 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + // Implementation of the pflags Value interface. package service diff --git a/test/dependencies b/test/dependencies new file mode 100644 index 000000000..ca33b4560 --- /dev/null +++ b/test/dependencies @@ -0,0 +1,3 @@ +https://github.com/cartesi/image-kernel/releases/download/v0.20.0/linux-6.5.13-ctsi-1-v0.20.0.bin +https://github.com/cartesi/machine-emulator-tools/releases/download/v0.16.1/rootfs-tools-v0.16.1.ext2 +https://github.com/cartesi/rollups-contracts/releases/download/v2.0.0-rc.12/rollups-contracts-2.0.0-rc.12-anvil-nightly-2044faec64f99a21f0e5f0094458a973612d0712.tar.gz diff --git a/test/dependencies.sha256 b/test/dependencies.sha256 new file mode 100644 index 000000000..221db542c --- /dev/null +++ b/test/dependencies.sha256 @@ -0,0 +1,3 @@ +65dd100ff6204346ac2f50f772721358b5c1451450ceb39a154542ee27b4c947 /tmp/linux-6.5.13-ctsi-1-v0.20.0.bin +4db885fdb4f013922d8ea8474768148ac4d45460a4ef30aea823836ea72ffed9 /tmp/rootfs-tools-v0.16.1.ext2 +691d229e16b866f97ee5cf27b88573d8434f064a959061c8755e91a048c2a811 /tmp/rollups-contracts-2.0.0-rc.12-anvil-nightly-2044faec64f99a21f0e5f0094458a973612d0712.tar.gz diff --git a/test/devnet/Dockerfile b/test/devnet/Dockerfile index 5644921d9..abdb47683 100644 --- a/test/devnet/Dockerfile +++ b/test/devnet/Dockerfile @@ -36,14 +36,7 @@ EOF # Install anvil state file and devnet deployment info. RUN mkdir -p ${DEVNET_BUILD_PATH} -RUN cat <<'EOF' > ${DEVNET_BUILD_PATH}/deployment.jq - . as $root | .contracts | - reduce to_entries[] as $c ( - {"name": $root.name, "ChainId": $root.chainId | tonumber}; - . + {($c.key): ($c.value.address)} - ) -EOF - +COPY test/devnet/deployment.jq ${DEVNET_BUILD_PATH} RUN <