Skip to content

Commit 839899d

Browse files
committed
etcd_docker 1: Refactor resources/docker package into resources/dockerexternal, resources/dockerm3, x/dockertest
PR 1 of etcd test refactor I am introducing a docker based etcd test framework. Overall structure I'm going for: resources/dockerm3 -- m3 docker containers resources/dockerexternal -- dependencies (prometheus, etcd) The reason I need this refactor: I'm going to pull the dockerexternal package into a bunch of internal tests. This introduces circular dependencies with the M3 resources, thus splitting out here. commit-id:f922a2aa
1 parent 42e0064 commit 839899d

File tree

15 files changed

+327
-209
lines changed

15 files changed

+327
-209
lines changed

src/integration/prometheus/prometheus.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
3838
"github.com/m3db/m3/src/dbnode/kvconfig"
3939
"github.com/m3db/m3/src/integration/resources"
40-
"github.com/m3db/m3/src/integration/resources/docker"
40+
"github.com/m3db/m3/src/integration/resources/docker/dockerexternal"
4141
"github.com/m3db/m3/src/query/api/v1/handler/database"
4242
"github.com/m3db/m3/src/query/api/v1/options"
4343
"github.com/m3db/m3/src/query/generated/proto/prompb"
@@ -89,7 +89,7 @@ func RunTest(t *testing.T, m3 resources.M3Resources, prom resources.ExternalReso
8989

9090
logger.Info("running prometheus tests")
9191

92-
p := prom.(*docker.Prometheus)
92+
p := prom.(*dockerexternal.Prometheus)
9393

9494
testPrometheusRemoteRead(t, p, logger)
9595
testPrometheusRemoteWriteMultiNamespaces(t, p, logger)
@@ -118,15 +118,15 @@ func RunTest(t *testing.T, m3 resources.M3Resources, prom resources.ExternalReso
118118
testDebugPromReturnsDuplicates(t, m3, logger)
119119
}
120120

121-
func testPrometheusRemoteRead(t *testing.T, p *docker.Prometheus, logger *zap.Logger) {
121+
func testPrometheusRemoteRead(t *testing.T, p *dockerexternal.Prometheus, logger *zap.Logger) {
122122
// Ensure Prometheus can proxy a Prometheus query
123123
logger.Info("testing prometheus remote read")
124124
verifyPrometheusQuery(t, p, "prometheus_remote_storage_samples_total", 100)
125125
}
126126

127127
func testPrometheusRemoteWriteMultiNamespaces(
128128
t *testing.T,
129-
p *docker.Prometheus,
129+
p *dockerexternal.Prometheus,
130130
logger *zap.Logger,
131131
) {
132132
logger.Info("testing remote write to multiple namespaces")
@@ -1839,9 +1839,9 @@ func requireSeriesSuccess(
18391839
}))
18401840
}
18411841

