|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Shared verification helper functions for VirtualMachine tests |
| 3 | + |
| 4 | +# verify_vm_exists: Waits for a VirtualMachine to be created |
| 5 | +# Usage: verify_vm_exists <vm-name> <namespace> [timeout] |
| 6 | +verify_vm_exists() { |
| 7 | + local vm_name="$1" |
| 8 | + local namespace="$2" |
| 9 | + local timeout="${3:-30s}" |
| 10 | + |
| 11 | + if ! kubectl wait --for=jsonpath='{.metadata.name}'="$vm_name" virtualmachine/"$vm_name" -n "$namespace" --timeout="$timeout" 2>/dev/null; then |
| 12 | + echo "VirtualMachine $vm_name not found in namespace $namespace" |
| 13 | + kubectl get virtualmachines -n "$namespace" |
| 14 | + return 1 |
| 15 | + fi |
| 16 | + echo "VirtualMachine $vm_name created successfully" |
| 17 | + return 0 |
| 18 | +} |
| 19 | + |
| 20 | +# verify_container_disk: Verifies that a VM uses a specific container disk OS |
| 21 | +# Usage: verify_container_disk <vm-name> <namespace> <os-name> |
| 22 | +# Example: verify_container_disk test-vm vm-test fedora |
| 23 | +verify_container_disk() { |
| 24 | + local vm_name="$1" |
| 25 | + local namespace="$2" |
| 26 | + local os_name="$3" |
| 27 | + |
| 28 | + # Get all container disk images from all volumes |
| 29 | + local container_disks |
| 30 | + container_disks=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.template.spec.volumes[*].containerDisk.image}') |
| 31 | + |
| 32 | + if [[ "$container_disks" =~ $os_name ]]; then |
| 33 | + echo "✓ VirtualMachine uses $os_name container disk" |
| 34 | + return 0 |
| 35 | + else |
| 36 | + echo "✗ Expected $os_name container disk, found volumes with images: $container_disks" |
| 37 | + kubectl get virtualmachine "$vm_name" -n "$namespace" -o yaml |
| 38 | + return 1 |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# verify_run_strategy: Verifies that runStrategy is set (in spec or status) |
| 43 | +# Usage: verify_run_strategy <vm-name> <namespace> |
| 44 | +verify_run_strategy() { |
| 45 | + local vm_name="$1" |
| 46 | + local namespace="$2" |
| 47 | + |
| 48 | + local spec_run_strategy |
| 49 | + local status_run_strategy |
| 50 | + spec_run_strategy=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.runStrategy}') |
| 51 | + status_run_strategy=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.status.runStrategy}') |
| 52 | + |
| 53 | + if [[ -n "$spec_run_strategy" ]]; then |
| 54 | + echo "✓ VirtualMachine uses runStrategy in spec: $spec_run_strategy" |
| 55 | + return 0 |
| 56 | + elif [[ -n "$status_run_strategy" ]]; then |
| 57 | + echo "✓ VirtualMachine has runStrategy in status: $status_run_strategy" |
| 58 | + echo " Note: VM may have been created with deprecated 'running' field, but runStrategy is set in status" |
| 59 | + return 0 |
| 60 | + else |
| 61 | + echo "✗ VirtualMachine missing runStrategy field in both spec and status" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +# verify_instancetype: Verifies that a VM has an instancetype reference |
| 67 | +# Usage: verify_instancetype <vm-name> <namespace> [expected-instancetype] [expected-kind] |
| 68 | +verify_instancetype() { |
| 69 | + local vm_name="$1" |
| 70 | + local namespace="$2" |
| 71 | + local expected_instancetype="$3" |
| 72 | + local expected_kind="${4:-VirtualMachineClusterInstancetype}" |
| 73 | + |
| 74 | + local instancetype |
| 75 | + instancetype=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.instancetype.name}') |
| 76 | + |
| 77 | + if [[ -z "$instancetype" ]]; then |
| 78 | + echo "✗ VirtualMachine has no instancetype reference" |
| 79 | + return 1 |
| 80 | + fi |
| 81 | + |
| 82 | + echo "✓ VirtualMachine has instancetype reference: $instancetype" |
| 83 | + |
| 84 | + # Check expected instancetype if provided |
| 85 | + if [[ -n "$expected_instancetype" ]]; then |
| 86 | + if [[ "$instancetype" == "$expected_instancetype" ]]; then |
| 87 | + echo "✓ Instancetype matches expected value: $expected_instancetype" |
| 88 | + else |
| 89 | + echo "✗ Expected instancetype '$expected_instancetype', found: $instancetype" |
| 90 | + return 1 |
| 91 | + fi |
| 92 | + fi |
| 93 | + |
| 94 | + # Verify instancetype kind |
| 95 | + local instancetype_kind |
| 96 | + instancetype_kind=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.instancetype.kind}') |
| 97 | + if [[ "$instancetype_kind" == "$expected_kind" ]]; then |
| 98 | + echo "✓ Instancetype kind is $expected_kind" |
| 99 | + else |
| 100 | + echo "⚠ Instancetype kind is: $instancetype_kind (expected: $expected_kind)" |
| 101 | + fi |
| 102 | + |
| 103 | + return 0 |
| 104 | +} |
| 105 | + |
| 106 | +# verify_instancetype_contains: Verifies that instancetype name contains a string |
| 107 | +# Usage: verify_instancetype_contains <vm-name> <namespace> <substring> [description] |
| 108 | +verify_instancetype_contains() { |
| 109 | + local vm_name="$1" |
| 110 | + local namespace="$2" |
| 111 | + local substring="$3" |
| 112 | + local description="${4:-$substring}" |
| 113 | + |
| 114 | + local instancetype |
| 115 | + instancetype=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.instancetype.name}') |
| 116 | + |
| 117 | + if [[ -z "$instancetype" ]]; then |
| 118 | + echo "✗ VirtualMachine has no instancetype reference" |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + |
| 122 | + if [[ "$instancetype" =~ $substring ]]; then |
| 123 | + echo "✓ Instancetype matches $description: $instancetype" |
| 124 | + return 0 |
| 125 | + else |
| 126 | + echo "⚠ Instancetype '$instancetype' doesn't match $description" |
| 127 | + return 0 # Return success for warnings |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +# verify_instancetype_prefix: Verifies that instancetype starts with a prefix |
| 132 | +# Usage: verify_instancetype_prefix <vm-name> <namespace> <prefix> [description] |
| 133 | +verify_instancetype_prefix() { |
| 134 | + local vm_name="$1" |
| 135 | + local namespace="$2" |
| 136 | + local prefix="$3" |
| 137 | + local description="${4:-$prefix}" |
| 138 | + |
| 139 | + local instancetype |
| 140 | + instancetype=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.instancetype.name}') |
| 141 | + |
| 142 | + if [[ -z "$instancetype" ]]; then |
| 143 | + echo "✗ VirtualMachine has no instancetype reference" |
| 144 | + return 1 |
| 145 | + fi |
| 146 | + |
| 147 | + if [[ "$instancetype" =~ ^${prefix}\. ]]; then |
| 148 | + echo "✓ Instancetype matches $description family: $instancetype" |
| 149 | + return 0 |
| 150 | + else |
| 151 | + echo "⚠ Instancetype '$instancetype' doesn't start with '$prefix'" |
| 152 | + return 0 # Return success for warnings |
| 153 | + fi |
| 154 | +} |
| 155 | + |
| 156 | +# verify_no_direct_resources: Verifies VM uses instancetype (no direct memory spec) |
| 157 | +# Usage: verify_no_direct_resources <vm-name> <namespace> |
| 158 | +verify_no_direct_resources() { |
| 159 | + local vm_name="$1" |
| 160 | + local namespace="$2" |
| 161 | + |
| 162 | + local guest_memory |
| 163 | + guest_memory=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.template.spec.domain.memory.guest}') |
| 164 | + |
| 165 | + if [[ -z "$guest_memory" ]]; then |
| 166 | + echo "✓ VirtualMachine uses instancetype for resources (no direct memory spec)" |
| 167 | + return 0 |
| 168 | + else |
| 169 | + echo "⚠ VirtualMachine has direct memory specification: $guest_memory" |
| 170 | + return 0 # Return success for warnings |
| 171 | + fi |
| 172 | +} |
| 173 | + |
| 174 | +# verify_has_resources_or_instancetype: Verifies VM has either instancetype or direct resources |
| 175 | +# Usage: verify_has_resources_or_instancetype <vm-name> <namespace> |
| 176 | +verify_has_resources_or_instancetype() { |
| 177 | + local vm_name="$1" |
| 178 | + local namespace="$2" |
| 179 | + |
| 180 | + local instancetype |
| 181 | + instancetype=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.instancetype.name}') |
| 182 | + |
| 183 | + if [[ -n "$instancetype" ]]; then |
| 184 | + echo "✓ VirtualMachine has instancetype reference: $instancetype" |
| 185 | + return 0 |
| 186 | + fi |
| 187 | + |
| 188 | + # Check for direct resource specification |
| 189 | + local guest_memory |
| 190 | + guest_memory=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.template.spec.domain.memory.guest}') |
| 191 | + |
| 192 | + if [[ -n "$guest_memory" ]]; then |
| 193 | + echo "⚠ No instancetype set, but VM has direct memory specification: $guest_memory" |
| 194 | + return 0 |
| 195 | + else |
| 196 | + echo "✗ VirtualMachine has no instancetype and no direct resource specification" |
| 197 | + kubectl get virtualmachine "$vm_name" -n "$namespace" -o yaml |
| 198 | + return 1 |
| 199 | + fi |
| 200 | +} |
0 commit comments