# ───────────────────────────────────────────────────────────────────────────── # Docker stack unit tests — run with: tofu test (from repo root) # Requires OpenTofu >= 1.7.0 (mock_provider support) # # All providers are mocked — no real Docker host needed. # Tests cover: variable validation, plan structure, source type modes. # ───────────────────────────────────────────────────────────────────────────── mock_provider "docker" {} # ─── Shared baseline variables ──────────────────────────────────────────────── # Every run block below can override individual values. variables { ssh_host = "test-host.example.com" ssh_user = "deploy" ssh_key_path = "/tmp/test-key" app_name = "testapp" environment = "dev" openresty_source_type = "bind_mount" openresty_remote_config_path = "/opt/apps/testapp/openresty" openresty_external_port = 8080 db_name = "testdb" db_user = "testuser" db_password = "test-secret" } # ─── Smoke tests: valid configurations plan without error ───────────────────── run "valid_dev_bind_mount" { command = plan # Verifies the baseline dev/bind_mount config produces a valid plan. } run "valid_prod_bind_mount" { command = plan variables { environment = "prod" } } run "valid_git_clone_with_tag" { command = plan variables { openresty_source_type = "git_clone" openresty_git_repo = "https://gitea.example.com/org/myapp.git" openresty_git_ref = "v2.1.0" } } run "valid_git_clone_with_commit_sha" { command = plan variables { openresty_source_type = "git_clone" openresty_git_repo = "https://gitea.example.com/org/myapp.git" openresty_git_ref = "a3f8c1d2e4b56789abcdef0123456789abcdef01" } } run "valid_local_build" { command = plan variables { openresty_source_type = "local_build" openresty_local_build_context = "./openresty" openresty_dockerfile = "Dockerfile" } } # ─── Validation: git_ref must not be a mutable branch name ─────────────────── run "reject_git_ref_main" { command = plan variables { openresty_source_type = "git_clone" openresty_git_repo = "https://gitea.example.com/org/myapp.git" openresty_git_ref = "main" } expect_failures = [var.openresty_git_ref] } run "reject_git_ref_master" { command = plan variables { openresty_source_type = "git_clone" openresty_git_ref = "master" } expect_failures = [var.openresty_git_ref] } run "reject_git_ref_develop" { command = plan variables { openresty_source_type = "git_clone" openresty_git_ref = "develop" } expect_failures = [var.openresty_git_ref] } run "reject_git_ref_HEAD" { command = plan variables { openresty_source_type = "git_clone" openresty_git_ref = "HEAD" } expect_failures = [var.openresty_git_ref] } run "reject_git_ref_latest" { command = plan variables { openresty_source_type = "git_clone" openresty_git_ref = "latest" } expect_failures = [var.openresty_git_ref] } # ─── Validation: environment must be 'prod' or 'dev' ───────────────────────── run "reject_environment_staging" { command = plan variables { environment = "staging" } expect_failures = [var.environment] } run "reject_environment_production" { command = plan variables { environment = "production" } expect_failures = [var.environment] } # ─── Validation: openresty_source_type must be one of the three modes ───────── run "reject_invalid_source_type" { command = plan variables { openresty_source_type = "s3_bucket" } expect_failures = [var.openresty_source_type] }