Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem in stats watcher #221

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions ccx_messaging/watchers/stats_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

import logging
import os
import tarfile
import time

from insights.core.archives import TarExtractor, ZipExtractor
from insights_messaging.watchers import EngineWatcher
from prometheus_client import Counter, Histogram, start_http_server, REGISTRY

Expand Down Expand Up @@ -136,9 +138,29 @@ def on_filter(self):
LOG.debug("Receiving 'on_filter' callback")
self._filtered_total.inc()

def on_extract(self, ctx, broker, extraction):
"""On extract event handler."""
def on_extract(
self, ctx, broker, extraction: tarfile.TarFile | TarExtractor | ZipExtractor
) -> None:
"""On extract event handler for extractor using engines."""
LOG.debug("Receiving 'on_extract' callback")

if isinstance(extraction, tarfile.TarFile):
self.on_extract_with_tarfile(extraction)

else:
self.on_extract_with_extractor(extraction)

def on_extract_with_tarfile(self, extraction: tarfile.TarFile) -> None:
"""On extract event handler for engines using tarfile."""
tarfile_contents = extraction.getnames()

if "openshift_lightspeed.json" in tarfile_contents:
self._archive_metadata["type"] = "ols"
elif os.path.join("config", "infrastructure.json") in tarfile_contents:
self._archive_metadata["type"] = "hypershift"

def on_extract_with_extractor(self, extraction: TarExtractor | ZipExtractor) -> None:
"""On extract event handler for engines using extractor."""
# Set archive_type label based on found file
if os.path.exists(os.path.join(extraction.tmp_dir, "openshift_lightspeed.json")):
self._archive_metadata["type"] = "ols"
Expand Down
50 changes: 48 additions & 2 deletions test/watchers/stats_watcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

"""Module containing unit tests for the `ConsumerWatcher` class."""

from unittest.mock import patch
import time
from tarfile import TarFile
from unittest.mock import MagicMock, patch

import pytest
import time
from insights.core.archives import TarExtractor

from ccx_messaging.watchers.stats_watcher import (
StatsWatcher,
Expand Down Expand Up @@ -340,3 +342,47 @@ def test_reset_archive_metadata(label_value):
w._reset_archive_metadata()

assert w._archive_metadata["type"] == "ocp"


ON_EXTRACT_WITH_EXTRACTOR_IDENTIFIERS = [
("ols", [True, False]),
("hypershift", [False, True]),
("ocp", [False, False]),
]


@patch("ccx_messaging.watchers.stats_watcher.start_http_server", lambda *args: None)
@pytest.mark.parametrize("type_, exists_side_effect", ON_EXTRACT_WITH_EXTRACTOR_IDENTIFIERS)
def test_on_extract_with_extractor(type_, exists_side_effect):
"""Test the method on_extract."""
# construct watcher object
w = StatsWatcher(prometheus_port=8009)

extraction_mock = MagicMock(spec=TarExtractor)
extraction_mock.tmp_dir = ""

with patch("os.path.exists") as exists_mock:
exists_mock.side_effect = exists_side_effect

w.on_extract(None, None, extraction_mock)
assert w._archive_metadata["type"] == type_


ON_EXTRACT_WITH_TARFILE_IDENTIFIERS = [
("ols", ["openshift_lightspeed.json"]),
("hypershift", ["config/id", "config/infrastructure.json"]),
("ocp", ["config/id"]),
]


@patch("ccx_messaging.watchers.stats_watcher.start_http_server", lambda *args: None)
@pytest.mark.parametrize("type_, file_content", ON_EXTRACT_WITH_TARFILE_IDENTIFIERS)
def test_on_extract_with_tarfile(type_, file_content):
"""Test the method on_extract."""
# construct watcher object
w = StatsWatcher(prometheus_port=8009)

extraction_mock = MagicMock(spec=TarFile)
extraction_mock.getnames.return_value = file_content
w.on_extract(None, None, extraction_mock)
assert w._archive_metadata["type"] == type_
Loading