Skip to content

Commit

Permalink
Run Python tests in libbeat (#28438) (#28485)
Browse files Browse the repository at this point in the history
(cherry picked from commit ce29bea)

Co-authored-by: Noémi Ványi <[email protected]>
  • Loading branch information
mergify[bot] and kvch authored Oct 18, 2021
1 parent acfdf97 commit 49d34f9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 161 deletions.
3 changes: 3 additions & 0 deletions libbeat/Jenkinsfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ stages:
goIntegTest:
mage: "mage goIntegTest"
stage: mandatory
pythonIntegTest:
mage: "mage pythonIntegTest"
stage: mandatory
crosscompile:
make: "make -C libbeat crosscompile"
stage: mandatory
Expand Down
1 change: 1 addition & 0 deletions libbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
func init() {
unittest.RegisterPythonTestDeps(Fields)
integtest.RegisterGoTestDeps(Fields)
integtest.RegisterPythonTestDeps(Fields)
}

// Build builds the Beat binary.
Expand Down
Binary file modified libbeat/tests/files/testbeat-dashboards.zip
Binary file not shown.
177 changes: 16 additions & 161 deletions libbeat/tests/system/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,6 @@ def test_load_only_index_patterns(self):

assert self.log_contains("Kibana dashboards successfully loaded") is True

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_export_dashboard_cmd_export_dashboard_by_id_and_decoding(self):
"""
Test testbeat export dashboard can export dashboards
and removes unsupported characters
"""
self.render_config_template()
self.test_load_dashboard()
beat = self.start_beat(
logging_args=["-e", "-d", "*"],
extra_args=["export",
"dashboard",
"-E", "setup.kibana.protocol=http",
"-E", "setup.kibana.host=" + self.get_kibana_host(),
"-E", "setup.kibana.port=" + self.get_kibana_port(),
"-decode",
"-id", "Metricbeat-system-overview"]
)

beat.check_wait(exit_code=0)

assert self.log_contains("\"id\": \"Metricbeat-system-overview\",") is True

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_export_dashboard_cmd_export_dashboard_by_id(self):
Expand All @@ -165,12 +141,12 @@ def test_export_dashboard_cmd_export_dashboard_by_id(self):
"-E", "setup.kibana.protocol=http",
"-E", "setup.kibana.host=" + self.get_kibana_host(),
"-E", "setup.kibana.port=" + self.get_kibana_port(),
"-id", "Metricbeat-system-overview"]
"-id", "Metricbeat-system-overview",
"-folder", "system-overview"]
)

beat.check_wait(exit_code=0)

assert self.log_contains("\"id\": \"Metricbeat-system-overview\",") is True
self._check_if_dashboard_exported("system-overview")

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
Expand All @@ -186,69 +162,15 @@ def test_export_dashboard_cmd_export_dashboard_by_id_unknown_id(self):
"-E", "setup.kibana.protocol=http",
"-E", "setup.kibana.host=" + self.get_kibana_host(),
"-E", "setup.kibana.port=" + self.get_kibana_port(),
"-id", "No-such-dashboard"]
"-id", "No-such-dashboard",
"-folder", "system-overview"]
)

beat.check_wait(exit_code=1)

expected_error = re.compile("error exporting dashboard:.*not found", re.IGNORECASE)
assert self.log_contains(expected_error)

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_export_dashboard_cmd_export_dashboard_from_yml(self):
"""
Test testbeat export dashboard can export dashboards from dashboards YAML file
and removes unsupported characters
"""

self.render_config_template()
self.test_load_dashboard()
beat = self.start_beat(
logging_args=["-e", "-d", "*"],
extra_args=["export",
"dashboard",
"-E", "setup.kibana.protocol=http",
"-E", "setup.kibana.host=" + self.get_kibana_host(),
"-E", "setup.kibana.port=" + self.get_kibana_port(),
"-yml", os.path.join(self.beat_path, "tests", "files", "dashboards.yml")]
)

beat.check_wait(exit_code=0)

version = self.get_version()
kibana_semver = semver.VersionInfo.parse(version)
exported_dashboard_path = os.path.join(self.beat_path, "tests", "files", "_meta",
"kibana", str(kibana_semver.major), "dashboard", "Metricbeat-system-test-overview.ndjson")

with open(exported_dashboard_path) as f:
content = f.read()
assert "Metricbeat-system-overview" in content

os.remove(exported_dashboard_path)

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_export_dashboard_cmd_export_dashboard_from_not_existent_yml(self):
"""
Test testbeat export dashboard fails gracefully when cannot find YAML file
"""

self.render_config_template()
beat = self.start_beat(
logging_args=["-e", "-d", "*"],
extra_args=["export",
"dashboard",
"-E", "setup.kibana.protocol=http",
"-E", "setup.kibana.host=" + self.get_kibana_host(),
"-E", "setup.kibana.port=" + self.get_kibana_port(),
"-yml", os.path.join(self.beat_path, "tests", "files", "no-such-file.yml")]
)

