Back to Repositories

Testing Docker Command Integration in Kamal Deployment Framework

This test suite validates Docker command functionality in the Kamal deployment tool, focusing on essential Docker operations like installation verification and system permissions. The tests ensure reliable Docker integration and command execution.

Test Coverage Overview

The test suite provides comprehensive coverage of Docker command operations in Kamal.

Key areas tested include:
  • Docker installation command verification
  • Docker version checking
  • Runtime status validation
  • Superuser permission verification
The suite ensures proper command string formation and system-level Docker operations.

Implementation Analysis

The testing approach utilizes Minitest’s assertion framework with ActiveSupport::TestCase as the base class. The implementation follows a setup-execution pattern where Docker commands are instantiated with predefined configurations.

Notable patterns include:
  • Configuration initialization in setup blocks
  • Command string validation through equality assertions
  • Shell command composition testing

Technical Details

Testing tools and configuration:
  • Framework: Minitest with ActiveSupport
  • Test Helper integration
  • Mock configuration using hash structures
  • Docker command string validation
The setup includes predefined configuration for registry credentials, server details, and architecture specifications.

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

Notable practices include:
  • Isolated test cases for each Docker command
  • Proper test setup and configuration management
  • Clear test naming conventions
  • Focused assertions for command string validation
The code organization follows a logical structure with setup and individual test methods clearly separated.

basecamp/kamal

test/commands/docker_test.rb

            
require "test_helper"

class CommandsDockerTest < ActiveSupport::TestCase
  setup do
    @config = {
      service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ], builder: { "arch" => "amd64" }
    }
    @docker = Kamal::Commands::Docker.new(Kamal::Configuration.new(@config))
  end

  test "install" do
    assert_equal "sh -c 'curl -fsSL https://get.docker.com || wget -O - https://get.docker.com || echo \"exit 1\"' | sh", @docker.install.join(" ")
  end

  test "installed?" do
    assert_equal "docker -v", @docker.installed?.join(" ")
  end

  test "running?" do
    assert_equal "docker version", @docker.running?.join(" ")
  end

  test "superuser?" do
    assert_equal '[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null', @docker.superuser?.join(" ")
  end
end