github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/hypervisors/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "strconv" 7 "testing" 8 9 "github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/hypervisors" 10 "github.com/huaweicloud/golangsdk/testhelper" 11 "github.com/huaweicloud/golangsdk/testhelper/client" 12 ) 13 14 // The first hypervisor represents what the specification says (~Newton) 15 // The second is exactly the same, but what you can get off a real system (~Kilo) 16 const HypervisorListBody = ` 17 { 18 "hypervisors": [ 19 { 20 "cpu_info": { 21 "arch": "x86_64", 22 "model": "Nehalem", 23 "vendor": "Intel", 24 "features": [ 25 "pge", 26 "clflush" 27 ], 28 "topology": { 29 "cores": 1, 30 "threads": 1, 31 "sockets": 4 32 } 33 }, 34 "current_workload": 0, 35 "status": "enabled", 36 "state": "up", 37 "disk_available_least": 0, 38 "host_ip": "1.1.1.1", 39 "free_disk_gb": 1028, 40 "free_ram_mb": 7680, 41 "hypervisor_hostname": "fake-mini", 42 "hypervisor_type": "fake", 43 "hypervisor_version": 2002000, 44 "id": 1, 45 "local_gb": 1028, 46 "local_gb_used": 0, 47 "memory_mb": 8192, 48 "memory_mb_used": 512, 49 "running_vms": 0, 50 "service": { 51 "host": "e6a37ee802d74863ab8b91ade8f12a67", 52 "id": 2, 53 "disabled_reason": null 54 }, 55 "vcpus": 1, 56 "vcpus_used": 0 57 }, 58 { 59 "cpu_info": "{\"arch\": \"x86_64\", \"model\": \"Nehalem\", \"vendor\": \"Intel\", \"features\": [\"pge\", \"clflush\"], \"topology\": {\"cores\": 1, \"threads\": 1, \"sockets\": 4}}", 60 "current_workload": 0, 61 "status": "enabled", 62 "state": "up", 63 "disk_available_least": 0, 64 "host_ip": "1.1.1.1", 65 "free_disk_gb": 1028, 66 "free_ram_mb": 7680, 67 "hypervisor_hostname": "fake-mini", 68 "hypervisor_type": "fake", 69 "hypervisor_version": 2.002e+06, 70 "id": 1, 71 "local_gb": 1028, 72 "local_gb_used": 0, 73 "memory_mb": 8192, 74 "memory_mb_used": 512, 75 "running_vms": 0, 76 "service": { 77 "host": "e6a37ee802d74863ab8b91ade8f12a67", 78 "id": 2, 79 "disabled_reason": null 80 }, 81 "vcpus": 1, 82 "vcpus_used": 0 83 } 84 ] 85 }` 86 87 const HypervisorsStatisticsBody = ` 88 { 89 "hypervisor_statistics": { 90 "count": 1, 91 "current_workload": 0, 92 "disk_available_least": 0, 93 "free_disk_gb": 1028, 94 "free_ram_mb": 7680, 95 "local_gb": 1028, 96 "local_gb_used": 0, 97 "memory_mb": 8192, 98 "memory_mb_used": 512, 99 "running_vms": 0, 100 "vcpus": 2, 101 "vcpus_used": 0 102 } 103 } 104 ` 105 106 const HypervisorGetBody = ` 107 { 108 "hypervisor":{ 109 "cpu_info":{ 110 "arch":"x86_64", 111 "model":"Nehalem", 112 "vendor":"Intel", 113 "features":[ 114 "pge", 115 "clflush" 116 ], 117 "topology":{ 118 "cores":1, 119 "threads":1, 120 "sockets":4 121 } 122 }, 123 "current_workload":0, 124 "status":"enabled", 125 "state":"up", 126 "disk_available_least":0, 127 "host_ip":"1.1.1.1", 128 "free_disk_gb":1028, 129 "free_ram_mb":7680, 130 "hypervisor_hostname":"fake-mini", 131 "hypervisor_type":"fake", 132 "hypervisor_version":2002000, 133 "id":1, 134 "local_gb":1028, 135 "local_gb_used":0, 136 "memory_mb":8192, 137 "memory_mb_used":512, 138 "running_vms":0, 139 "service":{ 140 "host":"e6a37ee802d74863ab8b91ade8f12a67", 141 "id":2, 142 "disabled_reason":null 143 }, 144 "vcpus":1, 145 "vcpus_used":0 146 } 147 } 148 ` 149 150 const HypervisorUptimeBody = ` 151 { 152 "hypervisor": { 153 "hypervisor_hostname": "fake-mini", 154 "id": 1, 155 "state": "up", 156 "status": "enabled", 157 "uptime": " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14" 158 } 159 } 160 ` 161 162 var ( 163 HypervisorFake = hypervisors.Hypervisor{ 164 CPUInfo: hypervisors.CPUInfo{ 165 Arch: "x86_64", 166 Model: "Nehalem", 167 Vendor: "Intel", 168 Features: []string{ 169 "pge", 170 "clflush", 171 }, 172 Topology: hypervisors.Topology{ 173 Cores: 1, 174 Threads: 1, 175 Sockets: 4, 176 }, 177 }, 178 CurrentWorkload: 0, 179 Status: "enabled", 180 State: "up", 181 DiskAvailableLeast: 0, 182 HostIP: "1.1.1.1", 183 FreeDiskGB: 1028, 184 FreeRamMB: 7680, 185 HypervisorHostname: "fake-mini", 186 HypervisorType: "fake", 187 HypervisorVersion: 2002000, 188 ID: 1, 189 LocalGB: 1028, 190 LocalGBUsed: 0, 191 MemoryMB: 8192, 192 MemoryMBUsed: 512, 193 RunningVMs: 0, 194 Service: hypervisors.Service{ 195 Host: "e6a37ee802d74863ab8b91ade8f12a67", 196 ID: 2, 197 DisabledReason: "", 198 }, 199 VCPUs: 1, 200 VCPUsUsed: 0, 201 } 202 HypervisorsStatisticsExpected = hypervisors.Statistics{ 203 Count: 1, 204 CurrentWorkload: 0, 205 DiskAvailableLeast: 0, 206 FreeDiskGB: 1028, 207 FreeRamMB: 7680, 208 LocalGB: 1028, 209 LocalGBUsed: 0, 210 MemoryMB: 8192, 211 MemoryMBUsed: 512, 212 RunningVMs: 0, 213 VCPUs: 2, 214 VCPUsUsed: 0, 215 } 216 HypervisorUptimeExpected = hypervisors.Uptime{ 217 HypervisorHostname: "fake-mini", 218 ID: 1, 219 State: "up", 220 Status: "enabled", 221 Uptime: " 08:32:11 up 93 days, 18:25, 12 users, load average: 0.20, 0.12, 0.14", 222 } 223 ) 224 225 func HandleHypervisorsStatisticsSuccessfully(t *testing.T) { 226 testhelper.Mux.HandleFunc("/os-hypervisors/statistics", func(w http.ResponseWriter, r *http.Request) { 227 testhelper.TestMethod(t, r, "GET") 228 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 229 230 w.Header().Add("Content-Type", "application/json") 231 fmt.Fprintf(w, HypervisorsStatisticsBody) 232 }) 233 } 234 235 func HandleHypervisorListSuccessfully(t *testing.T) { 236 testhelper.Mux.HandleFunc("/os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) { 237 testhelper.TestMethod(t, r, "GET") 238 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 239 240 w.Header().Add("Content-Type", "application/json") 241 fmt.Fprintf(w, HypervisorListBody) 242 }) 243 } 244 245 func HandleHypervisorGetSuccessfully(t *testing.T) { 246 v := strconv.Itoa(HypervisorFake.ID) 247 testhelper.Mux.HandleFunc("/os-hypervisors/"+v, func(w http.ResponseWriter, r *http.Request) { 248 testhelper.TestMethod(t, r, "GET") 249 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 250 251 w.Header().Add("Content-Type", "application/json") 252 fmt.Fprintf(w, HypervisorGetBody) 253 }) 254 } 255 256 func HandleHypervisorUptimeSuccessfully(t *testing.T) { 257 v := strconv.Itoa(HypervisorFake.ID) 258 testhelper.Mux.HandleFunc("/os-hypervisors/"+v+"/uptime", func(w http.ResponseWriter, r *http.Request) { 259 testhelper.TestMethod(t, r, "GET") 260 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID) 261 262 w.Header().Add("Content-Type", "application/json") 263 fmt.Fprintf(w, HypervisorUptimeBody) 264 }) 265 }