diff --git a/libbeat/tests/compose/compose.go b/libbeat/tests/compose/compose.go index d39459ee88f3..3eae2ce3655a 100644 --- a/libbeat/tests/compose/compose.go +++ b/libbeat/tests/compose/compose.go @@ -19,74 +19,152 @@ package compose import ( "errors" + "fmt" "os" "path/filepath" "strconv" + "strings" "testing" + "time" ) +// HostInfo exposes information about started scenario +type HostInfo interface { + // Host returns an address as host:port that can be used to connect to + // a running service. + Host() string + + // HostForPort returns an address as host:port that can be used to + // connect to a running service that has multiple exposed ports. The + // address returned is the one that can be used to connect to the + // indicated exposed port. + HostForPort(port int) string +} + // 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.TB, service string, options ...UpOption) HostInfo { + t.Helper() -// 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) { - // 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 + if hostInfo := HostInfoFromEnv(t, service); hostInfo != nil { + return hostInfo } - compose, err := getComposeProject() + compose, err := getComposeProject(os.Getenv("DOCKER_COMPOSE_PROJECT_NAME")) if err != nil { t.Fatal(err) } + defer compose.Close() // 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) - } + upOptions := UpOptions{ + Timeout: 60 * time.Second, + Create: CreateOptions{ + Build: true, + ForceRecreate: true, + }, + } + for _, option := range options { + option(&upOptions) + } + + // Start container + err = compose.Start(service, upOptions) + if err != nil { + t.Fatal("failed to start service", service, err) } // Wait for health - err = compose.Wait(timeout, services...) + err = compose.Wait(upOptions.Timeout, service) if err != nil { t.Fatal(err) } + + // Get host information + host, err := compose.HostInformation(service) + if err != nil { + t.Fatalf("getting host for %s", service) + } + + return host } -func getComposeProject() (*Project, error) { +// EnsureUpWithTimeout starts all the requested services (must be defined in docker-compose.yml) +// Wait for `timeout` seconds for health +func EnsureUpWithTimeout(t testing.TB, timeout int, service string) HostInfo { + return EnsureUp(t, service, UpWithTimeout(time.Duration(timeout)*time.Second)) +} + +// HostInfoFromEnv gets the host information to use for the test from environment variables. +func HostInfoFromEnv(t testing.TB, service string) HostInfo { + // If an environment variable with the form _HOST is used, its value + // is used as host instead of starting a new service. + envVar := fmt.Sprintf("%s_HOST", strings.ToUpper(service)) + host := os.Getenv(envVar) + if host != "" { + return &staticHostInfo{host: host} + } + + // 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. + // Kept for historical reasons, now it only complains if the host environment + // variable is not set. + noCompose, err := strconv.ParseBool(os.Getenv("NO_COMPOSE")) + if err == nil && noCompose { + t.Fatalf("%s environment variable must be set as the host:port where %s is running", envVar, service) + } + + return nil +} + +type staticHostInfo struct { + host string +} + +func (i *staticHostInfo) Host() string { + return i.host +} + +func (i *staticHostInfo) HostForPort(int) string { + return i.host +} + +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..3cc073fcc922 100644 --- a/libbeat/tests/compose/project.go +++ b/libbeat/tests/compose/project.go @@ -35,7 +35,26 @@ type CreateOptions struct { // UpOptions are the options when containers are started type UpOptions struct { - Create CreateOptions + Timeout time.Duration + Create CreateOptions + + // Set to true if it should inform the container of the host it should + // use to advertise itself to clients + SetupAdvertisedHostEnvFile bool +} + +// UpOption is a modifier for UpOptions +type UpOption func(*UpOptions) + +// UpWithTimeout sets a timeout for waiting for a healthy service +func UpWithTimeout(timeout time.Duration) UpOption { + return func(options *UpOptions) { options.Timeout = timeout } +} + +// UpWithAdvertisedHostEnvFile adds the /run/compose_env file with the +// host to use to advertise to client as the `SERVICE_HOST` variable +func UpWithAdvertisedHostEnvFile(options *UpOptions) { + options.SetupAdvertisedHostEnvFile = true } // Filter options for services @@ -58,9 +77,10 @@ type Driver interface { Up(ctx context.Context, opts UpOptions, service string) 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) LockFile() string + + Close() error } // ContainerStatus is an interface to obtain the status of a container @@ -69,6 +89,8 @@ type ContainerStatus interface { Healthy() bool Running() bool Old() bool + Host() string + HostForPort(int) string } // Project is a docker-compose project @@ -84,23 +106,24 @@ func NewProject(name string, files []string) (*Project, error) { if name == "" { name = filepath.Base(filepath.Dir(files[0])) } - return &Project{ - &wrapperDriver{ - Name: name, - Files: files, - }, - }, nil + driver, err := newWrapperDriver() + if err != nil { + return nil, err + } + driver.Name = name + driver.Files = files + return &Project{Driver: driver}, nil } // Start the container, unless it's running already -func (c *Project) Start(service string) error { +func (c *Project) Start(service string, options UpOptions) error { servicesStatus, err := c.getServices(service) if err != nil { return err } if servicesStatus[service] != nil { - if servicesStatus[service].Running { + if servicesStatus[service].Running() { // Someone is running it return nil } @@ -109,18 +132,13 @@ func (c *Project) Start(service string) error { c.Lock() defer c.Unlock() - return c.Driver.Up(context.Background(), UpOptions{ - Create: CreateOptions{ - Build: true, - ForceRecreate: true, - }, - }, service) + return c.Driver.Up(context.Background(), options, service) } // Wait ensures all wanted services are healthy. Wait loop (60s timeout) -func (c *Project) Wait(seconds int, services ...string) error { +func (c *Project) Wait(seconds time.Duration, services ...string) error { healthy := false - timeout := time.Now().Add(time.Duration(seconds) * time.Second) + timeout := time.Now().Add(seconds) for !healthy && time.Now().Before(timeout) { healthy = true @@ -134,12 +152,16 @@ func (c *Project) Wait(seconds int, services ...string) error { } for _, s := range servicesStatus { - if !s.Healthy { + if !s.Healthy() { healthy = false break } } + if healthy { + break + } + time.Sleep(1 * time.Second) } @@ -149,6 +171,25 @@ func (c *Project) Wait(seconds int, services ...string) error { return nil } +// HostInformation gets the host information of a service +func (c *Project) HostInformation(service string) (ServiceInfo, error) { + servicesStatus, err := c.getServices(service) + if err != nil { + return nil, err + } + + if len(servicesStatus) == 0 { + return nil, errors.New("no container running for service") + } + + status, ok := servicesStatus[service] + if !ok || status.Host() == "" { + return nil, errors.New("unknown host:port for service") + } + + return status, nil +} + // Kill a container func (c *Project) Kill(service string) error { c.Lock() @@ -175,12 +216,12 @@ func (c *Project) KillOld(except []string) error { for _, s := range servicesStatus { // Ignore the ones we want - if contains(except, s.Name) { + if contains(except, s.Name()) { continue } - if s.Old { - err = c.Kill(s.Name) + if s.Running() && s.Old() { + err = c.Kill(s.Name()) if err != nil { return err } @@ -221,20 +262,24 @@ func (c *Project) Unlock() { os.Remove(c.LockFile()) } -type serviceInfo struct { - Name string - Running bool - Healthy bool +// ServiceInfo is an interface for objects that give information about running services +type ServiceInfo interface { + Name() string + Running() bool + Healthy() bool // Has been up for too long?: - Old bool + Old() bool + + Host() string + HostForPort(int) string } -func (c *Project) getServices(filter ...string) (map[string]*serviceInfo, error) { +func (c *Project) getServices(filter ...string) (map[string]ServiceInfo, error) { c.Lock() defer c.Unlock() - result := make(map[string]*serviceInfo) + result := make(map[string]ServiceInfo) services, err := c.Driver.Ps(context.Background(), filter...) if err != nil { return nil, err @@ -244,27 +289,26 @@ func (c *Project) getServices(filter ...string) (map[string]*serviceInfo, error) name := c.ServiceName() // In case of several (stopped) containers, always prefer info about running ones - if result[name] != nil { - if result[name].Running { - continue - } + if r := result[name]; r != nil && r.Running() { + continue } - service := &serviceInfo{ - Name: name, - } - // fill details: - service.Healthy = c.Healthy() - service.Running = c.Running() - if service.Healthy { - service.Old = c.Old() - } - result[name] = service + result[name] = &containerServiceInfo{c} } return result, nil } +// containerServiceInfo wrapps a container status to provide information +// about the service implemented by the container +type containerServiceInfo struct { + ContainerStatus +} + +func (i *containerServiceInfo) Name() string { + return i.ContainerStatus.ServiceName() +} + func contains(list []string, item string) bool { for _, i := range list { if item == i { diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 4a10ef5135b5..6e26173bda8c 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -18,11 +18,19 @@ package compose import ( + "archive/tar" + "bytes" "context" "fmt" + "net" "os" "os/exec" + "path/filepath" + "regexp" + "runtime" + "strconv" "strings" + "time" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -38,6 +46,18 @@ const ( type wrapperDriver struct { Name string Files []string + + Environment []string + + client *client.Client +} + +func newWrapperDriver() (*wrapperDriver, error) { + c, err := client.NewEnvClient() + if err != nil { + return nil, err + } + return &wrapperDriver{client: c}, nil } type wrapperContainer struct { @@ -56,17 +76,82 @@ func (c *wrapperContainer) Running() bool { return c.info.State == "running" } +var statusOldRe = regexp.MustCompile(`(\d+) (minute|hour)s?`) + func (c *wrapperContainer) Old() bool { - return strings.Contains(c.info.Status, "minute") + match := statusOldRe.FindStringSubmatch(c.info.Status) + if len(match) < 3 { + return false + } + n, _ := strconv.Atoi(match[1]) + unit := match[2] + switch unit { + case "minute": + return n > 3 + default: + return true + } +} + +// 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(port int) 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 _, info := range c.info.Ports { + if info.PublicPort != uint16(0) && (port == 0 || info.PrivatePort == uint16(port)) { + return net.JoinHostPort(ip, strconv.Itoa(int(info.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(port int) string { + for _, info := range c.info.Ports { + if info.PublicPort != uint16(0) && (port == 0 || info.PrivatePort == uint16(port)) { + return net.JoinHostPort("localhost", strconv.Itoa(int(info.PublicPort))) + } + } + return "" +} + +func (c *wrapperContainer) Host() string { + return c.HostForPort(0) +} + +func (c *wrapperContainer) HostForPort(port int) string { + if runtime.GOOS == "linux" { + return c.privateHost(port) + } + // We can use `exposedHost()` in all platforms when we can use host + // network in the metricbeat container + return c.exposedHost(port) } func (d *wrapperDriver) LockFile() string { return d.Files[0] + ".lock" } +func (d *wrapperDriver) Close() error { + return errors.Wrap(d.client.Close(), "failed to close wrapper driver") +} + 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,6 +160,9 @@ 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 } @@ -95,7 +183,69 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) args = append(args, service) } - return d.cmd(ctx, "up", args...).Run() + err := d.cmd(ctx, "up", args...).Run() + if err != nil { + return err + } + if opts.SetupAdvertisedHostEnvFile { + return d.setupAdvertisedHost(ctx, service) + } + return nil +} + +func writeToContainer(ctx context.Context, cli *client.Client, 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") + } + + 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 +} + +// setupAdvertisedHost adds a file to a container with its address, this can +// be used in services that need to configure an address to be advertised to +// clients. +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, d.client, c.ID, "/run/compose_env", content) + if err != nil { + return err + } + } + return nil } func (d *wrapperDriver) Kill(ctx context.Context, signal string, service string) error { @@ -139,11 +289,6 @@ func (d *wrapperDriver) Containers(ctx context.Context, projectFilter Filter, fi } func (d *wrapperDriver) containers(ctx context.Context, projectFilter Filter, filter ...string) ([]types.Container, error) { - cli, err := client.NewEnvClient() - if err != nil { - return nil, errors.Wrap(err, "failed to start docker client") - } - var serviceFilters []filters.Args if len(filter) == 0 { f := makeFilter(d.Name, "", projectFilter) @@ -157,7 +302,7 @@ func (d *wrapperDriver) containers(ctx context.Context, projectFilter Filter, fi var containers []types.Container for _, f := range serviceFilters { - c, err := cli.ContainerList(ctx, types.ContainerListOptions{ + c, err := d.client.ContainerList(ctx, types.ContainerListOptions{ All: true, Filters: f, }) diff --git a/libbeat/tests/system/beat/compose.py b/libbeat/tests/system/beat/compose.py index b32880639154..17fd41a33f21 100644 --- a/libbeat/tests/system/beat/compose.py +++ b/libbeat/tests/system/beat/compose.py @@ -1,15 +1,13 @@ from __future__ import absolute_import import os import sys +import tarfile import time +import StringIO INTEGRATION_TESTS = os.environ.get('INTEGRATION_TESTS', False) -# TODO: To be removed when env files are removed from docker compose -OVERRIDE_HOST = os.environ.get('OVERRIDE_HOST', False) - - if INTEGRATION_TESTS: from compose.cli.command import get_project from compose.service import BuildAction @@ -30,6 +28,9 @@ class ComposeMixin(object): # timeout waiting for health (seconds) COMPOSE_TIMEOUT = 300 + # add advertised host environment file + COMPOSE_ADVERTISED_HOST = False + @classmethod def compose_up(cls): """ @@ -77,6 +78,10 @@ def is_healthy(container): if healthy: break + if cls.COMPOSE_ADVERTISED_HOST: + for service in cls.COMPOSE_SERVICES: + cls._setup_advertised_host(project, service) + time.sleep(1) timeout = time.time() - start > cls.COMPOSE_TIMEOUT if timeout: @@ -88,6 +93,30 @@ def is_healthy(container): "docker-compose services: %s" % ','.join(cls.COMPOSE_SERVICES)) + @classmethod + def _setup_advertised_host(cls, project, service): + """ + There are services like kafka that announce an advertised address + to clients, who should reconnect to this address. This method + sends the proper address to use to the container by adding a + environment file with the SERVICE_HOST variable set to this value. + """ + host = cls.compose_host(service=service) + + content = "SERVICE_HOST=%s" % host + info = tarfile.TarInfo(name="/run/compose_env") + info.mode = 0100644 + info.size = len(content) + + data = StringIO.StringIO() + tar = tarfile.TarFile(fileobj=data, mode='w') + tar.addfile(info, StringIO.StringIO(content)) + tar.close() + + containers = project.containers(service_names=[service]) + for container in containers: + container.client.put_archive(container=container.id, path="/", data=data.getvalue()) + @classmethod def compose_down(cls): """ @@ -135,11 +164,9 @@ def compose_host(cls, service=None, port=None): if service is None: service = cls.COMPOSE_SERVICES[0] - # TODO: Remove condition when env files are removed from docker compose - if OVERRIDE_HOST: - host_env = os.environ.get(service.upper() + "_HOST") - if host_env: - return host_env + host_env = os.environ.get(service.upper() + "_HOST") + if host_env: + return host_env container = cls.compose_project().containers(service_names=[service])[0] info = container.inspect() diff --git a/metricbeat/docker-compose.yml b/metricbeat/docker-compose.yml index c61291b387b6..ffdf435729fa 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: @@ -60,7 +27,8 @@ services: apache_2_4_12: build: context: ./module/apache/_meta - dockerfile: Dockerfile.2.4.12 + args: + APACHE_VERSION: 2.4.12 ports: - 80 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..1e0d1cdcd06c 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()) + service := compose.EnsureUp(t, "aerospike") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host())) if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "aerospike") + service := compose.EnsureUp(t, "aerospike") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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/status/status_integration_test.go b/metricbeat/module/apache/status/status_integration_test.go index 96a600831460..ffb563135085 100644 --- a/metricbeat/module/apache/status/status_integration_test.go +++ b/metricbeat/module/apache/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/apache" - - "github.com/stretchr/testify/assert" ) func TestFetch(t *testing.T) { - compose.EnsureUp(t, "apache") + service := compose.EnsureUp(t, "apache") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host())) events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) @@ -48,10 +47,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..94280103763d 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") + service := compose.EnsureUp(t, "metricbeat") for _, metricSet := range metricSets { - f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, service.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") + service := compose.EnsureUp(t, "metricbeat") for _, metricSet := range metricSets { - f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet)) + f := mbtest.NewReportingMetricSetV2Error(t, beat.GetConfig(metricSet, service.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..7f3fa8116083 100644 --- a/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go +++ b/metricbeat/module/ceph/cluster_disk/cluster_disk_integration_test.go @@ -15,53 +15,31 @@ // specific language governing permissions and limitations // under the License. -// +build integration +// +build integration,linux 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..a02999f92ee1 100644 --- a/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go +++ b/metricbeat/module/ceph/cluster_health/cluster_health_integration_test.go @@ -15,51 +15,31 @@ // specific language governing permissions and limitations // under the License. +// +build integration,linux + 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..5948dc4366e2 100644 --- a/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go +++ b/metricbeat/module/ceph/cluster_status/cluster_status_integration_test.go @@ -15,51 +15,31 @@ // specific language governing permissions and limitations // under the License. +// +build integration,linux + 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..7c828d0bd0c4 100644 --- a/metricbeat/module/ceph/osd_df/osd_df_integration_test.go +++ b/metricbeat/module/ceph/osd_df/osd_df_integration_test.go @@ -15,51 +15,31 @@ // specific language governing permissions and limitations // under the License. +// +build integration,linux + 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..07bb5ab4fbdf 100644 --- a/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go +++ b/metricbeat/module/ceph/osd_tree/osd_tree_integration_test.go @@ -15,51 +15,31 @@ // specific language governing permissions and limitations // under the License. +// +build integration,linux + 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..b6a9d9ed3f86 100644 --- a/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go +++ b/metricbeat/module/ceph/pool_disk/pool_disk_integration_test.go @@ -15,51 +15,31 @@ // specific language governing permissions and limitations // under the License. +// +build integration,linux + 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()) + service := compose.EnsureUpWithTimeout(t, 120, "ceph") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..43c4358df903 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") + service := compose.EnsureUp(t, "consul") - f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"})) + f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"}, service.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..5dfc8a32514c 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) + service := compose.EnsureUp(t, "consul") + f := mbtest.NewReportingMetricSetV2Error(t, consul.GetConfig([]string{"agent"}, service.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..b00fdad93998 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") + service := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..0878b3130c3a 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") + service := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..f84af0dccbf2 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") + service := compose.EnsureUp(t, "couchbase") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..08f443d78027 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") + service := compose.EnsureUp(t, "couchdb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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) + service := compose.EnsureUp(t, "couchdb") + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..fc09de69354d 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") + service := compose.EnsureUp(t, "dropwizard") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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 f70674385c45..641dcb894586 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") + service := compose.EnsureUpWithTimeout(t, 300, "elasticsearch") - host := net.JoinHostPort(getEnvHost(), getEnvPort()) + host := service.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") + service := compose.EnsureUpWithTimeout(t, 300, "elasticsearch") - host := net.JoinHostPort(getEnvHost(), getEnvPort()) + host := service.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, } } @@ -305,7 +283,7 @@ func createCCRStats(host string) error { return checkCCRStatsExists(host) } - exists, err := waitForSuccess(checkCCRStats, 300, 5) + exists, err := waitForSuccess(checkCCRStats, 500*time.Millisecond, 10) if err != nil { return errors.Wrap(err, "error checking if CCR stats exist") } @@ -451,7 +429,7 @@ func httpPutJSON(host, path string, body []byte) ([]byte, *http.Response, error) type checkSuccessFunction func() (bool, error) -func waitForSuccess(f checkSuccessFunction, retryIntervalMs time.Duration, numAttempts int) (bool, error) { +func waitForSuccess(f checkSuccessFunction, retryInterval time.Duration, numAttempts int) (bool, error) { for numAttempts > 0 { success, err := f() if err != nil { @@ -462,7 +440,7 @@ func waitForSuccess(f checkSuccessFunction, retryIntervalMs time.Duration, numAt return success, nil } - time.Sleep(retryIntervalMs * time.Millisecond) + time.Sleep(retryInterval) numAttempts-- } 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..b7d8a235a8e6 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") + service := compose.EnsureUp(t, "envoyproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "envoyproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..cc6aaf29dee4 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") + service := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..ff323c59b695 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") + service := compose.EnsureUp(t, "etcd") - m := mbtest.NewFetcher(t, getConfig()) + m := mbtest.NewFetcher(t, getConfig(service.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") + service := compose.EnsureUp(t, "etcd") - m := mbtest.NewFetcher(t, getConfig()) + m := mbtest.NewFetcher(t, getConfig(service.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..d83057859061 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") + service := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "etcd") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..20e954031289 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") + service := compose.EnsureUp(t, "etcd") - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(service.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") + service := compose.EnsureUp(t, "etcd") - f := mbtest.NewFetcher(t, getConfig()) + f := mbtest.NewFetcher(t, getConfig(service.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..a16b97fab42b 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") + service := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..11fcff59ec1d 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") + service := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "golang") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..209f3051f7db 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") + service := compose.EnsureUp(t, "haproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.HostForPort(14567))) 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") + service := compose.EnsureUp(t, "haproxy") - config := getConfig() + config := getConfig(service.HostForPort(14567)) 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..f50cd7b6f6c9 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") + service := compose.EnsureUp(t, "haproxy") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.HostForPort(14567))) 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") + service := compose.EnsureUp(t, "haproxy") - config := getConfig() + config := getConfig(service.HostForPort(14567)) 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..4f0645001e2d 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") + service := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("object")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("array")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("object")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..3fe9c31f3d75 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") + service := compose.EnsureUp(t, "jolokia") - for _, config := range getConfigs() { + for _, config := range getConfigs(service.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() { + service := compose.EnsureUp(t, "jolokia") + + for _, config := range getConfigs(service.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..68df3e8a4c6a 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 "SERVICE_HOST needed, will wait for it on /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..281e822ee73a 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,18 @@ const ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "kafka") + service := compose.EnsureUp(t, "kafka", + compose.UpWithTimeout(120*time.Second), + compose.UpWithAdvertisedHostEnvFile, + ) - c, err := startConsumer(t, "metricbeat-test") + c, err := startConsumer(t, service.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(service.Host())) for retries := 0; retries < 3; retries++ { err = mbtest.WriteEventsReporterV2Error(ms, t, "") if err == nil { @@ -65,15 +63,15 @@ func TestData(t *testing.T) { } func TestFetch(t *testing.T) { - compose.EnsureUp(t, "kafka") + service := compose.EnsureUp(t, "kafka") - c, err := startConsumer(t, "metricbeat-test") + c, err := startConsumer(t, service.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(service.Host())) var data []mb.Event var errors []error @@ -92,8 +90,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 +100,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..9045b3611048 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,14 @@ const ( ) func TestData(t *testing.T) { - compose.EnsureUp(t, "kafka") + service := compose.EnsureUp(t, "kafka", + compose.UpWithTimeout(120*time.Second), + compose.UpWithAdvertisedHostEnvFile, + ) - generateKafkaData(t, "metricbeat-generate-data") + generateKafkaData(t, service.Host(), "metricbeat-generate-data") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig("")) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host(), "")) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) @@ -59,8 +58,7 @@ func TestData(t *testing.T) { } func TestTopic(t *testing.T) { - - compose.EnsureUp(t, "kafka") + service := compose.EnsureUp(t, "kafka") logp.TestingSetup(logp.WithSelectors("kafka")) @@ -68,9 +66,9 @@ func TestTopic(t *testing.T) { testTopic := fmt.Sprintf("test-metricbeat-%s", id) // Create initial topic - generateKafkaData(t, testTopic) + generateKafkaData(t, service.Host(), testTopic) - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(testTopic)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host(), testTopic)) dataBefore, err := mbtest.ReportingFetchV2Error(f) if err != nil { t.Fatal("write", err) @@ -84,7 +82,7 @@ func TestTopic(t *testing.T) { var i int64 = 0 // Create n messages for ; i < n; i++ { - generateKafkaData(t, testTopic) + generateKafkaData(t, service.Host(), testTopic) } dataAfter, err := mbtest.ReportingFetchV2Error(f) @@ -123,7 +121,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 +134,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 +162,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 +171,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..088bbc71bf98 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") + service := compose.EnsureUpWithTimeout(t, 570, "kibana") - config := mtest.GetConfig("stats") + config := mtest.GetConfig("stats", service.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") + service := compose.EnsureUp(t, "kibana") - config := mtest.GetConfig("stats") + config := mtest.GetConfig("stats", service.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..d8a2be20c2ac 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") + service := compose.EnsureUpWithTimeout(t, 570, "kibana") - f := mbtest.NewReportingMetricSetV2Error(t, mtest.GetConfig("status")) + f := mbtest.NewReportingMetricSetV2Error(t, mtest.GetConfig("status", service.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..15eb637faf6d 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") + service := 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, service.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") + service := 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, service.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..cc3e21e028aa 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") + service := compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..2ef510797dca 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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..6eb2b47d7f0f 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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..afb1b082b3ce 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") + service := compose.EnsureUp(t, "mongodb") - err := initiateReplicaSet(t) + err := initiateReplicaSet(t, service.Host()) if !assert.NoError(t, err) { t.FailNow() } - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..c13f47073ab1 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") + service := compose.EnsureUp(t, "mongodb") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "mongodb") - config := getConfig() + config := getConfig(service.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..72fe1bf38b69 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") + service := compose.EnsureUp(t, "munin") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "munin") - config := getConfig() + config := getConfig(service.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..ff97f2d4ed8a 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") + service := compose.EnsureUp(t, "mysql") - db, err := NewDB(GetMySQLEnvDSN()) + db, err := NewDB(GetMySQLEnvDSN(service.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..687d70337a88 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") + service := compose.EnsureUp(t, "mysql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(false)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "mysql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig(true)) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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)) + service := compose.EnsureUp(t, "mysql") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..d5ea5c8dc122 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") + service := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..306ee96a344c 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") + service := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..87585d8a17f0 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") + service := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..3b67d32a3ecc 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") + service := compose.EnsureUp(t, "nats") - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "nats") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..f7cd2fe3d5a5 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") + service := compose.EnsureUp(t, "nginx") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(service.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) + service := compose.EnsureUp(t, "nginx") + f := mbtest.NewReportingMetricSetV2(t, getConfig(service.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/nginx/testing.go b/metricbeat/module/nginx/testing.go deleted file mode 100644 index cee26e1d11a5..000000000000 --- a/metricbeat/module/nginx/testing.go +++ /dev/null @@ -1,35 +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 nginx - -/* -Helper functions for testing used in the nginx metricsets -*/ - -import ( - "os" -) - -func GetNginxEnvHost() string { - host := os.Getenv("NGINX_HOST") - - if len(host) == 0 { - host = "127.0.0.1" - } - return 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..caeaaa823a6d 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") + service := compose.EnsureUp(t, "phpfpm") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..6fed9acf0cf4 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") + service := compose.EnsureUp(t, "phpfpm") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..ab7c48d4fbd6 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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..d4d5c290e799 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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..9593ec2683c3 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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..3a707114cdc7 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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "postgresql") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..3a0760a67723 100644 --- a/metricbeat/module/rabbitmq/node/node_integration_test.go +++ b/metricbeat/module/rabbitmq/node/node_integration_test.go @@ -30,7 +30,10 @@ import ( ) func TestData(t *testing.T) { - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + t.Skip("Skipping test as it was not stable. Probably first event is empty.") + service := compose.EnsureUpWithTimeout(t, 120, "rabbitmq") + + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host())) err := mbtest.WriteEventsReporterV2Error(ms, t, "") if err != nil { t.Fatal("write", err) @@ -39,11 +42,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") + service := compose.EnsureUpWithTimeout(t, 120, "rabbitmq") reporter := &mbtest.CapturingReporterV2{} - metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + metricSet := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.Host())) err := metricSet.Fetch(reporter) assert.NoError(t, err) @@ -51,8 +54,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..576103c75d31 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") + service := compose.EnsureUp(t, "redis") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "redis") - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..2a804d9a63b6 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") + service := compose.EnsureUp(t, "redis") - addEntry(t, "foo", 1) + addEntry(t, service.Host(), "foo", 1) - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(service.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") + service := compose.EnsureUp(t, "redis") - addEntry(t, "foo", 1) + addEntry(t, service.Host(), "foo", 1) - ms := mbtest.NewFetcher(t, getConfig()) + ms := mbtest.NewFetcher(t, getConfig(service.Host())) ms.WriteEvents(t, "") } func TestFetchMultipleKeyspaces(t *testing.T) { - compose.EnsureUp(t, "redis") + service := 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, service.Host(), name, keyspace) } - config := getConfig() + config := getConfig(service.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..e0b51c25dde7 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") + service := compose.EnsureUp(t, "redis") - addEntry(t) + addEntry(t, service.Host()) // Fetch data - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "redis") - addEntry(t) + addEntry(t, service.Host()) - ms := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..7d972030bb37 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") + service := compose.EnsureUp(t, "redis") + host := service.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..c33df1556f0d 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") + service := compose.EnsureUp(t, "redis") + host := service.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") + service := compose.EnsureUp(t, "redis") + host := service.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") + service := compose.EnsureUp(t, "redis") + host := service.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..cb97424ce293 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") + service := compose.EnsureUp(t, "traefik") - makeBadRequest(config) + makeBadRequest(service.Host()) + config := mtest.GetConfig("health", service.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..97b869f4a3e5 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") + service := compose.EnsureUp(t, "uwsgi_tcp") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("tcp")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("tcp", service.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")) + service := compose.EnsureUp(t, "uwsgi_http") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http", service.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") + service := compose.EnsureUp(t, "uwsgi_http") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http")) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig("http", service.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..257268f26b18 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()) + service := compose.EnsureUp(t, "zookeeper") + + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..7e530c802ce4 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") + service := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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") + service := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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..79789653121d 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") + service := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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) { + service := compose.EnsureUp(t, "zookeeper") - f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig(service.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/metricbeat/tests/system/test_kafka.py b/metricbeat/tests/system/test_kafka.py index 49c483ab56a0..f5f88201fc01 100644 --- a/metricbeat/tests/system/test_kafka.py +++ b/metricbeat/tests/system/test_kafka.py @@ -7,6 +7,8 @@ class KafkaTest(metricbeat.BaseTest): COMPOSE_SERVICES = ['kafka'] + COMPOSE_ADVERTISED_HOST = True + VERSION = "2.0.0" PRODUCER_USERNAME = "producer" 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..ddb034000225 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") + service := compose.EnsureUp(t, "cockroachdb") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(service.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..74e2e74d9f72 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") + service := compose.EnsureUp(t, "coredns") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) + f := mbtest.NewReportingMetricSetV2(t, getConfig(service.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..7375671f7ac5 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") + service := compose.EnsureUp(t, "mssql") - f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig("performance")) + f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig(service.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..baecb3ff6785 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") + service := compose.EnsureUp(t, "mssql") - f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig("transaction_log")) + f := mbtest.NewReportingMetricSetV2(t, mtest.GetConfig(service.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")