2020-08-21 13:49:19 +00: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 09:00:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-08-21 13:49:19 +00:00
|
|
|
"github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
2024-09-10 01:41:53 +00:00
|
|
|
"github.com/prometheus/common/promslog"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-21 13:49:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type robotSDTestSuite struct {
|
|
|
|
Mock *SDMock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *robotSDTestSuite) SetupTest(t *testing.T) {
|
|
|
|
s.Mock = NewSDMock(t)
|
|
|
|
s.Mock.Setup()
|
|
|
|
|
|
|
|
s.Mock.HandleRobotServers()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRobotSDRefresh(t *testing.T) {
|
|
|
|
suite := &robotSDTestSuite{}
|
|
|
|
suite.SetupTest(t)
|
|
|
|
cfg := DefaultSDConfig
|
|
|
|
cfg.HTTPClientConfig.BasicAuth = &config.BasicAuth{Username: robotTestUsername, Password: robotTestPassword}
|
|
|
|
cfg.robotEndpoint = suite.Mock.Endpoint()
|
|
|
|
|
2024-09-10 01:41:53 +00:00
|
|
|
d, err := newRobotDiscovery(&cfg, promslog.NewNopLogger())
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-21 13:49:19 +00:00
|
|
|
|
|
|
|
targetGroups, err := d.refresh(context.Background())
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, targetGroups, 1)
|
2020-08-21 13:49:19 +00:00
|
|
|
|
|
|
|
targetGroup := targetGroups[0]
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NotNil(t, targetGroup, "targetGroup should not be nil")
|
|
|
|
require.NotNil(t, targetGroup.Targets, "targetGroup.targets should not be nil")
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, targetGroup.Targets, 2)
|
2020-08-21 13:49:19 +00:00
|
|
|
|
|
|
|
for i, labelSet := range []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("123.123.123.123:80"),
|
|
|
|
"__meta_hetzner_role": model.LabelValue("robot"),
|
|
|
|
"__meta_hetzner_server_id": model.LabelValue("321"),
|
|
|
|
"__meta_hetzner_server_name": model.LabelValue("server1"),
|
|
|
|
"__meta_hetzner_server_status": model.LabelValue("ready"),
|
|
|
|
"__meta_hetzner_public_ipv4": model.LabelValue("123.123.123.123"),
|
|
|
|
"__meta_hetzner_public_ipv6_network": model.LabelValue("2a01:4f8:111:4221::/64"),
|
|
|
|
"__meta_hetzner_datacenter": model.LabelValue("nbg1-dc1"),
|
|
|
|
"__meta_hetzner_robot_product": model.LabelValue("DS 3000"),
|
|
|
|
"__meta_hetzner_robot_cancelled": model.LabelValue("false"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("123.123.123.124:80"),
|
|
|
|
"__meta_hetzner_role": model.LabelValue("robot"),
|
|
|
|
"__meta_hetzner_server_id": model.LabelValue("421"),
|
|
|
|
"__meta_hetzner_server_name": model.LabelValue("server2"),
|
|
|
|
"__meta_hetzner_server_status": model.LabelValue("in process"),
|
|
|
|
"__meta_hetzner_public_ipv4": model.LabelValue("123.123.123.124"),
|
|
|
|
"__meta_hetzner_datacenter": model.LabelValue("fsn1-dc10"),
|
|
|
|
"__meta_hetzner_robot_product": model.LabelValue("X5"),
|
|
|
|
"__meta_hetzner_robot_cancelled": model.LabelValue("true"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, labelSet, targetGroup.Targets[i])
|
2020-08-21 13:49:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 11:24:39 +00:00
|
|
|
|
|
|
|
func TestRobotSDRefreshHandleError(t *testing.T) {
|
|
|
|
suite := &robotSDTestSuite{}
|
|
|
|
suite.SetupTest(t)
|
|
|
|
cfg := DefaultSDConfig
|
|
|
|
cfg.robotEndpoint = suite.Mock.Endpoint()
|
|
|
|
|
2024-09-10 01:41:53 +00:00
|
|
|
d, err := newRobotDiscovery(&cfg, promslog.NewNopLogger())
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-31 11:24:39 +00:00
|
|
|
|
|
|
|
targetGroups, err := d.refresh(context.Background())
|
2024-10-06 16:35:29 +00:00
|
|
|
require.EqualError(t, err, "non 2xx status '401' response during hetzner service discovery with role robot")
|
2020-08-31 11:24:39 +00:00
|
|
|
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Empty(t, targetGroups)
|
2020-08-31 11:24:39 +00:00
|
|
|
}
|