prometheus/discovery/vultr/mock_test.go

171 lines
5.7 KiB
Go
Raw Normal View History

// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vultr
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
// SDMock is the interface for the Vultr mock
type SDMock struct {
t *testing.T
Server *httptest.Server
Mux *http.ServeMux
}
// NewSDMock returns a new Vultr mock server.
func NewSDMock(t *testing.T) *SDMock {
return &SDMock{
t: t,
}
}
// Endpoint returns the URI to the mock server.
func (m *SDMock) Endpoint() string {
return m.Server.URL + "/"
}
// Setup creates the mock server.
func (m *SDMock) Setup() {
m.Mux = http.NewServeMux()
m.Server = httptest.NewServer(m.Mux)
}
// ShutdownServer shuts down the mock server.
func (m *SDMock) ShutdownServer() {
m.Server.Close()
}
const APIKey = "ABCBTDG35OTGH2UKCC3S6CNMDUPCN3ZWSGFQ"
// HandleInstanceList mocks vultr instances list.
func (m *SDMock) HandleInstanceList() {
m.Mux.HandleFunc("/v2/instances", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", APIKey) {
w.WriteHeader(http.StatusForbidden)
return
}
w.Header().Add("content-type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `
{
"instances": [
{
"id": "dbdbd38c-9884-4c92-95fe-899e50dee717",
"os": "Marketplace",
"ram": 4096,
"disk": 128,
"main_ip": "149.28.234.27",
"vcpu_count": 2,
"region": "ewr",
"plan": "vhf-2c-4gb",
"date_created": "2022-03-10T02:25:30+00:00",
"status": "active",
"allowed_bandwidth": 3000,
"netmask_v4": "255.255.254.0",
"gateway_v4": "149.28.234.1",
"power_status": "running",
"server_status": "ok",
"v6_network": "",
"v6_main_ip": "",
"v6_network_size": 0,
"label": "np-2-eae38a19b0f3",
"internal_ip": "10.1.96.5",
"kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=djJ8Q2k2NlVpNUlZNE5kM3NZMWdBNzFKUkVadDBDU3ItTEJ8-yNWBdMn1AWPqX-EnpTVbA1qBeP0txBZbahSOh3UtHIL2p0fZKEdFJwFNjWtvBKC9BGvJkoJdWBoo7hF-6RzavdUKPD3KIU5xD_T6keV8X-PPukPgXMuSUaAqurAuJvwAWXcTcodBjaIH961xgbvfkFyqVl0ZgHf3ErZQE-EmpZ0Jc3uF1DO2McJ4UL8KUvHLwa-ZDUZ_AtWJjQWsjNkxjvOiA",
"hostname": "np-2-eae38a19b0f3",
"os_id": 426,
"app_id": 0,
"image_id": "vke-worker",
"firewall_group_id": "",
"features": ["backups"],
"tags": ["tag1", "tag2", "tag3"]
},
{
"id": "fccb117c-62f7-4b17-995d-a8e56dd30b33",
"os": "Marketplace",
"ram": 4096,
"disk": 128,
"main_ip": "45.63.1.222",
"vcpu_count": 2,
"region": "ewr",
"plan": "vhf-2c-4gb",
"date_created": "2022-03-10T02:25:32+00:00",
"status": "active",
"allowed_bandwidth": 3000,
"netmask_v4": "255.255.254.0",
"gateway_v4": "45.63.0.1",
"power_status": "running",
"server_status": "ok",
"v6_network": "",
"v6_main_ip": "",
"v6_network_size": 0,
"label": "np-2-fd0714b5fe42",
"internal_ip": "10.1.96.6",
"kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=djJ8R0tzbmI0VWVfSDR6VE51Q0FGUTBCamwyTFNzZDNkVTF8Zwm93t6L2BLTYSNmM0g0Ppf43r71aIbqLiJCx6_1NuCugUqIKz8POshH91vi6XXDorpb3hYTk8RrJWQbaFMak7XbbfMObzsrFxKAQUVGfbjTBIHCKftxNIXeQOp1fEIDa3p-vkfSxEpx6ZX8nChGYYSVuOjS1twV6oI_IKtHXKdFJiVFz0Unay_K3HuXW_H2wrYSjmAChloR-eA1jSmBXLccJQ",
"hostname": "np-2-fd0714b5fe42",
"os_id": 426,
"app_id": 0,
"image_id": "vke-worker",
"firewall_group_id": "",
"features": [],
"tags": []
},
{
"id": "c4e58767-f61b-4c5e-9bce-43761cc04868",
"os": "Marketplace",
"ram": 4096,
"disk": 128,
"main_ip": "149.28.237.151",
"vcpu_count": 2,
"region": "ewr",
"plan": "vhf-2c-4gb",
"date_created": "2022-03-10T02:25:33+00:00",
"status": "active",
"allowed_bandwidth": 3000,
"netmask_v4": "255.255.254.0",
"gateway_v4": "149.28.236.1",
"power_status": "running",
"server_status": "ok",
"v6_network": "",
"v6_main_ip": "",
"v6_network_size": 0,
"label": "np-2-d04e7829fa43",
"internal_ip": "10.1.96.7",
"kvm": "https://my.vultr.com/subs/vps/novnc/api.php?data=djJ8NFEzbFZmV2hOUWNrc2R4bndnckhFQTJ3ME1yWmxVeW98P-p-KeyNtP73wzVvuWZSOrV5PvQDiBmYyB3cp99btIy5nczmvX8DOGSChbEldk3idKNdFLQMIieWnpR5xU5K-dMYlL6n6oYv2u-rh8zxS6-8hQsSbL2OsoywNPoLpQ3xPkT9Rb-yPrZX4fDLbt8yPVKDHqHO0GB-TxVPemueovfkkUZMXSraAXgHtl1YgK2uLMQf1WBFVzgwnQ7MYcfMGGYFQw",
"hostname": "np-2-d04e7829fa43",
"os_id": 426,
"app_id": 0,
"image_id": "vke-worker",
"firewall_group_id": "",
"features": [],
"tags": []
}
],
"meta": {
"total": 3,
"links": {
"next": "",
"prev": ""
}
}
}
`)
})
}