2024-05-08 08:01:48 +00:00
|
|
|
package vultr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
|
|
)
|
|
|
|
|
2024-07-05 15:30:29 +00:00
|
|
|
func TestGetInstances_Success(t *testing.T) {
|
|
|
|
s := newMockVultrServer(func() []byte {
|
|
|
|
const resp = `{
|
|
|
|
"instances": [{
|
|
|
|
"id": "fake-id-07f7-4b68-88ac-fake-id",
|
|
|
|
"os": "Ubuntu 22.04 x64",
|
|
|
|
"ram": 1024,
|
|
|
|
"disk": 25,
|
|
|
|
"main_ip": "64.176.84.27",
|
|
|
|
"vcpu_count": 1,
|
|
|
|
"region": "sgp",
|
|
|
|
"plan": "vc2-1c-1gb",
|
|
|
|
"date_created": "2024-04-05T05:41:28+00:00",
|
|
|
|
"status": "active",
|
|
|
|
"allowed_bandwidth": 1,
|
|
|
|
"netmask_v4": "255.255.254.0",
|
|
|
|
"gateway_v4": "64.176.63.2",
|
|
|
|
"power_status": "running",
|
|
|
|
"server_status": "installingbooting",
|
|
|
|
"v6_network": "2002:18f0:4100:263a::",
|
|
|
|
"v6_main_ip": "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
|
|
|
|
"v6_network_size": 64,
|
|
|
|
"label": "vultr-sd",
|
|
|
|
"internal_ip": "",
|
|
|
|
"kvm": "https:\/\/my.vultr.com\/subs\/vps\/novnc\/api.php?data=secret_data_string",
|
|
|
|
"hostname": "vultr-sd",
|
|
|
|
"tag": "",
|
|
|
|
"tags": [],
|
|
|
|
"os_id": 1743,
|
|
|
|
"app_id": 0,
|
|
|
|
"image_id": "",
|
|
|
|
"firewall_group_id": "",
|
|
|
|
"features": ["ipv6"],
|
|
|
|
"user_scheme": "root"
|
|
|
|
}]
|
|
|
|
}`
|
2024-05-08 08:01:48 +00:00
|
|
|
|
2024-07-05 15:30:29 +00:00
|
|
|
return []byte(resp)
|
2024-05-08 08:01:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Prepare a discovery HTTP client who calls mock server.
|
2024-07-05 15:30:29 +00:00
|
|
|
client, err := discoveryutils.NewClient(s.URL, nil, nil, nil, &promauth.HTTPClientConfig{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error wen creating http client: %s", err)
|
|
|
|
}
|
2024-05-08 08:01:48 +00:00
|
|
|
cfg := &apiConfig{
|
|
|
|
c: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute `getInstances`
|
|
|
|
instances, err := getInstances(cfg)
|
|
|
|
if err != nil {
|
2024-07-05 15:30:29 +00:00
|
|
|
t.Fatalf("unexpected error in getInstances(): %s", err)
|
2024-05-08 08:01:48 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 15:30:29 +00:00
|
|
|
expectedInstances := []Instance{
|
2024-05-08 08:01:48 +00:00
|
|
|
{
|
|
|
|
ID: "fake-id-07f7-4b68-88ac-fake-id",
|
2024-07-05 15:30:29 +00:00
|
|
|
OS: "Ubuntu 22.04 x64",
|
2024-05-08 08:01:48 +00:00
|
|
|
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{},
|
2024-07-05 15:30:29 +00:00
|
|
|
OSID: 1743,
|
2024-05-08 08:01:48 +00:00
|
|
|
Features: []string{"ipv6"},
|
|
|
|
},
|
|
|
|
}
|
2024-07-05 15:30:29 +00:00
|
|
|
if !reflect.DeepEqual(instances, expectedInstances) {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%#v\nwant\n%#v", instances, expectedInstances)
|
|
|
|
}
|
|
|
|
}
|
2024-05-08 08:01:48 +00:00
|
|
|
|
2024-07-05 15:30:29 +00:00
|
|
|
func TestGetInstances_Failure(t *testing.T) {
|
|
|
|
s := newMockVultrServer(func() []byte {
|
|
|
|
return []byte("some error")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Prepare a discovery HTTP client who calls mock server.
|
|
|
|
client, err := discoveryutils.NewClient(s.URL, nil, nil, nil, &promauth.HTTPClientConfig{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error wen creating http client: %s", err)
|
|
|
|
}
|
|
|
|
cfg := &apiConfig{
|
|
|
|
c: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute `getInstances`
|
|
|
|
if _, err := getInstances(cfg); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error from getInstances()")
|
|
|
|
}
|
|
|
|
}
|
2024-05-08 08:01:48 +00:00
|
|
|
|
2024-07-05 15:30:29 +00:00
|
|
|
func TestGetInstances_Paging(t *testing.T) {
|
|
|
|
// Prepare a mock Vultr server.
|
|
|
|
// requestCount control the mock response for different page request.
|
|
|
|
requestCount := 0
|
|
|
|
s := newMockVultrServer(func() []byte {
|
|
|
|
// for the 1st request, response with `next` cursor
|
|
|
|
if requestCount == 0 {
|
|
|
|
requestCount++
|
|
|
|
return []byte(mockListInstanceSuccessPage0Resp)
|
|
|
|
}
|
|
|
|
// for the 2nd+ request, response with empty `next`.
|
|
|
|
return []byte(mockListInstanceSuccessPage1Resp)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Prepare a discovery HTTP client who calls mock server.
|
|
|
|
client, err := discoveryutils.NewClient(s.URL, nil, nil, nil, &promauth.HTTPClientConfig{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error wen creating http client: %s", err)
|
|
|
|
}
|
|
|
|
cfg := &apiConfig{
|
|
|
|
c: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute `getInstances`
|
|
|
|
instances, err := getInstances(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error in getInstances(): %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expectSuccessPagingInstances, instances) {
|
|
|
|
t.Fatalf("unexpected getInstances() result\ngot\n%#v\nwant\n%#v", instances, expectSuccessPagingInstances)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------ Test dataset ------------
|
2024-05-08 08:01:48 +00:00
|
|
|
var (
|
|
|
|
// mockListInstanceSuccessPage0Resp contains `next` cursor
|
|
|
|
mockListInstanceSuccessPage0Resp = `{
|
|
|
|
"instances": [{
|
2024-07-05 15:30:29 +00:00
|
|
|
"id": "1-fake-id-07f7-4b68-88ac-fake-id",
|
2024-05-08 08:01:48 +00:00
|
|
|
"os": "Ubuntu 22.04 x64",
|
|
|
|
"ram": 1024,
|
|
|
|
"disk": 25,
|
|
|
|
"main_ip": "64.176.84.27",
|
|
|
|
"vcpu_count": 1,
|
|
|
|
"region": "sgp",
|
|
|
|
"plan": "vc2-1c-1gb",
|
|
|
|
"date_created": "2024-04-05T05:41:28+00:00",
|
|
|
|
"status": "active",
|
|
|
|
"allowed_bandwidth": 1,
|
|
|
|
"netmask_v4": "255.255.254.0",
|
|
|
|
"gateway_v4": "64.176.63.2",
|
|
|
|
"power_status": "running",
|
|
|
|
"server_status": "installingbooting",
|
|
|
|
"v6_network": "2002:18f0:4100:263a::",
|
|
|
|
"v6_main_ip": "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
|
|
|
|
"v6_network_size": 64,
|
|
|
|
"label": "vultr-sd",
|
|
|
|
"internal_ip": "",
|
|
|
|
"kvm": "https:\/\/my.vultr.com\/subs\/vps\/novnc\/api.php?data=secret_data_string",
|
|
|
|
"hostname": "vultr-sd",
|
|
|
|
"tag": "",
|
|
|
|
"tags": [],
|
|
|
|
"os_id": 1743,
|
|
|
|
"app_id": 0,
|
|
|
|
"image_id": "",
|
|
|
|
"firewall_group_id": "",
|
|
|
|
"features": ["ipv6"],
|
|
|
|
"user_scheme": "root"
|
|
|
|
}],
|
|
|
|
"meta": {
|
|
|
|
"links": {
|
2024-07-05 15:30:29 +00:00
|
|
|
"next": "fake-cursor-string"
|
2024-05-08 08:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
2024-07-05 15:30:29 +00:00
|
|
|
// mockListInstanceSuccessPage1Resp contains empty 'next' cursor
|
2024-05-08 08:01:48 +00:00
|
|
|
mockListInstanceSuccessPage1Resp = `{
|
|
|
|
"instances": [{
|
2024-07-05 15:30:29 +00:00
|
|
|
"id": "2-fake-id-07f7-4b68-88ac-fake-id",
|
2024-05-08 08:01:48 +00:00
|
|
|
"os": "Ubuntu 22.04 x64",
|
|
|
|
"ram": 1024,
|
|
|
|
"disk": 25,
|
|
|
|
"main_ip": "64.176.84.27",
|
|
|
|
"vcpu_count": 1,
|
|
|
|
"region": "sgp",
|
|
|
|
"plan": "vc2-1c-1gb",
|
|
|
|
"date_created": "2024-04-05T05:41:28+00:00",
|
|
|
|
"status": "active",
|
|
|
|
"allowed_bandwidth": 1,
|
|
|
|
"netmask_v4": "255.255.254.0",
|
|
|
|
"gateway_v4": "64.176.63.2",
|
|
|
|
"power_status": "running",
|
|
|
|
"server_status": "installingbooting",
|
|
|
|
"v6_network": "2002:18f0:4100:263a::",
|
|
|
|
"v6_main_ip": "2002:18f0:4100:263a:5300:07ff:fdd7:691c",
|
|
|
|
"v6_network_size": 64,
|
|
|
|
"label": "vultr-sd",
|
|
|
|
"internal_ip": "",
|
|
|
|
"kvm": "https:\/\/my.vultr.com\/subs\/vps\/novnc\/api.php?data=secret_data_string",
|
|
|
|
"hostname": "vultr-sd",
|
|
|
|
"tag": "",
|
|
|
|
"tags": [],
|
|
|
|
"os_id": 1743,
|
|
|
|
"app_id": 0,
|
|
|
|
"image_id": "",
|
|
|
|
"firewall_group_id": "",
|
|
|
|
"features": ["ipv6"],
|
|
|
|
"user_scheme": "root"
|
|
|
|
}],
|
|
|
|
"meta": {
|
|
|
|
"links": {
|
2024-07-05 15:30:29 +00:00
|
|
|
"next": ""
|
2024-05-08 08:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
expectSuccessPagingInstances = []Instance{
|
|
|
|
{
|
2024-07-05 15:30:29 +00:00
|
|
|
ID: "1-fake-id-07f7-4b68-88ac-fake-id",
|
|
|
|
OS: "Ubuntu 22.04 x64",
|
2024-05-08 08:01:48 +00:00
|
|
|
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{},
|
2024-07-05 15:30:29 +00:00
|
|
|
OSID: 1743,
|
2024-05-08 08:01:48 +00:00
|
|
|
Features: []string{"ipv6"},
|
|
|
|
},
|
|
|
|
{
|
2024-07-05 15:30:29 +00:00
|
|
|
ID: "2-fake-id-07f7-4b68-88ac-fake-id",
|
|
|
|
OS: "Ubuntu 22.04 x64",
|
2024-05-08 08:01:48 +00:00
|
|
|
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{},
|
2024-07-05 15:30:29 +00:00
|
|
|
OSID: 1743,
|
2024-05-08 08:01:48 +00:00
|
|
|
Features: []string{"ipv6"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|