// 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 dockerswarm import ( "context" "fmt" "testing" "github.com/go-kit/kit/log" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/util/testutil" ) func TestDockerSwarmSDServicesRefresh(t *testing.T) { sdmock := NewSDMock(t, "swarmprom") sdmock.Setup() e := sdmock.Endpoint() cfg := DefaultSDConfig cfg.Host = e[:len(e)-1] d, err := NewDiscovery(&cfg, log.NewNopLogger()) testutil.Ok(t, err) ctx := context.Background() tgs, err := d.refreshServices(ctx) testutil.Ok(t, err) testutil.Equals(t, 1, len(tgs)) tg := tgs[0] testutil.Assert(t, tg != nil, "tg should not be nil") testutil.Assert(t, tg.Targets != nil, "tg.targets should not be nil") testutil.Equals(t, 10, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { "__address__": model.LabelValue("10.0.0.7:9100"), "__meta_dockerswarm_network_id": model.LabelValue("qvwhwd6p61k4o0ulsknqb066z"), "__meta_dockerswarm_network_ingress": model.LabelValue("true"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_name": model.LabelValue("ingress"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("3qvd7bwfptmoht16t1f7jllb6"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/swarmprom-node-exporter:v0.16.0"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_node-exporter"), "__meta_dockerswarm_service_mode": model.LabelValue("global"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/swarmprom-node-exporter:v0.16.0@sha256:1d2518ec0501dd848e718bf008df37852b1448c862d6f256f2d39244975758d6"), }, { "__address__": model.LabelValue("10.0.1.2:9100"), "__meta_dockerswarm_network_id": model.LabelValue("npq2closzy836m07eaq1425k3"), "__meta_dockerswarm_network_ingress": model.LabelValue("false"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_network_name": model.LabelValue("mon_net"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("3qvd7bwfptmoht16t1f7jllb6"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/swarmprom-node-exporter:v0.16.0"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_node-exporter"), "__meta_dockerswarm_service_mode": model.LabelValue("global"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/swarmprom-node-exporter:v0.16.0@sha256:1d2518ec0501dd848e718bf008df37852b1448c862d6f256f2d39244975758d6"), }, { "__address__": model.LabelValue("10.0.0.13:9090"), "__meta_dockerswarm_network_id": model.LabelValue("qvwhwd6p61k4o0ulsknqb066z"), "__meta_dockerswarm_network_ingress": model.LabelValue("true"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_name": model.LabelValue("ingress"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.1.15:9090"), "__meta_dockerswarm_network_id": model.LabelValue("npq2closzy836m07eaq1425k3"), "__meta_dockerswarm_network_ingress": model.LabelValue("false"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_network_name": model.LabelValue("mon_net"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.0.13:9093"), "__meta_dockerswarm_network_id": model.LabelValue("qvwhwd6p61k4o0ulsknqb066z"), "__meta_dockerswarm_network_ingress": model.LabelValue("true"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_name": model.LabelValue("ingress"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.1.15:9093"), "__meta_dockerswarm_network_id": model.LabelValue("npq2closzy836m07eaq1425k3"), "__meta_dockerswarm_network_ingress": model.LabelValue("false"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_network_name": model.LabelValue("mon_net"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.0.13:9094"), "__meta_dockerswarm_network_id": model.LabelValue("qvwhwd6p61k4o0ulsknqb066z"), "__meta_dockerswarm_network_ingress": model.LabelValue("true"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_name": model.LabelValue("ingress"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.1.15:9094"), "__meta_dockerswarm_network_id": model.LabelValue("npq2closzy836m07eaq1425k3"), "__meta_dockerswarm_network_ingress": model.LabelValue("false"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_network_name": model.LabelValue("mon_net"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("tkv91uso46cck763aaqcb4rek"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/caddy"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_caddy"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/caddy:latest@sha256:44541cfacb66f4799f81f17fcfb3cb757ccc8f327592745549f5930c42d115c9"), }, { "__address__": model.LabelValue("10.0.0.15:3000"), "__meta_dockerswarm_network_id": model.LabelValue("qvwhwd6p61k4o0ulsknqb066z"), "__meta_dockerswarm_network_ingress": model.LabelValue("true"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_name": model.LabelValue("ingress"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("uk9su5tb9ykfzew3qtmp14uzh"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/swarmprom-grafana:5.3.4"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_grafana"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/swarmprom-grafana:5.3.4@sha256:2aca8aa5716e6e0eed3fcdc88fec256a0a1828c491a8cf240486ae7cc473092d"), }, { "__address__": model.LabelValue("10.0.1.29:3000"), "__meta_dockerswarm_network_id": model.LabelValue("npq2closzy836m07eaq1425k3"), "__meta_dockerswarm_network_ingress": model.LabelValue("false"), "__meta_dockerswarm_network_internal": model.LabelValue("false"), "__meta_dockerswarm_network_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_network_name": model.LabelValue("mon_net"), "__meta_dockerswarm_network_scope": model.LabelValue("swarm"), "__meta_dockerswarm_service_endpoint_port_name": model.LabelValue(""), "__meta_dockerswarm_service_endpoint_port_publish_mode": model.LabelValue("ingress"), "__meta_dockerswarm_service_id": model.LabelValue("uk9su5tb9ykfzew3qtmp14uzh"), "__meta_dockerswarm_service_label_com_docker_stack_image": model.LabelValue("stefanprodan/swarmprom-grafana:5.3.4"), "__meta_dockerswarm_service_label_com_docker_stack_namespace": model.LabelValue("mon"), "__meta_dockerswarm_service_name": model.LabelValue("mon_grafana"), "__meta_dockerswarm_service_mode": model.LabelValue("replicated"), "__meta_dockerswarm_service_task_container_hostname": model.LabelValue(""), "__meta_dockerswarm_service_task_container_image": model.LabelValue("stefanprodan/swarmprom-grafana:5.3.4@sha256:2aca8aa5716e6e0eed3fcdc88fec256a0a1828c491a8cf240486ae7cc473092d"), }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { testutil.Equals(t, lbls, tg.Targets[i]) }) } }