Back to Repositories

Testing Container Lifecycle Management Operations in Kamal

This integration test suite validates core application lifecycle management operations in Kamal, focusing on container orchestration and deployment workflows. The test ensures reliable application deployment, startup/shutdown, logging, and container management functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of essential application management operations:

  • Deployment and lifecycle management (start, stop, boot)
  • Container operations (logs, images, containers)
  • Runtime execution capabilities (exec commands)
  • Cleanup and removal procedures

Implementation Analysis

The testing approach employs a sequential workflow validation pattern using Minitest framework. Each operation is verified with specific assertions to confirm the expected state changes and command outputs.

The implementation leverages custom helper methods like assert_app_is_up and wait_for_app_to_be_up to ensure reliable test execution across asynchronous operations.

Technical Details

  • Testing Framework: Minitest
  • Custom Assertions: assert_app_is_up, assert_app_not_found
  • Command Execution: kamal helper method
  • Output Capture: Using capture: true parameter
  • Regular Expression Matching: For version and container validation

Best Practices Demonstrated

The test exhibits several testing best practices including comprehensive lifecycle coverage, proper setup and teardown procedures, and thorough validation of command outputs.

  • Systematic state verification
  • Proper cleanup after test execution
  • Robust output validation using regex patterns
  • Handling of asynchronous operations

basecamp/kamal

test/integration/app_test.rb

            
require_relative "integration_test"

class AppTest < IntegrationTest
  test "stop, start, boot, logs, images, containers, exec, remove" do
    kamal :deploy

    assert_app_is_up

    kamal :app, :stop

    assert_app_not_found

    kamal :app, :start

    # kamal app start does not wait
    wait_for_app_to_be_up

    kamal :app, :boot

    wait_for_app_to_be_up

    logs = kamal :app, :logs, capture: true
    assert_match "App Host: vm1", logs
    assert_match "App Host: vm2", logs
    assert_match "GET /version HTTP/1.1", logs

    images = kamal :app, :images, capture: true
    assert_match "App Host: vm1", images
    assert_match "App Host: vm2", images
    assert_match /registry:4443\/app\s+#{latest_app_version}/, images
    assert_match /registry:4443\/app\s+latest/, images

    containers = kamal :app, :containers, capture: true
    assert_match "App Host: vm1", containers
    assert_match "App Host: vm2", containers
    assert_match "registry:4443/app:#{latest_app_version}", containers
    assert_match "registry:4443/app:latest", containers

    exec_output = kamal :app, :exec, :ps, capture: true
    assert_match "App Host: vm1", exec_output
    assert_match "App Host: vm2", exec_output
    assert_match /1 root      0:\d\d ps/, exec_output

    exec_output = kamal :app, :exec, "--reuse", :ps, capture: true
    assert_match "App Host: vm2", exec_output
    assert_match "App Host: vm1", exec_output
    assert_match /1 root      0:\d\d nginx/, exec_output

    kamal :app, :remove

    assert_app_not_found
    assert_app_directory_removed
  end
end