Merge pull request #7635 from roidelapluie/sdtests2

Tests for digitalocean and Docker Swarm configs
This commit is contained in:
Julien Pivotto 2020-07-22 10:56:37 +02:00 committed by GitHub
commit 89d2f5ec1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 13 deletions

View File

@ -30,7 +30,9 @@ import (
"github.com/prometheus/prometheus/discovery/azure" "github.com/prometheus/prometheus/discovery/azure"
sd_config "github.com/prometheus/prometheus/discovery/config" sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/consul" "github.com/prometheus/prometheus/discovery/consul"
"github.com/prometheus/prometheus/discovery/digitalocean"
"github.com/prometheus/prometheus/discovery/dns" "github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/discovery/dockerswarm"
"github.com/prometheus/prometheus/discovery/ec2" "github.com/prometheus/prometheus/discovery/ec2"
"github.com/prometheus/prometheus/discovery/file" "github.com/prometheus/prometheus/discovery/file"
"github.com/prometheus/prometheus/discovery/kubernetes" "github.com/prometheus/prometheus/discovery/kubernetes"
@ -605,6 +607,49 @@ var expectedConf = &Config{
}, },
}, },
}, },
{
JobName: "digitalocean-droplets",
HonorTimestamps: true,
ScrapeInterval: model.Duration(15 * time.Second),
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme,
ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{
DigitalOceanSDConfigs: []*digitalocean.SDConfig{
{
HTTPClientConfig: config_util.HTTPClientConfig{
BearerToken: "abcdef",
},
Port: 80,
RefreshInterval: model.Duration(60 * time.Second),
},
},
},
},
{
JobName: "dockerswarm",
HonorTimestamps: true,
ScrapeInterval: model.Duration(15 * time.Second),
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme,
ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{
DockerSwarmSDConfigs: []*dockerswarm.SDConfig{
{
Host: "http://127.0.0.1:2375",
Role: "nodes",
Port: 80,
RefreshInterval: model.Duration(60 * time.Second),
},
},
},
},
{ {
JobName: "service-openstack", JobName: "service-openstack",
@ -691,7 +736,7 @@ func TestElideSecrets(t *testing.T) {
yamlConfig := string(config) yamlConfig := string(config)
matches := secretRe.FindAllStringIndex(yamlConfig, -1) matches := secretRe.FindAllStringIndex(yamlConfig, -1)
testutil.Assert(t, len(matches) == 7, "wrong number of secret matches found") testutil.Assert(t, len(matches) == 8, "wrong number of secret matches found")
testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"), testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"),
"yaml marshal reveals authentication credentials.") "yaml marshal reveals authentication credentials.")
} }

View File

@ -259,6 +259,15 @@ scrape_configs:
cert_file: valid_cert_file cert_file: valid_cert_file
key_file: valid_key_file key_file: valid_key_file
- job_name: digitalocean-droplets
digitalocean_sd_configs:
- bearer_token: abcdef
- job_name: dockerswarm
dockerswarm_sd_configs:
- host: http://127.0.0.1:2375
role: nodes
- job_name: service-openstack - job_name: service-openstack
openstack_sd_configs: openstack_sd_configs:
- role: instance - role: instance

View File

@ -44,7 +44,6 @@ type SDConfig struct {
HTTPClientConfig config_util.HTTPClientConfig `yaml:",inline"` HTTPClientConfig config_util.HTTPClientConfig `yaml:",inline"`
Host string `yaml:"host"` Host string `yaml:"host"`
url *url.URL
Role string `yaml:"role"` Role string `yaml:"role"`
Port int `yaml:"port"` Port int `yaml:"port"`
@ -62,11 +61,9 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if c.Host == "" { if c.Host == "" {
return fmt.Errorf("host missing") return fmt.Errorf("host missing")
} }
url, err := url.Parse(c.Host) if _, err = url.Parse(c.Host); err != nil {
if err != nil {
return err return err
} }
c.url = url
switch c.Role { switch c.Role {
case "services", "nodes", "tasks": case "services", "nodes", "tasks":
case "": case "":
@ -89,18 +86,16 @@ type Discovery struct {
// NewDiscovery returns a new Discovery which periodically refreshes its targets. // NewDiscovery returns a new Discovery which periodically refreshes its targets.
func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
var err error var err error
d := &Discovery{ d := &Discovery{
port: conf.Port, port: conf.Port,
role: conf.Role, role: conf.Role,
} }
// This is used in tests. In normal situations, it is set when Unmarshaling. hostURL, err := url.Parse(conf.Host)
if conf.url == nil {
conf.url, err = url.Parse(conf.Host)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
opts := []client.Opt{ opts := []client.Opt{
client.WithHost(conf.Host), client.WithHost(conf.Host),
@ -110,7 +105,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
// There are other protocols than HTTP supported by the Docker daemon, like // There are other protocols than HTTP supported by the Docker daemon, like
// unix, which are not supported by the HTTP client. Passing HTTP client // unix, which are not supported by the HTTP client. Passing HTTP client
// options to the Docker client makes those non-HTTP requests fail. // options to the Docker client makes those non-HTTP requests fail.
if conf.url.Scheme == "http" || conf.url.Scheme == "https" { if hostURL.Scheme == "http" || hostURL.Scheme == "https" {
rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false) rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -120,7 +115,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
Transport: rt, Transport: rt,
Timeout: time.Duration(conf.RefreshInterval), Timeout: time.Duration(conf.RefreshInterval),
}), }),
client.WithScheme(conf.url.Scheme), client.WithScheme(hostURL.Scheme),
) )
} }