From e6051cd4ea3b19929c57200ff6f06a9d27f4137c Mon Sep 17 00:00:00 2001 From: Rick Newton-Rogers Date: Thu, 14 Nov 2024 06:28:52 +0000 Subject: [PATCH] Use Cassandra service for unit tests Use Cassandra service for unit tests. Motivation: To speed up and simplify unit tests which rely on a running Cassandra server. Modifications: * Use a Cassandra service instead of installing and running Cassandra in the test container * Tidy up existing actions workflows * Combine `main.yml` and `scheduled.yml` * Remove non-functional Swift 6 Language mode * Extend Cassandra timeouts in tests to combat flakiness seen in local tests. Result: Faster unit tests. --- .github/workflows/main.yml | 18 ++- .github/workflows/pull_request.yml | 47 ++++---- .github/workflows/scheduled.yml | 14 --- .github/workflows/unit_tests.yml | 109 ++++++++++++++++++ .../CassandraClientTests.swift | 2 + scripts/gha_run_unit_tests.sh | 40 ------- 6 files changed, 148 insertions(+), 82 deletions(-) delete mode 100644 .github/workflows/scheduled.yml create mode 100644 .github/workflows/unit_tests.yml delete mode 100755 scripts/gha_run_unit_tests.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6cb3eee..975343b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,15 +3,23 @@ name: Main on: push: branches: [main] + schedule: + - cron: "0 8,20 * * *" jobs: unit-tests: - name: Unit Tests - # Workaround https://github.com/nektos/act/issues/1875 - uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main + name: Unit tests + uses: ./.github/workflows/unit_tests.yml with: - name: "Unit tests" - matrix_linux_command: "I_AM_RUNNING_IN_CI=true ./scripts/gha_run_unit_tests.sh" + linux_5_9_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -strict-concurrency=complete" + linux_5_9_enabled: false + linux_5_10_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -strict-concurrency=complete" + linux_6_0_arguments_override: "--explicit-target-dependency-import-check error" + linux_6_0_enabled: false + linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error" + linux_nightly_6_0_enabled: false + linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error" + linux_nightly_main_enabled: false cxx-interop: name: Cxx interop diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index af6641d..b80d151 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -1,30 +1,31 @@ name: PR on: - pull_request: - types: [opened, reopened, synchronize] + pull_request: + types: [opened, reopened, synchronize] jobs: - soundness: - name: Soundness - uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main - with: - license_header_check_project_name: "Swift Cassandra Client" - api_breakage_check_enabled: false + soundness: + name: Soundness + uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main + with: + license_header_check_project_name: "Swift Cassandra Client" + api_breakage_check_enabled: false - unit-tests: - name: Unit Tests - # Workaround https://github.com/nektos/act/issues/1875 - uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main - with: - name: "Unit tests" - matrix_linux_command: "I_AM_RUNNING_IN_CI=true ./scripts/gha_run_unit_tests.sh" + new-unit-tests: + name: Unit tests + uses: ./.github/workflows/unit_tests.yml + with: + linux_5_9_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -strict-concurrency=complete" + linux_5_9_enabled: false + linux_5_10_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -strict-concurrency=complete" + linux_6_0_arguments_override: "--explicit-target-dependency-import-check error" + linux_6_0_enabled: false + linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error" + linux_nightly_6_0_enabled: false + linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error" + linux_nightly_main_enabled: false - cxx-interop: - name: Cxx interop - uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main - - swift-6-language-mode: - name: Swift 6 Language Mode - uses: apple/swift-nio/.github/workflows/swift_6_language_mode.yml@main - if: false # Disabled for now. + cxx-interop: + name: Cxx interop + uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main diff --git a/.github/workflows/scheduled.yml b/.github/workflows/scheduled.yml deleted file mode 100644 index a96196d..0000000 --- a/.github/workflows/scheduled.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Scheduled - -on: - schedule: - - cron: "0 8,20 * * *" - -jobs: - unit-tests: - name: Unit Tests - # Workaround https://github.com/nektos/act/issues/1875 - uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main - with: - name: "Unit tests" - matrix_linux_command: "I_AM_RUNNING_IN_CI=true ./scripts/gha_run_unit_tests.sh" diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml new file mode 100644 index 0000000..8e6d1e2 --- /dev/null +++ b/.github/workflows/unit_tests.yml @@ -0,0 +1,109 @@ +name: Unit tests + +on: + workflow_call: + inputs: + linux_5_9_enabled: + type: boolean + description: "Boolean to enable the Linux 5.9 Swift version matrix job. Defaults to true." + default: true + linux_5_9_arguments_override: + type: string + description: "The arguments passed to swift test in the Linux 5.9 Swift version matrix job." + default: "" + linux_5_10_enabled: + type: boolean + description: "Boolean to enable the Linux 5.10 Swift version matrix job. Defaults to true." + default: true + linux_5_10_arguments_override: + type: string + description: "The arguments passed to swift test in the Linux 5.10 Swift version matrix job." + default: "" + linux_6_0_enabled: + type: boolean + description: "Boolean to enable the Linux 6.0 Swift version matrix job. Defaults to true." + default: true + linux_6_0_arguments_override: + type: string + description: "The arguments passed to swift test in the Linux 6.0 Swift version matrix job." + default: "" + linux_nightly_6_0_enabled: + type: boolean + description: "Boolean to enable the Linux nightly 6.0 Swift version matrix job. Defaults to true." + default: true + linux_nightly_6_0_arguments_override: + type: string + description: "The arguments passed to swift test in the Linux nightly 6.0 Swift version matrix job." + default: "" + linux_nightly_main_enabled: + type: boolean + description: "Boolean to enable the Linux nightly main Swift version matrix job. Defaults to true." + default: true + linux_nightly_main_arguments_override: + type: string + description: "The arguments passed to swift test in the Linux nightly main Swift version matrix job." + default: "" + +jobs: + unit-tests: + name: Unit tests + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # We are specifying only the major and minor of the docker images to automatically pick up the latest patch release + swift: + - image: "swift:5.9-jammy" + swift_version: "5.9" + enabled: ${{ inputs.linux_5_9_enabled }} + - image: "swift:5.10-jammy" + swift_version: "5.10" + enabled: ${{ inputs.linux_5_10_enabled }} + - image: "swift:6.0-jammy" + swift_version: "6.0" + enabled: ${{ inputs.linux_6_0_enabled }} + - image: "swiftlang/swift:nightly-6.0-jammy" + swift_version: "nightly-6.0" + enabled: ${{ inputs.linux_nightly_6_0_enabled }} + - image: "swiftlang/swift:nightly-main-jammy" + swift_version: "nightly-main" + enabled: ${{ inputs.linux_nightly_main_enabled }} + steps: + - name: Checkout repository + if: ${{ matrix.swift.enabled }} + uses: actions/checkout@v4 + with: + persist-credentials: false + submodules: true + - name: Mark the workspace as safe + if: ${{ matrix.swift.enabled }} + # https://github.com/actions/checkout/issues/766 + run: git config --global --add safe.directory ${GITHUB_WORKSPACE} + - name: Run matrix job + if: ${{ matrix.swift.enabled }} + env: + SWIFT_VERSION: ${{ matrix.swift.swift_version }} + COMMAND: "swift test" + COMMAND_OVERRIDE_5_9: "swift test ${{ inputs.linux_5_9_arguments_override }}" + COMMAND_OVERRIDE_5_10: "swift test ${{ inputs.linux_5_10_arguments_override }}" + COMMAND_OVERRIDE_6_0: "swift test ${{ inputs.linux_6_0_arguments_override }}" + COMMAND_OVERRIDE_NIGHTLY_6_0: "swift test ${{ inputs.linux_nightly_6_0_arguments_override }}" + COMMAND_OVERRIDE_NIGHTLY_MAIN: "swift test ${{ inputs.linux_nightly_main_arguments_override }}" + CASSANDRA_HOST: cassandra + CASSANDRA_CQL_PORT: 9042 + CASSANDRA_USER: cassandra + CASSANDRA_PASSWORD: cassandra + CASSANDRA_KEYSPACE: cassandra + run: | + apt-get -qq update && apt-get -qq -y install curl + curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/check-matrix-job.sh | bash + container: + image: ${{ matrix.swift.image }} + services: + cassandra: + image: cassandra:3.11 + env: + CASSANDRA_CQL_PORT: 9042 + CASSANDRA_USER: cassandra + CASSANDRA_PASSWORD: cassandra + CASSANDRA_KEYSPACE: cassandra diff --git a/Tests/CassandraClientTests/CassandraClientTests.swift b/Tests/CassandraClientTests/CassandraClientTests.swift index 8497587..c8c516c 100644 --- a/Tests/CassandraClientTests/CassandraClientTests.swift +++ b/Tests/CassandraClientTests/CassandraClientTests.swift @@ -38,6 +38,8 @@ final class Tests: XCTestCase { self.configuration.username = env["CASSANDRA_USER"] self.configuration.password = env["CASSANDRA_PASSWORD"] self.configuration.keyspace = keyspace + self.configuration.requestTimeoutMillis = UInt32(24_000) // Default: 12_000 ms + self.configuration.connectTimeoutMillis = UInt32(10_000) // Default: 5_000 ms var logger = Logger(label: "test") logger.logLevel = .debug diff --git a/scripts/gha_run_unit_tests.sh b/scripts/gha_run_unit_tests.sh deleted file mode 100755 index 362b22c..0000000 --- a/scripts/gha_run_unit_tests.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the Swift Cassandra Client open source project -## -## Copyright (c) 2017-2018 Apple Inc. and the Swift Cassandra Client project authors -## Licensed under Apache License v2.0 -## -## See LICENSE.txt for license information -## See CONTRIBUTORS.txt for the list of Swift Cassandra Client project authors -## -## SPDX-License-Identifier: Apache-2.0 -## -##===----------------------------------------------------------------------===## - -set -e - -if [ -z "${I_AM_RUNNING_IN_CI}" ] -then - echo "Not running in CI" - exit 1 -fi - -printf '#!/bin/sh\nexit 0' > /usr/sbin/policy-rc.d -echo "deb [signed-by=/etc/apt/keyrings/apache-cassandra.asc] https://debian.cassandra.apache.org 41x main" | tee -a /etc/apt/sources.list.d/cassandra.sources.list -curl -o /etc/apt/keyrings/apache-cassandra.asc https://downloads.apache.org/cassandra/KEYS -apt-get update -apt install -y -q default-jre -apt-get install -y -q cassandra -swift build --explicit-target-dependency-import-check error -while ! nodetool status 2>/dev/null -do - echo Waiting for Cassandra.. - sleep 3 -done - -export CASSANDRA_USER=cassandra -export CASSANDRA_PASSWORD=cassandra -export CASSANDRA_KEYSPACE=cassandra -swift test --explicit-target-dependency-import-check error