2020-06-26 03:25:58 -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.
|
|
|
|
|
2021-04-13 00:33:54 -07:00
|
|
|
package moby
|
2020-06-26 03:25:58 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-23 06:55:36 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-06-26 03:25:58 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2024-09-09 18:41:53 -07:00
|
|
|
"github.com/prometheus/common/promslog"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-09 04:01:08 -07:00
|
|
|
"gopkg.in/yaml.v2"
|
2024-01-23 07:53:55 -08:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2020-06-26 03:25:58 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerSwarmNodesSDRefresh(t *testing.T) {
|
|
|
|
sdmock := NewSDMock(t, "swarmprom")
|
|
|
|
sdmock.Setup()
|
|
|
|
|
|
|
|
e := sdmock.Endpoint()
|
2020-07-09 04:01:08 -07:00
|
|
|
url := e[:len(e)-1]
|
|
|
|
cfgString := fmt.Sprintf(`
|
|
|
|
---
|
|
|
|
role: nodes
|
|
|
|
host: %s
|
|
|
|
`, url)
|
2021-04-13 00:33:54 -07:00
|
|
|
var cfg DockerSwarmSDConfig
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, yaml.Unmarshal([]byte(cfgString), &cfg))
|
2020-06-26 03:25:58 -07:00
|
|
|
|
2024-01-23 07:53:55 -08:00
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
refreshMetrics := discovery.NewRefreshMetrics(reg)
|
|
|
|
metrics := cfg.NewDiscovererMetrics(reg, refreshMetrics)
|
|
|
|
require.NoError(t, metrics.Register())
|
|
|
|
defer metrics.Unregister()
|
|
|
|
defer refreshMetrics.Unregister()
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
d, err := NewDiscovery(&cfg, promslog.NewNopLogger(), metrics)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-06-26 03:25:58 -07:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-07-09 04:01:08 -07:00
|
|
|
tgs, err := d.refresh(ctx)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-06-26 03:25:58 -07:00
|
|
|
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, tgs, 1)
|
2020-06-26 03:25:58 -07:00
|
|
|
|
|
|
|
tg := tgs[0]
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NotNil(t, tg)
|
|
|
|
require.NotNil(t, tg.Targets)
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, tg.Targets, 5)
|
2020-06-26 03:25:58 -07:00
|
|
|
|
|
|
|
for i, lbls := range []model.LabelSet{
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("10.0.232.3:80"),
|
|
|
|
"__meta_dockerswarm_node_address": model.LabelValue("10.0.232.3"),
|
|
|
|
"__meta_dockerswarm_node_availability": model.LabelValue("active"),
|
|
|
|
"__meta_dockerswarm_node_engine_version": model.LabelValue("19.03.11"),
|
|
|
|
"__meta_dockerswarm_node_hostname": model.LabelValue("master-3"),
|
|
|
|
"__meta_dockerswarm_node_id": model.LabelValue("bvtjl7pnrtg0k88ywialsldpd"),
|
|
|
|
"__meta_dockerswarm_node_manager_address": model.LabelValue("10.0.232.3:2377"),
|
|
|
|
"__meta_dockerswarm_node_manager_leader": model.LabelValue("false"),
|
|
|
|
"__meta_dockerswarm_node_manager_reachability": model.LabelValue("reachable"),
|
|
|
|
"__meta_dockerswarm_node_platform_architecture": model.LabelValue("x86_64"),
|
|
|
|
"__meta_dockerswarm_node_platform_os": model.LabelValue("linux"),
|
|
|
|
"__meta_dockerswarm_node_role": model.LabelValue("manager"),
|
|
|
|
"__meta_dockerswarm_node_status": model.LabelValue("ready"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("10.0.232.1:80"),
|
|
|
|
"__meta_dockerswarm_node_address": model.LabelValue("10.0.232.1"),
|
|
|
|
"__meta_dockerswarm_node_availability": model.LabelValue("active"),
|
|
|
|
"__meta_dockerswarm_node_engine_version": model.LabelValue("19.03.5-ce"),
|
|
|
|
"__meta_dockerswarm_node_hostname": model.LabelValue("oxygen"),
|
|
|
|
"__meta_dockerswarm_node_id": model.LabelValue("d3cw2msquo0d71yn42qrnb0tu"),
|
|
|
|
"__meta_dockerswarm_node_manager_address": model.LabelValue("10.0.232.1:2377"),
|
|
|
|
"__meta_dockerswarm_node_manager_leader": model.LabelValue("true"),
|
|
|
|
"__meta_dockerswarm_node_manager_reachability": model.LabelValue("reachable"),
|
|
|
|
"__meta_dockerswarm_node_platform_architecture": model.LabelValue("x86_64"),
|
|
|
|
"__meta_dockerswarm_node_platform_os": model.LabelValue("linux"),
|
|
|
|
"__meta_dockerswarm_node_role": model.LabelValue("manager"),
|
|
|
|
"__meta_dockerswarm_node_status": model.LabelValue("ready"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("10.0.232.2:80"),
|
|
|
|
"__meta_dockerswarm_node_address": model.LabelValue("10.0.232.2"),
|
|
|
|
"__meta_dockerswarm_node_availability": model.LabelValue("active"),
|
|
|
|
"__meta_dockerswarm_node_engine_version": model.LabelValue("19.03.11"),
|
|
|
|
"__meta_dockerswarm_node_hostname": model.LabelValue("master-2"),
|
|
|
|
"__meta_dockerswarm_node_id": model.LabelValue("i9woemzxymn1n98o9ufebclgm"),
|
|
|
|
"__meta_dockerswarm_node_manager_address": model.LabelValue("10.0.232.2:2377"),
|
|
|
|
"__meta_dockerswarm_node_manager_leader": model.LabelValue("false"),
|
|
|
|
"__meta_dockerswarm_node_manager_reachability": model.LabelValue("reachable"),
|
|
|
|
"__meta_dockerswarm_node_platform_architecture": model.LabelValue("x86_64"),
|
|
|
|
"__meta_dockerswarm_node_platform_os": model.LabelValue("linux"),
|
|
|
|
"__meta_dockerswarm_node_role": model.LabelValue("manager"),
|
|
|
|
"__meta_dockerswarm_node_status": model.LabelValue("ready"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("10.0.232.4:80"),
|
|
|
|
"__meta_dockerswarm_node_address": model.LabelValue("10.0.232.4"),
|
|
|
|
"__meta_dockerswarm_node_availability": model.LabelValue("active"),
|
|
|
|
"__meta_dockerswarm_node_engine_version": model.LabelValue("19.03.11"),
|
|
|
|
"__meta_dockerswarm_node_hostname": model.LabelValue("worker-1"),
|
|
|
|
"__meta_dockerswarm_node_id": model.LabelValue("jkc2xd7p3xdazf0u6sh1m9dmr"),
|
|
|
|
"__meta_dockerswarm_node_platform_architecture": model.LabelValue("x86_64"),
|
|
|
|
"__meta_dockerswarm_node_platform_os": model.LabelValue("linux"),
|
|
|
|
"__meta_dockerswarm_node_role": model.LabelValue("worker"),
|
|
|
|
"__meta_dockerswarm_node_status": model.LabelValue("ready"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"__address__": model.LabelValue("10.0.232.5:80"),
|
|
|
|
"__meta_dockerswarm_node_address": model.LabelValue("10.0.232.5"),
|
|
|
|
"__meta_dockerswarm_node_availability": model.LabelValue("active"),
|
|
|
|
"__meta_dockerswarm_node_engine_version": model.LabelValue("19.03.11"),
|
|
|
|
"__meta_dockerswarm_node_hostname": model.LabelValue("worker-2"),
|
|
|
|
"__meta_dockerswarm_node_id": model.LabelValue("ldawcom10uqi6owysgi28n4ve"),
|
|
|
|
"__meta_dockerswarm_node_platform_architecture": model.LabelValue("x86_64"),
|
|
|
|
"__meta_dockerswarm_node_platform_os": model.LabelValue("linux"),
|
|
|
|
"__meta_dockerswarm_node_role": model.LabelValue("worker"),
|
|
|
|
"__meta_dockerswarm_node_status": model.LabelValue("ready"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, lbls, tg.Targets[i])
|
2020-06-26 03:25:58 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|