Skip to content

Commit

Permalink
Read build args for metricbeat system tests from file (#14520)
Browse files Browse the repository at this point in the history
A new file is being added to add information about the supported
versions of a service, use this file to obtain the build args for
docker images needed by metricbeat system tests.

Python tests can now be decorated using the parameterized library
instead of manually copying them.

MySQL test files and docker compose definition has been moved
to its own directory.
  • Loading branch information
jsoriano authored Nov 19, 2019
1 parent 1c9f7bb commit 9913bbf
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Strip debug symbols from binaries to reduce binary sizes. {issue}12768[12768]
- Compare event by event in `testadata` framework to avoid sorting problems {pull}13747[13747]
- Added a `default_field` option to fields in fields.yml to offer a way to exclude fields from the default_field list. {issue}14262[14262] {pull}14341[14341]
- `supported-versions.yml` can be used in metricbeat python system tests to obtain the build args for docker compose builds. {pull}14520[14520]
2 changes: 1 addition & 1 deletion libbeat/tests/system/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ termcolor==1.1.0
texttable==0.9.1
urllib3==1.24.2
websocket-client==0.47.0
parameterized==0.6.1
parameterized==0.7.0
jsondiff==1.1.2
semver==2.8.1
9 changes: 0 additions & 9 deletions metricbeat/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,6 @@ services:
ports:
- 4949

mysql:
image: docker.elastic.co/observability-ci/beats-integration-mysql:${MYSQL_VARIANT:-mysql}-${MYSQL_VERSION:-5.7.12}-1
build:
context: ./module/mysql/_meta
args:
MYSQL_IMAGE: ${MYSQL_VARIANT:-mysql}:${MYSQL_VERSION:-5.7.12}
ports:
- 3306

nats:
image: docker.elastic.co/observability-ci/beats-integration-nats:${NATS_VERSION:-2.0.4}-1
build:
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/module/apache/_meta/supported-versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variants:
- APACHE_VERSION: 2.4.12
- APACHE_VERSION: 2.4.20
34 changes: 18 additions & 16 deletions metricbeat/module/apache/test_apache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from nose.plugins.attrib import attr
import urllib2
import time
import semver
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '../../tests/system'))
Expand Down Expand Up @@ -30,6 +31,7 @@
]


@metricbeat.parameterized_with_supported_versions
class ApacheStatusTest(metricbeat.BaseTest):

COMPOSE_SERVICES = ['apache']
Expand Down Expand Up @@ -74,22 +76,22 @@ def test_output(self):
def verify_fields(self, evt):
self.assertItemsEqual(self.de_dot(APACHE_FIELDS), evt.keys())
apache_status = evt["apache"]["status"]
self.assertItemsEqual(
self.de_dot(APACHE_STATUS_FIELDS), apache_status.keys())
self.assertItemsEqual(
self.de_dot(CPU_FIELDS), apache_status["cpu"].keys())
# There are more fields that could be checked.
if self.old_apache_version():
self.assertItemsEqual(
self.de_dot(APACHE_OLD_STATUS_FIELDS), apache_status.keys())
else:
self.assertItemsEqual(
self.de_dot(APACHE_STATUS_FIELDS), apache_status.keys())
self.assertItemsEqual(
self.de_dot(CPU_FIELDS), apache_status["cpu"].keys())
# There are more fields that could be checked.

def old_apache_version(self):
if not 'APACHE_VERSION' in self.COMPOSE_ENV:
return False

version = self.COMPOSE_ENV['APACHE_VERSION']
return semver.compare(version, '2.4.12') <= 0

def get_hosts(self):
return ['http://' + self.compose_host()]


class ApacheOldStatusTest(ApacheStatusTest):

COMPOSE_ENV = {'APACHE_VERSION': '2.4.12'}

