""" 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()