Files
charm-magpie/tests/unit/test_charm.py
Samuel Allan 5acbc4e5ba Rewrite charm in operator framework
This is a major breaking change too.  In the process, also:

- move all processing from juju status to actions
  (run the actions to get data; the status line will be minimal)
- switch to COS integration, no longer legacy prometheus
  for the iperf benchmarks

It should be mostly feature parity with the original magpie charm,
but some things still need improving and iterating on,
such as the spec for data returned from actions,
and actual functional tests.

Change-Id: I289d4e7a0dd373c5c6f2471ab710e754c167ab8c
2024-07-05 14:19:11 +09:30

58 lines
1.8 KiB
Python

# Copyright 2023 Ubuntu
# See LICENSE file for licensing details.
#
# Learn more about testing at: https://juju.is/docs/sdk/testing
from unittest.mock import Mock, call
import ops
import ops.testing
import pytest
from charm import MagpieCharm
from magpie_tools import status_for_speed_check
@pytest.fixture
def harness():
harness = ops.testing.Harness(MagpieCharm)
harness.begin()
yield harness
harness.cleanup()
@pytest.fixture
def os_system_mock(monkeypatch):
mock = Mock()
monkeypatch.setattr("charm.os.system", mock)
return mock
def test_example(harness, os_system_mock):
harness.charm.on.install.emit()
assert os_system_mock.call_count == 2
os_system_mock.assert_has_calls([call("apt update"), call("apt install -y iperf")])
def test_status_for_speed_check():
assert status_for_speed_check("0", 123, 150) == {"message": "min-speed disabled", "ok": True}
assert status_for_speed_check("0%", 123, 150) == {"message": "min-speed disabled", "ok": True}
assert status_for_speed_check(":P", 123, 150) == {
"message": "invalid min_speed: :P",
"ok": False,
}
assert status_for_speed_check("1", 10, 400) == {"message": "10 >= 1 mbit/s", "ok": True}
assert status_for_speed_check("12", 10, 400) == {
"message": "failed: 10 < 12 mbit/s",
"ok": False,
}
assert status_for_speed_check("50%", 100, 400) == {
"message": "failed: 100 < 200 mbit/s",
"ok": False,
}
assert status_for_speed_check("50%", 200, 400) == {"message": "200 >= 200 mbit/s", "ok": True}
assert status_for_speed_check("50%", 300, 400) == {"message": "300 >= 200 mbit/s", "ok": True}
assert status_for_speed_check("50%", 300, -1) == {
"message": "unknown, link speed undefined",
"ok": False,
}