Skip to content

Commit

Permalink
replace processor call with machine call (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij authored Jun 17, 2022
1 parent 5f4d32d commit 42f6781
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/charms/prometheus_k8s/v0/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ def inject_label_matchers(self, expression, topology):
return expression

def _get_tool_path(self) -> Optional[Path]:
arch = platform.processor()
arch = platform.machine()
arch = "amd64" if arch == "x86_64" else arch
res = "cos-tool-{}".format(arch)
try:
Expand Down
2 changes: 1 addition & 1 deletion lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ def inject_label_matchers(self, expression, topology) -> str:
return expression

def _get_tool_path(self) -> Optional[Path]:
arch = platform.processor()
arch = platform.machine()
arch = "amd64" if arch == "x86_64" else arch
res = "cos-tool-{}".format(arch)
try:
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def setUp(self):
self.harness.begin()

# pylint: disable=protected-access
@unittest.mock.patch("platform.processor", lambda: "teakettle")
@unittest.mock.patch("platform.machine", lambda: "teakettle")
def test_disable_on_invalid_arch(self):
tool = self.harness.charm.tool
self.assertIsNone(tool.path)
self.assertTrue(tool._disabled)

# pylint: disable=protected-access
@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_gives_path_on_valid_arch(self):
"""When given a valid arch, it should return the binary path."""
transformer = self.harness.charm.tool
self.assertIsInstance(transformer.path, PosixPath)

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_setup_transformer(self):
"""When setup it should know the path to the binary."""
tool = self.harness.charm.tool
Expand All @@ -55,7 +55,7 @@ def test_setup_transformer(self):
p = str(tool.path)
self.assertTrue(p.endswith("cos-tool-amd64"))

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
@unittest.mock.patch("subprocess.run")
def test_returns_original_expression_when_subprocess_call_errors(self, mocked_run):
mocked_run.side_effect = subprocess.CalledProcessError(
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_returns_original_expression_when_subprocess_call_errors(self, mocked_ru
)
self.assertEqual(output["groups"][0]["expr"], "process_cpu_seconds_total > 0.12")

@unittest.mock.patch("platform.processor", lambda: "invalid")
@unittest.mock.patch("platform.machine", lambda: "invalid")
def test_uses_original_expression_when_binary_missing(self):
tool = self.harness.charm.tool
output = tool.apply_label_matchers(
Expand All @@ -114,20 +114,20 @@ def test_uses_original_expression_when_binary_missing(self):
)
self.assertEqual(output["groups"][0]["expr"], "process_cpu_seconds_total > 0.12")

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_fetches_the_correct_expression(self):
tool = self.harness.charm.tool

output = tool.inject_label_matchers("up", {"juju_model": "some_juju_model"})
assert output == 'up{juju_model="some_juju_model"}'

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_handles_comparisons(self):
tool = self.harness.charm.tool
output = tool.inject_label_matchers("up > 1", {"juju_model": "some_juju_model"})
assert output == 'up{juju_model="some_juju_model"} > 1'

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_handles_multiple_labels(self):
tool = self.harness.charm.tool
output = tool.inject_label_matchers(
Expand Down Expand Up @@ -155,7 +155,7 @@ def setUp(self):
self.addCleanup(self.harness.cleanup)
self.harness.begin()

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_returns_errors_on_bad_rule_file(self):
tool = self.harness.charm.tool
valid, errs = tool.validate_alert_rules(
Expand All @@ -171,7 +171,7 @@ def test_returns_errors_on_bad_rule_file(self):
self.assertEqual(valid, False)
self.assertIn(errs, "error validating:")

@unittest.mock.patch("platform.processor", lambda: "x86_64")
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_successfully_validates_good_alert_rules(self):
tool = self.harness.charm.tool
valid, errs = tool.validate_alert_rules(
Expand Down

0 comments on commit 42f6781

Please sign in to comment.