2020-10-05 15:45:33 +02:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-10-05 15:45:33 +02:00
|
|
|
)
|
|
|
|
|
2024-07-09 22:32:54 +02:00
|
|
|
func TestParseHypervisorDetail_Failure(t *testing.T) {
|
|
|
|
f := func(data string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
_, err := parseHypervisorDetail([]byte(data))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2020-10-05 15:45:33 +02:00
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
|
|
|
|
// bad data
|
|
|
|
f(`{ff}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseHypervisorDetail_Success(t *testing.T) {
|
|
|
|
f := func(data string, resultExpected *hypervisorDetail) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result, err := parseHypervisorDetail([]byte(data))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parseHypervisorDetail() error: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, resultExpected) {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%#v\nwant\n%#v", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1 hypervisor
|
|
|
|
data := `{
|
2020-10-05 15:45:33 +02:00
|
|
|
"hypervisors": [
|
|
|
|
{
|
|
|
|
"cpu_info": {
|
|
|
|
"arch": "x86_64",
|
|
|
|
"model": "Nehalem",
|
|
|
|
"vendor": "Intel",
|
|
|
|
"features": [
|
|
|
|
"pge",
|
|
|
|
"clflush"
|
|
|
|
],
|
|
|
|
"topology": {
|
|
|
|
"cores": 1,
|
|
|
|
"threads": 1,
|
|
|
|
"sockets": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"current_workload": 0,
|
|
|
|
"status": "enabled",
|
|
|
|
"state": "up",
|
|
|
|
"disk_available_least": 0,
|
|
|
|
"host_ip": "1.1.1.1",
|
|
|
|
"free_disk_gb": 1028,
|
|
|
|
"free_ram_mb": 7680,
|
|
|
|
"hypervisor_hostname": "host1",
|
|
|
|
"hypervisor_type": "fake",
|
|
|
|
"hypervisor_version": 1000,
|
|
|
|
"id": 2,
|
|
|
|
"local_gb": 1028,
|
|
|
|
"local_gb_used": 0,
|
|
|
|
"memory_mb": 8192,
|
|
|
|
"memory_mb_used": 512,
|
|
|
|
"running_vms": 0,
|
|
|
|
"service": {
|
|
|
|
"host": "host1",
|
|
|
|
"id": 6,
|
|
|
|
"disabled_reason": null
|
|
|
|
},
|
|
|
|
"vcpus": 2,
|
|
|
|
"vcpus_used": 0
|
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
]}`
|
|
|
|
|
|
|
|
resultExpected := &hypervisorDetail{
|
|
|
|
Hypervisors: []hypervisor{
|
|
|
|
{
|
|
|
|
HostIP: "1.1.1.1",
|
|
|
|
ID: 2,
|
|
|
|
Hostname: "host1",
|
|
|
|
Status: "enabled",
|
|
|
|
State: "up",
|
|
|
|
Type: "fake",
|
2020-10-05 15:45:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
f(data, resultExpected)
|
2020-10-05 15:45:33 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 22:32:54 +02:00
|
|
|
func TestAddHypervisorLabels(t *testing.T) {
|
|
|
|
f := func(hvs []hypervisor, labelssExpected []*promutils.Labels) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
labelss := addHypervisorLabels(hvs, 9100)
|
|
|
|
discoveryutils.TestEqualLabelss(t, labelss, labelssExpected)
|
2020-10-05 15:45:33 +02:00
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
|
|
|
|
hvs := []hypervisor{
|
2020-10-05 15:45:33 +02:00
|
|
|
{
|
2024-07-09 22:32:54 +02:00
|
|
|
Type: "fake",
|
|
|
|
ID: 5,
|
|
|
|
State: "enabled",
|
|
|
|
Status: "up",
|
|
|
|
Hostname: "fakehost",
|
|
|
|
HostIP: "1.2.2.2",
|
2020-10-05 15:45:33 +02:00
|
|
|
},
|
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
labelssExpected := []*promutils.Labels{
|
|
|
|
promutils.NewLabelsFromMap(map[string]string{
|
|
|
|
"__address__": "1.2.2.2:9100",
|
|
|
|
"__meta_openstack_hypervisor_host_ip": "1.2.2.2",
|
|
|
|
"__meta_openstack_hypervisor_hostname": "fakehost",
|
|
|
|
"__meta_openstack_hypervisor_id": "5",
|
|
|
|
"__meta_openstack_hypervisor_state": "enabled",
|
|
|
|
"__meta_openstack_hypervisor_status": "up",
|
|
|
|
"__meta_openstack_hypervisor_type": "fake",
|
|
|
|
}),
|
2020-10-05 15:45:33 +02:00
|
|
|
}
|
2024-07-09 22:32:54 +02:00
|
|
|
f(hvs, labelssExpected)
|
2020-10-05 15:45:33 +02:00
|
|
|
}
|