Refactor dockerswarm refresh for testing (#7541)

We were missing testing on the behaviour of the configuration
unmarshalling.

This PR adds a refresh command that can be used to test that we
use the correct refresh function.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
Julien Pivotto 2020-07-09 13:01:08 +02:00 committed by GitHub
parent 663016ee0c
commit 93ecf0e14c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 20 deletions

View file

@ -82,6 +82,7 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Discovery struct {
*refresh.Discovery
client *client.Client
role string
port int
}
@ -89,6 +90,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
d := &Discovery{
port: conf.Port,
role: conf.Role,
}
rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
@ -117,21 +119,24 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
return nil, fmt.Errorf("error setting up docker swarm client: %w", err)
}
var r func(context.Context) ([]*targetgroup.Group, error)
switch conf.Role {
case "services":
r = d.refreshServices
case "nodes":
r = d.refreshNodes
case "tasks":
r = d.refreshTasks
}
d.Discovery = refresh.NewDiscovery(
logger,
"dockerswarm",
time.Duration(conf.RefreshInterval),
r,
d.refresh,
)
return d, nil
}
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
switch d.role {
case "services":
return d.refreshServices(ctx)
case "nodes":
return d.refreshNodes(ctx)
case "tasks":
return d.refreshTasks(ctx)
default:
panic(fmt.Errorf("unexpected role %s", d.role))
}
}

View file

@ -21,6 +21,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2"
)
func TestDockerSwarmNodesSDRefresh(t *testing.T) {
@ -28,14 +29,20 @@ func TestDockerSwarmNodesSDRefresh(t *testing.T) {
sdmock.Setup()
e := sdmock.Endpoint()
cfg := DefaultSDConfig
cfg.Host = e[:len(e)-1]
url := e[:len(e)-1]
cfgString := fmt.Sprintf(`
---
role: nodes
host: %s
`, url)
var cfg SDConfig
testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg))
d, err := NewDiscovery(&cfg, log.NewNopLogger())
testutil.Ok(t, err)
ctx := context.Background()
tgs, err := d.refreshNodes(ctx)
tgs, err := d.refresh(ctx)
testutil.Ok(t, err)
testutil.Equals(t, 1, len(tgs))

View file

@ -21,6 +21,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2"
)
func TestDockerSwarmSDServicesRefresh(t *testing.T) {
@ -28,14 +29,20 @@ func TestDockerSwarmSDServicesRefresh(t *testing.T) {
sdmock.Setup()
e := sdmock.Endpoint()
cfg := DefaultSDConfig
cfg.Host = e[:len(e)-1]
url := e[:len(e)-1]
cfgString := fmt.Sprintf(`
---
role: services
host: %s
`, url)
var cfg SDConfig
testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg))
d, err := NewDiscovery(&cfg, log.NewNopLogger())
testutil.Ok(t, err)
ctx := context.Background()
tgs, err := d.refreshServices(ctx)
tgs, err := d.refresh(ctx)
testutil.Ok(t, err)
testutil.Equals(t, 1, len(tgs))

View file

@ -21,6 +21,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2"
)
func TestDockerSwarmTasksSDRefresh(t *testing.T) {
@ -28,14 +29,20 @@ func TestDockerSwarmTasksSDRefresh(t *testing.T) {
sdmock.Setup()
e := sdmock.Endpoint()
cfg := DefaultSDConfig
cfg.Host = e[:len(e)-1]
url := e[:len(e)-1]
cfgString := fmt.Sprintf(`
---
role: tasks
host: %s
`, url)
var cfg SDConfig
testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg))
d, err := NewDiscovery(&cfg, log.NewNopLogger())
testutil.Ok(t, err)
ctx := context.Background()
tgs, err := d.refreshTasks(ctx)
tgs, err := d.refresh(ctx)
testutil.Ok(t, err)
testutil.Equals(t, 1, len(tgs))