# Lifecycle Deployment Tests with OTEL Validation
# Tests: staging deployment, production deployment, validation, verification

[meta]
name = "lifecycle_deployment_tests"
description = "Test lifecycle deployment flows with multi-environment validation"
version = "1.0.0"

[otel]
exporter = "otlp"
endpoint = "http://localhost:4318"
service_name = "ggen-lifecycle-deploy"
enable_tracing = true
enable_metrics = true

[service.ggen]
plugin = "generic_container"
image = "rust:latest"
volumes = ["/tmp/ggen-test:/workspace"]
working_dir = "/workspace"

# Test 1: Staging deployment with validation
[[scenario]]
name = "deploy_to_staging_with_validation"
service = "ggen"
run = """
mkdir -p /workspace/staging-deploy && cd /workspace/staging-deploy && \
cat > make.toml << 'EOF'
[project]
name = "staging-deployment"

[lifecycle.validate-staging]
command = "echo 'Validating staging configuration'"

[lifecycle.deploy-staging]
commands = [
    "echo 'Building for staging'",
    "echo 'Deploying to staging environment'",
    "echo 'Running smoke tests'"
]

[hooks]
before_deploy-staging = ["validate-staging"]
EOF
ggen lifecycle run deploy-staging
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.validate-staging"
must_precede = ["ggen.lifecycle.deploy-staging"]
attributes = { "environment" = "staging", "hook.type" = "before" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-staging"
must_follow = ["ggen.lifecycle.validate-staging"]
attributes = { "environment" = "staging", "deployment.type" = "standard" }

[scenario.expect.window]
parent = "ggen.lifecycle.deploy-staging"
must_contain = ["ggen.deploy.build", "ggen.deploy.push", "ggen.deploy.verify"]

[scenario.expect.temporal]
strict_ordering = [
  "ggen.lifecycle.validate-staging",
  "ggen.lifecycle.deploy-staging"
]

# Test 2: Production deployment with comprehensive checks
[[scenario]]
name = "deploy_to_production_with_checks"
service = "ggen"
run = """
mkdir -p /workspace/prod-deploy && cd /workspace/prod-deploy && \
cat > make.toml << 'EOF'
[project]
name = "production-deployment"

[lifecycle.pre-deploy-checks]
commands = [
    "echo 'Checking production readiness'",
    "echo 'Validating environment variables'",
    "echo 'Running security scan'"
]

[lifecycle.deploy-production]
commands = [
    "echo 'Building production artifacts'",
    "echo 'Deploying to production'",
    "echo 'Running health checks'",
    "echo 'Verifying deployment'"
]

[lifecycle.post-deploy-monitoring]
command = "echo 'Setting up monitoring and alerts'"