def verify_fields(self, evt):
self.assertItemsEqual(self.de_dot(APACHE_FIELDS), evt.keys())
apache_status = evt["apache"]["status"]
self.assertItemsEqual(
self.de_dot(APACHE_OLD_STATUS_FIELDS), apache_status.keys())
15 changes: 15 additions & 0 deletions metricbeat/module/mysql/_meta/supported-versions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variants:
- MYSQL_VARIANT: mysql
MYSQL_VERSION: 5.7.12
- MYSQL_VARIANT: mysql
MYSQL_VERSION: 8.0.13
- MYSQL_VARIANT: percona
MYSQL_VERSION: 5.7.24
- MYSQL_VARIANT: percona
MYSQL_VERSION: 8.0.13-4
- MYSQL_VARIANT: mariadb
MYSQL_VERSION: 10.2.23
- MYSQL_VARIANT: mariadb
MYSQL_VERSION: 10.3.14
- MYSQL_VARIANT: mariadb
MYSQL_VERSION: 10.4.4
11 changes: 11 additions & 0 deletions metricbeat/module/mysql/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2.3'

services:
mysql:
image: docker.elastic.co/observability-ci/beats-integration-mysql:${MYSQL_VARIANT:-mysql}-${MYSQL_VERSION:-5.7.12}-1
build:
context: ./_meta
args:
MYSQL_IMAGE: ${MYSQL_VARIANT:-mysql}:${MYSQL_VERSION:-5.7.12}
ports:
- 3306
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import os
import metricbeat
import sys
import unittest
from nose.plugins.attrib import attr

sys.path.append(os.path.join(os.path.dirname(__file__), '../../tests/system'))
import metricbeat

MYSQL_FIELDS = metricbeat.COMMON_FIELDS + ["mysql"]

MYSQL_STATUS_FIELDS = ["clients", "cluster", "cpu", "keyspace", "memory",
"persistence", "replication", "server", "stats"]


@metricbeat.parameterized_with_supported_versions
class Test(metricbeat.BaseTest):

COMPOSE_SERVICES = ['mysql']
Expand Down Expand Up @@ -44,45 +48,3 @@ def test_status(self):

def get_hosts(self):
return ['root:test@tcp({})/'.format(self.compose_host())]


class TestMysql80(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'mysql',
'MYSQL_VERSION': '8.0.13',
}


class TestPercona57(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'percona',
'MYSQL_VERSION': '5.7.24',
}


class TestPercona80(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'percona',
'MYSQL_VERSION': '8.0.13-4',
}


class TestMariadb102(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'mariadb',
'MYSQL_VERSION': '10.2.23',
}


class TestMariadb103(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'mariadb',
'MYSQL_VERSION': '10.3.14',
}


class TestMariadb104(Test):
COMPOSE_ENV = {
'MYSQL_VARIANT': 'mariadb',
'MYSQL_VERSION': '10.4.4',
}
34 changes: 34 additions & 0 deletions metricbeat/tests/system/metricbeat.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
import sys
import os
import yaml

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../libbeat/tests/system')))

from beat.beat import TestCase
from parameterized import parameterized_class

COMMON_FIELDS = ["@timestamp", "agent", "metricset.name", "metricset.host",
"metricset.module", "metricset.rtt", "host.name", "service.name", "event", "ecs"]
Expand Down Expand Up @@ -105,3 +107,35 @@ def check_metricset(self, module, metricset, hosts, fields=[], extras=[]):
self.assertItemsEqual(self.de_dot(fields), evt.keys())

self.assert_fields_are_documented(evt)


def supported_versions(path):
"""
Returns variants information as expected by parameterized_class,
that is as a list of lists with an only element that is used to
override the value of COMPOSE_ENV.
"""
if not os.path.exists(path):
# Return an empty variant so a class is instantiated with defaults
return [[{}]]

variants = []
with open(path) as f:
versions_info = yaml.safe_load(f)

for variant in versions_info['variants']:
variants += [[variant]]

return variants


def parameterized_with_supported_versions(base_class):
"""
Decorates a class so instead of the base class, multiple copies
of it are registered, one for each supported version.
"""
class_dir = os.path.abspath(os.path.dirname(sys.modules[base_class.__module__].__file__))
versions_path = os.path.join(class_dir, '_meta', 'supported-versions.yml')
variants = supported_versions(versions_path)
decorator = parameterized_class(['COMPOSE_ENV'], variants)
decorator(base_class)

0 comments on commit 9913bbf

Please sign in to comment.