1842-
func verifyPrometheusQuery(t *testing.T, p *docker.Prometheus, query string, threshold float64) {
1842+
func verifyPrometheusQuery(t *testing.T, p *dockerexternal.Prometheus, query string, threshold float64) {
18431843
require.NoError(t, resources.Retry(func() error {
1844-
res, err := p.Query(docker.PrometheusQueryRequest{
1844+
res, err := p.Query(dockerexternal.PrometheusQueryRequest{
18451845
Query: query,
18461846
})
18471847
if err != nil {

src/integration/prometheus/prometheus_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//go:build cluster_integration
12
// +build cluster_integration
3+
24
//
35
// Copyright (c) 2021 Uber Technologies, Inc.
46
//
@@ -23,12 +25,13 @@
2325
package prometheus
2426

2527
import (
28+
"context"
2629
"path"
2730
"runtime"
2831
"testing"
2932

3033
"github.com/m3db/m3/src/integration/resources"
31-
"github.com/m3db/m3/src/integration/resources/docker"
34+
"github.com/m3db/m3/src/integration/resources/docker/dockerexternal"
3235
"github.com/m3db/m3/src/integration/resources/inprocess"
3336

3437
"github.com/ory/dockertest/v3"
@@ -60,14 +63,14 @@ func testSetup(t *testing.T) (resources.M3Resources, resources.ExternalResources
6063
require.NoError(t, err)
6164

6265
_, filename, _, _ := runtime.Caller(0)
63-
prom := docker.NewPrometheus(docker.PrometheusOptions{
66+
prom := dockerexternal.NewPrometheus(dockerexternal.PrometheusOptions{
6467
Pool: pool,
65-
PathToCfg: path.Join(path.Dir(filename), "../resources/docker/config/prometheus.yml"),
68+
PathToCfg: path.Join(path.Dir(filename), "../resources/docker/dockerexternal/config/prometheus.yml"),
6669
})
67-
require.NoError(t, prom.Setup())
70+
require.NoError(t, prom.Setup(context.TODO()))
6871

6972
return m3, prom, func() {
70-
assert.NoError(t, prom.Close())
73+
assert.NoError(t, prom.Close(context.TODO()))
7174
assert.NoError(t, m3.Cleanup())
7275
}
7376
}

src/integration/resources/docker/prometheus.go renamed to src/integration/resources/docker/dockerexternal/prometheus.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021 Uber Technologies, Inc.
1+
// Copyright (c) 2022 Uber Technologies, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
package docker
21+
package dockerexternal
2222

2323
import (
2424
"context"
@@ -29,9 +29,10 @@ import (
2929
"net/http"
3030
"time"
3131

32-
"github.com/m3db/m3/src/integration/resources"
32+
xdockertest "github.com/m3db/m3/src/x/dockertest"
3333
"github.com/m3db/m3/src/x/instrument"
3434

35+
"github.com/cenkalti/backoff/v3"
3536
"github.com/ory/dockertest/v3"
3637
"github.com/prometheus/common/model"
3738
)
@@ -42,7 +43,7 @@ type Prometheus struct {
4243
pathToCfg string
4344
iOpts instrument.Options
4445

45-
resource *Resource
46+
resource *xdockertest.Resource
4647
}
4748

4849
// PrometheusOptions contains the options for
@@ -60,7 +61,7 @@ type PrometheusOptions struct {
6061

6162
// NewPrometheus creates a new docker-backed Prometheus
6263
// that implements the resources.ExternalResources interface.
63-
func NewPrometheus(opts PrometheusOptions) resources.ExternalResources {
64+
func NewPrometheus(opts PrometheusOptions) *Prometheus {
6465
if opts.InstrumentOptions == nil {
6566
opts.InstrumentOptions = instrument.NewOptions()
6667
}
@@ -72,19 +73,19 @@ func NewPrometheus(opts PrometheusOptions) resources.ExternalResources {
7273
}
7374

7475
// Setup is a method that setups up the prometheus instance.
75-
func (p *Prometheus) Setup() error {
76+
func (p *Prometheus) Setup(ctx context.Context) error {
7677
if p.resource != nil {
7778
return errors.New("prometheus already setup. must close resource " +
7879
"before attempting to setup again")
7980
}
8081

81-
if err := SetupNetwork(p.pool); err != nil {
82+
if err := xdockertest.SetupNetwork(p.pool, true); err != nil {
8283
return err
8384
}
8485

85-
res, err := NewDockerResource(p.pool, ResourceOptions{
86+
res, err := xdockertest.NewDockerResource(p.pool, xdockertest.ResourceOptions{
8687
ContainerName: "prometheus",
87-
Image: Image{
88+
Image: xdockertest.Image{
8889
Name: "prom/prometheus",
8990
Tag: "latest",
9091
},
@@ -100,11 +101,12 @@ func (p *Prometheus) Setup() error {
100101

101102
p.resource = res
102103

103-
return p.waitForHealthy()
104+
return p.waitForHealthy(ctx)
104105
}
105106

106-
func (p *Prometheus) waitForHealthy() error {
107-
return resources.Retry(func() error {
107+
func (p *Prometheus) waitForHealthy(ctx context.Context) error {
108+
retrier := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
109+
return backoff.Retry(func() error {
108110
req, err := http.NewRequestWithContext(
109111
context.Background(),
110112
http.MethodGet,
@@ -126,7 +128,7 @@ func (p *Prometheus) waitForHealthy() error {
126128
}
127129

128130
return errors.New("prometheus not ready")
129-
})
131+
}, retrier)
130132
}
131133

132134
// PrometheusQueryRequest contains the parameters for making a query request.
@@ -152,7 +154,7 @@ func (p *PrometheusQueryRequest) String() string {
152154
// Query executes a query request against the prometheus resource.
153155
func (p *Prometheus) Query(req PrometheusQueryRequest) (model.Vector, error) {
154156
if p.resource.Closed() {
155-
return nil, errClosed
157+
return nil, xdockertest.ErrClosed
156158
}
157159

158160
r, err := http.NewRequestWithContext(
@@ -201,9 +203,9 @@ type vectorResult struct {
201203
}
202204

203205
// Close cleans up the prometheus instance.
204-
func (p *Prometheus) Close() error {
206+
func (p *Prometheus) Close(context.Context) error {
205207
if p.resource.Closed() {
206-
return errClosed
208+
return xdockertest.ErrClosed
207209
}
208210

209211
if err := p.resource.Close(); err != nil {

src/integration/resources/docker/prometheus_test.go renamed to src/integration/resources/docker/dockerexternal/prometheus_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// +build integration_v2
2-
// Copyright (c) 2021 Uber Technologies, Inc.
1+
// Copyright (c) 2022 Uber Technologies, Inc.
32
//
43
// Permission is hereby granted, free of charge, to any person obtaining a copy
54
// of this software and associated documentation files (the "Software"), to deal
@@ -19,9 +18,13 @@
1918
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2019
// THE SOFTWARE.
2120

22-
package docker
21+
//go:build integration_v2
22+
// +build integration_v2
23+
24+
package dockerexternal
2325

2426
import (
27+
"context"
2528
"path"
2629
"runtime"
2730
"testing"
@@ -39,6 +42,6 @@ func TestNewPrometheus(t *testing.T) {
3942
Pool: pool,
4043
PathToCfg: path.Join(path.Dir(filename), "config/prometheus.yml"),
4144
})
42-
require.NoError(t, prom.Setup())
43-
require.NoError(t, prom.Close())
45+
require.NoError(t, prom.Setup(context.TODO()))
46+
require.NoError(t, prom.Close(context.TODO()))
4447
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2022 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package dockerm3
22+
23+
import (
24+
"go.uber.org/zap"
25+
"go.uber.org/zap/zapcore"
26+
)
27+
28+
// zapMethod appends the method as a log field.
29+
func zapMethod(s string) zapcore.Field { return zap.String("method", s) }

0 commit comments

Comments
 (0)