def test_routes(self, mock_forward_table): def _same(arg): return arg._mock_name.encode() route = mock.MagicMock() mock_cast_result = mock.Mock() mock_cast_result.contents = [route] self._ctypes_mock.cast.return_value = mock_cast_result self._network_module.ws2_32.Ws2_32.inet_ntoa.side_effect = _same route.dwForwardIfIndex = 'dwForwardIfIndex' route.dwForwardProto = 'dwForwardProto' route.dwForwardMetric1 = 'dwForwardMetric1' routes = self._network.routes() mock_forward_table.assert_called_once_with() enter = mock_forward_table.return_value.__enter__ enter.assert_called_once_with() exit_ = mock_forward_table.return_value.__exit__ exit_.assert_called_once_with(None, None, None) self.assertEqual(1, len(routes)) given_route = routes[0] self.assertEqual('dwForwardDest', given_route.destination) self.assertEqual('dwForwardNextHop', given_route.gateway) self.assertEqual('dwForwardMask', given_route.netmask) self.assertEqual('dwForwardIfIndex', given_route.interface) self.assertEqual('dwForwardMetric1', given_route.metric) self.assertEqual('dwForwardProto', given_route.flags)
def setUp(self): super(TestNetworkWindows, self).setUp() self._ctypes_mock = mock.MagicMock() self._winreg_mock = mock.Mock() self._win32com_mock = mock.Mock() self._wmi_mock = mock.Mock() self._module_patcher = mock.patch.dict( 'sys.modules', { 'ctypes': self._ctypes_mock, 'win32com': self._win32com_mock, 'wmi': self._wmi_mock, 'six.moves.winreg': self._winreg_mock }) self._module_patcher.start() self._iphlpapi = mock.Mock() self._kernel32 = mock.Mock() self._ws2_32 = mock.Mock() self._network_module = importlib.import_module( 'cloudinit.osys.windows.network') self._network_module.iphlpapi = self._iphlpapi self._network_module.kernel32 = self._kernel32 self._network_module.ws2_32 = self._ws2_32 self._network = self._network_module.Network()
def setUp(self): super(TestWindowsGeneral, self).setUp() self._ctypes_mock = mock.Mock() self._util_mock = mock.MagicMock() self._module_patcher = mock.patch.dict( 'sys.modules', {'ctypes': self._ctypes_mock, 'cloudinit.osys.windows.util': self._util_mock}) self._module_patcher.start() self._general_module = importlib.import_module( "cloudinit.osys.windows.general") self._kernel32 = self._general_module.kernel32 self._general = self._general_module.General()
class TestMain(TestCase): def test_help_exits_success(self): with mock.patch('cloudinit.shell.sys.stdout'): exc = self.assertRaises(SystemExit, shell.main, args=['cloud-init', '--help']) self.assertEqual(exc.code, 0) def test_invalid_arguments_exit_fail(self): # silence writes that get to stderr with mock.patch('cloudinit.shell.sys.stderr'): exc = self.assertRaises(SystemExit, shell.main, args=['cloud-init', 'bogus_argument']) self.assertNotEqual(exc.code, 0) @mock.patch('cloudinit.shell.sys.stdout') def test_version_shows_cloud_init(self, mock_out_write): shell.main(args=['cloud-init', 'version']) write_arg = mock_out_write.write.call_args[0][0] self.assertTrue(write_arg.startswith('cloud-init')) @mock.patch('cloudinit.shell.sys.stderr', new_callable=six.StringIO) def test_no_arguments_shows_usage(self, stderr): self.assertRaises(SystemExit, shell.main, args=['cloud-init']) self.assertIn('usage: cloud-init', stderr.getvalue()) @mock.patch('cloudinit.shell.sys.stderr', mock.MagicMock()) def test_no_arguments_exits_2(self): exc = self.assertRaises(SystemExit, shell.main, args=['cloud-init']) self.assertEqual(2, exc.code) @mock.patch('cloudinit.shell.sys.stderr', new_callable=six.StringIO) def test_no_arguments_shows_error_message(self, stderr): self.assertRaises(SystemExit, shell.main, args=['cloud-init']) self.assertIn('cloud-init: error: too few arguments', stderr.getvalue())
def test_check_os_version(self): m = mock.MagicMock() self._test_check_os_version(ret_value=m)
def _fake_registry(): return mock.Mock(registered_items={'a': mock.MagicMock(), 'b': mock.MagicMock()})