VictoriaMetrics/lib/promscrape/discovery/ovhcloud/dedicated_server_test.go
Zhu Jiekun d1d59d6348
feature: [vmagent] Add service discovery support for OVH Cloud VPS and dedicated server (#6160)
### Describe Your Changes
related issue:
https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6071

#### Added
- Added service discovery support for OVH Cloud:
    - VPS.
    - Dedicated server.

#### Docs
- `CHANGELOG.md`, `sd_configs.md`, `vmagent.md` are updated.

#### Note
- Useful links: 
    - OVH Cloud VPS API: https://eu.api.ovh.com/console/#/vps~GET
- OVH Cloud Dedicated server API:
https://eu.api.ovh.com/console/#/dedicated/server~GET
    - OVH Cloud SDK: https://github.com/ovh/go-ovh
- Prometheus SD:
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#ovhcloud_sd_config

Tested on OVH Cloud VPS and dedicated server.
<img width="1722" alt="image"
src="https://github.com/VictoriaMetrics/VictoriaMetrics/assets/30280396/d3f0adc8-b0ef-423e-9379-8a9b9b0792ee">

<img width="1724" alt="image"
src="https://github.com/VictoriaMetrics/VictoriaMetrics/assets/30280396/18b5b730-3512-4fc0-8b2c-f2450ac550fd">

---
Signed-off-by: Jiekun <jiekun@victoriametrics.com>
Co-authored-by: hagen1778 <roman@victoriametrics.com>
2024-09-30 15:06:14 +02:00

99 lines
3.2 KiB
Go

package ovhcloud
import (
"errors"
"reflect"
"sync/atomic"
"testing"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
)
func Test_getDedicatedServerLabels(t *testing.T) {
mockSvr := newMockOVHCloudServer(func(path string) ([]byte, error) {
switch path {
case "/dedicated/server":
return []byte(`["ns0000000.ip-00-00-000.eu"]`), nil
case "/dedicated/server/ns0000000.ip-00-00-000.eu":
return mockDedicatedServerDetail, nil
case "/dedicated/server/ns0000000.ip-00-00-000.eu/ips":
return []byte(`["2001:40d0:302:8874::/64","50.75.126.113/32"]`), nil
default:
return []byte{}, errors.New("invalid request")
}
})
c, _ := discoveryutils.NewClient(mockSvr.URL, nil, nil, nil, &promauth.HTTPClientConfig{})
td := atomic.Value{}
td.Store(time.Duration(1))
cfg := &apiConfig{
client: c,
applicationKey: "",
applicationSecret: "",
consumerKey: "",
timeDelta: td,
}
expectLabels := &promutils.Labels{}
expectLabels.Add("__address__", "50.75.126.113")
expectLabels.Add("instance", "ns0000000.ip-00-00-000.eu")
expectLabels.Add("__meta_ovhcloud_dedicated_server_state", "ok")
expectLabels.Add("__meta_ovhcloud_dedicated_server_commercial_range", "RISE-3")
expectLabels.Add("__meta_ovhcloud_dedicated_server_link_speed", "1000")
expectLabels.Add("__meta_ovhcloud_dedicated_server_rack", "G000A00")
expectLabels.Add("__meta_ovhcloud_dedicated_server_no_intervention", "false")
expectLabels.Add("__meta_ovhcloud_dedicated_server_os", "centos7_64")
expectLabels.Add("__meta_ovhcloud_dedicated_server_support_level", "pro")
expectLabels.Add("__meta_ovhcloud_dedicated_server_server_id", "1000000")
expectLabels.Add("__meta_ovhcloud_dedicated_server_reverse", "ns0000000.ip-00-00-000.eu")
expectLabels.Add("__meta_ovhcloud_dedicated_server_datacenter", "gra2")
expectLabels.Add("__meta_ovhcloud_dedicated_server_name", "ns0000000.ip-00-00-000.eu")
expectLabels.Add("__meta_ovhcloud_dedicated_server_ipv4", "50.75.126.113")
expectLabels.Add("__meta_ovhcloud_dedicated_server_ipv6", "")
expect := []*promutils.Labels{
expectLabels,
}
result, err := getDedicatedServerLabels(cfg)
if err != nil {
t.Fatalf("getDedicatedServerLabels unexpected error: %v", err)
}
if !reflect.DeepEqual(expect, result) {
t.Fatalf("getDedicatedServerLabels incorrect, want: %v, got: %v", expect, result)
}
}
var mockDedicatedServerDetail = []byte(
`{
"name": "ns0000000.ip-00-00-000.eu",
"availabilityZone": "eu-west-gra-a",
"datacenter": "gra2",
"bootScript": null,
"linkSpeed": 1000,
"reverse": "ns0000000.ip-00-00-000.eu",
"serverId": 1000000,
"monitoring": false,
"rootDevice": null,
"noIntervention": false,
"newUpgradeSystem": true,
"rack": "G000A00",
"rescueSshKey": null,
"supportLevel": "pro",
"powerState": "poweron",
"commercialRange": "RISE-3",
"professionalUse": false,
"rescueMail": null,
"region": "eu-west-gra",
"bootId": 1,
"state": "ok",
"os": "centos7_64",
"ip": "50.75.126.113",
"iam": {
"displayName": "ns0000000.ip-00-00-000.eu",
"id": "000da00d-00d0-0b00-0000-00000a0000bd",
"urn": "urn:v1:eu:resource:dedicatedServer:ns0000000.ip-00-00-000.eu"
}
}`)