[hooks]
before_deploy-production = ["pre-deploy-checks"]
after_deploy-production = ["post-deploy-monitoring"]
EOF
ggen lifecycle run deploy-production
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.pre-deploy-checks"
must_precede = ["ggen.lifecycle.deploy-production"]
attributes = { "environment" = "production", "check.type" = "pre_deployment" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-production"
must_precede = ["ggen.lifecycle.post-deploy-monitoring"]
must_follow = ["ggen.lifecycle.pre-deploy-checks"]
attributes = { "environment" = "production", "deployment.critical" = "true" }

[[scenario.expect.span]]
name = "ggen.lifecycle.post-deploy-monitoring"
must_follow = ["ggen.lifecycle.deploy-production"]
attributes = { "monitoring.enabled" = "true" }

[scenario.expect.window]
parent = "ggen.lifecycle.deploy-production"
must_contain = ["ggen.deploy.build", "ggen.deploy.healthcheck", "ggen.deploy.verify"]
min_child_spans = 4

[scenario.expect.temporal]
strict_ordering = [
  "ggen.lifecycle.pre-deploy-checks",
  "ggen.lifecycle.deploy-production",
  "ggen.lifecycle.post-deploy-monitoring"
]

# Test 3: Deployment validation failure prevents deploy
[[scenario]]
name = "validation_failure_prevents_deploy"
service = "ggen"
run = """
mkdir -p /workspace/validation-fail && cd /workspace/validation-fail && \
cat > make.toml << 'EOF'
[project]
name = "validation-failure"

[lifecycle.validate]
command = "exit 1"

[lifecycle.deploy]
command = "echo 'Should not deploy'"

[hooks]
before_deploy = ["validate"]
EOF
ggen lifecycle run deploy || echo "Deploy prevented by validation"
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.validate"
expect_error = true
attributes = { "validation.result" = "failed" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy"
should_not_exist = true

[[scenario.expect.span]]
name = "ggen.deploy.prevented"
must_follow = ["ggen.lifecycle.validate"]
attributes = { "reason" = "validation_failed" }

[scenario.expect.graph]
must_not_include = [
  ["ggen.lifecycle.validate", "ggen.lifecycle.deploy"]
]

# Test 4: Blue-green deployment
[[scenario]]
name = "blue_green_deployment"
service = "ggen"
run = """
mkdir -p /workspace/blue-green && cd /workspace/blue-green && \
cat > make.toml << 'EOF'
[project]
name = "blue-green-deployment"

[lifecycle.deploy-green]
commands = [
    "echo 'Deploying to green environment'",
    "echo 'Running health checks on green'",
    "echo 'Green environment ready'"
]

[lifecycle.switch-traffic]
commands = [
    "echo 'Routing 10% traffic to green'",
    "echo 'Monitoring metrics'",
    "echo 'Routing 100% traffic to green'",
    "echo 'Decommissioning blue'"
]
EOF
ggen lifecycle run deploy-green && \
ggen lifecycle run switch-traffic
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-green"
must_precede = ["ggen.lifecycle.switch-traffic"]
attributes = { "deployment.strategy" = "blue_green", "target" = "green" }

[[scenario.expect.span]]
name = "ggen.deploy.healthcheck"
parent = "ggen.lifecycle.deploy-green"
attributes = { "environment" = "green" }

[[scenario.expect.span]]
name = "ggen.lifecycle.switch-traffic"
must_follow = ["ggen.lifecycle.deploy-green"]
attributes = { "traffic.strategy" = "gradual" }

[scenario.expect.temporal]
strict_ordering = [
  "ggen.lifecycle.deploy-green",
  "ggen.deploy.healthcheck",
  "ggen.lifecycle.switch-traffic"
]

# Test 5: Canary deployment
[[scenario]]
name = "canary_deployment"
service = "ggen"
run = """
mkdir -p /workspace/canary && cd /workspace/canary && \
cat > make.toml << 'EOF'
[project]
name = "canary-deployment"

[lifecycle.deploy-canary]
commands = [
    "echo 'Deploying canary version'",
    "echo 'Routing 5% traffic to canary'",
    "echo 'Monitoring canary metrics'"
]

[lifecycle.promote-canary]
commands = [
    "echo 'Canary metrics healthy'",
    "echo 'Promoting canary to full deployment'",
    "echo 'Deployment complete'"
]
EOF
ggen lifecycle run deploy-canary && \
ggen lifecycle run promote-canary
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-canary"
must_precede = ["ggen.lifecycle.promote-canary"]
attributes = { "deployment.strategy" = "canary", "traffic.percentage" = "5" }

[[scenario.expect.span]]
name = "ggen.deploy.monitor.canary"
parent = "ggen.lifecycle.deploy-canary"
attributes = { "metrics.monitored" = "true" }

[[scenario.expect.span]]
name = "ggen.lifecycle.promote-canary"
must_follow = ["ggen.lifecycle.deploy-canary"]
attributes = { "promotion.result" = "success" }

# Test 6: Multi-region deployment
[[scenario]]
name = "multi_region_deployment"
service = "ggen"
run = """
mkdir -p /workspace/multi-region && cd /workspace/multi-region && \
cat > make.toml << 'EOF'
[project]
name = "multi-region-deployment"

[lifecycle.deploy-us-east]
command = "echo 'Deploying to us-east-1'"

[lifecycle.deploy-eu-west]
command = "echo 'Deploying to eu-west-1'"

[lifecycle.deploy-ap-south]
command = "echo 'Deploying to ap-south-1'"

[lifecycle.verify-all-regions]
commands = [
    "echo 'Verifying us-east-1'",
    "echo 'Verifying eu-west-1'",
    "echo 'Verifying ap-south-1'"
]
EOF
ggen lifecycle run deploy-us-east && \
ggen lifecycle run deploy-eu-west && \
ggen lifecycle run deploy-ap-south && \
ggen lifecycle run verify-all-regions
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-us-east"
attributes = { "region" = "us-east-1" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-eu-west"
attributes = { "region" = "eu-west-1" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-ap-south"
attributes = { "region" = "ap-south-1" }

[[scenario.expect.span]]
name = "ggen.lifecycle.verify-all-regions"
must_follow = ["ggen.lifecycle.deploy-us-east", "ggen.lifecycle.deploy-eu-west", "ggen.lifecycle.deploy-ap-south"]

[scenario.expect.graph]
must_include = [
  ["ggen.lifecycle.deploy-us-east", "ggen.lifecycle.verify-all-regions"],
  ["ggen.lifecycle.deploy-eu-west", "ggen.lifecycle.verify-all-regions"],
  ["ggen.lifecycle.deploy-ap-south", "ggen.lifecycle.verify-all-regions"]
]

# Test 7: Deployment with artifact publishing
[[scenario]]
name = "deployment_with_artifacts"
service = "ggen"
run = """
mkdir -p /workspace/artifacts && cd /workspace/artifacts && \
cat > make.toml << 'EOF'
[project]
name = "artifact-deployment"

[lifecycle.build-artifacts]
commands = [
    "echo 'Building release artifacts'",
    "echo 'Generating checksums'",
    "echo 'Creating deployment package'"
]

[lifecycle.publish-artifacts]
commands = [
    "echo 'Publishing to artifact registry'",
    "echo 'Updating metadata'",
    "echo 'Artifacts published'"
]

[lifecycle.deploy-from-artifacts]
commands = [
    "echo 'Fetching published artifacts'",
    "echo 'Verifying checksums'",
    "echo 'Deploying from artifacts'"
]
EOF
ggen lifecycle run build-artifacts && \
ggen lifecycle run publish-artifacts && \
ggen lifecycle run deploy-from-artifacts
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.build-artifacts"
must_precede = ["ggen.lifecycle.publish-artifacts"]

[[scenario.expect.span]]
name = "ggen.artifacts.generate"
parent = "ggen.lifecycle.build-artifacts"
attributes = { "artifact.type" = "release" }

[[scenario.expect.span]]
name = "ggen.lifecycle.publish-artifacts"
must_precede = ["ggen.lifecycle.deploy-from-artifacts"]
must_follow = ["ggen.lifecycle.build-artifacts"]

[[scenario.expect.span]]
name = "ggen.artifacts.publish"
parent = "ggen.lifecycle.publish-artifacts"
attributes = { "registry.type" = "artifact_registry" }

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-from-artifacts"
must_follow = ["ggen.lifecycle.publish-artifacts"]

[scenario.expect.temporal]
strict_ordering = [
  "ggen.lifecycle.build-artifacts",
  "ggen.lifecycle.publish-artifacts",
  "ggen.lifecycle.deploy-from-artifacts"
]

# Test 8: Deployment with database migrations
[[scenario]]
name = "deployment_with_migrations"
service = "ggen"
run = """
mkdir -p /workspace/migrations && cd /workspace/migrations && \
cat > make.toml << 'EOF'
[project]
name = "migration-deployment"

[lifecycle.backup-database]
command = "echo 'Creating database backup'"

[lifecycle.run-migrations]
commands = [
    "echo 'Running database migrations'",
    "echo 'Validating schema changes'",
    "echo 'Migrations complete'"
]

[lifecycle.deploy-application]
command = "echo 'Deploying application with new schema'"

[hooks]
before_deploy-application = ["backup-database", "run-migrations"]
EOF
ggen lifecycle run deploy-application
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.backup-database"
must_precede = ["ggen.lifecycle.run-migrations"]
attributes = { "backup.type" = "pre_migration" }

[[scenario.expect.span]]
name = "ggen.lifecycle.run-migrations"
must_precede = ["ggen.lifecycle.deploy-application"]
must_follow = ["ggen.lifecycle.backup-database"]

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-application"
must_follow = ["ggen.lifecycle.run-migrations"]

[scenario.expect.window]
parent = "ggen.lifecycle.deploy.orchestrate"
must_contain = ["ggen.lifecycle.backup-database", "ggen.lifecycle.run-migrations", "ggen.lifecycle.deploy-application"]
temporal_order = true

# Test 9: Zero-downtime deployment
[[scenario]]
name = "zero_downtime_deployment"
service = "ggen"
run = """
mkdir -p /workspace/zero-downtime && cd /workspace/zero-downtime && \
cat > make.toml << 'EOF'
[project]
name = "zero-downtime-deployment"

[lifecycle.deploy-new-version]
commands = [
    "echo 'Starting new version in parallel'",
    "echo 'Waiting for health checks'",
    "echo 'New version healthy'"
]

[lifecycle.switch-load-balancer]
commands = [
    "echo 'Draining connections from old version'",
    "echo 'Routing traffic to new version'",
    "echo 'Traffic switched'"
]

[lifecycle.terminate-old-version]
commands = [
    "echo 'Waiting for graceful shutdown'",
    "echo 'Terminating old version'",
    "echo 'Deployment complete - zero downtime'"
]
EOF
ggen lifecycle run deploy-new-version && \
ggen lifecycle run switch-load-balancer && \
ggen lifecycle run terminate-old-version
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy-new-version"
must_precede = ["ggen.lifecycle.switch-load-balancer"]
attributes = { "deployment.strategy" = "zero_downtime" }

[[scenario.expect.span]]
name = "ggen.deploy.healthcheck"
parent = "ggen.lifecycle.deploy-new-version"
attributes = { "check.passed" = "true" }

[[scenario.expect.span]]
name = "ggen.lifecycle.switch-load-balancer"
must_precede = ["ggen.lifecycle.terminate-old-version"]
must_follow = ["ggen.lifecycle.deploy-new-version"]

[[scenario.expect.span]]
name = "ggen.lifecycle.terminate-old-version"
must_follow = ["ggen.lifecycle.switch-load-balancer"]
attributes = { "shutdown.graceful" = "true" }

# Test 10: Deployment verification with smoke tests
[[scenario]]
name = "deployment_verification_smoke_tests"
service = "ggen"
run = """
mkdir -p /workspace/smoke-tests && cd /workspace/smoke-tests && \
cat > make.toml << 'EOF'
[project]
name = "smoke-test-deployment"

[lifecycle.deploy]
command = "echo 'Deploying application'"

[lifecycle.smoke-tests]
commands = [
    "echo 'Testing health endpoint'",
    "echo 'Testing critical API endpoints'",
    "echo 'Testing database connectivity'",
    "echo 'All smoke tests passed'"
]

[hooks]
after_deploy = ["smoke-tests"]
EOF
ggen lifecycle run deploy
"""

[[scenario.expect.span]]
name = "ggen.lifecycle.deploy"
must_precede = ["ggen.lifecycle.smoke-tests"]

[[scenario.expect.span]]
name = "ggen.lifecycle.smoke-tests"
must_follow = ["ggen.lifecycle.deploy"]
attributes = { "test.type" = "smoke", "hook.type" = "after" }

[[scenario.expect.span]]
name = "ggen.test.execute"
parent = "ggen.lifecycle.smoke-tests"
count = 4
attributes_pattern = { "test.name" = ".*" }

[scenario.expect.attestation]
deployment_verified = true
smoke_tests_passed = true

# Summary expectations
[expect.report]
format = "json"
output_path = "/tmp/lifecycle-deploy-report.json"
include_metrics = true
include_traces = true

[expect.performance]
max_duration_ms = 15000
max_memory_mb = 1024

[expect.determinism]
require_same_traces_on_retry = true
retry_count = 2
