mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 05:34:05 -08:00
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:
parent
663016ee0c
commit
93ecf0e14c
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue