Skip to content

Commit

Permalink
Make config unconditional and refine unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Deezzir committed Sep 26, 2024
1 parent b20eb23 commit 02851cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 40 deletions.
23 changes: 11 additions & 12 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class DCGMExporter(SnapExporter):

exporter_name: str = "dcgm"
port: int = 9400
metrics_location: Path = Path("/var/snap/dcgm/common/")
snap_common: Path = Path("/var/snap/dcgm/common/")
metric_config: str = "dcgm-exporter-metrics-file"

def __init__(self, charm_dir: Path, config: ConfigData):
Expand All @@ -466,17 +466,16 @@ def install(self) -> bool:
if not super().install():
return False

if self.snap_client.get(self.metric_config) != self.metric_config_value:
try:
logger.info(
"Creating a custom metrics file and configuring the DCGM snap to use it."
)
shutil.copy(self.metrics_file, self.metrics_location)
self.snap_client.set({self.metric_config: self.metric_config_value})
self.snap_client.restart(reload=True)
except Exception as err: # pylint: disable=broad-except
logger.error("Failed to configure custom DCGM metrics: %s", err)
return False
try:
logger.info(
"Creating a custom metrics file and configuring the DCGM snap to use it."
)
shutil.copy(self.metrics_file, self.snap_common)
self.snap_client.set({self.metric_config: self.metric_config_value})
self.snap_client.restart(reload=True)
except Exception as err: # pylint: disable=broad-except
logger.error("Failed to configure custom DCGM metrics: %s", err)
return False

return True

Expand Down
38 changes: 10 additions & 28 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,21 +819,19 @@ class TestDCGMSnapExporter(unittest.TestCase):
def setUp(self) -> None:
"""Set up harness for each test case."""
snap_lib_patcher = mock.patch.object(service, "snap")
shutil_lib_patcher = mock.patch.object(service, "shutil")
self.mock_snap = snap_lib_patcher.start()
self.mock_shutil = shutil_lib_patcher.start()
self.addCleanup(snap_lib_patcher.stop)
self.addCleanup(shutil_lib_patcher.stop)

search_path = pathlib.Path(f"{__file__}/../../..").resolve()
self.mock_config = {
"dcgm-snap-channel": "latest/stable",
}

self.exporter = service.DCGMExporter(search_path, self.mock_config)

# Set up mocks
self.exporter.strategy = mock.MagicMock()
self.exporter.snap_client = mock.MagicMock()
self.exporter.snap_client.present = mock.MagicMock()
self.exporter.snap_client.get = mock.MagicMock()

def test_exporter_name(self):
self.assertEqual(self.exporter.exporter_name, "dcgm")
Expand All @@ -846,48 +844,32 @@ def test_install_failed(self):

exporter_install_ok = self.exporter.install()

self.exporter.snap_client.get.assert_not_called()
self.exporter.strategy.install.accept_called()
self.mock_shutil.copy.accept_not_called()
self.assertFalse(exporter_install_ok)

@mock.patch("service.shutil", return_value=mock.MagicMock())
def test_install_success(self, mock_shutil):
def test_install_success(self):
self.exporter.snap_client.present = True
self.exporter.snap_client.get.return_value = ""

exporter_install_ok = self.exporter.install()

self.exporter.strategy.install.accept_called()
self.exporter.snap_client.get.accept_called_with(self.exporter.metric_config)
mock_shutil.copy.accept_called_with(
self.exporter.metrics_file, self.exporter.metrics_location
self.mock_shutil.copy.accept_called_with(
self.exporter.metrics_file, self.exporter.snap_common
)
self.exporter.snap_client.set.accept_called_with(
{self.exporter.metric_config: self.exporter.metric_config_value}
)
self.exporter.snap_client.restart.accept_called_with(reload=True)
self.assertTrue(exporter_install_ok)

@mock.patch("service.shutil", return_value=mock.MagicMock())
def test_install_metrics_preset(self, mock_shutil):
self.exporter.snap_client.present = True
self.exporter.snap_client.get.return_value = self.exporter.metric_config_value
exporter_install_ok = self.exporter.install()

self.exporter.strategy.install.accept_called()
self.exporter.snap_client.get.accept_called_with(self.exporter.metric_config_value)
mock_shutil.copy.accept_not_called()
self.assertTrue(exporter_install_ok)

@mock.patch("service.shutil", return_value=mock.MagicMock())
def test_install_metrics_copy_fail(self, mock_shutil):
def test_install_metrics_copy_fail(self):
self.exporter.snap_client.present = True
self.exporter.snap_client.get.return_value = ""
mock_shutil.copy.side_effect = FileNotFoundError
self.mock_shutil.copy.side_effect = FileNotFoundError

exporter_install_ok = self.exporter.install()

self.exporter.strategy.install.accept_called()
self.exporter.snap_client.get.accept_called_with(self.exporter.metric_config_value)
self.exporter.snap_client.restart.accept_not_called()
self.assertFalse(exporter_install_ok)

Expand Down

0 comments on commit 02851cf

Please sign in to comment.