From a4fd67d7f9b8229d8dffbb775ffb6301ddb16717 Mon Sep 17 00:00:00 2001 From: Lili Nie Date: Mon, 5 Feb 2024 09:31:00 -0500 Subject: [PATCH] test: Add test for _get_recipe_info function --- tests/unit/data/result.xml | 1 + tests/unit/test_beaker.py | 60 ++++++++++++++++++++++++++++++++++++++ tests/unit/utils.py | 7 +++++ 3 files changed, 68 insertions(+) create mode 100644 tests/unit/data/result.xml create mode 100644 tests/unit/test_beaker.py diff --git a/tests/unit/data/result.xml b/tests/unit/data/result.xml new file mode 100644 index 00000000..87927800 --- /dev/null +++ b/tests/unit/data/result.xml @@ -0,0 +1 @@ +This job has been created using mrack. diff --git a/tests/unit/test_beaker.py b/tests/unit/test_beaker.py new file mode 100644 index 00000000..cd36286d --- /dev/null +++ b/tests/unit/test_beaker.py @@ -0,0 +1,60 @@ +# Copyright 2021 Red Hat Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for mrack.providers.beaker""" + +from unittest import mock +from unittest.mock import Mock, patch + +import pytest + +from mrack.providers.beaker import BeakerProvider + +from .utils import get_content + + +class TestBeakerProvider: + def setup_method(self): + + self.mock_hub = Mock() + self.result_xml = get_content("result.xml") + self.mock_hub.taskactions.to_xml = Mock(return_value=self.result_xml) + self.mock_hub_class = Mock(return_value=self.mock_hub) + self.hub_patcher = patch( + "mrack.providers.beaker.HubProxy", new=self.mock_hub_class + ) + self.hub_patcher.start() + self.log_msg_start = "Beaker [client.testdomain.test]" + self.beaker_id = 8874545 + self.distros = ["Fedora-32%", "Fedora-37%", "CentOS-Stream-9%"] + self.timeout = 120 + self.reserve_duration = 86400 + + def teardown_method(self): + mock.patch.stopall() + + @pytest.mark.asyncio + async def test_get_repo_info(self): + provider = BeakerProvider() + await provider.init(self.distros, self.timeout, self.reserve_duration) + bkr_res = provider._get_recipe_info(self.beaker_id, self.log_msg_start) + assert bkr_res["system"] == "test.example.com" + assert bkr_res["status"] == "Completed" + assert bkr_res["result"] == "Pass" + assert bkr_res["rid"] == "15482633" + assert bkr_res["id"] == "8874545" + assert bkr_res["logs"] == { + "console.log": "https://test.example.com/logs/console.log", + "anaconda.log": "https://test.example.com/logs/anaconda.log", + } diff --git a/tests/unit/utils.py b/tests/unit/utils.py index 6f984f0d..4f064d8a 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -15,6 +15,13 @@ def get_data(name, data_dir="data"): return data +def get_content(name, file_dir="data"): + path = os.path.join(test_dir_path(), file_dir, name) + with open(os.path.expanduser(path), "r", encoding="utf-8") as file_content: + content = file_content.read() + return content + + def copy_as_temp(data_path, text=True): """ Makes a temporary copy of file relative to test dir and return it file path.