Lindley Werner d65811f2d5 Adding unit tests in pybox python scripts.
Enabling automatic tests with tox and zuul for each new patchset.

To see the unit test logs, go to:
  1- Zuul Summary
  2- tox-unittests
  3- Logs
  4- job-output.txt

Test Plan:
PASS: Run "tox -e unittests" in the terminal, this will:
  - Set the PYTHONPATH environment variable
  - Run the tests
  - Show the coverage report

Task: 47929
Story: 2005051

Change-Id: I7f527860f3498c53b28691c654035d017d70f68b
Signed-off-by: Lindley Werner <lindley.vieira@encora.com>
2023-07-03 15:37:12 -03:00

124 lines
3.6 KiB
Python

"""
Unit tests related to install_lab
"""
import unittest
from unittest.mock import MagicMock, patch
import install_lab
class UpdatePlatformCpusTestCase(unittest.TestCase):
"""
Class to test update_platform_cpus method
"""
@patch("install_lab.serial")
def test_update_platform_cpus(self, mock_serial):
"""
Test update_platform_cpus method
"""
# Setup
mock_stream = MagicMock()
mock_hostname = "hostname"
mock_cpu_num = 5
# Run
install_lab.update_platform_cpus(mock_stream, mock_hostname, cpu_num=mock_cpu_num)
# Assert
command_string = (
"\nsource /etc/platform/openrc; system host-cpu-modify "
f"{mock_hostname} -f platform -p0 {mock_cpu_num}"
)
mock_serial.send_bytes.assert_called_once_with(
mock_stream, command_string, prompt="keystone", timeout=300
)
class SetDnsTestCase(unittest.TestCase):
"""
Class to test set_dns method
"""
@patch("install_lab.serial")
def test_set_dns(self, mock_serial):
"""
Test set_dns method
"""
# Setup
mock_stream = MagicMock()
mock_dns_ip = "8.8.8.8"
# Run
install_lab.set_dns(mock_stream, mock_dns_ip)
# Assert
command_string = (
"source /etc/platform/openrc; system dns-modify "
f"nameservers={mock_dns_ip}"
)
mock_serial.send_bytes.assert_called_once_with(
mock_stream, command_string, prompt="keystone"
)
class ConfigControllerTestCase(unittest.TestCase):
"""
Class to test config_controller method
"""
command_string = (
"ansible-playbook /usr/share/ansible/stx-ansible/playbooks/bootstrap.yml"
)
mock_stream = MagicMock()
mock_password = "Li69nux*"
@patch("install_lab.serial")
@patch("install_lab.host_helper.check_password")
def test_config_controller_successful(self, mock_check_password, mock_serial):
"""
Test config_controller method with success
"""
# Setup
mock_serial.expect_bytes.return_value = 0
# Run
install_lab.config_controller(self.mock_stream, password=self.mock_password)
# Assert
mock_serial.send_bytes.assert_called_once_with(
self.mock_stream, self.command_string, expect_prompt=False
)
mock_check_password.assert_called_once_with(self.mock_stream, password=self.mock_password)
mock_serial.expect_bytes.assert_called_once_with(self.mock_stream, "~$",
timeout=install_lab.HostTimeout.LAB_CONFIG)
@patch("install_lab.serial")
@patch("install_lab.host_helper.check_password")
def test_config_controller_unsuccessful(self, mock_check_password, mock_serial):
"""
Test config_controller method without success raising an exception
"""
# Setup
mock_serial.expect_bytes.return_value = 1
# Run
with self.assertRaises(Exception):
install_lab.config_controller(self.mock_stream, password=self.mock_password)
# Assert
mock_serial.send_bytes.assert_called_once_with(
self.mock_stream, self.command_string, expect_prompt=False
)
mock_check_password.assert_called_once_with(self.mock_stream, password=self.mock_password)
mock_serial.expect_bytes.assert_called_once_with(self.mock_stream, "~$",
timeout=install_lab.HostTimeout.LAB_CONFIG)
if __name__ == '__main__':
unittest.main()