diff options
author | James Falcon <james.falcon@canonical.com> | 2021-12-15 20:16:38 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 19:16:38 -0700 |
commit | bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf (patch) | |
tree | 1fbb3269fc87e39832e3286ef42eefd2b23fcd44 /tests/unittests/cmd/devel/test_hotplug_hook.py | |
parent | 2bcf4fa972fde686c2e3141c58e640640b44dd00 (diff) | |
download | vyos-cloud-init-bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf.tar.gz vyos-cloud-init-bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf.zip |
Adopt Black and isort (SC-700) (#1157)
Applied Black and isort, fixed any linting issues, updated tox.ini
and CI.
Diffstat (limited to 'tests/unittests/cmd/devel/test_hotplug_hook.py')
-rw-r--r-- | tests/unittests/cmd/devel/test_hotplug_hook.py | 162 |
1 files changed, 85 insertions, 77 deletions
diff --git a/tests/unittests/cmd/devel/test_hotplug_hook.py b/tests/unittests/cmd/devel/test_hotplug_hook.py index e1c64e2f..842e8dfd 100644 --- a/tests/unittests/cmd/devel/test_hotplug_hook.py +++ b/tests/unittests/cmd/devel/test_hotplug_hook.py @@ -1,8 +1,9 @@ -import pytest from collections import namedtuple from unittest import mock from unittest.mock import call +import pytest + from cloudinit.cmd.devel.hotplug_hook import handle_hotplug from cloudinit.distros import Distro from cloudinit.event import EventType @@ -11,9 +12,8 @@ from cloudinit.net.network_state import NetworkState from cloudinit.sources import DataSource from cloudinit.stages import Init - -hotplug_args = namedtuple('hotplug_args', 'udevaction, subsystem, devpath') -FAKE_MAC = '11:22:33:44:55:66' +hotplug_args = namedtuple("hotplug_args", "udevaction, subsystem, devpath") +FAKE_MAC = "11:22:33:44:55:66" @pytest.yield_fixture @@ -26,28 +26,28 @@ def mocks(): m_init.fetch.return_value = m_datasource read_sys_net = mock.patch( - 'cloudinit.cmd.devel.hotplug_hook.read_sys_net_safe', - return_value=FAKE_MAC + "cloudinit.cmd.devel.hotplug_hook.read_sys_net_safe", + return_value=FAKE_MAC, ) update_event_enabled = mock.patch( - 'cloudinit.stages.update_event_enabled', + "cloudinit.stages.update_event_enabled", return_value=True, ) m_network_state = mock.MagicMock(spec=NetworkState) parse_net = mock.patch( - 'cloudinit.cmd.devel.hotplug_hook.parse_net_config_data', - return_value=m_network_state + "cloudinit.cmd.devel.hotplug_hook.parse_net_config_data", + return_value=m_network_state, ) m_activator = mock.MagicMock(spec=NetworkActivator) select_activator = mock.patch( - 'cloudinit.cmd.devel.hotplug_hook.activators.select_activator', - return_value=m_activator + "cloudinit.cmd.devel.hotplug_hook.activators.select_activator", + return_value=m_activator, ) - sleep = mock.patch('time.sleep') + sleep = mock.patch("time.sleep") read_sys_net.start() update_event_enabled.start() @@ -55,7 +55,7 @@ def mocks(): select_activator.start() m_sleep = sleep.start() - yield namedtuple('mocks', 'm_init m_network_state m_activator m_sleep')( + yield namedtuple("mocks", "m_init m_network_state m_activator m_sleep")( m_init=m_init, m_network_state=m_network_state, m_activator=m_activator, @@ -72,42 +72,43 @@ def mocks(): class TestUnsupportedActions: def test_unsupported_subsystem(self, mocks): with pytest.raises( - Exception, - match='cannot handle events for subsystem: not_real' + Exception, match="cannot handle events for subsystem: not_real" ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - subsystem='not_real', - udevaction='add' + devpath="/dev/fake", + subsystem="not_real", + udevaction="add", ) def test_unsupported_udevaction(self, mocks): - with pytest.raises(ValueError, match='Unknown action: not_real'): + with pytest.raises(ValueError, match="Unknown action: not_real"): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='not_real', - subsystem='net' + devpath="/dev/fake", + udevaction="not_real", + subsystem="net", ) class TestHotplug: def test_succcessful_add(self, mocks): init = mocks.m_init - mocks.m_network_state.iter_interfaces.return_value = [{ - 'mac_address': FAKE_MAC, - }] + mocks.m_network_state.iter_interfaces.return_value = [ + { + "mac_address": FAKE_MAC, + } + ] handle_hotplug( hotplug_init=init, - devpath='/dev/fake', - udevaction='add', - subsystem='net' + devpath="/dev/fake", + udevaction="add", + subsystem="net", + ) + init.datasource.update_metadata_if_supported.assert_called_once_with( + [EventType.HOTPLUG] ) - init.datasource.update_metadata_if_supported.assert_called_once_with([ - EventType.HOTPLUG - ]) - mocks.m_activator.bring_up_interface.assert_called_once_with('fake') + mocks.m_activator.bring_up_interface.assert_called_once_with("fake") mocks.m_activator.bring_down_interface.assert_not_called() init._write_to_cache.assert_called_once_with() @@ -116,113 +117,120 @@ class TestHotplug: mocks.m_network_state.iter_interfaces.return_value = [{}] handle_hotplug( hotplug_init=init, - devpath='/dev/fake', - udevaction='remove', - subsystem='net' + devpath="/dev/fake", + udevaction="remove", + subsystem="net", ) - init.datasource.update_metadata_if_supported.assert_called_once_with([ - EventType.HOTPLUG - ]) - mocks.m_activator.bring_down_interface.assert_called_once_with('fake') + init.datasource.update_metadata_if_supported.assert_called_once_with( + [EventType.HOTPLUG] + ) + mocks.m_activator.bring_down_interface.assert_called_once_with("fake") mocks.m_activator.bring_up_interface.assert_not_called() init._write_to_cache.assert_called_once_with() def test_update_event_disabled(self, mocks, caplog): init = mocks.m_init with mock.patch( - 'cloudinit.stages.update_event_enabled', - return_value=False + "cloudinit.stages.update_event_enabled", return_value=False ): handle_hotplug( hotplug_init=init, - devpath='/dev/fake', - udevaction='remove', - subsystem='net' + devpath="/dev/fake", + udevaction="remove", + subsystem="net", ) - assert 'hotplug not enabled for event of type' in caplog.text + assert "hotplug not enabled for event of type" in caplog.text init.datasource.update_metadata_if_supported.assert_not_called() mocks.m_activator.bring_up_interface.assert_not_called() mocks.m_activator.bring_down_interface.assert_not_called() init._write_to_cache.assert_not_called() def test_update_metadata_failed(self, mocks): - mocks.m_init.datasource.update_metadata_if_supported.return_value = \ + mocks.m_init.datasource.update_metadata_if_supported.return_value = ( False + ) with pytest.raises( - RuntimeError, match='Datasource .* not updated for event hotplug' + RuntimeError, match="Datasource .* not updated for event hotplug" ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='remove', - subsystem='net' + devpath="/dev/fake", + udevaction="remove", + subsystem="net", ) def test_detect_hotplugged_device_not_detected_on_add(self, mocks): mocks.m_network_state.iter_interfaces.return_value = [{}] with pytest.raises( RuntimeError, - match='Failed to detect {} in updated metadata'.format(FAKE_MAC) + match="Failed to detect {} in updated metadata".format(FAKE_MAC), ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='add', - subsystem='net' + devpath="/dev/fake", + udevaction="add", + subsystem="net", ) def test_detect_hotplugged_device_detected_on_remove(self, mocks): - mocks.m_network_state.iter_interfaces.return_value = [{ - 'mac_address': FAKE_MAC, - }] + mocks.m_network_state.iter_interfaces.return_value = [ + { + "mac_address": FAKE_MAC, + } + ] with pytest.raises( - RuntimeError, - match='Failed to detect .* in updated metadata' + RuntimeError, match="Failed to detect .* in updated metadata" ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='remove', - subsystem='net' + devpath="/dev/fake", + udevaction="remove", + subsystem="net", ) def test_apply_failed_on_add(self, mocks): - mocks.m_network_state.iter_interfaces.return_value = [{ - 'mac_address': FAKE_MAC, - }] + mocks.m_network_state.iter_interfaces.return_value = [ + { + "mac_address": FAKE_MAC, + } + ] mocks.m_activator.bring_up_interface.return_value = False with pytest.raises( - RuntimeError, match='Failed to bring up device: /dev/fake' + RuntimeError, match="Failed to bring up device: /dev/fake" ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='add', - subsystem='net' + devpath="/dev/fake", + udevaction="add", + subsystem="net", ) def test_apply_failed_on_remove(self, mocks): mocks.m_network_state.iter_interfaces.return_value = [{}] mocks.m_activator.bring_down_interface.return_value = False with pytest.raises( - RuntimeError, match='Failed to bring down device: /dev/fake' + RuntimeError, match="Failed to bring down device: /dev/fake" ): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='remove', - subsystem='net' + devpath="/dev/fake", + udevaction="remove", + subsystem="net", ) def test_retry(self, mocks): with pytest.raises(RuntimeError): handle_hotplug( hotplug_init=mocks.m_init, - devpath='/dev/fake', - udevaction='add', - subsystem='net' + devpath="/dev/fake", + udevaction="add", + subsystem="net", ) assert mocks.m_sleep.call_count == 5 assert mocks.m_sleep.call_args_list == [ - call(1), call(3), call(5), call(10), call(30) + call(1), + call(3), + call(5), + call(10), + call(30), ] |