VictoriaMetrics/lib/promscrape/discovery/vultr/vultr_test.go
Aliaksandr Valialkin f3be3573e7
lib/promscrape/discovery/vultr: follow-up after 17e3d019d2
- Sort the discovered labels in alphabetical order at https://docs.victoriametrics.com/sd_configs/#vultr_sd_configs
- Rename VultrConfigs to VultrSDConfigs to be consistent with the naming for other SD configs.
- Prepare query arg filters for `list instances API` at newAPIConfig() instead of passing them in a separate listParams struct.
  This simplifies the code a bit.
- Return error when bearer token isn't set at vultr_sd_configs, since this token is mandatory
  according to https://docs.victoriametrics.com/sd_configs/#vultr_sd_configs
- Remove unused fields from the parsed response from Vultr list instances API in order to simplify the code a bit.
- Remove double logging of errors inside getInstances() function, since these errors must be already logged by the caller.
- Simplify tests, so they are easier to maintain.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6041
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6068
2024-07-05 17:40:39 +02:00

97 lines
3.9 KiB
Go

package vultr
import (
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
)
func TestGetInstanceLabels(t *testing.T) {
input := []Instance{
{
ID: "fake-id-07f7-4b68-88ac-fake-id",
OS: "Ubuntu 22.04 x64",
RAM: 1024,
Disk: 25,
MainIP: "64.176.84.27",
VCPUCount: 1,
Region: "sgp",
Plan: "vc2-1c-1gb",
AllowedBandwidth: 1,
ServerStatus: "installingbooting",
V6MainIP: "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
Label: "vultr-sd",
InternalIP: "",
Hostname: "vultr-sd",
Tags: []string{"mock tags"},
OSID: 1743,
Features: []string{"ipv6"},
},
{
ID: "fake-id-07f7-4b68-88ac-fake-id",
OS: "Ubuntu 22.04 x64",
RAM: 1024,
Disk: 25,
MainIP: "64.176.84.27",
VCPUCount: 1,
Region: "sgp",
Plan: "vc2-1c-1gb",
AllowedBandwidth: 1,
ServerStatus: "installingbooting",
V6MainIP: "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
Label: "vultr-sd",
InternalIP: "",
Hostname: "vultr-sd",
Tags: []string{"mock tags"},
OSID: 1743,
Features: []string{"ipv6"},
},
}
expect := []*promutils.Labels{
promutils.NewLabelsFromMap(map[string]string{
"__address__": "64.176.84.27:8080",
"__meta_vultr_instance_id": "fake-id-07f7-4b68-88ac-fake-id",
"__meta_vultr_instance_label": "vultr-sd",
"__meta_vultr_instance_os": "Ubuntu 22.04 x64",
"__meta_vultr_instance_os_id": "1743",
"__meta_vultr_instance_region": "sgp",
"__meta_vultr_instance_plan": "vc2-1c-1gb",
"__meta_vultr_instance_main_ip": "64.176.84.27",
"__meta_vultr_instance_internal_ip": "",
"__meta_vultr_instance_main_ipv6": "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
"__meta_vultr_instance_hostname": "vultr-sd",
"__meta_vultr_instance_server_status": "installingbooting",
"__meta_vultr_instance_vcpu_count": "1",
"__meta_vultr_instance_ram_mb": "1024",
"__meta_vultr_instance_allowed_bandwidth_gb": "1",
"__meta_vultr_instance_disk_gb": "25",
"__meta_vultr_instance_features": ",ipv6,",
"__meta_vultr_instance_tags": ",mock tags,",
}),
promutils.NewLabelsFromMap(map[string]string{
"__address__": "64.176.84.27:8080",
"__meta_vultr_instance_id": "fake-id-07f7-4b68-88ac-fake-id",
"__meta_vultr_instance_label": "vultr-sd",
"__meta_vultr_instance_os": "Ubuntu 22.04 x64",
"__meta_vultr_instance_os_id": "1743",
"__meta_vultr_instance_region": "sgp",
"__meta_vultr_instance_plan": "vc2-1c-1gb",
"__meta_vultr_instance_main_ip": "64.176.84.27",
"__meta_vultr_instance_internal_ip": "",
"__meta_vultr_instance_main_ipv6": "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
"__meta_vultr_instance_hostname": "vultr-sd",
"__meta_vultr_instance_server_status": "installingbooting",
"__meta_vultr_instance_vcpu_count": "1",
"__meta_vultr_instance_ram_mb": "1024",
"__meta_vultr_instance_allowed_bandwidth_gb": "1",
"__meta_vultr_instance_disk_gb": "25",
"__meta_vultr_instance_features": ",ipv6,",
"__meta_vultr_instance_tags": ",mock tags,",
}),
}
labels := getInstanceLabels(input, 8080)
discoveryutils.TestEqualLabelss(t, labels, expect)
}