beat.check_wait(exit_code=1)
assert self.log_contains("Error exporting dashboards from yml")
assert self.log_contains("error opening the list of dashboards")

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_dev_tool_export_dashboard_by_id(self):
Expand All @@ -258,60 +180,17 @@ def test_dev_tool_export_dashboard_by_id(self):

self.test_load_dashboard()

path = os.path.normpath(self.beat_path + "/../dev-tools/cmd/dashboards/export_dashboards.go")
command = path + " -kibana http://" + self.get_kibana_host() + ":" + self.get_kibana_port()
command = "go run " + command + " -dashboard Metricbeat-system-overview"

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
content, err = p.communicate()

assert p.returncode == 0

assert os.path.isfile("output.ndjson") is True

with open('output.ndjson') as f:
content = f.read()
assert "Metricbeat-system-overview" in content

os.remove("output.ndjson")

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_dev_tool_export_dashboard_by_id_to_folder(self):
"""
Test dev-tools/cmd/dashboards exports dashboard and removes unsupported characters
and separates each asset into a file under the appropriate folder
"""

self.test_load_dashboard()

folder_name = "my-system"
folder_name = "system-overview"
path = os.path.normpath(self.beat_path + "/../dev-tools/cmd/dashboards/export_dashboards.go")
command = path + " -kibana http://" + self.get_kibana_host() + ":" + self.get_kibana_port()
command = "go run " + command + " -dashboard Metricbeat-system-overview -folder " + folder_name

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
content, err = p.communicate()
print(content, err)

assert p.returncode == 0

assert os.path.isfile("output.ndjson") is False
assert os.path.isdir(folder_name) is True

kibana_semver = semver.VersionInfo.parse(self.get_version())
assets_root = os.path.join(folder_name, "_meta", "kibana", str(kibana_semver.major))
assert os.path.isdir(assets_root) is True
assert os.path.isdir(os.path.join(assets_root, "dashboard")) is True
assert os.path.isdir(os.path.join(assets_root, "visualization")) is True

with open(os.path.join(assets_root, "dashboard", "Metricbeat-system-overview.json")) as dashboard_file:
dashboard = json.load(dashboard_file)
for reference in dashboard["references"]:
reference_path = os.path.join(assets_root, reference["type"], reference["id"]+".json")
assert os.path.isfile(reference_path)

shutil.rmtree(folder_name)
self._check_if_dashboard_exported(folder_name)

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
Expand Down Expand Up @@ -343,52 +222,28 @@ def test_dev_tool_export_dashboard_by_id_from_space(self):

self.test_load_dashboard_into_space(False)

folder_name = "system-overview"
path = os.path.normpath(self.beat_path + "/../dev-tools/cmd/dashboards/export_dashboards.go")
command = path + " -kibana http://" + self.get_kibana_host() + ":" + self.get_kibana_port()
command = "go run " + command + " -dashboard Metricbeat-system-overview -space-id foo-bar"
command = "go run " + command + " -dashboard Metricbeat-system-overview -space-id foo-bar -folder " + folder_name

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
content, err = p.communicate()

assert p.returncode == 0

assert os.path.isfile("output.ndjson") is True

with open('output.ndjson') as f:
content = f.read()
assert "Metricbeat-system-overview" in content

os.remove("output.ndjson")

@unittest.skipUnless(INTEGRATION_TESTS, "integration test")
@pytest.mark.tag('integration')
def test_dev_tool_export_dashboard_from_yml(self):
"""
Test dev-tools/cmd/dashboards exports dashboard from dashboards YAML file
and removes unsupported characters
"""

self.test_load_dashboard()

path = os.path.normpath(self.beat_path + "/../dev-tools/cmd/dashboards/export_dashboards.go")
command = path + " -kibana http://" + self.get_kibana_host() + ":" + self.get_kibana_port()
command = "go run " + command + " -yml " + os.path.join(self.beat_path, "tests", "files", "dashboards.yml")

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
content, err = p.communicate()

assert p.returncode == 0
self._check_if_dashboard_exported(folder_name)

version = self.get_version()
kibana_semver = semver.VersionInfo.parse(version)
exported_dashboard_path = os.path.join(self.beat_path, "tests", "files", "_meta",
"kibana", str(kibana_semver.major), "dashboard", "Metricbeat-system-test-overview.ndjson")
def _check_if_dashboard_exported(self, folder_name):
kibana_semver = semver.VersionInfo.parse(self.get_version())
dashboard_folder = os.path.join(folder_name, "_meta", "kibana", str(kibana_semver.major), "dashboard")
assert os.path.isdir(dashboard_folder)

with open(exported_dashboard_path) as f:
with open(os.path.join(dashboard_folder, "Metricbeat-system-overview.json")) as f:
content = f.read()
assert "Metricbeat-system-overview" in content

os.remove(exported_dashboard_path)
shutil.rmtree(folder_name)

def get_host(self):
return os.getenv('ES_HOST', 'localhost') + ':' + os.getenv('ES_PORT', '9200')
Expand Down

0 comments on commit 49d34f9

Please sign in to comment.