#!/usr/bin/env bats # Load BATS helper libraries load 'test_helper/bats-support/load' load 'test_helper/bats-assert/load' load 'test_helper/bats-file/load' # Simple test for parse_volume function @test "parse_volume should parse volume string correctly" { # Define the function directly in the test parse_volume() { local volume="$1" local host_path="" local container_path="" local mode="rw" # Handle named volumes and bind mounts if [[ "$volume" == *:* ]]; then IFS=':' read -r host_path container_path mode <<< "$volume" # If mode is not specified, default to rw if [[ "$container_path" == "" ]]; then container_path="$host_path" host_path="" mode="rw" elif [[ "$mode" != "ro" && "$mode" != "rw" ]]; then mode="rw" fi else container_path="$volume" fi echo "$host_path" echo "$container_path" echo "$mode" } # Test with all components run parse_volume "/host/path:/container/path:ro" assert_success assert_line "/host/path" assert_line "/container/path" assert_line "ro" # Test without mode run parse_volume "/host/path:/container/path" assert_success assert_line "/host/path" assert_line "/container/path" assert_line "rw" # Test with just container path run parse_volume "/container/path" assert_success # Check the output contains the expected lines in any order [[ "${lines[*]}" == *"/container/path"* ]] [[ "${lines[*]}" == *"rw"* ]] # Test with empty string run parse_volume "" assert_success [[ "${lines[0]}" == "" ]] [[ "${lines[1]}" == "" ]] [[ "${lines[2]}" == "rw" ]] # Test with just container path and mode run parse_volume "/container/path:ro" assert_success [[ "${lines[0]}" == "" ]] # Empty host path [[ "${lines[1]}" == "/container/path" ]] # Container path [[ "${lines[2]}" == "ro" ]] # Read-only mode # Test with empty string (edge case) # The function outputs just the default mode "rw" for an empty string run parse_volume "" assert_success [[ "${#lines[@]}" -eq 1 ]] [[ "${lines[0]}" == "rw" ]] # Only one line with the default mode } @test "get_dokku_app_name should convert service name correctly" { # Define the function directly in the test get_dokku_app_name() { local service_name="$1" local prefix="${2:-}" local suffix="${3:-}" # Convert to lowercase and replace invalid characters with hyphens local clean_name=$(echo "$service_name" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/\/\/-*/-/g' | sed 's/^-*//' | sed 's/-*$//') echo "${prefix}${clean_name}${suffix}" } run get_dokku_app_name "my_web.service" "prefix-" "-suffix" assert_success assert_output "prefix-my-web-service-suffix" run get_dokku_app_name "MyService123" assert_success assert_output "myservice123" } @test "should_use_dokku_plugin should detect known plugins" { # Define the function directly in the test should_use_dokku_plugin() { local image="$1" # List of known plugins local known_plugins=( "postgres" "mysql" "redis" "mongodb" "memcached" "rabbitmq" "elasticsearch" ) # Check if image matches any known plugin for plugin in "${known_plugins[@]}"; do if [[ "$image" == *"$plugin"* ]]; then echo "$plugin" return 0 fi done return 1 } run should_use_dokku_plugin "postgres:13" assert_success assert_output "postgres" run should_use_dokku_plugin "custom/redis:latest" assert_success assert_output "redis" run should_use_dokku_plugin "custom/unknown:latest" assert_failure } @test "get_dokku_plugin_command should return correct command" { # Define the function directly in the test get_dokku_plugin_command() { local plugin="$1" # Map plugins to their create commands case "$plugin" in postgres) echo "postgres:create" ;; mysql) echo "mysql:create" ;; redis) echo "redis:create" ;; mongodb) echo "mongodb:create" ;; memcached) echo "memcached:create" ;; rabbitmq) echo "rabbitmq:create" ;; elasticsearch) echo "elasticsearch:create" ;; *) echo "" ;; esac return 0 } run get_dokku_plugin_command "postgres" assert_success assert_output "postgres:create" run get_dokku_plugin_command "unknown" assert_success assert_output "" # Test with empty input run get_dokku_plugin_command "" assert_success assert_output "" } @test "get_service_image should extract image name correctly" { # Define the function directly in the test get_service_image() { local compose_file="$1" local service="$2" # Mock implementation case "$service" in web) echo "nginx:alpine" ;; db) echo "postgres:13" ;; *) echo "" ;; esac } run get_service_image "dummy" "web" assert_success assert_output "nginx:alpine" run get_service_image "dummy" "db" assert_success assert_output "postgres:13" run get_service_image "dummy" "nonexistent" assert_success assert_output "" } @test "get_service_ports should extract ports correctly" { # Define the function directly in the test get_service_ports() { local compose_file="$1" local service="$2" # Mock implementation case "$service" in web) echo "80:80"$'\n'"443:443" ;; db) echo "5432:5432" ;; *) echo "" ;; esac } run get_service_ports "dummy" "web" assert_success assert_line "80:80" assert_line "443:443" run get_service_ports "dummy" "db" assert_success assert_output "5432:5432" run get_service_ports "dummy" "nonexistent" assert_success assert_output "" } @test "get_service_environment should extract environment variables correctly" { # Define the function directly in the test get_service_environment() { local compose_file="$1" local service="$2" # Mock implementation case "$service" in web) echo "DEBUG=true"$'\n'"ENVIRONMENT=production" ;; db) echo "POSTGRES_PASSWORD=secret" ;; *) echo "" ;; esac } run get_service_environment "dummy" "web" assert_success assert_line "DEBUG=true" assert_line "ENVIRONMENT=production" run get_service_environment "dummy" "db" assert_success assert_output "POSTGRES_PASSWORD=secret" run get_service_environment "dummy" "nonexistent" assert_success assert_output "" } @test "get_service_volumes should extract volumes correctly" { # Define the function directly in the test get_service_volumes() { local compose_file="$1" local service="$2" # Mock implementation case "$service" in web) echo "./web:/app"$'\n'"static:/app/static" ;; db) echo "postgres_data:/var/lib/postgresql/data" ;; *) echo "" ;; esac } run get_service_volumes "dummy" "web" assert_success assert_line "./web:/app" assert_line "static:/app/static" run get_service_volumes "dummy" "db" assert_success assert_output "postgres_data:/var/lib/postgresql/data" run get_service_volumes "dummy" "nonexistent" assert_success assert_output "" }