Skip to content

Commit

Permalink
test: increase unit test coverage to > 50% (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchiwai-ray authored May 30, 2024
1 parent e1d84ba commit c64c6b9
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion tests/unit/test_elasticbeats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@
sys.modules["charms.apt"] = layer_mock
sys.modules["charms.layer.status"] = layer_mock

from elasticbeats import enable_beat_on_boot, get_package_candidate # noqa: E402
from elasticbeats import ( # noqa: E402
enable_beat_on_boot,
get_package_candidate,
render_without_context,
)


class TestElasticBeats(TestCase):
"""Tests our Elastic Beat library."""

def setUp(self):
mkdir_patcher = mock.patch("elasticbeats.mkdir")
render_patcher = mock.patch("elasticbeats.render")
mkdir_patcher.start()
render_patcher.start()
self.addCleanup(mkdir_patcher.stop)
self.addCleanup(render_patcher.stop)

@mock.patch("elasticbeats.get_package_version")
@mock.patch("elasticbeats.subprocess.Popen")
def test_get_package_candidate(self, mock_sub, mock_pkg_ver):
Expand Down Expand Up @@ -63,3 +75,31 @@ def test_enable_beats_on_boot(self, resume_mock, remove_beat_on_boot_mock):

resume_mock.assert_called_once_with(service_name)
remove_beat_on_boot_mock.assert_called_once_with(service_name)

@mock.patch("elasticbeats.kv")
@mock.patch("elasticbeats.path")
@mock.patch("elasticbeats.config")
@mock.patch("elasticbeats.model_info_cache")
@mock.patch("elasticbeats.principal_unit_cache")
@mock.patch("elasticbeats.write_file")
def test_render_without_context(
self,
mock_write_file,
mock_principal_unit_cache,
mock_model_info_cache,
mock_config,
mock_path,
mock_kv,
):
test_kv = {
"model_name": "test_model",
"model_uuid": "xxxx-xxxx",
"principal_name": "ubuntu/0",
"beat.logstash": "logstash",
"beat.elasticsearch": "elasticsearch",
"beat.kafka": "kafka",
}
mock_kv.return_value = test_kv
mock_config.return_value = {"logstash_hosts": "logstash", "kafka_hosts": "kafka"}
render_without_context(mock.Mock(), mock.Mock())
mock_write_file.assert_called_once()

0 comments on commit c64c6b9

Please sign in to comment.