Back to Repositories

Testing SSL Proxy Configuration Validation in Kamal

This test suite validates SSL proxy configuration handling in Kamal, focusing on host validation and SSL settings verification. The tests ensure proper handling of single and multiple hosts, error conditions, and SSL state management.

Test Coverage Overview

The test suite provides comprehensive coverage of proxy SSL configuration scenarios.

Key areas tested include:
  • Single host SSL validation
  • Multiple host configurations via both ‘host’ and ‘hosts’ parameters
  • Error handling for missing hosts
  • Conflict detection between host parameters
  • Basic SSL state verification

Implementation Analysis

The testing approach utilizes ActiveSupport::TestCase for structured unit testing of the Kamal::Configuration proxy functionality. The implementation employs setup fixtures for consistent test state and validates both positive and negative test cases using assert and assert_raises patterns.

Technical patterns include:
  • Fixture-based test setup
  • Hash-based configuration testing
  • Error condition validation
  • Boolean state verification

Technical Details

Testing infrastructure includes:
  • Minitest framework integration
  • ActiveSupport test cases
  • Custom configuration helper methods
  • Kamal::Configuration class testing
  • Kamal::ConfigurationError exception handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, clear setup methods, and comprehensive error checking. Each test focuses on a specific aspect of SSL configuration, with proper separation of concerns and clear naming conventions.

Notable practices:
  • DRY test setup using setup blocks
  • Descriptive test naming
  • Isolated test scenarios
  • Comprehensive edge case coverage
  • Clear error validation

basecamp/kamal

test/configuration/proxy_test.rb

            
require "test_helper"

class ConfigurationProxyTest < ActiveSupport::TestCase
  setup do
    @deploy = {
      service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
      builder: { "arch" => "amd64" }, servers: [ "1.1.1.1" ]
    }
  end

  test "ssl with host" do
    @deploy[:proxy] = { "ssl" => true, "host" => "example.com" }
    assert_equal true, config.proxy.ssl?
  end

  test "ssl with multiple hosts passed via host" do
    @deploy[:proxy] = { "ssl" => true, "host" => "example.com,anotherexample.com" }
    assert_equal true, config.proxy.ssl?
  end

  test "ssl with multiple hosts passed via hosts" do
    @deploy[:proxy] = { "ssl" => true, "hosts" => [ "example.com", "anotherexample.com" ] }
    assert_equal true, config.proxy.ssl?
  end

  test "ssl with no host" do
    @deploy[:proxy] = { "ssl" => true }
    assert_raises(Kamal::ConfigurationError) { config.proxy.ssl? }
  end

  test "ssl with both host and hosts" do
    @deploy[:proxy] = { "ssl" => true, host: "example.com", hosts: [ "anotherexample.com" ] }
    assert_raises(Kamal::ConfigurationError) { config.proxy.ssl? }
  end

  test "ssl false" do
    @deploy[:proxy] = { "ssl" => false }
    assert_not config.proxy.ssl?
  end

  private
    def config
      Kamal::Configuration.new(@deploy)
    end
end