mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-29 23:30:04 +01:00
9edeecabc8
This makes easier to read and debug these tests. This also reduces test lines count by 15% from 3K to 2.5K . See https://itnext.io/f-tests-as-a-replacement-for-table-driven-tests-in-go-8814a8b19e9e . While at it, consistently use t.Fatal* instead of t.Error*, since t.Error* usually leads to more complicated and fragile tests, while it doesn't bring any practical benefits over t.Fatal*.
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
package openstack
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
)
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
// 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 := `{
|
|
"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
|
|
}
|
|
]}`
|
|
|
|
resultExpected := &hypervisorDetail{
|
|
Hypervisors: []hypervisor{
|
|
{
|
|
HostIP: "1.1.1.1",
|
|
ID: 2,
|
|
Hostname: "host1",
|
|
Status: "enabled",
|
|
State: "up",
|
|
Type: "fake",
|
|
},
|
|
},
|
|
}
|
|
f(data, resultExpected)
|
|
}
|
|
|
|
func TestAddHypervisorLabels(t *testing.T) {
|
|
f := func(hvs []hypervisor, labelssExpected []*promutils.Labels) {
|
|
t.Helper()
|
|
|
|
labelss := addHypervisorLabels(hvs, 9100)
|
|
discoveryutils.TestEqualLabelss(t, labelss, labelssExpected)
|
|
}
|
|
|
|
hvs := []hypervisor{
|
|
{
|
|
Type: "fake",
|
|
ID: 5,
|
|
State: "enabled",
|
|
Status: "up",
|
|
Hostname: "fakehost",
|
|
HostIP: "1.2.2.2",
|
|
},
|
|
}
|
|
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",
|
|
}),
|
|
}
|
|
f(hvs, labelssExpected)
|
|
}
|