provisioning-catalog/providers/demo/tests/integration/test_api_client.nu

167 lines
5 KiB
Text
Raw Normal View History

#!/usr/bin/env nu
# Integration tests for API client
def test-result [name: string, result: bool] {
if $result {
print $"✓ ($name)"
} else {
print $"✗ ($name)"
}
$result
}
def load-mocks [] {
open tests/mocks/mock_api_responses.json
}
def test-servers-list-structure [] {
let mocks = (load-mocks)
let servers = $mocks.servers_list.servers
let t1 = (test-result "API: Servers list has data" (($servers | length) > 0))
let t2 = (test-result "API: Server has id field" ($servers | get -o 0 | get -o id | is-not-empty))
let t3 = (test-result "API: Server has name field" ($servers | get -o 0 | get -o name | is-not-empty))
let t4 = (test-result "API: Server has status field" ($servers | get -o 0 | get -o status | is-not-empty))
$t1 and $t2 and $t3 and $t4
}
def test-volumes-list-structure [] {
let mocks = (load-mocks)
let volumes = $mocks.volumes_list.volumes
let t1 = (test-result "API: Volumes list has data" (($volumes | length) > 0))
let t2 = (test-result "API: Volume has id field" ($volumes | get -o 0 | get -o id | is-not-empty))
let t3 = (test-result "API: Volume has size field" ($volumes | get -o 0 | get -o size | is-not-empty))
$t1 and $t2 and $t3
}
def test-networks-list-structure [] {
let mocks = (load-mocks)
let networks = $mocks.networks_list.networks
test-result "API: Networks list has data" (($networks | length) > 0)
}
def test-pricing-structure [] {
let mocks = (load-mocks)
let pricing = $mocks.pricing_data
let t1 = (test-result "API: Pricing has data" (($pricing | length) > 0))
let t2 = (test-result "API: Pricing has hourly rate" ($pricing | get -o 0 | get -o hourly_rate | is-not-empty))
$t1 and $t2
}
def test-error-401-structure [] {
let mocks = (load-mocks)
let error = $mocks.error_401
let t1 = (test-result "API: Error 401 has message" ($error | get -o error | get -o message | is-not-empty))
let t2 = (test-result "API: Error 401 has code" ($error | get -o error | get -o code | is-not-empty))
$t1 and $t2
}
def test-error-404-structure [] {
let mocks = (load-mocks)
let error = $mocks.error_404
test-result "API: Error 404 has error" ($error | get -o error | is-not-empty)
}
def test-error-429-structure [] {
let mocks = (load-mocks)
let error = $mocks.error_429
test-result "API: Error 429 has error" ($error | get -o error | is-not-empty)
}
def test-server-states [] {
let mocks = (load-mocks)
let servers = $mocks.servers_list.servers
let states = ($servers | each {|s| $s.status})
let running = ($states | where {|s| $s == "running"} | length)
let stopped = ($states | where {|s| $s == "stopped"} | length)
let t1 = (test-result "API: Running servers exist" ($running > 0))
let t2 = (test-result "API: Stopped servers exist" ($stopped > 0))
$t1 and $t2
}
def test-volume-states [] {
let mocks = (load-mocks)
let volumes = $mocks.volumes_list.volumes
let states = ($volumes | each {|v| $v.status})
let available = ($states | where {|s| $s == "available"} | length)
let in_use = ($states | where {|s| $s == "in-use"} | length)
let t1 = (test-result "API: Available volumes exist" ($available > 0))
let t2 = (test-result "API: In-use volumes exist" ($in_use > 0))
$t1 and $t2
}
def test-pricing-multiple-types [] {
let mocks = (load-mocks)
let pricing = $mocks.pricing_data
let types = ($pricing | each {|p| $p.instance_type})
let has_small = ($types | where {|t| $t == "small"} | length) > 0
let has_medium = ($types | where {|t| $t == "medium"} | length) > 0
let has_large = ($types | where {|t| $t == "large"} | length) > 0
let t1 = (test-result "API: Small instance pricing" $has_small)
let t2 = (test-result "API: Medium instance pricing" $has_medium)
let t3 = (test-result "API: Large instance pricing" $has_large)
$t1 and $t2 and $t3
}
def test-pricing-monthly-estimate [] {
let mocks = (load-mocks)
let pricing = ($mocks.pricing_data | get -o 0)
test-result "API: Pricing has monthly estimate" ($pricing | get -o monthly_estimate | is-not-empty)
}
def main [] {
print "=== API Client Integration Tests ==="
print ""
let results = [
(test-servers-list-structure),
(test-volumes-list-structure),
(test-networks-list-structure),
(test-pricing-structure),
(test-error-401-structure),
(test-error-404-structure),
(test-error-429-structure),
(test-server-states),
(test-volume-states),
(test-pricing-multiple-types),
(test-pricing-monthly-estimate),
(test-servers-list-structure),
(test-volumes-list-structure)
]
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