def testNetworkDaemon(self, mock_lock, mock_logger, mock_watcher, mock_network_utils, mock_network_setup, mock_ip_forwarding): mock_logger_instance = mock.Mock() mock_logger.Logger.return_value = mock_logger_instance mocks = mock.Mock() mocks.attach_mock(mock_lock, 'lock') mocks.attach_mock(mock_logger, 'logger') mocks.attach_mock(mock_network_utils, 'network') mocks.attach_mock(mock_ip_forwarding, 'forwarding') mocks.attach_mock(mock_network_setup, 'network_setup') mocks.attach_mock(mock_watcher, 'watcher') metadata_key = network_daemon.NetworkDaemon.network_interface_metadata_key with mock.patch.object(network_daemon.NetworkDaemon, 'HandleNetworkInterfaces') as mock_handle: network_daemon.NetworkDaemon(ip_forwarding_enabled=True, proto_id='66', ip_aliases=None, target_instance_ips=None, dhclient_script='x', dhcp_command='y', network_setup_enabled=True, debug=True) expected_calls = [ mock.call.logger.Logger(name=mock.ANY, debug=True, facility=mock.ANY), mock.call.forwarding.IpForwarding(proto_id='66', debug=True), mock.call.network_setup.NetworkSetup(debug=True, dhclient_script='x', dhcp_command='y'), mock.call.network.NetworkUtils(logger=mock_logger_instance), mock.call.watcher.MetadataWatcher(logger=mock_logger_instance), mock.call.lock.LockFile(network_daemon.LOCKFILE), mock.call.lock.LockFile().__enter__(), mock.call.logger.Logger().info(mock.ANY), mock.call.watcher.MetadataWatcher().WatchMetadata( mock_handle, metadata_key=metadata_key, recursive=True, timeout=mock.ANY), mock.call.lock.LockFile().__exit__(None, None, None), ] self.assertEqual(mocks.mock_calls, expected_calls)
def testNetworkDaemonError(self, mock_lock, mock_logger, mock_watcher, mock_network_utils, mock_network_setup, mock_ip_forwarding): mock_logger_instance = mock.Mock() mock_logger.Logger.return_value = mock_logger_instance mocks = mock.Mock() mocks.attach_mock(mock_lock, 'lock') mocks.attach_mock(mock_logger, 'logger') mocks.attach_mock(mock_ip_forwarding, 'forwarding') mocks.attach_mock(mock_network_setup, 'network_setup') mocks.attach_mock(mock_network_utils, 'network') mocks.attach_mock(mock_watcher, 'watcher') self.mock_setup._ExtractInterfaceMetadata.return_value = [] mock_lock.LockFile.side_effect = IOError('Test Error') with mock.patch.object(network_daemon.NetworkDaemon, 'HandleNetworkInterfaces'): network_daemon.NetworkDaemon(ip_forwarding_enabled=False, proto_id='66', ip_aliases=None, target_instance_ips=None, dhclient_script='x', dhcp_command='y', network_setup_enabled=False, debug=True) expected_calls = [ mock.call.logger.Logger(name=mock.ANY, debug=True, facility=mock.ANY), mock.call.forwarding.IpForwarding(proto_id='66', debug=True), mock.call.network_setup.NetworkSetup(debug=True, dhclient_script='x', dhcp_command='y'), mock.call.network.NetworkUtils(logger=mock_logger_instance), mock.call.watcher.MetadataWatcher(logger=mock_logger_instance), mock.call.lock.LockFile(network_daemon.LOCKFILE), mock.call.logger.Logger().warning('Test Error'), ] self.assertEqual(mocks.mock_calls, expected_calls)