Skip to content

Upgrade bats-core to latest and fix background process handling in tests #19

Description

@mengesb

Context

PR #18 upgraded GitHub Actions but had to pin bats-core at v1.2.1 because newer versions (1.7+) hang on the test suite. The root cause is well-understood and the fix is straightforward.

Problem

bats-core 1.7+ changed behavior: it now waits for all background processes spawned during setup_file() to exit before considering a test file complete. Two test files start ec2-metadata-mock as a background process in setup_file() but never kill it in teardown_file():

  • test/aws-credentials.bats
  • test/vault-iam-auth.bats

This causes bats to hang indefinitely after all tests pass, waiting for the mock to exit.

Fix

Add pkill -f ec2-metadata-mock || true to teardown_file() in both files:

function teardown_file() {
  pkill -f ec2-metadata-mock || true
  unset EC2_METADATA_MOCK
}

Once that's in place, update .github/workflows/bats-unit.yml to install a modern bats version:

    - name: Setup BATS
      shell: bash
      run: |
        git clone --depth 1 --branch v1.11.1 https://github.com/bats-core/bats-core.git /tmp/bats-core
        sudo /tmp/bats-core/install.sh /usr/local

Verification

After making the changes:

  1. Run bats test/requirements.bats to confirm bats is functional
  2. Run each test file individually and confirm it exits cleanly: bats test/aws-credentials.bats
  3. Run the full suite sequentially to confirm no port conflicts between files

Additional notes

  • test/gcp-credentials.bats and test/vault-gce-auth.bats may have a similar pattern with the GCE metadata mock — check those too
  • The 3>&- trick on the ec2-metadata-mock launch line was a workaround for older bats FD inheritance; it's no longer sufficient in 1.7+
  • Current pinned version (v1.2.1) works but misses years of bats improvements (parallel test execution, better TAP output, etc.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions