def test_execute(self): self.inst = HostnameResolver() self.inst.api = MagicMock() # Set up mock for api.get_data - what we are to retrieve ips = ['10.1.1.1', '192.168.1.12'] self.inst.api.get_data.return_value.json.return_value = ips # Set up mock for api.send_file remote_path = 'file:///tmp/obsrvbl/hostnames/resolutions.json' output = {} def _send_file(data_type, path, now, suffix=None): with io.open(path, 'rb') as infile: output[index] = infile.read() return remote_path self.inst.api.send_file.side_effect = _send_file # Do the deed index = 0 self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 1) call_args, call_kwargs = self.inst.api.send_file.call_args self.assertEqual(call_args[0], 'hostnames') self.assertEqual(call_kwargs['suffix'], 'hosts') self.assertEqual(output[0], json.dumps(DNS_RESOLUTIONS)) self.inst.api.send_signal.assert_called_once_with( 'hostnames', {'path': remote_path})
def test_execute_no_ips(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.api.get_data.return_value.json.return_value = [] self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 0) self.assertEqual(self.inst.api.send_signal.call_count, 0)
def test_execute_error(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.api.get_data.return_value.json.side_effect = ValueError self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 0) self.assertEqual(self.inst.api.send_signal.call_count, 0)
def test_execute_ipsplosion(self, mock_get_data, mock_update, mock_resolve): mock_get_data.return_value.json.side_effect = ValueError resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with("hostnames") self.assertEquals(mock_resolve.call_args_list, []) self.assertEquals(mock_update.call_args_list, [])
def test_service(self, mock_requests): resolver = HostnameResolver() resolver.poll_seconds = 0 def killer(signum, frame): resolver.stop() signal.signal(signal.SIGALRM, killer) signal.alarm(1) resolver.run()
def test_execute_no_ips(self, mock_get_data, mock_update, mock_resolve): ips = [] mock_get_data.return_value.json.return_value = ips resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with("hostnames") self.assertEquals(mock_resolve.call_args_list, []) self.assertEquals(mock_update.call_args_list, [])
def test_execute_ipsplosion(self, mock_get_data, mock_update, mock_resolve): mock_get_data.return_value.json.side_effect = ValueError resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with('hostnames') self.assertEquals(mock_resolve.call_args_list, []) self.assertEquals(mock_update.call_args_list, [])
def test_execute_no_ips(self, mock_get_data, mock_update, mock_resolve): ips = [] mock_get_data.return_value.json.return_value = ips resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with('hostnames') self.assertEquals(mock_resolve.call_args_list, []) self.assertEquals(mock_update.call_args_list, [])
def test_service(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.poll_seconds = 0 def killer(signum, frame): self.inst.stop() signal.signal(signal.SIGALRM, killer) signal.alarm(1) self.inst.run()
def test_execute(self, mock_get_data, mock_update, mock_resolve): ips = ["1.2.3.4", "5.6.7.8"] hosts = ["host1", "host2"] mock_get_data.return_value.json.return_value = ips mock_resolve.return_value = hosts resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with("hostnames") mock_resolve.assert_called_once_with(ips) mock_update.assert_called_once_with(hosts, None)
def test_execute(self, mock_get_data, mock_update, mock_resolve): ips = ['1.2.3.4', '5.6.7.8'] hosts = ['host1', 'host2'] mock_get_data.return_value.json.return_value = ips mock_resolve.return_value = hosts resolver = HostnameResolver() resolver.execute() mock_get_data.assert_called_once_with('hostnames') mock_resolve.assert_called_once_with(ips) mock_update.assert_called_once_with(hosts, None)
def test_update_host_names(self, mock_tempfile, mock_upload, mock_signal): handle = Mock() handle.name = "foobar" mock_tempfile.return_value.__enter__.return_value = handle mock_upload.return_value = "s3://blah/blah" hosts = ["host1", "host2"] time = datetime.utcnow() resolver = HostnameResolver() resolver._update_host_names(hosts, time) handle.write.assert_called_once_with(json.dumps(hosts)) mock_upload.assert_called_once_with("hostnames", "foobar", time, suffix="hosts") mock_signal.assert_called_once_with("hostnames", {"path": mock_upload.return_value})
def test_execute_both(self, mock_check_output): self.inst = HostnameResolver() self.inst.api = MagicMock() # Set up mock for api.get_data - what we are to retrieve ips = ['192.0.2.1', '192.168.1.12', '10.1.1.1', '198.51.100.1'] self.inst.api.get_data.return_value.json.return_value = ips # Set up mock for api.send_file remote_path = 'file:///tmp/obsrvbl/hostnames/resolutions.json' output = {} def _send_file(data_type, path, now, suffix=None): with io.open(path, 'rb') as infile: output[index] = infile.read() return remote_path self.inst.api.send_file.side_effect = _send_file # Set up the resolver def _check_output(*popenargs, **kwargs): ip = popenargs[0][-1] if ip == '192.0.2.1': return '\tTEST_3 <00> - M <ACTIVE> \n' elif ip == '192.168.1.12': return '\tTEST_2.BOGUS <00> - M <ACTIVE> \n' raise CalledProcessError(None, None) mock_check_output.side_effect = _check_output expected_resolutions = { '192.0.2.1': NETBIOS_RESOLUTIONS['192.0.2.1'], '192.168.1.12': NETBIOS_RESOLUTIONS['192.168.1.12'], '10.1.1.1': DNS_RESOLUTIONS['10.1.1.1'], '198.51.100.1': None, } # Do the deed index = 0 self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 1) call_args, call_kwargs = self.inst.api.send_file.call_args self.assertEqual(call_args[0], 'hostnames') self.assertEqual(call_kwargs['suffix'], 'hosts') self.assertEqual(output[0], json.dumps(expected_resolutions)) self.inst.api.send_signal.assert_called_once_with( 'hostnames', {'path': remote_path})
def test_update_host_names(self, mock_tempfile, mock_upload, mock_signal): handle = Mock() handle.name = 'foobar' mock_tempfile.return_value.__enter__.return_value = handle mock_upload.return_value = 's3://blah/blah' hosts = ['host1', 'host2'] time = datetime.utcnow() resolver = HostnameResolver() resolver._update_host_names(hosts, time) handle.write.assert_called_once_with(json.dumps(hosts)) mock_upload.assert_called_once_with('hostnames', 'foobar', time, suffix='hosts') mock_signal.assert_called_once_with('hostnames', {'path': mock_upload.return_value})
class HostnameResolverTest(TestCase): def test_gethostbyaddr(self): self.assertEqual(gethostbyaddr('127.0.0.1'), 'localhost') self.assertIsNone(gethostbyaddr('127.0.0.256')) self.assertIsNone(gethostbyaddr('bogus')) @patch(PATCH_PATH.format('subprocess.check_output'), autospec=True) def test_nmblookup(self, mock_check_output): mock_check_output.return_value = ( 'Ignoring unknown parameter "server role"\n' 'Looking up status of 192.0.2.1\n' '\tWRONG <00> - M <OFFLINE>\n' '\tWKSOBSR01 <00> - M <ACTIVE> \n' '\tON <00> - <GROUP> M <ACTIVE> \n' '\tON <1c> - <GROUP> M <ACTIVE> \n' '\tWKSOBSR01 <20> - M <ACTIVE> \n' '\n\tMAC Address = 02-04-01-01-04-02\n' '\n') self.assertEqual(nmblookup('192.0.2.1'), 'wksobsr01') mock_check_output.assert_called_once_with( 'timeout 1s nmblookup -A 192.0.2.1'.split()) @patch(PATCH_PATH.format('subprocess.check_output'), autospec=True) def test_nmblookup_fail(self, mock_check_output): mock_check_output.return_value = ( 'Ignoring unknown parameter "server role"\n' 'Looking up status of 192.0.2.1\n' 'No reply from 192.0.2.1\n\n') self.assertIsNone(nmblookup('192.0.2.1')) mock_check_output.side_effect = CalledProcessError(None, None) self.assertIsNone(nmblookup('192.0.2.1')) @patch(PATCH_PATH.format('sleep'), autospec=True) def test_resolve_host_names(self, mock_sleep): resolvers = [DNS_RESOLUTIONS.get, NETBIOS_RESOLUTIONS.get] ips = ['10.1.1.1', '192.168.1.12', '192.0.2.1', '198.51.100.1'] actual = resolve_host_names(ips, resolvers) expected = { '10.1.1.1': 'test_1', '192.168.1.12': 'test_2', '192.0.2.1': 'test_3', '198.51.100.1': None, } self.assertEqual(actual, expected) self.assertEqual(mock_sleep.call_args_list, [call(0.1)] * len(ips)) @patch(PATCH_PATH.format('gethostbyaddr'), DNS_RESOLUTIONS.get) def test_execute(self): self.inst = HostnameResolver() self.inst.api = MagicMock() # Set up mock for api.get_data - what we are to retrieve ips = ['10.1.1.1', '192.168.1.12'] self.inst.api.get_data.return_value.json.return_value = ips # Set up mock for api.send_file remote_path = 'file:///tmp/obsrvbl/hostnames/resolutions.json' output = {} def _send_file(data_type, path, now, suffix=None): with io.open(path, 'rb') as infile: output[index] = infile.read() return remote_path self.inst.api.send_file.side_effect = _send_file # Do the deed index = 0 self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 1) call_args, call_kwargs = self.inst.api.send_file.call_args self.assertEqual(call_args[0], 'hostnames') self.assertEqual(call_kwargs['suffix'], 'hosts') self.assertEqual(output[0], json.dumps(DNS_RESOLUTIONS)) self.inst.api.send_signal.assert_called_once_with( 'hostnames', {'path': remote_path}) @patch.dict('os.environ', { ENV_HOSTNAME_DNS: 'false', ENV_HOSTNAME_NETBIOS: 'true' }) @patch(PATCH_PATH.format('subprocess.check_output'), autospec=True) def test_execute_netbios(self, mock_check_output): self.inst = HostnameResolver() self.inst.api = MagicMock() # Set up mock for api.get_data - what we are to retrieve ips = ['192.0.2.1', '192.168.1.12'] self.inst.api.get_data.return_value.json.return_value = ips # Set up mock for api.send_file remote_path = 'file:///tmp/obsrvbl/hostnames/resolutions.json' output = {} def _send_file(data_type, path, now, suffix=None): with io.open(path, 'rb') as infile: output[index] = infile.read() return remote_path self.inst.api.send_file.side_effect = _send_file # Set up the resolver def _check_output(*popenargs, **kwargs): ip = popenargs[0][-1] if ip == '192.0.2.1': return '\tTEST_3 <00> - M <ACTIVE> \n' elif ip == '192.168.1.12': return '\tTEST_2.BOGUS <00> - M <ACTIVE> \n' raise CalledProcessError(None, None) mock_check_output.side_effect = _check_output # Do the deed index = 0 self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 1) call_args, call_kwargs = self.inst.api.send_file.call_args self.assertEqual(call_args[0], 'hostnames') self.assertEqual(call_kwargs['suffix'], 'hosts') self.assertEqual(output[0], json.dumps(NETBIOS_RESOLUTIONS)) self.inst.api.send_signal.assert_called_once_with( 'hostnames', {'path': remote_path}) @patch.dict('os.environ', {ENV_HOSTNAME_NETBIOS: 'true'}) @patch(PATCH_PATH.format('gethostbyaddr'), DNS_RESOLUTIONS.get) @patch(PATCH_PATH.format('subprocess.check_output'), autospec=True) def test_execute_both(self, mock_check_output): self.inst = HostnameResolver() self.inst.api = MagicMock() # Set up mock for api.get_data - what we are to retrieve ips = ['192.0.2.1', '192.168.1.12', '10.1.1.1', '198.51.100.1'] self.inst.api.get_data.return_value.json.return_value = ips # Set up mock for api.send_file remote_path = 'file:///tmp/obsrvbl/hostnames/resolutions.json' output = {} def _send_file(data_type, path, now, suffix=None): with io.open(path, 'rb') as infile: output[index] = infile.read() return remote_path self.inst.api.send_file.side_effect = _send_file # Set up the resolver def _check_output(*popenargs, **kwargs): ip = popenargs[0][-1] if ip == '192.0.2.1': return '\tTEST_3 <00> - M <ACTIVE> \n' elif ip == '192.168.1.12': return '\tTEST_2.BOGUS <00> - M <ACTIVE> \n' raise CalledProcessError(None, None) mock_check_output.side_effect = _check_output expected_resolutions = { '192.0.2.1': NETBIOS_RESOLUTIONS['192.0.2.1'], '192.168.1.12': NETBIOS_RESOLUTIONS['192.168.1.12'], '10.1.1.1': DNS_RESOLUTIONS['10.1.1.1'], '198.51.100.1': None, } # Do the deed index = 0 self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 1) call_args, call_kwargs = self.inst.api.send_file.call_args self.assertEqual(call_args[0], 'hostnames') self.assertEqual(call_kwargs['suffix'], 'hosts') self.assertEqual(output[0], json.dumps(expected_resolutions)) self.inst.api.send_signal.assert_called_once_with( 'hostnames', {'path': remote_path}) @patch.dict('os.environ', { ENV_HOSTNAME_DNS: 'false', ENV_HOSTNAME_NETBIOS: 'false' }) def test_execute_no_resolvers(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.api.get_data.return_value.json.return_value = [] self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 0) self.assertEqual(self.inst.api.send_signal.call_count, 0) def test_execute_no_ips(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.api.get_data.return_value.json.return_value = [] self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 0) self.assertEqual(self.inst.api.send_signal.call_count, 0) def test_execute_error(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.api.get_data.return_value.json.side_effect = ValueError self.inst.execute() self.assertEqual(self.inst.api.send_file.call_count, 0) self.assertEqual(self.inst.api.send_signal.call_count, 0) def test_service(self): self.inst = HostnameResolver() self.inst.api = MagicMock() self.inst.poll_seconds = 0 def killer(signum, frame): self.inst.stop() signal.signal(signal.SIGALRM, killer) signal.alarm(1) self.inst.run()