Back to Repositories

Testing Docker Registry Authentication Commands in Kamal

This test suite validates Docker registry authentication functionality in Kamal, focusing on login/logout operations and environment variable handling. The tests ensure secure credential management and proper command generation for Docker registry interactions.

Test Coverage Overview

The test suite provides comprehensive coverage of Docker registry authentication scenarios. Key functionality includes:

  • Basic registry login with username/password
  • Environment variable-based credential handling
  • Special character escaping in credentials
  • Registry logout verification
Integration points focus on Docker command generation and configuration parsing.

Implementation Analysis

The testing approach utilizes Minitest’s ActiveSupport::TestCase framework for structured unit testing. The implementation employs setup fixtures for configuration and helper methods for environment manipulation.

Notable patterns include:
  • Configuration object initialization
  • Command string assertion testing
  • Environment variable mocking using with_test_secrets

Technical Details

Testing tools and configuration:

  • Test Framework: Minitest
  • Test Helper Methods: with_test_secrets for environment simulation
  • Configuration Setup: Hash-based configuration objects
  • Command Generation: String manipulation and joining

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby development. Notable practices include:

  • Isolated test cases with clear setup/teardown
  • Comprehensive edge case coverage
  • Security-focused testing for credential handling
  • DRY principle application through helper methods
  • Clear test naming conventions

basecamp/kamal

test/commands/registry_test.rb

            
require "test_helper"

class CommandsRegistryTest < ActiveSupport::TestCase
  setup do
    @config = { service: "app",
      image: "dhh/app",
      registry: { "username" => "dhh",
        "password" => "secret",
        "server" => "hub.docker.com"
      },
      builder: { "arch" => "amd64" },
      servers: [ "1.1.1.1" ]
    }
  end

  test "registry login" do
    assert_equal \
      "docker login hub.docker.com -u \"dhh\" -p \"secret\"",
      registry.login.join(" ")
  end

  test "registry login with ENV password" do
    with_test_secrets("secrets" => "KAMAL_REGISTRY_PASSWORD=more-secret") do
      @config[:registry]["password"] = [ "KAMAL_REGISTRY_PASSWORD" ]

      assert_equal \
        "docker login hub.docker.com -u \"dhh\" -p \"more-secret\"",
        registry.login.join(" ")
    end
  end

  test "registry login escape password" do
    with_test_secrets("secrets" => "KAMAL_REGISTRY_PASSWORD=more-secret'\"") do
      @config[:registry]["password"] = [ "KAMAL_REGISTRY_PASSWORD" ]

      assert_equal \
        "docker login hub.docker.com -u \"dhh\" -p \"more-secret'\\\"\"",
        registry.login.join(" ")
    end
  end

  test "registry login with ENV username" do
    with_test_secrets("secrets" => "KAMAL_REGISTRY_USERNAME=also-secret") do
      @config[:registry]["username"] = [ "KAMAL_REGISTRY_USERNAME" ]

      assert_equal \
        "docker login hub.docker.com -u \"also-secret\" -p \"secret\"",
        registry.login.join(" ")
    end
  end

  test "registry logout" do
    assert_equal \
      "docker logout hub.docker.com",
      registry.logout.join(" ")
  end

  private
    def registry
      Kamal::Commands::Registry.new Kamal::Configuration.new(@config)
    end
end