diff --git a/libbeat/tests/compose/compose.go b/libbeat/tests/compose/compose.go index d39459ee88f3..69785e96b39a 100644 --- a/libbeat/tests/compose/compose.go +++ b/libbeat/tests/compose/compose.go @@ -19,74 +19,97 @@ package compose import ( "errors" + "fmt" "os" "path/filepath" "strconv" + "strings" "testing" ) // EnsureUp starts all the requested services (must be defined in docker-compose.yml) // with a default timeout of 300 seconds -func EnsureUp(t *testing.T, services ...string) { - EnsureUpWithTimeout(t, 60, services...) +func EnsureUp(t *testing.T, service string) R { + return EnsureUpWithTimeout(t, 60, service) } // EnsureUpWithTimeout starts all the requested services (must be defined in docker-compose.yml) // Wait for `timeout` seconds for health -func EnsureUpWithTimeout(t *testing.T, timeout int, services ...string) { +func EnsureUpWithTimeout(t *testing.T, timeout int, service string) R { // The NO_COMPOSE env variables makes it possible to skip the starting of the environment. // This is useful if the service is already running locally. if noCompose, err := strconv.ParseBool(os.Getenv("NO_COMPOSE")); err == nil && noCompose { - return + envVar := fmt.Sprintf("%s_HOST", strings.ToUpper(service)) + host := os.Getenv(envVar) + if host == "" { + t.Fatalf("%s environment variable must be set as the host:port where %s is running", envVar, service) + } + return &runnerControl{host: host} } - compose, err := getComposeProject() + compose, err := getComposeProject(os.Getenv("DOCKER_COMPOSE_PROJECT_NAME")) if err != nil { t.Fatal(err) } // Kill no longer used containers - err = compose.KillOld(services) + err = compose.KillOld([]string{service}) if err != nil { t.Fatal(err) } - for _, service := range services { - err = compose.Start(service) - if err != nil { - t.Fatal("failed to start service", service, err) - } + // Start container + err = compose.Start(service, RecreateOnUp(false)) + if err != nil { + t.Fatal("failed to start service", service, err) } // Wait for health - err = compose.Wait(timeout, services...) + err = compose.Wait(timeout, service) if err != nil { t.Fatal(err) } + + // Get host information + host, err := compose.Host(service) + if err != nil { + t.Fatalf("getting host for %s", service) + } + + return &runnerControl{host: host} } -func getComposeProject() (*Project, error) { +func findComposePath() (string, error) { // find docker-compose path, err := os.Getwd() if err != nil { - return nil, err + return "", err } for { if path == "/" { - return nil, errors.New("docker-compose.yml not found") + break } - if _, err = os.Stat(filepath.Join(path, "docker-compose.yml")); err != nil { - path = filepath.Dir(path) - } else { - break + composePath := filepath.Join(path, "docker-compose.yml") + if _, err = os.Stat(composePath); err == nil { + return composePath, nil } + path = filepath.Dir(path) + } + + return "", errors.New("docker-compose.yml not found") +} + +func getComposeProject(name string) (*Project, error) { + path, err := findComposePath() + if err != nil { + return nil, err } return NewProject( - os.Getenv("DOCKER_COMPOSE_PROJECT_NAME"), + name, []string{ - filepath.Join(path, "docker-compose.yml"), + path, }, ) } diff --git a/libbeat/tests/compose/project.go b/libbeat/tests/compose/project.go index 6df5d3585600..8a78f0e421ee 100644 --- a/libbeat/tests/compose/project.go +++ b/libbeat/tests/compose/project.go @@ -38,6 +38,16 @@ type UpOptions struct { Create CreateOptions } +// UpOption customizes UpOptions +type UpOption func(*UpOptions) + +// RecreateOnUp enables or disables recreation of container on startup +func RecreateOnUp(recreate bool) UpOption { + return func(opts *UpOptions) { + opts.Create.ForceRecreate = recreate + } +} + // Filter options for services type Filter struct { State State @@ -56,9 +66,11 @@ const ( // Driver is the interface of docker compose implementations type Driver interface { Up(ctx context.Context, opts UpOptions, service string) error + Down(ctx context.Context) error Kill(ctx context.Context, signal string, service string) error Ps(ctx context.Context, filter ...string) ([]ContainerStatus, error) - // Containers(ctx context.Context, projectFilter Filter, filter ...string) ([]string, error) + + SetParameters(map[string]string) LockFile() string } @@ -69,6 +81,7 @@ type ContainerStatus interface { Healthy() bool Running() bool Old() bool + Host() string } // Project is a docker-compose project @@ -93,28 +106,22 @@ func NewProject(name string, files []string) (*Project, error) { } // Start the container, unless it's running already -func (c *Project) Start(service string) error { - servicesStatus, err := c.getServices(service) - if err != nil { - return err - } - - if servicesStatus[service] != nil { - if servicesStatus[service].Running { - // Someone is running it - return nil - } - } - +func (c *Project) Start(service string, upOptions ...UpOption) error { c.Lock() defer c.Unlock() - return c.Driver.Up(context.Background(), UpOptions{ + options := UpOptions{ Create: CreateOptions{ Build: true, ForceRecreate: true, }, - }, service) + } + + for _, option := range upOptions { + option(&options) + } + + return c.Driver.Up(context.Background(), options, service) } // Wait ensures all wanted services are healthy. Wait loop (60s timeout) @@ -149,6 +156,25 @@ func (c *Project) Wait(seconds int, services ...string) error { return nil } +// Host gets the host and port of a service +func (c *Project) Host(service string) (string, error) { + servicesStatus, err := c.getServices(service) + if err != nil { + return "", err + } + + if len(servicesStatus) == 0 { + return "", errors.New("no container running for service") + } + + status, ok := servicesStatus[service] + if !ok || status.Host == "" { + return "", errors.New("unknown host:port for service") + } + + return status.Host, nil +} + // Kill a container func (c *Project) Kill(service string) error { c.Lock() @@ -190,6 +216,14 @@ func (c *Project) KillOld(except []string) error { return nil } +// Down removes all resources of a project +func (c *Project) Down() error { + c.Lock() + defer c.Unlock() + + return c.Driver.Down(context.Background()) +} + // Lock acquires the lock (300s) timeout // Normally it should only be seconds that the lock is used, but in some cases it can take longer. func (c *Project) Lock() { @@ -226,6 +260,8 @@ type serviceInfo struct { Running bool Healthy bool + Host string + // Has been up for too long?: Old bool } @@ -259,6 +295,7 @@ func (c *Project) getServices(filter ...string) (map[string]*serviceInfo, error) if service.Healthy { service.Old = c.Old() } + service.Host = c.Host() result[name] = service } diff --git a/libbeat/tests/compose/runner.go b/libbeat/tests/compose/runner.go new file mode 100644 index 000000000000..95e851458f63 --- /dev/null +++ b/libbeat/tests/compose/runner.go @@ -0,0 +1,263 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package compose + +import ( + "crypto/sha1" + "flag" + "fmt" + "math/rand" + "net" + "os" + "sort" + "strings" + "testing" + "time" + + "github.com/pkg/errors" +) + +var composeReuse bool +var composeCleanup bool + +func init() { + rand.Seed(time.Now().UnixNano()) + + flag.BoolVar(&composeReuse, "compose.reuse", false, "Set to true to reuse scenarios") + flag.BoolVar(&composeCleanup, "compose.cleanup", false, "Set to true to remove reused scenarios") +} + +// TestRunner starts a service with different combinations of options and +// runs tests on each one of these combinations +type TestRunner struct { + // Name of the service managed by this runner + Service string + + // Map of options with the list of possible values + Options RunnerOptions + + // Set to true if this runner can run in parallel with other runners + Parallel bool + + // Timeout to start the managed service + Timeout int +} + +// Suite is a set of tests to be run with a TestRunner +// Each test must be one of: +// - func(R) +// - func(*testing.T, R) +// - func(*testing.T) +type Suite map[string]interface{} + +// RunnerOptions are the possible options of a runner scenario +type RunnerOptions map[string][]string + +func (r *TestRunner) scenarios() []map[string]string { + n := 1 + options := make(map[string][]string) + for env, values := range r.Options { + // Allow to override options from environment variables + value := os.Getenv(env) + if value != "" { + values = []string{value} + } + options[env] = values + n *= len(values) + } + + scenarios := make([]map[string]string, n) + for variable, values := range options { + v := 0 + for i, s := range scenarios { + if s == nil { + s = make(map[string]string) + scenarios[i] = s + } + s[variable] = values[v] + v = (v + 1) % len(values) + } + } + + return scenarios +} + +func (r *TestRunner) runSuite(t *testing.T, tests Suite, ctl R) { + for name, test := range tests { + var testFunc func(t *testing.T) + switch f := test.(type) { + case func(R): + testFunc = func(t *testing.T) { f(ctl.WithT(t)) } + case func(*testing.T, R): + testFunc = func(t *testing.T) { f(t, ctl.WithT(t)) } + case func(*testing.T): + testFunc = func(t *testing.T) { f(t) } + default: + t.Fatalf("incorrect test suite function '%s'", name) + } + t.Run(name, testFunc) + } +} + +func (r *TestRunner) runHostOverride(t *testing.T, tests Suite) bool { + env := strings.ToUpper(r.Service) + "_HOST" + host := os.Getenv(env) + if host == "" { + return false + } + + t.Logf("Test host overriden by %s=%s", env, host) + + ctl := &runnerControl{ + host: host, + } + r.runSuite(t, tests, ctl) + return true +} + +// Run runs a tests suite +func (r *TestRunner) Run(t *testing.T, tests Suite) { + t.Helper() + + if r.runHostOverride(t, tests) { + return + } + + timeout := r.Timeout + if timeout == 0 { + timeout = 300 + } + + scenarios := r.scenarios() + if len(scenarios) == 0 { + t.Fatal("Test runner configuration doesn't produce scenarios") + } + for _, s := range scenarios { + s := s + var vars []string + for k, v := range s { + vars = append(vars, fmt.Sprintf("%s=%s", k, v)) + } + sort.Strings(vars) + desc := strings.Join(vars, ",") + if desc == "" { + desc = "WithoutOptions" + } + + var name string + if composeReuse { + name = fmt.Sprintf("%s_%x", r.Service, sha1.Sum([]byte(desc))) + } else { + seq := make([]byte, 16) + rand.Read(seq) + name = fmt.Sprintf("%s_%x", r.Service, seq) + } + + project, err := getComposeProject(name) + if err != nil { + t.Fatal(err) + } + project.SetParameters(s) + + t.Run(desc, func(t *testing.T) { + if composeReuse && composeCleanup { + project.Down() + t.Skipf("Stopping scenario, test won't run") + } + + if !composeReuse && r.Parallel { + t.Parallel() + } + + err := project.Start(r.Service, RecreateOnUp(!composeReuse)) + if !composeReuse { + // Down() is "idempotent", Start() has several points where it can fail, + // so run Down() even if Start() fails. + defer project.Down() + } + if err != nil { + t.Fatal(err) + } + + err = project.Wait(timeout, r.Service) + if err != nil { + t.Fatal(errors.Wrapf(err, "waiting for %s/%s", r.Service, desc)) + } + + host, err := project.Host(r.Service) + if err != nil { + t.Fatal(errors.Wrapf(err, "getting host for %s/%s", r.Service, desc)) + } + + ctl := &runnerControl{ + host: host, + scenario: s, + } + r.runSuite(t, tests, ctl) + }) + } +} + +// R extends the testing.T interface with methods that expose information about current scenario +type R interface { + testing.TB + + WithT(t *testing.T) R + + Host() string + Hostname() string + Port() string + + Option(string) string +} + +type runnerControl struct { + *testing.T + + host string + scenario map[string]string +} + +// WithT creates a copy of R with the given T +func (r *runnerControl) WithT(t *testing.T) R { + ctl := *r + ctl.T = t + return &ctl +} + +// Host returns the host:port the test should use to connect to the service +func (r *runnerControl) Host() string { + return r.host +} + +// Hostname is the address of the host +func (r *runnerControl) Hostname() string { + hostname, _, _ := net.SplitHostPort(r.host) + return hostname +} + +// Port is the port of the host +func (r *runnerControl) Port() string { + _, port, _ := net.SplitHostPort(r.host) + return port +} + +// Option returns the value of an option for the current scenario +func (r *runnerControl) Option(key string) string { + return r.scenario[key] +} diff --git a/libbeat/tests/compose/runner_test.go b/libbeat/tests/compose/runner_test.go new file mode 100644 index 000000000000..30b5d1884607 --- /dev/null +++ b/libbeat/tests/compose/runner_test.go @@ -0,0 +1,80 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package compose + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRunnerScenarios(t *testing.T) { + cases := []struct { + Title string + Options RunnerOptions + Expected []map[string]string + }{ + { + Title: "Nil options", + Options: nil, + Expected: []map[string]string{nil}, + }, + { + Title: "Empty options", + Options: RunnerOptions{}, + Expected: []map[string]string{nil}, + }, + { + Title: "One option, two values", + Options: RunnerOptions{ + "FOO": {"bar", "baz"}, + }, + Expected: []map[string]string{ + {"FOO": "bar"}, + {"FOO": "baz"}, + }, + }, + { + Title: "Multiple options", + Options: RunnerOptions{ + "FOO": {"bar", "baz"}, + "BAZ": {"stuff"}, + }, + Expected: []map[string]string{ + {"FOO": "bar", "BAZ": "stuff"}, + {"FOO": "baz", "BAZ": "stuff"}, + }, + }, + { + Title: "Multiple options, single values", + Options: RunnerOptions{ + "FOO": {"bar"}, + "BAZ": {"stuff"}, + }, + Expected: []map[string]string{ + {"FOO": "bar", "BAZ": "stuff"}, + }, + }, + } + + for _, c := range cases { + r := TestRunner{Options: c.Options} + found := r.scenarios() + assert.Equal(t, c.Expected, found, c.Title) + } +} diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 4a10ef5135b5..6cb69a96b0c1 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -18,11 +18,18 @@ package compose import ( + "archive/tar" + "bytes" "context" "fmt" + "net" "os" "os/exec" + "path/filepath" + "runtime" + "strconv" "strings" + "time" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -38,6 +45,8 @@ const ( type wrapperDriver struct { Name string Files []string + + Environment []string } type wrapperContainer struct { @@ -60,13 +69,58 @@ func (c *wrapperContainer) Old() bool { return strings.Contains(c.info.Status, "minute") } +// privateHost returns the address of the container, it should be reachable +// from the host if docker is being run natively. To be used when the tests +// are run from another container in the same network. It also works when +// running from the hoist network if the docker daemon runs natively. +func (c *wrapperContainer) privateHost() string { + var ip string + for _, net := range c.info.NetworkSettings.Networks { + if len(net.IPAddress) > 0 { + ip = net.IPAddress + break + } + } + if len(ip) == 0 { + return "" + } + + for _, port := range c.info.Ports { + if port.PublicPort != 0 { + return net.JoinHostPort(ip, strconv.Itoa(int(port.PrivatePort))) + } + } + return "" +} + +// exposedHost returns the exposed address in the host, can be used when the +// test is run from the host network. Recommended when using docker machines. +func (c *wrapperContainer) exposedHost() string { + for _, port := range c.info.Ports { + if port.PublicPort != 0 { + return net.JoinHostPort("localhost", strconv.Itoa(int(port.PublicPort))) + } + } + return "" +} + +func (c *wrapperContainer) Host() string { + // TODO: Support multiple networks/ports + if runtime.GOOS == "linux" { + return c.privateHost() + } + // We can use `exposedHost()` in all platforms when we can use host + // network in the metricbeat container + return c.exposedHost() +} + func (d *wrapperDriver) LockFile() string { return d.Files[0] + ".lock" } func (d *wrapperDriver) cmd(ctx context.Context, command string, arg ...string) *exec.Cmd { var args []string - args = append(args, "--project-name", d.Name) + args = append(args, "--no-ansi", "--project-name", d.Name) for _, f := range d.Files { args = append(args, "--file", f) } @@ -75,9 +129,20 @@ func (d *wrapperDriver) cmd(ctx context.Context, command string, arg ...string) cmd := exec.CommandContext(ctx, "docker-compose", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + if len(d.Environment) > 0 { + cmd.Env = append(os.Environ(), d.Environment...) + } return cmd } +func (d *wrapperDriver) SetParameters(params map[string]string) { + var env []string + for k, v := range params { + env = append(env, fmt.Sprintf("%s=%s", k, v)) + } + d.Environment = env +} + func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) error { var args []string @@ -95,7 +160,82 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) args = append(args, service) } - return d.cmd(ctx, "up", args...).Run() + for { + // It can fail if we have reached some system limit, specially + // number of networks, retry while the context is not done + err := d.cmd(ctx, "up", args...).Run() + if err == nil { + return d.setupAdvertisedHost(ctx, service) + } + + select { + case <-time.After(time.Second): + case <-ctx.Done(): + return err + } + } +} + +func writeToContainer(ctx context.Context, id, filename, content string) error { + var buf bytes.Buffer + tw := tar.NewWriter(&buf) + now := time.Now() + err := tw.WriteHeader(&tar.Header{ + Typeflag: tar.TypeReg, + Name: filepath.Base(filename), + Mode: 0100644, + Size: int64(len(content)), + ModTime: now, + AccessTime: now, + ChangeTime: now, + }) + if err != nil { + return errors.Wrap(err, "failed to write tar header") + } + if _, err := tw.Write([]byte(content)); err != nil { + return errors.Wrap(err, "failed to write tar file") + } + if err := tw.Close(); err != nil { + return errors.Wrap(err, "failed to close tar") + } + + cli, err := client.NewEnvClient() + if err != nil { + return errors.Wrap(err, "failed to start docker client") + } + defer cli.Close() + + opts := types.CopyToContainerOptions{} + err = cli.CopyToContainer(ctx, id, filepath.Dir(filename), bytes.NewReader(buf.Bytes()), opts) + if err != nil { + return errors.Wrapf(err, "failed to copy environment to container %s", id) + } + return nil +} + +func (d *wrapperDriver) setupAdvertisedHost(ctx context.Context, service string) error { + containers, err := d.containers(ctx, Filter{State: AnyState}, service) + if err != nil { + return errors.Wrap(err, "setupAdvertisedHost") + } + if len(containers) == 0 { + return errors.Errorf("no containers for service %s", service) + } + + for _, c := range containers { + w := &wrapperContainer{info: c} + content := fmt.Sprintf("SERVICE_HOST=%s", w.Host()) + + err := writeToContainer(ctx, c.ID, "/run/compose_env", content) + if err != nil { + return err + } + } + return nil +} + +func (d *wrapperDriver) Down(ctx context.Context) error { + return d.cmd(ctx, "down", "-v", "--rmi=local").Run() } func (d *wrapperDriver) Kill(ctx context.Context, signal string, service string) error { @@ -143,6 +283,7 @@ func (d *wrapperDriver) containers(ctx context.Context, projectFilter Filter, fi if err != nil { return nil, errors.Wrap(err, "failed to start docker client") } + defer cli.Close() var serviceFilters []filters.Args if len(filter) == 0 { diff --git a/metricbeat/docker-compose.yml b/metricbeat/docker-compose.yml index c61291b387b6..3277ebf5cd5a 100644 --- a/metricbeat/docker-compose.yml +++ b/metricbeat/docker-compose.yml @@ -10,41 +10,8 @@ services: - ${PWD}/..:/go/src/github.com/elastic/beats/ # This is required to on-demand launching the rest on containers for tests & also docker module tests: - /var/run/docker.sock:/var/run/docker.sock + network_mode: host command: make - env_file: - - ./module/aerospike/_meta/env - - ./module/apache/_meta/env - - ./module/beat/_meta/env - - ./module/ceph/_meta/env - - ./module/consul/_meta/env - - ./module/couchbase/_meta/env - - ./module/couchdb/_meta/env - - ./module/dropwizard/_meta/env - - ./module/elasticsearch/_meta/env - - ./module/envoyproxy/_meta/env - - ./module/etcd/_meta/env - - ./module/golang/_meta/env - - ./module/haproxy/_meta/env - - ./module/http/_meta/env - - ./module/jolokia/_meta/env - - ./module/kafka/_meta/env - - ./module/kibana/_meta/env - #- ./module/kubernetes/_meta/env - - ./module/logstash/_meta/env - - ./module/memcached/_meta/env - - ./module/mongodb/_meta/env - - ./module/munin/_meta/env - - ./module/mysql/_meta/env - - ./module/nats/_meta/env - - ./module/nginx/_meta/env - - ./module/php_fpm/_meta/env - - ./module/postgresql/_meta/env - - ./module/prometheus/_meta/env - - ./module/rabbitmq/_meta/env - - ./module/redis/_meta/env - - ./module/traefik/_meta/env - - ./module/uwsgi/_meta/env - - ./module/zookeeper/_meta/env # Modules aerospike: @@ -53,16 +20,17 @@ services: - 3000 apache: - build: ./module/apache/_meta - ports: - - 80 + extends: + file: ./module/apache/docker-compose.yml + service: apache apache_2_4_12: + extends: + file: ./module/apache/docker-compose.yml + service: apache build: - context: ./module/apache/_meta - dockerfile: Dockerfile.2.4.12 - ports: - - 80 + args: + APACHE_VERSION: 2.4.12 metricbeat: build: ./module/beat/_meta diff --git a/metricbeat/module/aerospike/_meta/env b/metricbeat/module/aerospike/_meta/env deleted file mode 100644 index 2c95ea957cb2..000000000000 --- a/metricbeat/module/aerospike/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -AEROSPIKE_HOST=aerospike -AEROSPIKE_PORT=3000 diff --git a/metricbeat/module/aerospike/namespace/namespace_integration_test.go b/metricbeat/module/aerospike/namespace/namespace_integration_test.go index fcbd812d13db..9a834b8734e1 100644 --- a/metricbeat/module/aerospike/namespace/namespace_integration_test.go +++ b/metricbeat/module/aerospike/namespace/namespace_integration_test.go @@ -26,20 +26,21 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/aerospike" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUp(t, "aerospike") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "aerospike") + r := compose.EnsureUp(t, "aerospike") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -50,10 +51,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "aerospike", "metricsets": []string{"namespace"}, - "hosts": []string{aerospike.GetAerospikeEnvHost() + ":" + aerospike.GetAerospikeEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/aerospike/testing.go b/metricbeat/module/aerospike/testing.go deleted file mode 100644 index 298634e85c07..000000000000 --- a/metricbeat/module/aerospike/testing.go +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package aerospike - -import ( - "os" -) - -// Helper functions for testing used in the aerospike MetricSets. - -// GetAerospikeEnvHost returns the hostname of the Aerospike server to use for -// testing. It reads the value from the AEROSPIKE_HOST environment variable and -// returns localhost if it is not set. -func GetAerospikeEnvHost() string { - host := os.Getenv("AEROSPIKE_HOST") - - if len(host) == 0 { - host = "localhost" - } - return host -} - -// GetAerospikeEnvPort returns the port of the Aerospike server to use for -// testing. It reads the value from the AEROSPIKE_PORT environment variable and -// returns 3000 if it is not set. -func GetAerospikeEnvPort() string { - port := os.Getenv("AEROSPIKE_PORT") - - if len(port) == 0 { - port = "3000" - } - return port -} diff --git a/metricbeat/module/apache/_meta/Dockerfile b/metricbeat/module/apache/_meta/Dockerfile index 06b9d21c98c5..419471057ca8 100644 --- a/metricbeat/module/apache/_meta/Dockerfile +++ b/metricbeat/module/apache/_meta/Dockerfile @@ -1,4 +1,5 @@ -FROM httpd:2.4.20 +ARG APACHE_VERSION=2.4.20 +FROM httpd:$APACHE_VERSION RUN sed -i "/jessie-updates/d" /etc/apt/sources.list RUN apt-get update && apt-get install -y curl HEALTHCHECK --interval=1s --retries=90 CMD curl -f http://localhost diff --git a/metricbeat/module/apache/_meta/Dockerfile.2.4.12 b/metricbeat/module/apache/_meta/Dockerfile.2.4.12 deleted file mode 100644 index 8170e61ba4d1..000000000000 --- a/metricbeat/module/apache/_meta/Dockerfile.2.4.12 +++ /dev/null @@ -1,5 +0,0 @@ -FROM httpd:2.4.12 -RUN sed -i "/jessie-updates/d" /etc/apt/sources.list -RUN apt-get update && apt-get install -y curl -HEALTHCHECK --interval=1s --retries=90 CMD curl -f http://localhost -COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf diff --git a/metricbeat/module/apache/_meta/env b/metricbeat/module/apache/_meta/env deleted file mode 100644 index 5339c3407ed8..000000000000 --- a/metricbeat/module/apache/_meta/env +++ /dev/null @@ -1,3 +0,0 @@ -APACHE_HOST=apache -APACHE_OLD_HOST=apache_2_4_12 -APACHE_PORT=80 diff --git a/metricbeat/module/apache/docker-compose.yml b/metricbeat/module/apache/docker-compose.yml new file mode 100644 index 000000000000..b8cd62e14824 --- /dev/null +++ b/metricbeat/module/apache/docker-compose.yml @@ -0,0 +1,8 @@ +version: '2.3' + +services: + apache: + build: + context: ./_meta + ports: + - 80 diff --git a/metricbeat/module/nginx/testing.go b/metricbeat/module/apache/mtest/runner.go similarity index 72% rename from metricbeat/module/nginx/testing.go rename to metricbeat/module/apache/mtest/runner.go index cee26e1d11a5..2b86eedb79cf 100644 --- a/metricbeat/module/nginx/testing.go +++ b/metricbeat/module/apache/mtest/runner.go @@ -15,21 +15,24 @@ // specific language governing permissions and limitations // under the License. -package nginx +// +build integration -/* -Helper functions for testing used in the nginx metricsets -*/ +package mtest import ( - "os" + "github.com/elastic/beats/libbeat/tests/compose" ) -func GetNginxEnvHost() string { - host := os.Getenv("NGINX_HOST") - - if len(host) == 0 { - host = "127.0.0.1" +var ( + // Runner is the compose test runner for apache + Runner = compose.TestRunner{ + Service: "apache", + Options: map[string][]string{ + "APACHE_VERSION": []string{ + "2.4.12", + "2.4.20", + }, + }, + Parallel: true, } - return host -} +) diff --git a/metricbeat/module/apache/status/status_integration_test.go b/metricbeat/module/apache/status/status_integration_test.go index 96a600831460..beb002b2f124 100644 --- a/metricbeat/module/apache/status/status_integration_test.go +++ b/metricbeat/module/apache/status/status_integration_test.go @@ -24,16 +24,26 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/apache" + "github.com/elastic/beats/metricbeat/module/apache/mtest" "github.com/stretchr/testify/assert" ) -func TestFetch(t *testing.T) { - compose.EnsureUp(t, "apache") +func TestStatus(t *testing.T) { + mtest.Runner.Run(t, compose.Suite{ + "Data": testData, + "Fetch": testFetch, + }) +} + +func testData(t *testing.T, r compose.R) { + f := mbtest.NewFetcher(t, getConfig(r.Host())) + f.WriteEvents(t, "") +} - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) - events, errs := mbtest.ReportingFetchV2Error(f) +func testFetch(t *testing.T, r compose.R) { + f := mbtest.NewFetcher(t, getConfig(r.Host())) + events, errs := f.FetchEvents() if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) } @@ -48,10 +58,10 @@ func TestFetch(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "apache", "metricsets": []string{"status"}, - "hosts": []string{apache.GetApacheEnvHost()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/apache/testing.go b/metricbeat/module/apache/testing.go deleted file mode 100644 index 4b2e0571a359..000000000000 --- a/metricbeat/module/apache/testing.go +++ /dev/null @@ -1,36 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package apache - -import ( - "os" -) - -// Helper functions for testing the Apache module's MetricSets. - -// GetApacheEnvHost returns the apache server hostname to use for testing. It -// reads the value from the APACHE_HOST environment variable and returns -// 127.0.0.1 if it is not set. -func GetApacheEnvHost() string { - host := os.Getenv("APACHE_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} diff --git a/metricbeat/module/beat/_meta/env b/metricbeat/module/beat/_meta/env deleted file mode 100644 index 3e4c68f72f2e..000000000000 --- a/metricbeat/module/beat/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -BEAT_HOST=metricbeat -BEAT_PORT=5066 diff --git a/metricbeat/module/beat/beat_integration_test.go b/metricbeat/module/beat/beat_integration_test.go index e894fa9a8243..7f9ef04de56d 100644 --- a/metricbeat/module/beat/beat_integration_test.go +++ b/metricbeat/module/beat/beat_integration_test.go @@ -37,10 +37,10 @@ var metricSets = []string{ } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "metricbeat") + r := compose.EnsureUp(t, "metricbeat") for _, metricSet := range metricSets { - f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -54,10 +54,10 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "metricbeat") + r := compose.EnsureUp(t, "metricbeat") for _, metricSet := range metricSets { - f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, r.Host())) err := mbtest.WriteEventsReporterV2Error(f, t, metricSet) if err != nil { t.Fatal("write", err) diff --git a/metricbeat/module/beat/testing.go b/metricbeat/module/beat/testing.go index 5a282228d05a..6168279a49be 100644 --- a/metricbeat/module/beat/testing.go +++ b/metricbeat/module/beat/testing.go @@ -17,33 +17,11 @@ package beat -import "os" - -// GetEnvHost for Metricbeat -func GetEnvHost() string { - host := os.Getenv("BEAT_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort for Metricbeat -func GetEnvPort() string { - port := os.Getenv("BEAT_PORT") - - if len(port) == 0 { - port = "5066" - } - return port -} - // GetConfig for Metricbeat -func GetConfig(metricset string) map[string]interface{} { +func GetConfig(metricset string, host string) map[string]interface{} { return map[string]interface{}{ "module": ModuleName, "metricsets": []string{metricset}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/ceph/_meta/env b/metricbeat/module/ceph/_meta/env deleted file mode 100644 index 27b7ce7dbfb3..000000000000 --- a/metricbeat/module/ceph/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -CEPH_HOST=ceph -CEPH_PORT=5000 diff --git a/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go b/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go index 8b815dca0f7b..01ec63e34e1e 100644 --- a/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go +++ b/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go @@ -20,48 +20,26 @@ package cluster_disk import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"cluster_disk"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go b/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go index de9c05c50211..7ec1e2ff29f0 100644 --- a/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go +++ b/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go @@ -18,48 +18,26 @@ package cluster_health import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"cluster_health"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go b/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go index d3b20eefd212..dcf8ef179dc1 100644 --- a/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go +++ b/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go @@ -18,48 +18,26 @@ package cluster_status import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"cluster_status"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/ceph/osd_df/osd_df_integration_test.go b/metricbeat/module/ceph/osd_df/osd_df_integration_test.go index 959579be89e0..36d363d2fe41 100644 --- a/metricbeat/module/ceph/osd_df/osd_df_integration_test.go +++ b/metricbeat/module/ceph/osd_df/osd_df_integration_test.go @@ -18,48 +18,26 @@ package osd_df import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"osd_df"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go b/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go index e53aa740d7e1..73a8aa263e02 100644 --- a/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go +++ b/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go @@ -18,48 +18,26 @@ package osd_tree import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"osd_tree"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go b/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go index c2dc8b3f80cd..dec045364dd1 100644 --- a/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go +++ b/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go @@ -18,48 +18,26 @@ package pool_disk import ( - "fmt" - "os" "testing" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "ceph", "metricsets": []string{"pool_disk"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults + "hosts": []string{host}, } - return a } diff --git a/metricbeat/module/consul/_meta/env b/metricbeat/module/consul/_meta/env deleted file mode 100644 index 211d9aa87693..000000000000 --- a/metricbeat/module/consul/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -CONSUL_HOST=consul -CONSUL_PORT=8500 diff --git a/metricbeat/module/consul/agent/agent_integration_test.go b/metricbeat/module/consul/agent/agent_integration_test.go index 82beb66d32d5..d2d34f496359 100644 --- a/metricbeat/module/consul/agent/agent_integration_test.go +++ b/metricbeat/module/consul/agent/agent_integration_test.go @@ -37,9 +37,9 @@ func TestFetch(t *testing.T) { t.Skip("Skip flaky test on Consul Agent") logp.TestingSetup() - compose.EnsureUp(t, "consul") + r := compose.EnsureUp(t, "consul") - f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"})) + f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"}, r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) diff --git a/metricbeat/module/consul/agent/data_integration_test.go b/metricbeat/module/consul/agent/data_integration_test.go index ffe787a4ecd0..ae103c10968d 100644 --- a/metricbeat/module/consul/agent/data_integration_test.go +++ b/metricbeat/module/consul/agent/data_integration_test.go @@ -22,24 +22,17 @@ package agent import ( "testing" - "github.com/elastic/beats/metricbeat/module/consul" - _ "github.com/denisenkom/go-mssqldb" - "github.com/stretchr/testify/assert" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" + "github.com/elastic/beats/metricbeat/module/consul" ) func TestData(t *testing.T) { - t.Skip("Skipping `data.json` generation test") - - f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"})) - events, errs := mbtest.ReportingFetchV2Error(f) - if len(errs) > 0 { - t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) - } - assert.NotEmpty(t, events) + r := compose.EnsureUp(t, "consul") + f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"}, r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } diff --git a/metricbeat/module/consul/testing.go b/metricbeat/module/consul/testing.go index 135d02921298..7372bcbff8f3 100644 --- a/metricbeat/module/consul/testing.go +++ b/metricbeat/module/consul/testing.go @@ -17,26 +17,11 @@ package consul -import ( - "fmt" - "os" -) - //GetConfig returns a config object specific for a Consul module and a provided Metricset in 'ms' -func GetConfig(ms []string) map[string]interface{} { +func GetConfig(ms []string, host string) map[string]interface{} { return map[string]interface{}{ "module": "consul", "metricsets": ms, - "hosts": []string{fmt.Sprintf("%s:%s", EnvOr("CONSUL_HOST", "localhost"), EnvOr("CONSUL_PORT", "8500"))}, - } -} - -// EnvOr returns the value of the specified environment variable if it is -// non-empty. Otherwise it return def. -func EnvOr(name, def string) string { - s := os.Getenv(name) - if s == "" { - return def + "hosts": []string{host}, } - return s } diff --git a/metricbeat/module/couchbase/_meta/env b/metricbeat/module/couchbase/_meta/env deleted file mode 100644 index 38aa8e347786..000000000000 --- a/metricbeat/module/couchbase/_meta/env +++ /dev/null @@ -1,3 +0,0 @@ -COUCHBASE_HOST=couchbase -COUCHBASE_PORT=8091 -COUCHBASE_DSN=http://Administrator:password@couchbase:8091 diff --git a/metricbeat/module/couchbase/bucket/_meta/data.json b/metricbeat/module/couchbase/bucket/_meta/data.json index 9c67b39b9f1c..db05a545ba0e 100644 --- a/metricbeat/module/couchbase/bucket/_meta/data.json +++ b/metricbeat/module/couchbase/bucket/_meta/data.json @@ -1,26 +1,22 @@ { "@timestamp": "2017-10-12T08:05:34.853Z", - "agent": { - "hostname": "host.example.com", - "name": "host.example.com" - }, "couchbase": { "bucket": { "data": { "used": { - "bytes": 19921318 + "bytes": 3371588 } }, "disk": { "fetches": 0, "used": { - "bytes": 23665025 + "bytes": 9473 } }, - "item_count": 7303, + "item_count": 0, "memory": { "used": { - "bytes": 53961776 + "bytes": 49976304 } }, "name": "beer-sample", @@ -30,7 +26,7 @@ "bytes": 104857600 }, "use": { - "pct": 51.46195983886719 + "pct": 47.66111755371094 } }, "type": "membase" @@ -45,7 +41,7 @@ "name": "bucket" }, "service": { - "address": "localhost:8091", + "address": "192.168.160.7:8091", "type": "couchbase" } } \ No newline at end of file diff --git a/metricbeat/module/couchbase/bucket/bucket_integration_test.go b/metricbeat/module/couchbase/bucket/bucket_integration_test.go index 3c80bbcc980e..113e12a3195c 100644 --- a/metricbeat/module/couchbase/bucket/bucket_integration_test.go +++ b/metricbeat/module/couchbase/bucket/bucket_integration_test.go @@ -30,9 +30,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "couchbase") + r := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,9 +43,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "couchbase") + r := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -57,10 +57,10 @@ func TestData(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "couchbase", "metricsets": []string{"bucket"}, - "hosts": []string{couchbase.GetEnvDSN()}, + "hosts": []string{couchbase.GetDSN(host)}, } } diff --git a/metricbeat/module/couchbase/cluster/cluster_integration_test.go b/metricbeat/module/couchbase/cluster/cluster_integration_test.go index 4e4060646a6c..013747d22e4a 100644 --- a/metricbeat/module/couchbase/cluster/cluster_integration_test.go +++ b/metricbeat/module/couchbase/cluster/cluster_integration_test.go @@ -30,9 +30,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "couchbase") + r := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -42,10 +42,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "couchbase", "metricsets": []string{"cluster"}, - "hosts": []string{couchbase.GetEnvDSN()}, + "hosts": []string{couchbase.GetDSN(host)}, } } diff --git a/metricbeat/module/couchbase/node/node_integration_test.go b/metricbeat/module/couchbase/node/node_integration_test.go index fb55a8a91c4d..586f7ff937c9 100644 --- a/metricbeat/module/couchbase/node/node_integration_test.go +++ b/metricbeat/module/couchbase/node/node_integration_test.go @@ -30,9 +30,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "couchbase") + r := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -42,10 +42,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "couchbase", "metricsets": []string{"node"}, - "hosts": []string{couchbase.GetEnvDSN()}, + "hosts": []string{couchbase.GetDSN(host)}, } } diff --git a/metricbeat/module/couchbase/testing.go b/metricbeat/module/couchbase/testing.go index 7fe665308f38..225a8e35986b 100644 --- a/metricbeat/module/couchbase/testing.go +++ b/metricbeat/module/couchbase/testing.go @@ -17,13 +17,11 @@ package couchbase -import "os" +import ( + "fmt" +) -func GetEnvDSN() string { - dsn := os.Getenv("COUCHBASE_DSN") - - if len(dsn) == 0 { - dsn = "http://Administrator:password@localhost:8091" - } - return dsn +// GetDSN returns a complete DSN for a given host +func GetDSN(host string) string { + return fmt.Sprintf("http://Administrator:password@%s", host) } diff --git a/metricbeat/module/couchdb/_meta/env b/metricbeat/module/couchdb/_meta/env deleted file mode 100644 index d790ad1f99b3..000000000000 --- a/metricbeat/module/couchdb/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -COUCHDB_HOST=couchdb -COUCHDB_PORT=5984 diff --git a/metricbeat/module/couchdb/server/server_integration_test.go b/metricbeat/module/couchdb/server/server_integration_test.go index bb00bdb87e72..5ff1fb3ef3b1 100644 --- a/metricbeat/module/couchdb/server/server_integration_test.go +++ b/metricbeat/module/couchdb/server/server_integration_test.go @@ -20,7 +20,6 @@ package server import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "couchdb") + r := compose.EnsureUp(t, "couchdb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,42 +42,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "couchdb") - - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) - events, errs := mbtest.ReportingFetchV2Error(f) - if len(errs) > 0 { - t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) - } - assert.NotEmpty(t, events) + r := compose.EnsureUp(t, "couchdb") + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "couchdb", "metricsets": []string{"server"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, - } -} - -func GetEnvHost() string { - host := os.Getenv("COUCHDB_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("COUCHDB_PORT") - - if len(port) == 0 { - port = "5984" + "hosts": []string{host}, } - return port } diff --git a/metricbeat/module/dropwizard/_meta/env b/metricbeat/module/dropwizard/_meta/env deleted file mode 100644 index 771807862fd8..000000000000 --- a/metricbeat/module/dropwizard/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -DROPWIZARD_HOST=dropwizard -DROPWIZARD_PORT=8080 diff --git a/metricbeat/module/dropwizard/collector/collector_integration_test.go b/metricbeat/module/dropwizard/collector/collector_integration_test.go index f97b7c3f8bae..f49a8ebf9468 100644 --- a/metricbeat/module/dropwizard/collector/collector_integration_test.go +++ b/metricbeat/module/dropwizard/collector/collector_integration_test.go @@ -20,7 +20,6 @@ package collector import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -31,9 +30,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "dropwizard") + r := compose.EnsureUp(t, "dropwizard") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -75,29 +74,11 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events) } -func getEnvHost() string { - host := os.Getenv("DROPWIZARD_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func getEnvPort() string { - port := os.Getenv("DROPWIZARD_PORT") - - if len(port) == 0 { - port = "8080" - } - return port -} - -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "dropwizard", "metricsets": []string{"collector"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "namespace": "testnamespace", "metrics_path": "/test/metrics", "enabled": true, diff --git a/metricbeat/module/elasticsearch/_meta/env b/metricbeat/module/elasticsearch/_meta/env deleted file mode 100644 index 5856225ceaf9..000000000000 --- a/metricbeat/module/elasticsearch/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -ES_HOST=elasticsearch -ES_PORT=9200 diff --git a/metricbeat/module/elasticsearch/elasticsearch_integration_test.go b/metricbeat/module/elasticsearch/elasticsearch_integration_test.go index 486ea0255630..c3680089d59e 100644 --- a/metricbeat/module/elasticsearch/elasticsearch_integration_test.go +++ b/metricbeat/module/elasticsearch/elasticsearch_integration_test.go @@ -24,9 +24,7 @@ import ( "encoding/json" "fmt" "io/ioutil" - "net" "net/http" - "os" "testing" "time" @@ -63,9 +61,9 @@ var metricSets = []string{ } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "elasticsearch") + r := compose.EnsureUp(t, "elasticsearch") - host := net.JoinHostPort(getEnvHost(), getEnvPort()) + host := r.Host() err := createIndex(host) assert.NoError(t, err) @@ -86,7 +84,7 @@ func TestFetch(t *testing.T) { for _, metricSet := range metricSets { checkSkip(t, metricSet, version) t.Run(metricSet, func(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet, host)) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -100,9 +98,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "elasticsearch") + r := compose.EnsureUp(t, "elasticsearch") - host := net.JoinHostPort(getEnvHost(), getEnvPort()) + host := r.Host() version, err := getElasticsearchVersion(host) if err != nil { @@ -112,7 +110,7 @@ func TestData(t *testing.T) { for _, metricSet := range metricSets { checkSkip(t, metricSet, version) t.Run(metricSet, func(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(metricSet, host)) err := mbtest.WriteEventsReporterV2Error(f, t, metricSet) if err != nil { t.Fatal("write", err) @@ -121,32 +119,12 @@ func TestData(t *testing.T) { } } -// GetEnvHost returns host for Elasticsearch -func getEnvHost() string { - host := os.Getenv("ES_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort returns port for Elasticsearch -func getEnvPort() string { - port := os.Getenv("ES_PORT") - - if len(port) == 0 { - port = "9200" - } - return port -} - // GetConfig returns config for elasticsearch module -func getConfig(metricset string) map[string]interface{} { +func getConfig(metricset string, host string) map[string]interface{} { return map[string]interface{}{ "module": elasticsearch.ModuleName, "metricsets": []string{metricset}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "index_recovery.active_only": false, } } diff --git a/metricbeat/module/envoyproxy/_meta/env b/metricbeat/module/envoyproxy/_meta/env deleted file mode 100644 index 75a2386e7f6e..000000000000 --- a/metricbeat/module/envoyproxy/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -ENVOYPROXY_HOST=envoyproxy -ENVOYPROXY_PORT=9901 diff --git a/metricbeat/module/envoyproxy/server/server_integration_test.go b/metricbeat/module/envoyproxy/server/server_integration_test.go index cbf316666ec2..6870341f9cec 100644 --- a/metricbeat/module/envoyproxy/server/server_integration_test.go +++ b/metricbeat/module/envoyproxy/server/server_integration_test.go @@ -20,7 +20,6 @@ package server import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "envoyproxy") + r := compose.EnsureUp(t, "envoyproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,9 +42,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "envoyproxy") + r := compose.EnsureUp(t, "envoyproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -57,28 +56,10 @@ func TestData(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "envoyproxy", "metricsets": []string{"server"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("ENVOYPROXY_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("ENVOYPROXY_PORT") - - if len(port) == 0 { - port = "9901" - } - return port -} diff --git a/metricbeat/module/etcd/_meta/env b/metricbeat/module/etcd/_meta/env deleted file mode 100644 index ea9d54409853..000000000000 --- a/metricbeat/module/etcd/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -ETCD_HOST=etcd -ETCD_PORT=2379 diff --git a/metricbeat/module/etcd/leader/leader_integration_test.go b/metricbeat/module/etcd/leader/leader_integration_test.go index e7a069a9567e..cb866700c95e 100644 --- a/metricbeat/module/etcd/leader/leader_integration_test.go +++ b/metricbeat/module/etcd/leader/leader_integration_test.go @@ -20,7 +20,6 @@ package leader import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -34,9 +33,9 @@ import ( func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -46,9 +45,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -60,28 +59,10 @@ func TestData(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "etcd", "metricsets": []string{"leader"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("ETCD_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("ETCD_PORT") - - if len(port) == 0 { - port = "2379" - } - return port -} diff --git a/metricbeat/module/etcd/metrics/metrics_integration_test.go b/metricbeat/module/etcd/metrics/metrics_integration_test.go index d3b7b99c557b..08b31426812a 100644 --- a/metricbeat/module/etcd/metrics/metrics_integration_test.go +++ b/metricbeat/module/etcd/metrics/metrics_integration_test.go @@ -20,7 +20,6 @@ package metrics import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -33,9 +32,9 @@ import ( func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - m := mbtest.NewFetcher(t, getConfig()) + m := mbtest.NewFetcher(t, getConfig(r.Host())) events, errs := m.FetchEvents() if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -45,34 +44,16 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - m := mbtest.NewFetcher(t, getConfig()) + m := mbtest.NewFetcher(t, getConfig(r.Host())) m.WriteEvents(t, "") } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "etcd", "metricsets": []string{"metrics"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("ETCD_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("ETCD_PORT") - - if len(port) == 0 { - port = "2379" - } - return port -} diff --git a/metricbeat/module/etcd/self/self_integration_test.go b/metricbeat/module/etcd/self/self_integration_test.go index 3bf00e37145f..046487ea6d1c 100644 --- a/metricbeat/module/etcd/self/self_integration_test.go +++ b/metricbeat/module/etcd/self/self_integration_test.go @@ -20,7 +20,6 @@ package self import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,9 +42,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -57,28 +56,10 @@ func TestData(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "etcd", "metricsets": []string{"self"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("ETCD_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("ETCD_PORT") - - if len(port) == 0 { - port = "2379" - } - return port -} diff --git a/metricbeat/module/etcd/store/store_integration_test.go b/metricbeat/module/etcd/store/store_integration_test.go index cf3d9e0b3ec6..5068133ee478 100644 --- a/metricbeat/module/etcd/store/store_integration_test.go +++ b/metricbeat/module/etcd/store/store_integration_test.go @@ -20,7 +20,6 @@ package store import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -32,9 +31,9 @@ import ( func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(r.Host())) events, errs := ms.FetchEvents() if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,34 +42,16 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "etcd") + r := compose.EnsureUp(t, "etcd") - f := mbtest.NewFetcher(t, getConfig()) + f := mbtest.NewFetcher(t, getConfig(r.Host())) f.WriteEvents(t, "") } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "etcd", "metricsets": []string{"store"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("ETCD_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("ETCD_PORT") - - if len(port) == 0 { - port = "2379" - } - return port -} diff --git a/metricbeat/module/golang/_meta/env b/metricbeat/module/golang/_meta/env deleted file mode 100644 index f030c0adc854..000000000000 --- a/metricbeat/module/golang/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -GOLANG_HOST=golang -GOLANG_PORT=6060 diff --git a/metricbeat/module/golang/expvar/expvar_integration_test.go b/metricbeat/module/golang/expvar/expvar_integration_test.go index d5b808ec9639..e0a3c164107a 100644 --- a/metricbeat/module/golang/expvar/expvar_integration_test.go +++ b/metricbeat/module/golang/expvar/expvar_integration_test.go @@ -20,7 +20,6 @@ package expvar import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "golang") + r := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(f, t, "") if !assert.NoError(t, err) { @@ -41,9 +40,9 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "golang") + r := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { @@ -55,29 +54,11 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "golang", "metricsets": []string{"expvar"}, "expvar.namespace": "metricbeat", - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("GOLANG_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("GOLANG_PORT") - - if len(port) == 0 { - port = "6060" - } - return port -} diff --git a/metricbeat/module/golang/heap/heap_integration_test.go b/metricbeat/module/golang/heap/heap_integration_test.go index 6576979141bb..513ed85d5dd2 100644 --- a/metricbeat/module/golang/heap/heap_integration_test.go +++ b/metricbeat/module/golang/heap/heap_integration_test.go @@ -20,7 +20,6 @@ package heap import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "golang") + r := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(f, t, "") if !assert.NoError(t, err) { @@ -41,9 +40,9 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "golang") + r := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { @@ -55,28 +54,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "golang", "metricsets": []string{"heap"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("GOLANG_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("GOLANG_PORT") - - if len(port) == 0 { - port = "6060" - } - return port -} diff --git a/metricbeat/module/haproxy/_meta/env b/metricbeat/module/haproxy/_meta/env deleted file mode 100644 index c0762e53c17b..000000000000 --- a/metricbeat/module/haproxy/_meta/env +++ /dev/null @@ -1,4 +0,0 @@ -HAPROXY_1_6_HOST=haproxy_1_6 -HAPROXY_1_7_HOST=haproxy_1_7 -HAPROXY_HOST=haproxy -HAPROXY_PORT=14567 diff --git a/metricbeat/module/haproxy/info/info_integration_test.go b/metricbeat/module/haproxy/info/info_integration_test.go index ca251f9d0ed3..d101a51f97cc 100644 --- a/metricbeat/module/haproxy/info/info_integration_test.go +++ b/metricbeat/module/haproxy/info/info_integration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/haproxy" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "haproxy") + r := compose.EnsureUp(t, "haproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -46,9 +45,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "haproxy") + r := compose.EnsureUp(t, "haproxy") - config := getConfig() + config := getConfig(r.Host()) f := mbtest.NewReportingMetricSetV2Error(t, config) err := mbtest.WriteEventsReporterV2Error(f, t, ".") if err != nil { @@ -57,10 +56,10 @@ func TestData(t *testing.T) { } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "haproxy", "metricsets": []string{"info"}, - "hosts": []string{"tcp://" + haproxy.GetEnvHost() + ":" + haproxy.GetEnvPort()}, + "hosts": []string{"tcp://" + host}, } } diff --git a/metricbeat/module/haproxy/stat/stat_integration_test.go b/metricbeat/module/haproxy/stat/stat_integration_test.go index 3538ea007dd0..7a7f95b3c262 100644 --- a/metricbeat/module/haproxy/stat/stat_integration_test.go +++ b/metricbeat/module/haproxy/stat/stat_integration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/haproxy" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "haproxy") + r := compose.EnsureUp(t, "haproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -46,9 +45,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "haproxy") + r := compose.EnsureUp(t, "haproxy") - config := getConfig() + config := getConfig(r.Host()) f := mbtest.NewReportingMetricSetV2Error(t, config) err := mbtest.WriteEventsReporterV2Error(f, t, ".") if err != nil { @@ -57,10 +56,10 @@ func TestData(t *testing.T) { } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "haproxy", "metricsets": []string{"stat"}, - "hosts": []string{"tcp://" + haproxy.GetEnvHost() + ":" + haproxy.GetEnvPort()}, + "hosts": []string{"tcp://" + host}, } } diff --git a/metricbeat/module/haproxy/testing.go b/metricbeat/module/haproxy/testing.go deleted file mode 100644 index f5bf99134136..000000000000 --- a/metricbeat/module/haproxy/testing.go +++ /dev/null @@ -1,46 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package haproxy - -import "os" - -// Helper functions for testing used in the haproxy metricsets - -// GetEnvHost returns the hostname of the HAProxy server to use for testing. -// It reads the value from the HAPROXY_HOST environment variable and returns -// 127.0.0.1 if it is not set. -func GetEnvHost() string { - host := os.Getenv("HAPROXY_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetRedisEnvPort returns the port of the HAProxy server to use for testing. -// It reads the value from the HAPROXY_PORT environment variable and returns -// 14567 if it is not set. -func GetEnvPort() string { - port := os.Getenv("HAPROXY_PORT") - - if len(port) == 0 { - port = "14567" - } - return port -} diff --git a/metricbeat/module/http/_meta/env b/metricbeat/module/http/_meta/env deleted file mode 100644 index dfe04431ecac..000000000000 --- a/metricbeat/module/http/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -HTTP_HOST=http -HTTP_PORT=8080 diff --git a/metricbeat/module/http/json/json_integration_test.go b/metricbeat/module/http/json/json_integration_test.go index b5e4cc741c10..54853cf81ee9 100644 --- a/metricbeat/module/http/json/json_integration_test.go +++ b/metricbeat/module/http/json/json_integration_test.go @@ -20,7 +20,6 @@ package json import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetchObject(t *testing.T) { - compose.EnsureUp(t, "http") + r := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("object")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), "object")) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -43,9 +42,9 @@ func TestFetchObject(t *testing.T) { } func TestFetchArray(t *testing.T) { - compose.EnsureUp(t, "http") + r := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("array")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), "array")) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -55,15 +54,15 @@ func TestFetchArray(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } func TestData(t *testing.T) { - compose.EnsureUp(t, "http") + r := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("object")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), "object")) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig(jsonType string) map[string]interface{} { +func getConfig(host string, jsonType string) map[string]interface{} { var path string var responseIsArray bool switch jsonType { @@ -78,27 +77,9 @@ func getConfig(jsonType string) map[string]interface{} { return map[string]interface{}{ "module": "http", "metricsets": []string{"json"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "path": path, "namespace": "testnamespace", "json.is_array": responseIsArray, } } - -func getEnvHost() string { - host := os.Getenv("HTTP_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func getEnvPort() string { - port := os.Getenv("HTTP_PORT") - - if len(port) == 0 { - port = "8080" - } - return port -} diff --git a/metricbeat/module/jolokia/_meta/env b/metricbeat/module/jolokia/_meta/env deleted file mode 100644 index 9c0340b6f3c5..000000000000 --- a/metricbeat/module/jolokia/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -JOLOKIA_HOST=jolokia -JOLOKIA_PORT=8778 diff --git a/metricbeat/module/jolokia/jmx/jmx_integration_test.go b/metricbeat/module/jolokia/jmx/jmx_integration_test.go index 55a837455570..bd4d4440d608 100644 --- a/metricbeat/module/jolokia/jmx/jmx_integration_test.go +++ b/metricbeat/module/jolokia/jmx/jmx_integration_test.go @@ -20,7 +20,6 @@ package jmx import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "jolokia") + r := compose.EnsureUp(t, "jolokia") - for _, config := range getConfigs() { + for _, config := range getConfigs(r.Host()) { f := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -42,7 +41,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - for _, config := range getConfigs() { + r := compose.EnsureUp(t, "jolokia") + + for _, config := range getConfigs(r.Host()) { f := mbtest.NewReportingMetricSetV2Error(t, config) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -54,12 +55,12 @@ func TestData(t *testing.T) { } } -func getConfigs() []map[string]interface{} { +func getConfigs(host string) []map[string]interface{} { return []map[string]interface{}{ { "module": "jolokia", "metricsets": []string{"jmx"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "namespace": "testnamespace", "jmx.mappings": []map[string]interface{}{ { @@ -102,7 +103,7 @@ func getConfigs() []map[string]interface{} { { "module": "jolokia", "metricsets": []string{"jmx"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "namespace": "testnamespace", "jmx.mappings": []map[string]interface{}{ { @@ -151,7 +152,7 @@ func getConfigs() []map[string]interface{} { { "module": "jolokia", "metricsets": []string{"jmx"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, "namespace": "testnamespace", "http_method": "GET", "jmx.mappings": []map[string]interface{}{ @@ -194,21 +195,3 @@ func getConfigs() []map[string]interface{} { }, } } - -func getEnvHost() string { - host := os.Getenv("JOLOKIA_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func getEnvPort() string { - port := os.Getenv("JOLOKIA_PORT") - - if len(port) == 0 { - port = "8778" - } - return port -} diff --git a/metricbeat/module/kafka/_meta/env b/metricbeat/module/kafka/_meta/env deleted file mode 100644 index 227657f404e3..000000000000 --- a/metricbeat/module/kafka/_meta/env +++ /dev/null @@ -1,3 +0,0 @@ -KAFKA_0_10_2_HOST=kafka_0_10_2 -KAFKA_HOST=kafka -KAFKA_PORT=9092 diff --git a/metricbeat/module/kafka/_meta/run.sh b/metricbeat/module/kafka/_meta/run.sh index 0102b9709740..34e430610092 100755 --- a/metricbeat/module/kafka/_meta/run.sh +++ b/metricbeat/module/kafka/_meta/run.sh @@ -1,6 +1,26 @@ #!/bin/bash -KAFKA_ADVERTISED_HOST=$(dig +short $HOSTNAME):9092 +if [ -n "$KAFKA_ADVERTISED_HOST_AUTO" ]; then + KAFKA_ADVERTISED_HOST=$(dig +short $HOSTNAME):9092 +fi + +# Check if KAFKA_ADVERTISED_HOST is set +# if not wait to read it from file +if [ -z "$KAFKA_ADVERTISED_HOST" ]; then + echo "KAFKA_ADVERTISED_HOST needed, will wait for it on /var/run/compose_env" + while true; do + if [ -f /run/compose_env ]; then + source /run/compose_env + KAFKA_ADVERTISED_HOST=$SERVICE_HOST + fi + if [ -n "$KAFKA_ADVERTISED_HOST" ]; then + # Remove it so it is not reused + > /run/compose_env + break + fi + sleep 1 + done +fi wait_for_port() { count=20 @@ -14,6 +34,7 @@ wait_for_port() { nc -z localhost $port } + echo "Starting ZooKeeper" ${KAFKA_HOME}/bin/zookeeper-server-start.sh ${KAFKA_HOME}/config/zookeeper.properties & wait_for_port 2181 diff --git a/metricbeat/module/kafka/consumergroup/consumergroup_integration_test.go b/metricbeat/module/kafka/consumergroup/consumergroup_integration_test.go index 8fa09a2902b0..14c58efc1f8c 100644 --- a/metricbeat/module/kafka/consumergroup/consumergroup_integration_test.go +++ b/metricbeat/module/kafka/consumergroup/consumergroup_integration_test.go @@ -20,9 +20,7 @@ package consumergroup import ( - "fmt" "io" - "os" "testing" "time" @@ -35,9 +33,6 @@ import ( ) const ( - kafkaDefaultHost = "localhost" - kafkaDefaultPort = "9092" - kafkaSASLConsumerUsername = "consumer" kafkaSASLConsumerPassword = "consumer-secret" kafkaSASLUsername = "stats" @@ -45,15 +40,15 @@ const ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "kafka") + r := compose.EnsureUp(t, "kafka") - c, err := startConsumer(t, "metricbeat-test") + c, err := startConsumer(t, r.Host(), "metricbeat-test") if err != nil { t.Fatal(errors.Wrap(err, "starting kafka consumer")) } defer c.Close() - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) for retries := 0; retries < 3; retries++ { err = mbtest.WriteEventsReporterV2Error(ms, t, "") if err == nil { @@ -65,15 +60,15 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "kafka") + r := compose.EnsureUp(t, "kafka") - c, err := startConsumer(t, "metricbeat-test") + c, err := startConsumer(t, r.Host(), "metricbeat-test") if err != nil { t.Fatal(errors.Wrap(err, "starting kafka consumer")) } defer c.Close() - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) var data []mb.Event var errors []error @@ -92,8 +87,8 @@ func TestFetch(t *testing.T) { } } -func startConsumer(t *testing.T, topic string) (io.Closer, error) { - brokers := []string{getTestKafkaHost()} +func startConsumer(t *testing.T, host string, topic string) (io.Closer, error) { + brokers := []string{host} topics := []string{topic} config := saramacluster.NewConfig() config.Net.SASL.Enable = true @@ -102,30 +97,12 @@ func startConsumer(t *testing.T, topic string) (io.Closer, error) { return saramacluster.NewConsumer(brokers, "test-group", topics, config) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "kafka", "metricsets": []string{"consumergroup"}, - "hosts": []string{getTestKafkaHost()}, + "hosts": []string{host}, "username": kafkaSASLUsername, "password": kafkaSASLPassword, } } - -func getTestKafkaHost() string { - return fmt.Sprintf("%v:%v", - getenv("KAFKA_HOST", kafkaDefaultHost), - getenv("KAFKA_PORT", kafkaDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults - } - return a -} diff --git a/metricbeat/module/kafka/partition/partition_integration_test.go b/metricbeat/module/kafka/partition/partition_integration_test.go index 58a639150804..a6e67e3304fc 100644 --- a/metricbeat/module/kafka/partition/partition_integration_test.go +++ b/metricbeat/module/kafka/partition/partition_integration_test.go @@ -22,7 +22,6 @@ package partition import ( "fmt" "math/rand" - "os" "strconv" "testing" "time" @@ -37,9 +36,6 @@ import ( ) const ( - kafkaDefaultHost = "localhost" - kafkaDefaultPort = "9092" - kafkaSASLProducerUsername = "producer" kafkaSASLProducerPassword = "producer-secret" kafkaSASLUsername = "stats" @@ -47,11 +43,11 @@ const ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "kafka") + r := compose.EnsureUp(t, "kafka") - generateKafkaData(t, "metricbeat-generate-data") + generateKafkaData(t, r.Host(), "metricbeat-generate-data") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig("")) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), "")) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) @@ -59,8 +55,7 @@ func TestData(t *testing.T) { } func TestTopic(t *testing.T) { - - compose.EnsureUp(t, "kafka") + r := compose.EnsureUp(t, "kafka") logp.TestingSetup(logp.WithSelectors("kafka")) @@ -68,9 +63,9 @@ func TestTopic(t *testing.T) { testTopic := fmt.Sprintf("test-metricbeat-%s", id) // Create initial topic - generateKafkaData(t, testTopic) + generateKafkaData(t, r.Host(), testTopic) - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(testTopic)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), testTopic)) dataBefore, err := mbtest.ReportingFetchV2Error(f) if err != nil { t.Fatal("write", err) @@ -84,7 +79,7 @@ func TestTopic(t *testing.T) { var i int64 = 0 // Create n messages for ; i < n; i++ { - generateKafkaData(t, testTopic) + generateKafkaData(t, r.Host(), testTopic) } dataAfter, err := mbtest.ReportingFetchV2Error(f) @@ -123,7 +118,7 @@ func TestTopic(t *testing.T) { assert.True(t, offsetBefore+n == offsetAfter) } -func generateKafkaData(t *testing.T, topic string) { +func generateKafkaData(t *testing.T, host string, topic string) { t.Logf("Send Kafka Event to topic: %v", topic) config := sarama.NewConfig() @@ -136,7 +131,7 @@ func generateKafkaData(t *testing.T, topic string) { config.Net.SASL.Enable = true config.Net.SASL.User = kafkaSASLProducerUsername config.Net.SASL.Password = kafkaSASLProducerPassword - client, err := sarama.NewClient([]string{getTestKafkaHost()}, config) + client, err := sarama.NewClient([]string{host}, config) if err != nil { t.Errorf("%s", err) t.FailNow() @@ -164,7 +159,7 @@ func generateKafkaData(t *testing.T, topic string) { } } -func getConfig(topic string) map[string]interface{} { +func getConfig(host string, topic string) map[string]interface{} { var topics []string if topic != "" { topics = []string{topic} @@ -173,27 +168,9 @@ func getConfig(topic string) map[string]interface{} { return map[string]interface{}{ "module": "kafka", "metricsets": []string{"partition"}, - "hosts": []string{getTestKafkaHost()}, + "hosts": []string{host}, "topics": topics, "username": kafkaSASLUsername, "password": kafkaSASLPassword, } } - -func getTestKafkaHost() string { - return fmt.Sprintf("%v:%v", - getenv("KAFKA_HOST", kafkaDefaultHost), - getenv("KAFKA_PORT", kafkaDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults - } - return a -} diff --git a/metricbeat/module/kibana/_meta/env b/metricbeat/module/kibana/_meta/env deleted file mode 100644 index a22fc93ec709..000000000000 --- a/metricbeat/module/kibana/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -KIBANA_HOST=kibana -KIBANA_PORT=5601 \ No newline at end of file diff --git a/metricbeat/module/kibana/mtest/testing.go b/metricbeat/module/kibana/mtest/testing.go index a6dbc1f2f7e6..a037d5589a72 100644 --- a/metricbeat/module/kibana/mtest/testing.go +++ b/metricbeat/module/kibana/mtest/testing.go @@ -17,36 +17,11 @@ package mtest -import ( - "net" - "os" -) - -// GetEnvHost returns host for Kibana -func GetEnvHost() string { - host := os.Getenv("KIBANA_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort returns port for Kibana -func GetEnvPort() string { - port := os.Getenv("KIBANA_PORT") - - if len(port) == 0 { - port = "5601" - } - return port -} - // GetConfig returns config for kibana module -func GetConfig(metricset string) map[string]interface{} { +func GetConfig(metricset string, host string) map[string]interface{} { return map[string]interface{}{ "module": "kibana", "metricsets": []string{metricset}, - "hosts": []string{net.JoinHostPort(GetEnvHost(), GetEnvPort())}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/kibana/stats/stats_integration_test.go b/metricbeat/module/kibana/stats/stats_integration_test.go index 7173baf41fe2..b24d9d9320e1 100644 --- a/metricbeat/module/kibana/stats/stats_integration_test.go +++ b/metricbeat/module/kibana/stats/stats_integration_test.go @@ -36,9 +36,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUpWithTimeout(t, 570, "elasticsearch", "kibana") + r := compose.EnsureUpWithTimeout(t, 570, "kibana") - config := mtest.GetConfig("stats") + config := mtest.GetConfig("stats", r.Host()) host := config["hosts"].([]string)[0] version, err := getKibanaVersion(t, host) if err != nil { @@ -67,9 +67,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "kibana") + r := compose.EnsureUp(t, "kibana") - config := mtest.GetConfig("stats") + config := mtest.GetConfig("stats", r.Host()) host := config["hosts"].([]string)[0] version, err := getKibanaVersion(t, host) if err != nil { diff --git a/metricbeat/module/kibana/status/status_integration_test.go b/metricbeat/module/kibana/status/status_integration_test.go index 93af0ddb887c..46bc16282697 100644 --- a/metricbeat/module/kibana/status/status_integration_test.go +++ b/metricbeat/module/kibana/status/status_integration_test.go @@ -30,9 +30,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUpWithTimeout(t, 570, "elasticsearch", "kibana") + r := compose.EnsureUpWithTimeout(t, 570, "kibana") - f := mbtest.NewReportingMetricSetV2Error(t, mtest.GetConfig("status")) + f := mbtest.NewReportingMetricSetV2Error(t, mtest.GetConfig("status", r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) diff --git a/metricbeat/module/kubernetes/_meta/env b/metricbeat/module/kubernetes/_meta/env deleted file mode 100644 index 03cff050efa3..000000000000 --- a/metricbeat/module/kubernetes/_meta/env +++ /dev/null @@ -1,4 +0,0 @@ -KUBELET_HOST=172.17.0.1 -KUBELET_PORT=10255 -KUBE_STATE_METRICS_HOST=kubestate -KUBE_STATE_METRICS_PORT=8080 diff --git a/metricbeat/module/logstash/_meta/env b/metricbeat/module/logstash/_meta/env deleted file mode 100644 index 57f4dc268f76..000000000000 --- a/metricbeat/module/logstash/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -LOGSTASH_HOST=logstash -LOGSTASH_PORT=9600 diff --git a/metricbeat/module/logstash/logstash_integration_test.go b/metricbeat/module/logstash/logstash_integration_test.go index 1a95572e2b18..1560348e0458 100644 --- a/metricbeat/module/logstash/logstash_integration_test.go +++ b/metricbeat/module/logstash/logstash_integration_test.go @@ -37,31 +37,36 @@ var metricSets = []string{ } func TestFetch(t *testing.T) { - compose.EnsureUpWithTimeout(t, 300, "logstash") + r := compose.EnsureUpWithTimeout(t, 300, "logstash") for _, metricSet := range metricSets { - f := mbtest.NewReportingMetricSetV2Error(t, logstash.GetConfig(metricSet)) - events, errs := mbtest.ReportingFetchV2Error(f) + t.Run(metricSet, func(t *testing.T) { + config := logstash.GetConfig(metricSet, r.Host()) + f := mbtest.NewReportingMetricSetV2Error(t, config) + events, errs := mbtest.ReportingFetchV2Error(f) - assert.Empty(t, errs) - if !assert.NotEmpty(t, events) { - t.FailNow() - } + assert.Empty(t, errs) + if !assert.NotEmpty(t, events) { + t.FailNow() + } - t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), - events[0].BeatEvent("logstash", metricSet).Fields.StringToPrint()) + t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), + events[0].BeatEvent("logstash", metricSet).Fields.StringToPrint()) + }) } } func TestData(t *testing.T) { - compose.EnsureUp(t, "logstash") + r := compose.EnsureUp(t, "logstash") for _, metricSet := range metricSets { - config := logstash.GetConfig(metricSet) - f := mbtest.NewReportingMetricSetV2Error(t, config) - err := mbtest.WriteEventsReporterV2Error(f, t, metricSet) - if err != nil { - t.Fatal("write", err) - } + t.Run(metricSet, func(t *testing.T) { + config := logstash.GetConfig(metricSet, r.Host()) + f := mbtest.NewReportingMetricSetV2Error(t, config) + err := mbtest.WriteEventsReporterV2Error(f, t, metricSet) + if err != nil { + t.Fatal("write", err) + } + }) } } diff --git a/metricbeat/module/logstash/testing.go b/metricbeat/module/logstash/testing.go index 0f76fada202f..b63ce5cc5c25 100644 --- a/metricbeat/module/logstash/testing.go +++ b/metricbeat/module/logstash/testing.go @@ -17,33 +17,11 @@ package logstash -import "os" - -// GetEnvHost for Logstash -func GetEnvHost() string { - host := os.Getenv("LOGSTASH_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort for Logstash -func GetEnvPort() string { - port := os.Getenv("LOGSTASH_PORT") - - if len(port) == 0 { - port = "9600" - } - return port -} - // GetConfig for Logstash -func GetConfig(metricset string) map[string]interface{} { +func GetConfig(metricset string, host string) map[string]interface{} { return map[string]interface{}{ "module": ModuleName, "metricsets": []string{metricset}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/memcached/_meta/env b/metricbeat/module/memcached/_meta/env deleted file mode 100644 index 3efa58cda165..000000000000 --- a/metricbeat/module/memcached/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -MEMCACHED_HOST=memcached -MEMCACHED_PORT=11211 diff --git a/metricbeat/module/memcached/stats/stats_integration_test.go b/metricbeat/module/memcached/stats/stats_integration_test.go index 9ac77eb4eef8..d74fcf351912 100644 --- a/metricbeat/module/memcached/stats/stats_integration_test.go +++ b/metricbeat/module/memcached/stats/stats_integration_test.go @@ -20,7 +20,6 @@ package stats import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "memcached") + r := compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -45,9 +44,9 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "memcached") + r := compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -58,28 +57,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "memcached", "metricsets": []string{"stats"}, - "hosts": []string{getEnvHost() + ":" + getEnvPort()}, + "hosts": []string{host}, } } - -func getEnvHost() string { - host := os.Getenv("MEMCACHED_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func getEnvPort() string { - port := os.Getenv("MEMCACHED_PORT") - - if len(port) == 0 { - port = "11211" - } - return port -} diff --git a/metricbeat/module/mongodb/_meta/env b/metricbeat/module/mongodb/_meta/env deleted file mode 100644 index a51807cbe81e..000000000000 --- a/metricbeat/module/mongodb/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -MONGODB_HOST=mongodb -MONGODB_PORT=27017 diff --git a/metricbeat/module/mongodb/collstats/collstats_integration_test.go b/metricbeat/module/mongodb/collstats/collstats_integration_test.go index 515b9ab03c6a..4856ea47a60a 100644 --- a/metricbeat/module/mongodb/collstats/collstats_integration_test.go +++ b/metricbeat/module/mongodb/collstats/collstats_integration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/mongodb" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -53,18 +52,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "mongodb", "metricsets": []string{"collstats"}, - "hosts": []string{mongodb.GetEnvHost() + ":" + mongodb.GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/mongodb/dbstats/dbstats_integration_test.go b/metricbeat/module/mongodb/dbstats/dbstats_integration_test.go index 2d3b67d50355..d8091f333ea5 100644 --- a/metricbeat/module/mongodb/dbstats/dbstats_integration_test.go +++ b/metricbeat/module/mongodb/dbstats/dbstats_integration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/mongodb" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -78,18 +77,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "mongodb", "metricsets": []string{"dbstats"}, - "hosts": []string{mongodb.GetEnvHost() + ":" + mongodb.GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/mongodb/metrics/metrics_intergration_test.go b/metricbeat/module/mongodb/metrics/metrics_intergration_test.go index b014005df3a7..8100c9adb17d 100644 --- a/metricbeat/module/mongodb/metrics/metrics_intergration_test.go +++ b/metricbeat/module/mongodb/metrics/metrics_intergration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/mongodb" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -51,18 +50,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "mongodb", "metricsets": []string{"metrics"}, - "hosts": []string{mongodb.GetEnvHost() + ":" + mongodb.GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go b/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go index 95a68af9124d..33862b998c50 100644 --- a/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go +++ b/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go @@ -34,14 +34,14 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - err := initiateReplicaSet(t) + err := initiateReplicaSet(t, r.Host()) if !assert.NoError(t, err) { t.FailNow() } - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -75,24 +75,24 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "mongodb", "metricsets": []string{"replstatus"}, - "hosts": []string{mongodb.GetEnvHost() + ":" + mongodb.GetEnvPort()}, + "hosts": []string{host}, } } -func initiateReplicaSet(t *testing.T) error { - url := getConfig()["hosts"].([]string)[0] +func initiateReplicaSet(t *testing.T, host string) error { + url := host dialInfo, err := mgo.ParseURL(url) if err != nil { diff --git a/metricbeat/module/mongodb/status/status_integration_test.go b/metricbeat/module/mongodb/status/status_integration_test.go index 2e7fa76ff621..4c84bd0f1eef 100644 --- a/metricbeat/module/mongodb/status/status_integration_test.go +++ b/metricbeat/module/mongodb/status/status_integration_test.go @@ -26,13 +26,12 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/mongodb" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -57,9 +56,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "mongodb") + r := compose.EnsureUp(t, "mongodb") - config := getConfig() + config := getConfig(r.Host()) f := mbtest.NewReportingMetricSetV2Error(t, config) err := mbtest.WriteEventsReporterV2Error(f, t, ".") if err != nil { @@ -68,10 +67,10 @@ func TestData(t *testing.T) { } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "mongodb", "metricsets": []string{"status"}, - "hosts": []string{mongodb.GetEnvHost() + ":" + mongodb.GetEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/mongodb/testing.go b/metricbeat/module/mongodb/testing.go deleted file mode 100644 index 31d380b8322c..000000000000 --- a/metricbeat/module/mongodb/testing.go +++ /dev/null @@ -1,46 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package mongodb - -import "os" - -// Helper functions for testing mongodb metricsets. - -// GetEnvHost returns the hostname of the Mongodb server to use for testing. -// It reads the value from the MONGODB_HOST environment variable and returns -// 127.0.0.1 if it is not set. -func GetEnvHost() string { - host := os.Getenv("MONGODB_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort returns the port of the Mongodb server to use for testing. -// It reads the value from the MONGODB_PORT environment variable and returns -// 27017 if it is not set. -func GetEnvPort() string { - port := os.Getenv("MONGODB_PORT") - - if len(port) == 0 { - port = "27017" - } - return port -} diff --git a/metricbeat/module/munin/_meta/env b/metricbeat/module/munin/_meta/env deleted file mode 100644 index b81c5ee86790..000000000000 --- a/metricbeat/module/munin/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -MUNIN_HOST=munin -MUNIN_PORT=4949 diff --git a/metricbeat/module/munin/node/node_integration_test.go b/metricbeat/module/munin/node/node_integration_test.go index 201a0ecac1da..3a728fc909e8 100644 --- a/metricbeat/module/munin/node/node_integration_test.go +++ b/metricbeat/module/munin/node/node_integration_test.go @@ -20,7 +20,6 @@ package node import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "munin") + r := compose.EnsureUp(t, "munin") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -45,9 +44,9 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "munin") + r := compose.EnsureUp(t, "munin") - config := getConfig() + config := getConfig(r.Host()) f := mbtest.NewReportingMetricSetV2Error(t, config) err := mbtest.WriteEventsReporterV2Error(f, t, ".") if err != nil { @@ -55,34 +54,10 @@ func TestData(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "munin", "metricsets": []string{"node"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -// GetEnvHost returns the hostname of the Mongodb server to use for testing. -// It reads the value from the MONGODB_HOST environment variable and returns -// 127.0.0.1 if it is not set. -func GetEnvHost() string { - host := os.Getenv("MUNIN_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvPort returns the port of the Mongodb server to use for testing. -// It reads the value from the MONGODB_PORT environment variable and returns -// 27017 if it is not set. -func GetEnvPort() string { - port := os.Getenv("MUNIN_PORT") - - if len(port) == 0 { - port = "4949" - } - return port -} diff --git a/metricbeat/module/mysql/_meta/env b/metricbeat/module/mysql/_meta/env deleted file mode 100644 index e6aa44eaf4a6..000000000000 --- a/metricbeat/module/mysql/_meta/env +++ /dev/null @@ -1,3 +0,0 @@ -MYSQL_DSN=root:test@tcp(mysql:3306)/ -MYSQL_HOST=mysql -MYSQL_PORT=3306 diff --git a/metricbeat/module/mysql/mysql_integration_test.go b/metricbeat/module/mysql/mysql_integration_test.go index 772fd91ae80d..8d8b260c4bbd 100644 --- a/metricbeat/module/mysql/mysql_integration_test.go +++ b/metricbeat/module/mysql/mysql_integration_test.go @@ -29,9 +29,9 @@ import ( ) func TestNewDB(t *testing.T) { - compose.EnsureUp(t, "mysql") + r := compose.EnsureUp(t, "mysql") - db, err := NewDB(GetMySQLEnvDSN()) + db, err := NewDB(GetMySQLEnvDSN(r.Host())) assert.NoError(t, err) err = db.Ping() diff --git a/metricbeat/module/mysql/status/status_integration_test.go b/metricbeat/module/mysql/status/status_integration_test.go index bc7bf8e40e9f..9cb03e6ab6c8 100644 --- a/metricbeat/module/mysql/status/status_integration_test.go +++ b/metricbeat/module/mysql/status/status_integration_test.go @@ -31,9 +31,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "mysql") + r := compose.EnsureUp(t, "mysql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(false)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), false)) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 errors, had %d. %v\n", len(errs), errs) @@ -56,9 +56,9 @@ func TestFetch(t *testing.T) { } func TestFetchRaw(t *testing.T) { - compose.EnsureUp(t, "mysql") + r := compose.EnsureUp(t, "mysql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(true)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), true)) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 errors, had %d. %v\n", len(errs), errs) @@ -83,7 +83,9 @@ func TestFetchRaw(t *testing.T) { } func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(false)) + r := compose.EnsureUp(t, "mysql") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host(), false)) err := mbtest.WriteEventsReporterV2Error(f, t, "") if err != nil { @@ -91,11 +93,11 @@ func TestData(t *testing.T) { } } -func getConfig(raw bool) map[string]interface{} { +func getConfig(host string, raw bool) map[string]interface{} { return map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, - "hosts": []string{mysql.GetMySQLEnvDSN()}, + "hosts": []string{mysql.GetMySQLEnvDSN(host)}, "raw": raw, } } diff --git a/metricbeat/module/mysql/testing.go b/metricbeat/module/mysql/testing.go index f732c1f205ef..878b8d9df2ce 100644 --- a/metricbeat/module/mysql/testing.go +++ b/metricbeat/module/mysql/testing.go @@ -18,8 +18,6 @@ package mysql import ( - "os" - "github.com/go-sql-driver/mysql" ) @@ -28,16 +26,11 @@ import ( // GetMySQLEnvDSN returns the MySQL server DSN to use for testing. It // reads the value from the MYSQL_DSN environment variable and returns // root@tcp(127.0.0.1:3306)/ if it is not set. -func GetMySQLEnvDSN() string { - dsn := os.Getenv("MYSQL_DSN") - - if len(dsn) == 0 { - c := mysql.NewConfig() - c.Net = "tcp" - c.Addr = "127.0.0.1:3306" - c.User = "root" - c.Passwd = "test" - dsn = c.FormatDSN() - } - return dsn +func GetMySQLEnvDSN(host string) string { + c := mysql.NewConfig() + c.Net = "tcp" + c.Addr = host + c.User = "root" + c.Passwd = "test" + return c.FormatDSN() } diff --git a/metricbeat/module/nats/_meta/env b/metricbeat/module/nats/_meta/env deleted file mode 100644 index 69e439d0a4fd..000000000000 --- a/metricbeat/module/nats/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -NATS_HOST=nats -NATS_PORT=8222 diff --git a/metricbeat/module/nats/connections/connections_integration_test.go b/metricbeat/module/nats/connections/connections_integration_test.go index 0cef2f663e41..3496c6d89cf5 100644 --- a/metricbeat/module/nats/connections/connections_integration_test.go +++ b/metricbeat/module/nats/connections/connections_integration_test.go @@ -20,7 +20,6 @@ package connections import ( - "os" "testing" "github.com/elastic/beats/libbeat/tests/compose" @@ -28,9 +27,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(metricSet, t, "./test_data.json") if err != nil { t.Fatal("write", err) @@ -38,39 +37,21 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) metricSet.Fetch(reporter) e := mbtest.StandardizeEvent(metricSet, reporter.GetEvents()[0]) t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "nats", "metricsets": []string{"connections"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("NATS_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("NATS_PORT") - - if len(port) == 0 { - port = "8222" - } - return port -} diff --git a/metricbeat/module/nats/routes/routes_integration_test.go b/metricbeat/module/nats/routes/routes_integration_test.go index 89f607c08691..3cafe01fa337 100644 --- a/metricbeat/module/nats/routes/routes_integration_test.go +++ b/metricbeat/module/nats/routes/routes_integration_test.go @@ -20,7 +20,6 @@ package routes import ( - "os" "testing" "github.com/elastic/beats/libbeat/tests/compose" @@ -28,9 +27,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(metricSet, t, "./test_data.json") if err != nil { t.Fatal("write", err) @@ -38,39 +37,21 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) metricSet.Fetch(reporter) e := mbtest.StandardizeEvent(metricSet, reporter.GetEvents()[0]) t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "nats", "metricsets": []string{"routes"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("NATS_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("NATS_PORT") - - if len(port) == 0 { - port = "8222" - } - return port -} diff --git a/metricbeat/module/nats/stats/stats_integration_test.go b/metricbeat/module/nats/stats/stats_integration_test.go index ae9223e1c1fa..83d16ebba01f 100644 --- a/metricbeat/module/nats/stats/stats_integration_test.go +++ b/metricbeat/module/nats/stats/stats_integration_test.go @@ -20,7 +20,6 @@ package stats import ( - "os" "testing" "github.com/elastic/beats/libbeat/tests/compose" @@ -28,9 +27,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(metricSet, t, "./test_data.json") if err != nil { t.Fatal("write", err) @@ -38,39 +37,21 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) metricSet.Fetch(reporter) e := mbtest.StandardizeEvent(metricSet, reporter.GetEvents()[0]) t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "nats", "metricsets": []string{"stats"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("NATS_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("NATS_PORT") - - if len(port) == 0 { - port = "8222" - } - return port -} diff --git a/metricbeat/module/nats/subscriptions/subscriptions_integration_test.go b/metricbeat/module/nats/subscriptions/subscriptions_integration_test.go index aa78f04ed612..af974a7aa7ef 100644 --- a/metricbeat/module/nats/subscriptions/subscriptions_integration_test.go +++ b/metricbeat/module/nats/subscriptions/subscriptions_integration_test.go @@ -20,7 +20,6 @@ package subscriptions import ( - "os" "testing" "github.com/elastic/beats/libbeat/tests/compose" @@ -28,9 +27,9 @@ import ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(metricSet, t, "./test_data.json") if err != nil { t.Fatal("write", err) @@ -38,39 +37,21 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "nats") + r := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) metricSet.Fetch(reporter) e := mbtest.StandardizeEvent(metricSet, reporter.GetEvents()[0]) t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "nats", "metricsets": []string{"subscriptions"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("NATS_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("NATS_PORT") - - if len(port) == 0 { - port = "8222" - } - return port -} diff --git a/metricbeat/module/nginx/_meta/env b/metricbeat/module/nginx/_meta/env deleted file mode 100644 index 693e4c78989b..000000000000 --- a/metricbeat/module/nginx/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -NGINX_HOST=nginx -NGINX_PORT=80 diff --git a/metricbeat/module/nginx/stubstatus/stubstatus_integration_test.go b/metricbeat/module/nginx/stubstatus/stubstatus_integration_test.go index c8cd5bac0f4a..e8c5053e95a4 100644 --- a/metricbeat/module/nginx/stubstatus/stubstatus_integration_test.go +++ b/metricbeat/module/nginx/stubstatus/stubstatus_integration_test.go @@ -24,15 +24,14 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/nginx" "github.com/stretchr/testify/assert" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "nginx") + r := compose.EnsureUp(t, "nginx") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -47,24 +46,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "nginx") - - f := mbtest.NewReportingMetricSetV2(t, getConfig()) - events, errs := mbtest.ReportingFetchV2(f) - if len(errs) > 0 { - t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) - } - assert.NotEmpty(t, events) + r := compose.EnsureUp(t, "nginx") + f := mbtest.NewReportingMetricSetV2(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "nginx", "metricsets": []string{"stubstatus"}, - "hosts": []string{nginx.GetNginxEnvHost()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/php_fpm/_meta/env b/metricbeat/module/php_fpm/_meta/env deleted file mode 100644 index 3437fa5b8c42..000000000000 --- a/metricbeat/module/php_fpm/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -PHPFPM_HOST=phpfpm -PHPFPM_PORT=81 diff --git a/metricbeat/module/php_fpm/pool/pool_integration_test.go b/metricbeat/module/php_fpm/pool/pool_integration_test.go index 2251669f9fec..991ec182b8c3 100644 --- a/metricbeat/module/php_fpm/pool/pool_integration_test.go +++ b/metricbeat/module/php_fpm/pool/pool_integration_test.go @@ -20,7 +20,6 @@ package pool import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "phpfpm") + r := compose.EnsureUp(t, "phpfpm") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -45,28 +44,10 @@ func TestFetch(t *testing.T) { } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "php_fpm", "metricsets": []string{"pool"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("PHPFPM_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("PHPFPM_PORT") - - if len(port) == 0 { - port = "81" - } - return port -} diff --git a/metricbeat/module/php_fpm/process/process_integration_test.go b/metricbeat/module/php_fpm/process/process_integration_test.go index a5f7a1d8d640..ac79ebf66e4f 100644 --- a/metricbeat/module/php_fpm/process/process_integration_test.go +++ b/metricbeat/module/php_fpm/process/process_integration_test.go @@ -20,7 +20,6 @@ package process import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -30,9 +29,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "phpfpm") + r := compose.EnsureUp(t, "phpfpm") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -45,28 +44,10 @@ func TestFetch(t *testing.T) { } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "php_fpm", "metricsets": []string{"process"}, - "hosts": []string{GetEnvHost() + ":" + GetEnvPort()}, + "hosts": []string{host}, } } - -func GetEnvHost() string { - host := os.Getenv("PHPFPM_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -func GetEnvPort() string { - port := os.Getenv("PHPFPM_PORT") - - if len(port) == 0 { - port = "81" - } - return port -} diff --git a/metricbeat/module/postgresql/_meta/env b/metricbeat/module/postgresql/_meta/env deleted file mode 100644 index eef1c64138ce..000000000000 --- a/metricbeat/module/postgresql/_meta/env +++ /dev/null @@ -1,4 +0,0 @@ -POSTGRESQL_DSN=postgres://postgresql:5432?sslmode=disable -POSTGRESQL_HOST=postgresql -POSTGRESQL_PORT=5432 -POSTGRESQL_USERNAME=postgres diff --git a/metricbeat/module/postgresql/activity/activity_integration_test.go b/metricbeat/module/postgresql/activity/activity_integration_test.go index 7ffd4fc16d4d..4791ec78f818 100644 --- a/metricbeat/module/postgresql/activity/activity_integration_test.go +++ b/metricbeat/module/postgresql/activity/activity_integration_test.go @@ -31,9 +31,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -57,19 +57,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "postgresql", "metricsets": []string{"activity"}, - "hosts": []string{postgresql.GetEnvDSN()}, + "hosts": []string{postgresql.GetDSN(host)}, "username": postgresql.GetEnvUsername(), "password": postgresql.GetEnvPassword(), } diff --git a/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go b/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go index d2bd146ea8df..6cc88a77e36c 100644 --- a/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go +++ b/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go @@ -31,9 +31,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -62,19 +62,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "postgresql", "metricsets": []string{"bgwriter"}, - "hosts": []string{postgresql.GetEnvDSN()}, + "hosts": []string{postgresql.GetDSN(host)}, "username": postgresql.GetEnvUsername(), "password": postgresql.GetEnvPassword(), } diff --git a/metricbeat/module/postgresql/database/database_integration_test.go b/metricbeat/module/postgresql/database/database_integration_test.go index 73ac082fa2a2..432d631ab23f 100644 --- a/metricbeat/module/postgresql/database/database_integration_test.go +++ b/metricbeat/module/postgresql/database/database_integration_test.go @@ -31,9 +31,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -59,19 +59,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "postgresql", "metricsets": []string{"database"}, - "hosts": []string{postgresql.GetEnvDSN()}, + "hosts": []string{postgresql.GetDSN(host)}, "username": postgresql.GetEnvUsername(), "password": postgresql.GetEnvPassword(), } diff --git a/metricbeat/module/postgresql/statement/statement_integration_test.go b/metricbeat/module/postgresql/statement/statement_integration_test.go index fc5ebc12ff6e..251d4f0c0ee9 100644 --- a/metricbeat/module/postgresql/statement/statement_integration_test.go +++ b/metricbeat/module/postgresql/statement/statement_integration_test.go @@ -31,9 +31,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -90,19 +90,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "postgresql") + r := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "postgresql", "metricsets": []string{"statement"}, - "hosts": []string{postgresql.GetEnvDSN()}, + "hosts": []string{postgresql.GetDSN(host)}, "username": postgresql.GetEnvUsername(), "password": postgresql.GetEnvPassword(), } diff --git a/metricbeat/module/postgresql/testing.go b/metricbeat/module/postgresql/testing.go index 55e8f7f08432..64d2d635dbcb 100644 --- a/metricbeat/module/postgresql/testing.go +++ b/metricbeat/module/postgresql/testing.go @@ -17,19 +17,28 @@ package postgresql -import "os" +import ( + "fmt" + "os" +) -//GetEnvDSN returns the Data Source Name -func GetEnvDSN() string { - return os.Getenv("POSTGRESQL_DSN") +//GetDSN returns the Data Source Name for a given host +func GetDSN(host string) string { + return fmt.Sprintf("postgres://%s?sslmode=disable", host) } //GetEnvUsername returns the username func GetEnvUsername() string { - return os.Getenv("POSTGRESQL_USERNAME") + if username := os.Getenv("POSTGRESQL_USERNAME"); username != "" { + return username + } + return "postgres" } //GetEnvPassword returns the password func GetEnvPassword() string { - return os.Getenv("POSTGRESQL_PASSWORD") + if password := os.Getenv("POSTGRESQL_USERNAME"); password != "" { + return password + } + return "postgres" } diff --git a/metricbeat/module/prometheus/_meta/env b/metricbeat/module/prometheus/_meta/env deleted file mode 100644 index a77775f7c6f1..000000000000 --- a/metricbeat/module/prometheus/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -PROMETHEUS_HOST=prometheus -PROMETHEUS_PORT=9090 diff --git a/metricbeat/module/rabbitmq/_meta/env b/metricbeat/module/rabbitmq/_meta/env deleted file mode 100644 index a46bb9673d88..000000000000 --- a/metricbeat/module/rabbitmq/_meta/env +++ /dev/null @@ -1,4 +0,0 @@ -RABBITMQ_HOST=rabbitmq -RABBITMQ_PORT=15672 -RABBITMQ_USERNAME=guest -RABBITMQ_PASSWORD=guest diff --git a/metricbeat/module/rabbitmq/mtest/integration.go b/metricbeat/module/rabbitmq/mtest/integration.go index 3160c0cf2598..cba6493391b3 100644 --- a/metricbeat/module/rabbitmq/mtest/integration.go +++ b/metricbeat/module/rabbitmq/mtest/integration.go @@ -18,35 +18,25 @@ package mtest import ( - "net" "os" ) // GetIntegrationConfig generates a base configuration with common values for // integration tests -func GetIntegrationConfig() map[string]interface{} { +func GetIntegrationConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "rabbitmq", - "hosts": getTestRabbitMQHost(), + "hosts": []string{host}, "username": getTestRabbitMQUsername(), "password": getTestRabbitMQPassword(), } } const ( - rabbitmqDefaultHost = "localhost" - rabbitmqDefaultPort = "15672" rabbitmqDefaultUsername = "guest" rabbitmqDefaultPassword = "guest" ) -func getTestRabbitMQHost() string { - return net.JoinHostPort( - getenv("RABBITMQ_HOST", rabbitmqDefaultHost), - getenv("RABBITMQ_PORT", rabbitmqDefaultPort), - ) -} - func getTestRabbitMQUsername() string { return getenv("RABBITMQ_USERNAME", rabbitmqDefaultUsername) } diff --git a/metricbeat/module/rabbitmq/node/node_integration_test.go b/metricbeat/module/rabbitmq/node/node_integration_test.go index 162a3194ef66..9456ac892158 100644 --- a/metricbeat/module/rabbitmq/node/node_integration_test.go +++ b/metricbeat/module/rabbitmq/node/node_integration_test.go @@ -30,7 +30,9 @@ import ( ) func TestData(t *testing.T) { - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUp(t, "rabbitmq") + + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) @@ -39,11 +41,11 @@ func TestData(t *testing.T) { func TestFetch(t *testing.T) { t.Skip("Skipping test as it was not stable. Probably first event is empty.") - compose.EnsureUp(t, "rabbitmq") + r := compose.EnsureUp(t, "rabbitmq") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := metricSet.Fetch(reporter) assert.NoError(t, err) @@ -51,8 +53,8 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) } -func getConfig() map[string]interface{} { - config := mtest.GetIntegrationConfig() +func getConfig(host string) map[string]interface{} { + config := mtest.GetIntegrationConfig(host) config["metricsets"] = []string{"node"} return config } diff --git a/metricbeat/module/redis/_meta/env b/metricbeat/module/redis/_meta/env deleted file mode 100644 index 82b68167bc63..000000000000 --- a/metricbeat/module/redis/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -REDIS_HOST=redis -REDIS_PORT=6379 diff --git a/metricbeat/module/redis/info/info_integration_test.go b/metricbeat/module/redis/info/info_integration_test.go index 32072825a2a7..4ff12ae54dfc 100644 --- a/metricbeat/module/redis/info/info_integration_test.go +++ b/metricbeat/module/redis/info/info_integration_test.go @@ -25,17 +25,14 @@ import ( "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/redis" "github.com/stretchr/testify/assert" ) -var redisHost = redis.GetRedisEnvHost() + ":" + redis.GetRedisEnvPort() - func TestFetch(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, err := mbtest.ReportingFetchV2Error(ms) if err != nil { t.Fatal("fetch", err) @@ -54,19 +51,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "redis", "metricsets": []string{"info"}, - "hosts": []string{redisHost}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/redis/key/key_integration_test.go b/metricbeat/module/redis/key/key_integration_test.go index 61bacd1f9bab..7c983c3de231 100644 --- a/metricbeat/module/redis/key/key_integration_test.go +++ b/metricbeat/module/redis/key/key_integration_test.go @@ -29,17 +29,14 @@ import ( "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/redis" ) -var host = redis.GetRedisEnvHost() + ":" + redis.GetRedisEnvPort() - func TestFetch(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - addEntry(t, "foo", 1) + addEntry(t, r.Host(), "foo", 1) - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(r.Host())) events, err := ms.FetchEvents() if err != nil { t.Fatal("fetch", err) @@ -50,16 +47,16 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - addEntry(t, "foo", 1) + addEntry(t, r.Host(), "foo", 1) - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(r.Host())) ms.WriteEvents(t, "") } func TestFetchMultipleKeyspaces(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") expectedKeyspaces := map[string]uint{ "foo": 0, @@ -69,10 +66,10 @@ func TestFetchMultipleKeyspaces(t *testing.T) { expectedEvents := len(expectedKeyspaces) for name, keyspace := range expectedKeyspaces { - addEntry(t, name, keyspace) + addEntry(t, r.Host(), name, keyspace) } - config := getConfig() + config := getConfig(r.Host()) config["key.patterns"] = []map[string]interface{}{ { "pattern": "foo", @@ -109,7 +106,7 @@ func TestFetchMultipleKeyspaces(t *testing.T) { } // addEntry adds an entry to redis -func addEntry(t *testing.T, key string, keyspace uint) { +func addEntry(t *testing.T, host string, key string, keyspace uint) { // Insert at least one event to make sure db exists c, err := rd.Dial("tcp", host) if err != nil { @@ -126,7 +123,7 @@ func addEntry(t *testing.T, key string, keyspace uint) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "redis", "metricsets": []string{"key"}, diff --git a/metricbeat/module/redis/keyspace/keyspace_integration_test.go b/metricbeat/module/redis/keyspace/keyspace_integration_test.go index 3ad32b96540f..4aeb7d1f4fde 100644 --- a/metricbeat/module/redis/keyspace/keyspace_integration_test.go +++ b/metricbeat/module/redis/keyspace/keyspace_integration_test.go @@ -25,21 +25,18 @@ import ( "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/redis" rd "github.com/garyburd/redigo/redis" "github.com/stretchr/testify/assert" ) -var host = redis.GetRedisEnvHost() + ":" + redis.GetRedisEnvPort() - func TestFetch(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - addEntry(t) + addEntry(t, r.Host()) // Fetch data - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, err := mbtest.ReportingFetchV2Error(ms) if err != nil { t.Fatal("fetch", err) @@ -59,11 +56,11 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") - addEntry(t) + addEntry(t, r.Host()) - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) @@ -71,7 +68,7 @@ func TestData(t *testing.T) { } // addEntry adds an entry to redis -func addEntry(t *testing.T) { +func addEntry(t *testing.T, host string) { // Insert at least one event to make sure db exists c, err := rd.Dial("tcp", host) if err != nil { @@ -84,7 +81,7 @@ func addEntry(t *testing.T) { } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "redis", "metricsets": []string{"keyspace"}, diff --git a/metricbeat/module/redis/metricset_integration_test.go b/metricbeat/module/redis/metricset_integration_test.go index 0bb3dec6dec2..6f91d5eaf097 100644 --- a/metricbeat/module/redis/metricset_integration_test.go +++ b/metricbeat/module/redis/metricset_integration_test.go @@ -39,7 +39,8 @@ const ( func TestPasswords(t *testing.T) { t.Skip("Changing password affects other tests, see https://github.com/elastic/beats/issues/10955") - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") + host := r.Host() registry := mb.NewRegister() err := registry.AddModule("redis", mb.DefaultModuleFactory) @@ -63,21 +64,21 @@ func TestPasswords(t *testing.T) { } // Test Fetch metrics with missing password - ms := getMetricSet(t, registry, getConfig("")) + ms := getMetricSet(t, registry, getConfig(host, "")) _, err = ms.Connection().Do("PING") if assert.Error(t, err, "missing password") { assert.Contains(t, err, "NOAUTH Authentication required.") } // Config redis and metricset with an invalid password - ms = getMetricSet(t, registry, getConfig("blah")) + ms = getMetricSet(t, registry, getConfig(host, "blah")) _, err = ms.Connection().Do("PING") if assert.Error(t, err, "invalid password") { assert.Contains(t, err, "ERR invalid password") } // Config redis and metricset with a valid password - ms = getMetricSet(t, registry, getConfig(password)) + ms = getMetricSet(t, registry, getConfig(host, password)) _, err = ms.Connection().Do("PING") assert.Empty(t, err, "valid password") } @@ -141,7 +142,7 @@ func getMetricSet(t *testing.T, registry *mb.Register, config map[string]interfa return ms.MetricSet } -func getConfig(password string) map[string]interface{} { +func getConfig(host string, password string) map[string]interface{} { return map[string]interface{}{ "module": "redis", "metricsets": "test", diff --git a/metricbeat/module/redis/redis_integration_test.go b/metricbeat/module/redis/redis_integration_test.go index 933a5ce6ee72..4621849cf1ce 100644 --- a/metricbeat/module/redis/redis_integration_test.go +++ b/metricbeat/module/redis/redis_integration_test.go @@ -31,10 +31,9 @@ import ( _ "github.com/elastic/beats/metricbeat/mb/testing" ) -var host = GetRedisEnvHost() + ":" + GetRedisEnvPort() - func TestFetchRedisInfo(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") + host := r.Host() conn, err := rd.Dial("tcp", host) if err != nil { @@ -69,7 +68,8 @@ func TestFetchRedisInfo(t *testing.T) { } func TestFetchKeys(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") + host := r.Host() conn, err := rd.Dial("tcp", host) if err != nil { @@ -95,7 +95,8 @@ func TestFetchKeys(t *testing.T) { } func TestFetchKeyInfo(t *testing.T) { - compose.EnsureUp(t, "redis") + r := compose.EnsureUp(t, "redis") + host := r.Host() conn, err := rd.Dial("tcp", host) if err != nil { diff --git a/metricbeat/module/redis/testing.go b/metricbeat/module/redis/testing.go deleted file mode 100644 index 3149a3043b2d..000000000000 --- a/metricbeat/module/redis/testing.go +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package redis - -import ( - "os" -) - -// Helper functions for testing used in the redis metricsets - -// GetRedisEnvHost returns the hostname of the Redis server to use for testing. -// It reads the value from the REDIS_HOST environment variable and returns -// 127.0.0.1 if it is not set. -func GetRedisEnvHost() string { - host := os.Getenv("REDIS_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetRedisEnvPort returns the port of the Redis server to use for testing. -// It reads the value from the REDIS_PORT environment variable and returns -// 6379 if it is not set. -func GetRedisEnvPort() string { - port := os.Getenv("REDIS_PORT") - - if len(port) == 0 { - port = "6379" - } - return port -} diff --git a/metricbeat/module/traefik/_meta/env b/metricbeat/module/traefik/_meta/env deleted file mode 100644 index d8aa826efe63..000000000000 --- a/metricbeat/module/traefik/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -TRAEFIK_HOST=traefik -TRAEFIK_API_PORT=8080 diff --git a/metricbeat/module/traefik/health/health_integration_test.go b/metricbeat/module/traefik/health/health_integration_test.go index aa2a15308bfe..5118f7469317 100644 --- a/metricbeat/module/traefik/health/health_integration_test.go +++ b/metricbeat/module/traefik/health/health_integration_test.go @@ -30,9 +30,7 @@ import ( "github.com/stretchr/testify/assert" ) -func makeBadRequest(config map[string]interface{}) error { - host := config["hosts"].([]string)[0] - +func makeBadRequest(host string) error { resp, err := http.Get("http://" + host + "/foobar") if err != nil { return err @@ -42,12 +40,11 @@ func makeBadRequest(config map[string]interface{}) error { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "traefik") - - config := mtest.GetConfig("health") + r := compose.EnsureUp(t, "traefik") - makeBadRequest(config) + makeBadRequest(r.Host()) + config := mtest.GetConfig("health", r.Host()) ms := mbtest.NewReportingMetricSetV2Error(t, config) reporter := &mbtest.CapturingReporterV2{} diff --git a/metricbeat/module/traefik/mtest/testing.go b/metricbeat/module/traefik/mtest/testing.go index c71b9accb012..938f2ee7d4e3 100644 --- a/metricbeat/module/traefik/mtest/testing.go +++ b/metricbeat/module/traefik/mtest/testing.go @@ -17,33 +17,11 @@ package mtest -import "os" - -// GetEnvHost for Traefik -func GetEnvHost() string { - host := os.Getenv("TRAEFIK_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return host -} - -// GetEnvAPIPort for Traefik -func GetEnvAPIPort() string { - port := os.Getenv("TRAEFIK_API_PORT") - - if len(port) == 0 { - port = "8080" - } - return port -} - // GetConfig for Traefik -func GetConfig(metricset string) map[string]interface{} { +func GetConfig(metricset string, host string) map[string]interface{} { return map[string]interface{}{ "module": "traefik", "metricsets": []string{metricset}, - "hosts": []string{GetEnvHost() + ":" + GetEnvAPIPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/uwsgi/_meta/env b/metricbeat/module/uwsgi/_meta/env deleted file mode 100644 index 2f9a17b57d49..000000000000 --- a/metricbeat/module/uwsgi/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -UWSGI_STAT_TCP_SERVER=tcp://uwsgi_tcp:9191 -UWSGI_STAT_HTTP_SERVER=http://uwsgi_http:9192 diff --git a/metricbeat/module/uwsgi/status/status_integration_test.go b/metricbeat/module/uwsgi/status/status_integration_test.go index 18673776b1e1..d26b8de56956 100644 --- a/metricbeat/module/uwsgi/status/status_integration_test.go +++ b/metricbeat/module/uwsgi/status/status_integration_test.go @@ -22,17 +22,16 @@ package status import ( "testing" + "github.com/stretchr/testify/assert" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/uwsgi" - - "github.com/stretchr/testify/assert" ) func TestFetchTCP(t *testing.T) { - compose.EnsureUp(t, "uwsgi_tcp") + r := compose.EnsureUp(t, "uwsgi_tcp") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("tcp")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("tcp", r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -45,7 +44,9 @@ func TestFetchTCP(t *testing.T) { } func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http")) + r := compose.EnsureUp(t, "uwsgi_http") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http", r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) @@ -53,9 +54,9 @@ func TestData(t *testing.T) { } func TestFetchHTTP(t *testing.T) { - compose.EnsureUp(t, "uwsgi_http") + r := compose.EnsureUp(t, "uwsgi_http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http", r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -67,19 +68,17 @@ func TestFetchHTTP(t *testing.T) { assert.Equal(t, 1, len(totals)) } -func getConfig(scheme string) map[string]interface{} { +func getConfig(scheme string, host string) map[string]interface{} { conf := map[string]interface{}{ "module": "uwsgi", "metricsets": []string{"status"}, } switch scheme { - case "tcp": - conf["hosts"] = []string{uwsgi.GetEnvTCPServer()} case "http", "https": - conf["hosts"] = []string{uwsgi.GetEnvHTTPServer()} + conf["hosts"] = []string{"http://" + host} default: - conf["hosts"] = []string{uwsgi.GetEnvTCPServer()} + conf["hosts"] = []string{"tcp://" + host} } return conf } diff --git a/metricbeat/module/uwsgi/testing.go b/metricbeat/module/uwsgi/testing.go deleted file mode 100644 index b8c465354e6f..000000000000 --- a/metricbeat/module/uwsgi/testing.go +++ /dev/null @@ -1,38 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package uwsgi - -import "os" - -// GetEnvTCPServer returns uwsgi stat server host with tcp mode -func GetEnvTCPServer() string { - env := os.Getenv("UWSGI_STAT_TCP_SERVER") - if len(env) == 0 { - env = "tcp://127.0.0.1:9191" - } - return env -} - -// GetEnvHTTPServer returns uwsgi stat server host with http mode -func GetEnvHTTPServer() string { - env := os.Getenv("UWSGI_STAT_HTTP_SERVER") - if len(env) == 0 { - env = "http://127.0.0.1:9192" - } - return env -} diff --git a/metricbeat/module/zookeeper/_meta/env b/metricbeat/module/zookeeper/_meta/env deleted file mode 100644 index 6a063ecec28a..000000000000 --- a/metricbeat/module/zookeeper/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -ZOOKEEPER_HOST=zookeeper -ZOOKEEPER_PORT=2181 diff --git a/metricbeat/module/zookeeper/connection/connection_integration_test.go b/metricbeat/module/zookeeper/connection/connection_integration_test.go index 7437f87842ff..a26e5240c32e 100644 --- a/metricbeat/module/zookeeper/connection/connection_integration_test.go +++ b/metricbeat/module/zookeeper/connection/connection_integration_test.go @@ -22,22 +22,23 @@ package connection import ( "testing" - "github.com/elastic/beats/metricbeat/module/zookeeper" - + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) func TestData(t *testing.T) { - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + r := compose.EnsureUp(t, "zookeeper") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "zookeeper", "metricsets": []string{"connection"}, - "hosts": []string{zookeeper.GetZookeeperEnvHost() + ":" + zookeeper.GetZookeeperEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/zookeeper/mntr/mntr_integration_test.go b/metricbeat/module/zookeeper/mntr/mntr_integration_test.go index 79af49f4b931..41c7259cf1a3 100644 --- a/metricbeat/module/zookeeper/mntr/mntr_integration_test.go +++ b/metricbeat/module/zookeeper/mntr/mntr_integration_test.go @@ -27,13 +27,12 @@ import ( "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" - "github.com/elastic/beats/metricbeat/module/zookeeper" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "zookeeper") + r := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) assert.Empty(t, errs) @@ -59,19 +58,19 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - compose.EnsureUp(t, "zookeeper") + r := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) err := mbtest.WriteEventsReporterV2Error(f, t, ".") if err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "zookeeper", "metricsets": []string{"mntr"}, - "hosts": []string{zookeeper.GetZookeeperEnvHost() + ":" + zookeeper.GetZookeeperEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/zookeeper/server/server_integration_test.go b/metricbeat/module/zookeeper/server/server_integration_test.go index 1adb6a278144..bac3034298f5 100644 --- a/metricbeat/module/zookeeper/server/server_integration_test.go +++ b/metricbeat/module/zookeeper/server/server_integration_test.go @@ -22,11 +22,9 @@ package server import ( "testing" - "github.com/elastic/beats/libbeat/logp" - "github.com/elastic/beats/metricbeat/module/zookeeper" - "github.com/stretchr/testify/assert" + "github.com/elastic/beats/libbeat/logp" "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" ) @@ -34,9 +32,9 @@ import ( func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "zookeeper") + r := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -59,17 +57,18 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { + r := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "zookeeper", "metricsets": []string{"server"}, - "hosts": []string{zookeeper.GetZookeeperEnvHost() + ":" + zookeeper.GetZookeeperEnvPort()}, + "hosts": []string{host}, } } diff --git a/metricbeat/module/zookeeper/testing.go b/metricbeat/module/zookeeper/testing.go deleted file mode 100644 index afa3cac8398e..000000000000 --- a/metricbeat/module/zookeeper/testing.go +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package zookeeper - -import ( - "os" -) - -// Helper functions for testing used in the zookeeper MetricSets. - -// GetZookeeperEnvHost returns the hostname of the ZooKeeper server to use for -// testing. It reads the value from the ZOOKEEPER_HOST environment variable and -// returns localhost if it is not set. -func GetZookeeperEnvHost() string { - host := os.Getenv("ZOOKEEPER_HOST") - - if len(host) == 0 { - host = "localhost" - } - return host -} - -// GetZookeeperEnvPort returns the port of the ZooKeeper server to use for -// testing. It reads the value from the ZOOKEEPER_PORT environment variable and -// returns 2181 if it is not set. -func GetZookeeperEnvPort() string { - port := os.Getenv("ZOOKEEPER_PORT") - - if len(port) == 0 { - port = "2181" - } - return port -} diff --git a/x-pack/metricbeat/docker-compose.yml b/x-pack/metricbeat/docker-compose.yml index ddd1eaa76def..21dcdc823fe4 100644 --- a/x-pack/metricbeat/docker-compose.yml +++ b/x-pack/metricbeat/docker-compose.yml @@ -8,13 +8,8 @@ services: volumes: - ${PWD}/../..:/go/src/github.com/elastic/beats/ - /var/run/docker.sock:/var/run/docker.sock + network_mode: host command: make - env_file: - - ../../metricbeat/module/elasticsearch/_meta/env - - ../../metricbeat/module/kibana/_meta/env - - ./module/cockroachdb/_meta/env - - ./module/coredns/_meta/env - - ./module/mssql/_meta/env # Modules cockroachdb: diff --git a/x-pack/metricbeat/module/cockroachdb/_meta/env b/x-pack/metricbeat/module/cockroachdb/_meta/env deleted file mode 100644 index 0d443272f0e9..000000000000 --- a/x-pack/metricbeat/module/cockroachdb/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -COCKROACHDB_HOST=cockroachdb -COCKROACHDB_PORT=8080 diff --git a/x-pack/metricbeat/module/cockroachdb/status/status_integration_test.go b/x-pack/metricbeat/module/cockroachdb/status/status_integration_test.go index e2c65a094d5a..79b37d39421f 100644 --- a/x-pack/metricbeat/module/cockroachdb/status/status_integration_test.go +++ b/x-pack/metricbeat/module/cockroachdb/status/status_integration_test.go @@ -29,9 +29,9 @@ func init() { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "cockroachdb") + r := compose.EnsureUp(t, "cockroachdb") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -40,18 +40,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { - host := os.Getenv("COCKROACHDB_HOST") - if host == "" { - host = "127.0.0.1" - } - port := os.Getenv("COCKROACHDB_PORT") - if port == "" { - port = "8080" - } +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "cockroachdb", "metricsets": []string{"status"}, - "hosts": []string{host + ":" + port}, + "hosts": []string{host}, } } diff --git a/x-pack/metricbeat/module/coredns/_meta/env b/x-pack/metricbeat/module/coredns/_meta/env deleted file mode 100644 index b8ad57b40ff5..000000000000 --- a/x-pack/metricbeat/module/coredns/_meta/env +++ /dev/null @@ -1,2 +0,0 @@ -COREDNS_HOST=coredns -COREDNS_PORT=9153 diff --git a/x-pack/metricbeat/module/coredns/stats/stats_integration_test.go b/x-pack/metricbeat/module/coredns/stats/stats_integration_test.go index d2ae928c348e..ec2cb90f2ada 100644 --- a/x-pack/metricbeat/module/coredns/stats/stats_integration_test.go +++ b/x-pack/metricbeat/module/coredns/stats/stats_integration_test.go @@ -7,7 +7,6 @@ package stats import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -17,9 +16,9 @@ import ( ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "coredns") + r := compose.EnsureUp(t, "coredns") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(r.Host())) events, errs := mbtest.ReportingFetchV2(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -28,19 +27,10 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func getConfig() map[string]interface{} { - - host := os.Getenv("COREDNS_HOST") - port := os.Getenv("COREDNS_PORT") - if len(host) == 0 { - host = "127.0.0.1" - } - if len(port) == 0 { - port = "9153" - } +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "coredns", "metricsets": []string{"stats"}, - "hosts": []string{host + ":" + port}, + "hosts": []string{host}, } } diff --git a/x-pack/metricbeat/module/mssql/_meta/env b/x-pack/metricbeat/module/mssql/_meta/env deleted file mode 100644 index 145a143e51f8..000000000000 --- a/x-pack/metricbeat/module/mssql/_meta/env +++ /dev/null @@ -1,4 +0,0 @@ -MSSQL_HOST=mssql -MSSQL_PORT=1433 -MSSQL_USER=SA -MSSQL_PASSWORD=1234_asdf diff --git a/x-pack/metricbeat/module/mssql/performance/performance_integration_test.go b/x-pack/metricbeat/module/mssql/performance/performance_integration_test.go index 1c17350731a7..fc70b55b0d24 100644 --- a/x-pack/metricbeat/module/mssql/performance/performance_integration_test.go +++ b/x-pack/metricbeat/module/mssql/performance/performance_integration_test.go @@ -25,9 +25,9 @@ type keyAssertion struct { func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "mssql") + r := compose.EnsureUp(t, "mssql") - f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig("performance")) + f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig(r.Host(), "performance")) events, errs := mbtest.ReportingFetchV2(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) diff --git a/x-pack/metricbeat/module/mssql/testing/mssql.go b/x-pack/metricbeat/module/mssql/testing/mssql.go index 971b06badb5c..68f96af796a0 100644 --- a/x-pack/metricbeat/module/mssql/testing/mssql.go +++ b/x-pack/metricbeat/module/mssql/testing/mssql.go @@ -8,13 +8,13 @@ import "os" // GetConfig returns the required configuration options for testing a MSSQL // metricset. -func GetConfig(metricSets ...string) map[string]interface{} { +func GetConfig(host string, metricSets ...string) map[string]interface{} { return map[string]interface{}{ "module": "mssql", "metricsets": metricSets, - "hosts": []string{EnvOr("MSSQL_HOST", "localhost")}, + "hosts": []string{host}, "username": EnvOr("MSSQL_USER", "SA"), - "password": EnvOr("MSSQL_PASSWORD", ""), + "password": EnvOr("MSSQL_PASSWORD", "1234_asdf"), } } diff --git a/x-pack/metricbeat/module/mssql/transaction_log/transaction_log_integration_test.go b/x-pack/metricbeat/module/mssql/transaction_log/transaction_log_integration_test.go index f899ec261414..55e7f05c5af1 100644 --- a/x-pack/metricbeat/module/mssql/transaction_log/transaction_log_integration_test.go +++ b/x-pack/metricbeat/module/mssql/transaction_log/transaction_log_integration_test.go @@ -19,9 +19,9 @@ import ( func TestFetch(t *testing.T) { logp.TestingSetup() - compose.EnsureUp(t, "mssql") + r := compose.EnsureUp(t, "mssql") - f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig("transaction_log")) + f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig(r.Host(), "transaction_log")) events, errs := mbtest.ReportingFetchV2(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) diff --git a/x-pack/metricbeat/module/oracle/tablespace/metricset_test.go b/x-pack/metricbeat/module/oracle/tablespace/metricset_test.go index eb4791a37872..a7934647826e 100644 --- a/x-pack/metricbeat/module/oracle/tablespace/metricset_test.go +++ b/x-pack/metricbeat/module/oracle/tablespace/metricset_test.go @@ -9,27 +9,28 @@ package tablespace import ( "testing" - "github.com/elastic/beats/x-pack/metricbeat/module/oracle" - _ "gopkg.in/goracle.v2" + "github.com/elastic/beats/libbeat/tests/compose" mbtest "github.com/elastic/beats/metricbeat/mb/testing" + "github.com/elastic/beats/x-pack/metricbeat/module/oracle" ) func TestData(t *testing.T) { - //t.Skip("Skip until a proper Docker image is setup for Metricbeat") + t.Skip("Skip until a proper Docker image is setup for Metricbeat") + r := compose.EnsureUp(t, "oracle") - f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig()) + f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig(r.Host())) if err := mbtest.WriteEventsReporterV2WithContext(f, t, ""); err != nil { t.Fatal("write", err) } } -func getConfig() map[string]interface{} { +func getConfig(host string) map[string]interface{} { return map[string]interface{}{ "module": "oracle", "metricsets": []string{"tablespace"}, - "hosts": []string{oracle.GetOracleConnectionDetails()}, + "hosts": []string{oracle.GetOracleConnectionDetails(host)}, } } diff --git a/x-pack/metricbeat/module/oracle/testing.go b/x-pack/metricbeat/module/oracle/testing.go index 9496c6ea6754..06c87b10b562 100644 --- a/x-pack/metricbeat/module/oracle/testing.go +++ b/x-pack/metricbeat/module/oracle/testing.go @@ -12,9 +12,9 @@ import ( ) // GetOracleConnectionDetails return a valid SID to use for testing -func GetOracleConnectionDetails() string { +func GetOracleConnectionDetails(host string) string { params := goracle.ConnectionParams{ - SID: fmt.Sprintf("%s:%s/%s", GetOracleEnvHost(), GetOracleEnvPort(), GetOracleEnvServiceName()), + SID: fmt.Sprintf("%s/%s", host, GetOracleEnvServiceName()), Username: GetOracleEnvUsername(), Password: GetOracleEnvPassword(), IsSysDBA: true, @@ -23,26 +23,6 @@ func GetOracleConnectionDetails() string { return params.StringWithPassword() } -// GetOracleEnvHost returns the hostname to use with Oracle testing server or the value of the environment variable ORACLE_HOST if not empty -func GetOracleEnvHost() string { - host := os.Getenv("ORACLE_HOST") - - if len(host) == 0 { - host = "localhost" - } - return host -} - -// GetOracleEnvPort returns the port to use with Oracle testing server or the value of the environment variable ORACLE_PORT if not empty -func GetOracleEnvPort() string { - port := os.Getenv("ORACLE_PORT") - - if len(port) == 0 { - port = "1521" - } - return port -} - // GetOracleEnvServiceName returns the service name to use with Oracle testing server or the value of the environment variable ORACLE_SERVICE_NAME if not empty func GetOracleEnvServiceName() string { serviceName := os.Getenv("ORACLE_SERVICE_NAME")