2020-08-21 06:49:19 -07:00
|
|
|
// Copyright 2020 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 hetzner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-22 02:00:08 -07:00
|
|
|
"testing"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2020-08-21 06:49:19 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-21 06:49:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type hcloudSDTestSuite struct {
|
|
|
|
Mock *SDMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *hcloudSDTestSuite) SetupTest(t *testing.T) {
|
|
|
|
s.Mock = NewSDMock(t)
|
|
|
|
s.Mock.Setup()
|
|
|
|
|
|
|
|
s.Mock.HandleHcloudServers()
|
|
|
|
s.Mock.HandleHcloudNetworks()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHCloudSDRefresh(t *testing.T) {
|
|
|
|
suite := &hcloudSDTestSuite{}
|
|
|
|
suite.SetupTest(t)
|
|
|
|
|
|
|
|
cfg := DefaultSDConfig
|
|
|
|
cfg.HTTPClientConfig.BearerToken = hcloudTestToken
|
|
|
|
cfg.hcloudEndpoint = suite.Mock.Endpoint()
|
|
|
|
|
|
|
|
d, err := newHcloudDiscovery(&cfg, log.NewNopLogger())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-21 06:49:19 -07:00
|
|
|
|
|
|
|
targetGroups, err := d.refresh(context.Background())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, targetGroups, 1)
|
2020-08-21 06:49:19 -07:00
|
|
|
|
|
|
|
targetGroup := targetGroups[0]
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NotNil(t, targetGroup, "targetGroup should not be nil")
|
|
|
|
require.NotNil(t, targetGroup.Targets, "targetGroup.targets should not be nil")
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, targetGroup.Targets, 3)
|
2020-08-21 06:49:19 -07:00
|
|
|
|
|
|
|
for i, labelSet := range []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("1.2.3.4:80"),
|
|
|
|
"__meta_hetzner_role": model.LabelValue("hcloud"),
|
|
|
|
"__meta_hetzner_server_id": model.LabelValue("42"),
|
|
|
|
"__meta_hetzner_server_name": model.LabelValue("my-server"),
|
|
|
|
"__meta_hetzner_server_status": model.LabelValue("running"),
|
|
|
|
"__meta_hetzner_public_ipv4": model.LabelValue("1.2.3.4"),
|
|
|
|
"__meta_hetzner_public_ipv6_network": model.LabelValue("2001:db8::/64"),
|
|
|
|
"__meta_hetzner_datacenter": model.LabelValue("fsn1-dc8"),
|
|
|
|
"__meta_hetzner_hcloud_image_name": model.LabelValue("ubuntu-20.04"),
|
|
|
|
"__meta_hetzner_hcloud_image_description": model.LabelValue("Ubuntu 20.04 Standard 64 bit"),
|
|
|
|
"__meta_hetzner_hcloud_image_os_flavor": model.LabelValue("ubuntu"),
|
|
|
|
"__meta_hetzner_hcloud_image_os_version": model.LabelValue("20.04"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location": model.LabelValue("fsn1"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location_network_zone": model.LabelValue("eu-central"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_cores": model.LabelValue("1"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_type": model.LabelValue("shared"),
|
|
|
|
"__meta_hetzner_hcloud_memory_size_gb": model.LabelValue("1"),
|
|
|
|
"__meta_hetzner_hcloud_disk_size_gb": model.LabelValue("25"),
|
|
|
|
"__meta_hetzner_hcloud_server_type": model.LabelValue("cx11"),
|
|
|
|
"__meta_hetzner_hcloud_private_ipv4_mynet": model.LabelValue("10.0.0.2"),
|
2021-07-02 16:51:50 -07:00
|
|
|
"__meta_hetzner_hcloud_labelpresent_my_key": model.LabelValue("true"),
|
2020-08-21 06:49:19 -07:00
|
|
|
"__meta_hetzner_hcloud_label_my_key": model.LabelValue("my-value"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("1.2.3.5:80"),
|
|
|
|
"__meta_hetzner_role": model.LabelValue("hcloud"),
|
|
|
|
"__meta_hetzner_server_id": model.LabelValue("44"),
|
|
|
|
"__meta_hetzner_server_name": model.LabelValue("another-server"),
|
|
|
|
"__meta_hetzner_server_status": model.LabelValue("stopped"),
|
|
|
|
"__meta_hetzner_datacenter": model.LabelValue("fsn1-dc14"),
|
|
|
|
"__meta_hetzner_public_ipv4": model.LabelValue("1.2.3.5"),
|
|
|
|
"__meta_hetzner_public_ipv6_network": model.LabelValue("2001:db9::/64"),
|
|
|
|
"__meta_hetzner_hcloud_image_name": model.LabelValue("ubuntu-20.04"),
|
|
|
|
"__meta_hetzner_hcloud_image_description": model.LabelValue("Ubuntu 20.04 Standard 64 bit"),
|
|
|
|
"__meta_hetzner_hcloud_image_os_flavor": model.LabelValue("ubuntu"),
|
|
|
|
"__meta_hetzner_hcloud_image_os_version": model.LabelValue("20.04"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location": model.LabelValue("fsn1"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location_network_zone": model.LabelValue("eu-central"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_cores": model.LabelValue("2"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_type": model.LabelValue("shared"),
|
|
|
|
"__meta_hetzner_hcloud_memory_size_gb": model.LabelValue("1"),
|
|
|
|
"__meta_hetzner_hcloud_disk_size_gb": model.LabelValue("50"),
|
|
|
|
"__meta_hetzner_hcloud_server_type": model.LabelValue("cpx11"),
|
2021-07-02 16:51:50 -07:00
|
|
|
"__meta_hetzner_hcloud_labelpresent_key": model.LabelValue("true"),
|
|
|
|
"__meta_hetzner_hcloud_labelpresent_other_key": model.LabelValue("true"),
|
|
|
|
"__meta_hetzner_hcloud_label_key": model.LabelValue(""),
|
|
|
|
"__meta_hetzner_hcloud_label_other_key": model.LabelValue("value"),
|
2020-08-21 06:49:19 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("1.2.3.6:80"),
|
|
|
|
"__meta_hetzner_role": model.LabelValue("hcloud"),
|
|
|
|
"__meta_hetzner_server_id": model.LabelValue("36"),
|
|
|
|
"__meta_hetzner_server_name": model.LabelValue("deleted-image-server"),
|
|
|
|
"__meta_hetzner_server_status": model.LabelValue("stopped"),
|
|
|
|
"__meta_hetzner_datacenter": model.LabelValue("fsn1-dc14"),
|
|
|
|
"__meta_hetzner_public_ipv4": model.LabelValue("1.2.3.6"),
|
|
|
|
"__meta_hetzner_public_ipv6_network": model.LabelValue("2001:db7::/64"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location": model.LabelValue("fsn1"),
|
|
|
|
"__meta_hetzner_hcloud_datacenter_location_network_zone": model.LabelValue("eu-central"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_cores": model.LabelValue("2"),
|
|
|
|
"__meta_hetzner_hcloud_cpu_type": model.LabelValue("shared"),
|
|
|
|
"__meta_hetzner_hcloud_memory_size_gb": model.LabelValue("1"),
|
|
|
|
"__meta_hetzner_hcloud_disk_size_gb": model.LabelValue("50"),
|
|
|
|
"__meta_hetzner_hcloud_server_type": model.LabelValue("cpx11"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, labelSet, targetGroup.Targets[i])
|
2020-08-21 06:49:19 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|