-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Add support for resources.ExternalResources (#3811)
This commit adds an interface for adding external (i.e. not M3) resources that can be used in integration tests. An implementation of the interface that allows users to spin up a docker-backed Prometheus is added as well. This also functions as a working example for how the interface can be implemented to support other external resources.
- Loading branch information
Showing
7 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# my global config | ||
global: | ||
external_labels: | ||
role: "remote" | ||
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. | ||
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. | ||
# scrape_timeout is set to the global default (10s). | ||
|
||
# Alertmanager configuration | ||
alerting: | ||
alertmanagers: | ||
- static_configs: | ||
- targets: | ||
# - alertmanager:9093 | ||
|
||
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. | ||
rule_files: | ||
# - "first_rules.yml" | ||
# - "second_rules.yml" | ||
|
||
# A scrape configuration containing exactly one endpoint to scrape: | ||
# Here it's Prometheus itself. | ||
scrape_configs: | ||
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. | ||
- job_name: 'prometheus' | ||
|
||
# metrics_path defaults to '/metrics' | ||
# scheme defaults to 'http'. | ||
|
||
static_configs: | ||
- targets: ['localhost:9090'] | ||
|
||
- job_name: 'coordinator' | ||
static_configs: | ||
- targets: ['coordinator01:7203'] | ||
|
||
- job_name: 'dbnode' | ||
static_configs: | ||
- targets: ['dbnode01:9004'] | ||
|
||
remote_read: | ||
- url: http://coordinator01:7201/api/v1/prom/remote/read | ||
|
||
remote_write: | ||
- url: http://coordinator01:7201/api/v1/prom/remote/write | ||
write_relabel_configs: | ||
- target_label: metrics_storage | ||
replacement: m3db_remote |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package docker | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/ory/dockertest/v3" | ||
|
||
"github.com/m3db/m3/src/integration/resources" | ||
"github.com/m3db/m3/src/x/instrument" | ||
) | ||
|
||
type prometheus struct { | ||
pool *dockertest.Pool | ||
pathToCfg string | ||
iOpts instrument.Options | ||
|
||
resource *dockerResource | ||
} | ||
|
||
// PrometheusOptions contains the options for | ||
// spinning up docker container running Prometheus | ||
type PrometheusOptions struct { | ||
// Pool is the connection to the docker API | ||
Pool *dockertest.Pool | ||
// PathToCfg contains the path to the prometheus.yml configuration | ||
// file to be used on startup. | ||
PathToCfg string | ||
// InstrumentOptions are the instrument.Options to use when | ||
// creating the resource. | ||
InstrumentOptions instrument.Options | ||
} | ||
|
||
// NewPrometheus creates a new docker-backed Prometheus | ||
// that implements the resources.ExternalResources interface. | ||
func NewPrometheus(opts PrometheusOptions) resources.ExternalResources { | ||
if opts.InstrumentOptions == nil { | ||
opts.InstrumentOptions = instrument.NewOptions() | ||
} | ||
return &prometheus{ | ||
pool: opts.Pool, | ||
pathToCfg: opts.PathToCfg, | ||
iOpts: opts.InstrumentOptions, | ||
} | ||
} | ||
|
||
func (p *prometheus) Setup() error { | ||
if p.resource != nil { | ||
return errors.New("prometheus already setup. must close resource " + | ||
"before attempting to setup again") | ||
} | ||
|
||
if err := setupNetwork(p.pool); err != nil { | ||
return err | ||
} | ||
|
||
res, err := newDockerResource(p.pool, dockerResourceOptions{ | ||
containerName: "prometheus", | ||
image: dockerImage{ | ||
name: "prom/prometheus", | ||
tag: "latest", | ||
}, | ||
portList: []int{9090}, | ||
mounts: []string{ | ||
fmt.Sprintf("%s:/etc/prometheus/prometheus.yml", p.pathToCfg), | ||
}, | ||
iOpts: p.iOpts, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.resource = res | ||
|
||
return nil | ||
} | ||
|
||
func (p *prometheus) Close() error { | ||
if p.resource.closed { | ||
return errClosed | ||
} | ||
|
||
if err := p.resource.close(); err != nil { | ||
return err | ||
} | ||
|
||
p.resource = nil | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// +build integration_v2 | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package docker | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"path" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v3" | ||
"github.com/ory/dockertest/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewPrometheus(t *testing.T) { | ||
pool, err := dockertest.NewPool("") | ||
require.NoError(t, err) | ||
|
||
_, filename, _, _ := runtime.Caller(0) | ||
prom := NewPrometheus(PrometheusOptions{ | ||
Pool: pool, | ||
PathToCfg: path.Join(path.Dir(filename), "config/prometheus.yml"), | ||
}) | ||
require.NoError(t, prom.Setup()) | ||
defer func() { | ||
assert.NoError(t, prom.Close()) | ||
}() | ||
|
||
bo := backoff.NewConstantBackOff(1 * time.Second) | ||
var ( | ||
attempts = 0 | ||
maxAttempts = 5 | ||
) | ||
err = backoff.Retry(func() error { | ||
req, err := http.NewRequestWithContext(context.Background(), "GET", "http://0.0.0.0:9090/-/ready", nil) | ||
require.NoError(t, err) | ||
|
||
client := http.Client{} | ||
res, _ := client.Do(req) | ||
if res != nil && res.StatusCode == 200 { | ||
return nil | ||
} | ||
|
||
attempts++ | ||
if attempts >= maxAttempts { | ||
return backoff.Permanent(errors.New("prometheus not ready")) | ||
} | ||
return errors.New("retryable error") | ||
}, bo) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters