def test_connect_runner(self, mock_connect): """Test running a connect scan with mocked connection.""" scanner = ConnectTaskRunner(self.scan_job, self.scan_task) result_store = MockResultStore(['1.2.3.4']) conn_dict = scanner.run_with_result_store(result_store) mock_connect.assert_called_with(ANY, ANY, ANY, 22, forks=50) self.assertEqual(conn_dict[1], ScanTask.COMPLETED)
def test_connect_exception(self, mock_run): """Test pause of connect.""" # Test cancel at run() level mock_run.side_effect = AnsibleRunnerException('fail') scanner = ConnectTaskRunner(self.scan_job3, self.scan_task3) _, scan_result = scanner.run(Value('i', ScanJob.JOB_RUN)) self.assertEqual(scan_result, ScanTask.FAILED)
def test_run_success_return_connect(self, mock_run): """Test pause of connect.""" # Test cancel at run() level mock_run.side_effect = [[None, ScanTask.COMPLETED]] scanner = ConnectTaskRunner(self.scan_job3, self.scan_task3) _, scan_result = scanner.run(Value('i', ScanJob.JOB_RUN)) self.assertEqual(scan_result, ScanTask.COMPLETED)
def test_empty_hosts(self, mock_run): """Test running a connect scan with mocked connection.""" mock_run.return_value.status = 'successful' scanner = ConnectTaskRunner(self.scan_job, self.scan_task) result_store = MockResultStore([]) _, result = scanner.run_with_result_store( Value('i', ScanJob.JOB_RUN), result_store) self.assertEqual(result, ScanTask.COMPLETED)
def test_unexpected_runner_response(self, mock_run): """Test unexpected runner response.""" mock_run.return_value.status = 'unknown' scanner = ConnectTaskRunner(self.scan_job, self.scan_task) result_store = MockResultStore(['1.2.3.4']) conn_dict = scanner.run_with_result_store( Value('i', ScanJob.JOB_RUN), result_store) self.assertEqual(conn_dict[1], ScanTask.FAILED)
def test_discovery(self, mock_connect): """Test running a discovery scan with mocked connection.""" expected = ([('1.2.3.4', {'id': '1'})], []) mock_connect.return_value = expected scanner = ConnectTaskRunner(self.scan_job, self.scan_task, self.conn_results) conn_dict = scanner.run() mock_connect.assert_called_with(ANY, ANY, 22, forks=50) self.assertEqual(conn_dict, ScanTask.COMPLETED)
def test_store_discovery_success(self): """Test running a discovery scan _store_connect_result.""" scanner = ConnectTaskRunner(self.scan_job, self.scan_task, self.conn_results) hc_serializer = CredentialSerializer(self.cred) cred = hc_serializer.data connected = [('1.2.3.4', cred)] failed = ['1.2.3.5'] expected = {'1.2.3.4': {'name': 'cred1'}, '1.2.3.5': None} # pylint: disable=protected-access result = scanner._store_connect_result(connected, failed) self.assertEqual(len(result), len(expected)) self.assertIn('1.2.3.5', result) self.assertIsNone(result['1.2.3.5'])
def test_connect_runner_src2(self, mock_connect): """Test running a connect scan with mocked connection.""" scanner = ConnectTaskRunner(self.scan_job2, self.scan_task2) result_store = MockResultStore(['1.2.3.4']) conn_dict = scanner.run_with_result_store(Value('i', ScanJob.JOB_RUN), result_store) mock_connect.assert_called_with(ANY, self.scan_task2, ANY, ANY, ANY, 22, False, forks=50) self.assertEqual(conn_dict[1], ScanTask.COMPLETED)
def test_pause_connect(self, mock_run): """Test pause of connect.""" # Test cancel at _connect level serializer = SourceSerializer(self.source3) source = serializer.data hosts = source['hosts'] connection_port = source['port'] with self.assertRaises(NetworkPauseException): _connect(Value('i', ScanJob.JOB_TERMINATE_PAUSE), self.scan_task, hosts, Mock(), self.cred, connection_port, self.concurrency) # Test cancel at run() level mock_run.side_effect = NetworkPauseException() scanner = ConnectTaskRunner(self.scan_job3, self.scan_task3) _, scan_result = scanner.run(Value('i', ScanJob.JOB_RUN)) self.assertEqual(scan_result, ScanTask.PAUSED)
def test_run_manager_interupt(self): """Test manager interupt for run method.""" scanner = ConnectTaskRunner(self.scan_job, self.scan_task) conn_dict = scanner.run(Value('i', ScanJob.JOB_TERMINATE_CANCEL)) self.assertEqual(conn_dict[1], ScanTask.CANCELED)