provisioning-catalog/providers/aws/tests/integration/test_server_lifecycle.nu

194 lines
7.2 KiB
Text
Raw Normal View History

#!/usr/bin/env nu
# Integration tests for AWS server lifecycle (create, modify, delete)
def test-result [
name: string
result: bool
] {
if $result {
print $"✓ ($name)"
} else {
print $"✗ ($name)"
}
$result
}
def load-mock-responses [] {
open ($env.PROVISIONING)/extensions/providers/aws/tests/mocks/mock_api_responses.json
}
# Test: List servers returns instances
def test-list-servers [] {
let mocks = (load-mock-responses)
let response = $mocks.describe_instances_list
let instances = ($response.Reservations | each {|r| $r.Instances} | flatten)
let test1 = (test-result "Lifecycle: List servers returns instances" ($instances | is-not-empty))
let test2 = (test-result "Lifecycle: Each instance has hostname tag" (($instances | where {|i| ($i.Tags | where {|t| $t.Key == "hostname"} | length) > 0} | length) > 0))
$test1 and $test2
}
# Test: Server info contains required fields
def test-server-info-structure [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
let test1 = (test-result "Lifecycle: Server info has InstanceId" ($instance | get -o InstanceId | is-not-empty))
let test2 = (test-result "Lifecycle: Server info has InstanceType" ($instance | get -o InstanceType | is-not-empty))
let test3 = (test-result "Lifecycle: Server info has State" ($instance | get -o State | is-not-empty))
let test4 = (test-result "Lifecycle: Server info has PrivateIpAddress" ($instance | get -o PrivateIpAddress | is-not-empty))
let test5 = (test-result "Lifecycle: Server info has PublicIpAddress" ($instance | get -o PublicIpAddress | is-not-empty))
$test1 and $test2 and $test3 and $test4 and $test5
}
# Test: Hostname matching
def test-server-hostname-matching [] {
let mocks = (load-mock-responses)
let instances = ($mocks.describe_instances_list.Reservations | each {|r| $r.Instances} | flatten)
let target_hostname = "test-server-1"
let found = ($instances | where {|i|
let hostname = ($i.Tags | where {|t| $t.Key == "hostname"} | get -o 0 | get -o Value)
$hostname == $target_hostname
})
test-result "Lifecycle: Server hostname lookup matches" ($found | is-not-empty)
}
# Test: Server state validation
def test-server-state-correct [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
let state = ($instance | get -o State | get -o Name)
let test1 = (test-result "Lifecycle: Server state is 'running'" ($state == "running"))
let test2 = (test-result "Lifecycle: State has Code field" (($instance | get -o State | get -o Code) | is-not-empty))
$test1 and $test2
}
# Test: Public IP address extraction
def test-server-public-ip [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
test-result "Lifecycle: Public IP is valid format" (($instance | get -o PublicIpAddress) | str contains ".")
}
# Test: Create server response
def test-create-server-response [] {
let mocks = (load-mock-responses)
let response = $mocks.create_instance_response
let instance = ($response.Instances | get -o 0)
let test1 = (test-result "Lifecycle: Created instance has InstanceId" ($instance | get -o InstanceId | is-not-empty))
let test2 = (test-result "Lifecycle: Created instance is in pending state" (($instance | get -o State | get -o Name) == "pending"))
let test3 = (test-result "Lifecycle: Created instance has KeyName" ($instance | get -o KeyName | is-not-empty))
$test1 and $test2 and $test3
}
# Test: Instance type details
def test-instance-type-info [] {
let mocks = (load-mock-responses)
let t3_micro = ($mocks.describe_instance_types.InstanceTypes | where {|it| $it.InstanceType == "t3.micro"} | get -o 0)
let test1 = (test-result "Lifecycle: t3.micro has vCPU count" ($t3_micro | get -o VCpuInfo | get -o DefaultVCpus | is-not-empty))
let test2 = (test-result "Lifecycle: t3.micro has memory info" ($t3_micro | get -o MemoryInfo | get -o SizeInMiB | is-not-empty))
$test1 and $test2
}
# Test: Availability zone information
def test-instance-availability-zone [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
let az = ($instance.Placement | get -o AvailabilityZone)
test-result "Lifecycle: Instance has AZ info" ($az | is-not-empty)
}
# Test: Block device mappings (storage)
def test-instance-storage-devices [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
let devices = ($instance | get -o BlockDeviceMappings)
let test1 = (test-result "Lifecycle: Instance has storage devices" ($devices | is-not-empty))
let test2 = (test-result "Lifecycle: Storage device has device name" (($devices | get -o 0 | get -o DeviceName) == "/dev/xvda"))
$test1 and $test2
}
# Test: Security groups assignment
def test-instance-security-groups [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
let sgs = ($instance | get -o SecurityGroups)
let test1 = (test-result "Lifecycle: Instance has security groups" ($sgs | is-not-empty))
let test2 = (test-result "Lifecycle: Security group has GroupId" (($sgs | get -o 0 | get -o GroupId) | is-not-empty))
$test1 and $test2
}
# Test: Launch time tracking
def test-instance-launch-time [] {
let mocks = (load-mock-responses)
let instance = ($mocks.describe_instances_single.Reservations | get -o 0 | get -o Instances | get -o 0)
test-result "Lifecycle: Instance has LaunchTime" (($instance | get -o LaunchTime) | is-not-empty)
}
# Test: Multiple instances state tracking
def test-multiple-instances-state [] {
let mocks = (load-mock-responses)
let instances = ($mocks.describe_instances_list.Reservations | each {|r| $r.Instances} | flatten)
let running = ($instances | where {|i| ($i.State | get -o Name) == "running"} | length)
let stopped = ($instances | where {|i| ($i.State | get -o Name) == "stopped"} | length)
let test1 = (test-result "Lifecycle: Running instances found" ($running > 0))
let test2 = (test-result "Lifecycle: Stopped instances found" ($stopped > 0))
$test1 and $test2
}
# Main test execution
def main [] {
print "=== AWS Server Lifecycle Integration Tests ==="
print ""
let results = [
(test-list-servers),
(test-server-info-structure),
(test-server-hostname-matching),
(test-server-state-correct),
(test-server-public-ip),
(test-create-server-response),
(test-instance-type-info),
(test-instance-availability-zone),
(test-instance-storage-devices),
(test-instance-security-groups),
(test-instance-launch-time),
(test-multiple-instances-state)
]
let passed = ($results | where {|it| $it == true} | length)
let failed = ($results | where {|it| $it == false} | length)
print ""
print $"Test Results: ($passed) passed, ($failed) failed"
{
passed: $passed,
failed: $failed,
total: ($passed + $failed)
}
}
main