343 lines
9.4 KiB
Bash
Executable File
343 lines
9.4 KiB
Bash
Executable File
#!/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 with special characters in volume paths
|
|
run parse_volume "/host/path/with spaces:/container/path:ro"
|
|
assert_success
|
|
[[ "${lines[0]}" == "/host/path/with spaces" ]]
|
|
[[ "${lines[1]}" == "/container/path" ]]
|
|
[[ "${lines[2]}" == "ro" ]]
|
|
|
|
# Test with relative paths
|
|
run parse_volume "./relative/path:/absolute/path"
|
|
assert_success
|
|
[[ "${lines[0]}" == "./relative/path" ]]
|
|
[[ "${lines[1]}" == "/absolute/path" ]]
|
|
[[ "${lines[2]}" == "rw" ]]
|
|
}
|
|
|
|
@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"
|
|
|
|
# Simple implementation for testing
|
|
case "$image" in
|
|
*postgres*) echo "postgres" ; return 0 ;;
|
|
*redis*) echo "redis" ; return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
# Test with official images
|
|
echo "Testing with postgres:13"
|
|
run should_use_dokku_plugin "postgres:13"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with custom image names (should fail as no known plugin matches)
|
|
echo "Testing with custom-image"
|
|
run should_use_dokku_plugin "custom-image"
|
|
echo "Status: $status, Output: $output"
|
|
assert_failure
|
|
|
|
# Test with custom/redis (should work as it contains 'redis')
|
|
echo "Testing with custom/redis:latest"
|
|
run should_use_dokku_plugin "custom/redis:latest"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "redis"
|
|
|
|
# Test with Docker Hub official images (should work as it contains 'postgres')
|
|
echo "Testing with library/postgres:13"
|
|
run should_use_dokku_plugin "library/postgres:13"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with Docker Hub official images with registry (should work as it contains 'postgres')
|
|
echo "Testing with docker.io/library/postgres:13"
|
|
run should_use_dokku_plugin "docker.io/library/postgres:13"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with private registry (should work as it contains 'postgres')
|
|
echo "Testing with myregistry.example.com/postgres:13"
|
|
run should_use_dokku_plugin "myregistry.example.com/postgres:13"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with custom repository path (should work as it contains 'postgres')
|
|
echo "Testing with myorg/postgres:13"
|
|
run should_use_dokku_plugin "myorg/postgres:13"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with latest tag
|
|
echo "Testing with postgres:latest"
|
|
run should_use_dokku_plugin "postgres:latest"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
# Test with no tag
|
|
echo "Testing with postgres"
|
|
run should_use_dokku_plugin "postgres"
|
|
echo "Status: $status, Output: $output"
|
|
assert_success
|
|
assert_output "postgres"
|
|
|
|
|
|
# Test with custom/unknown (should fail as no known plugin matches)
|
|
echo "Testing with custom/unknown:latest"
|
|
run should_use_dokku_plugin "custom/unknown:latest"
|
|
echo "Status: $status, Output: $output"
|
|
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 ""
|
|
}
|