def setUp(self): """Create test setup.""" self.cred = Credential.objects.create( name='cred1', username='******', password='******', become_password=None, ssh_keyfile=None) self.cred_for_upload = self.cred.id self.source = Source( name='source1', source_type='network', port=22) self.source.save() self.source.credentials.add(self.cred) self.connect_scan = Scan(name='connect_test', scan_type=ScanTask.SCAN_TYPE_CONNECT) self.connect_scan.save() self.connect_scan.sources.add(self.source) self.connect_scan.save() self.inspect_scan = Scan(name='inspect_test') self.inspect_scan.save() self.inspect_scan.sources.add(self.source) self.inspect_scan.save()
def test_delete_with_scans(self): """Delete a Source used by a scan.""" cred = Credential(name='cred2', username='******', password='******') cred.save() source = Source(name='cred_source', source_type=Source.NETWORK_SOURCE_TYPE, hosts=['1.2.3.4']) source.save() source.credentials.add(cred) source.save() scan = Scan(name='test_scan', scan_type=ScanTask.SCAN_TYPE_CONNECT) scan.save() scan.sources.add(source) url = reverse('source-detail', args=(source.id,)) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) response_json = response.json() self.assertEqual( response_json['detail'], messages.SOURCE_DELETE_NOT_VALID_W_SCANS) self.assertEqual(response_json['scans'][0]['name'], 'test_scan')
def setUp(self): """Create test setup.""" management.call_command('flush', '--no-input') self.cred = Credential.objects.create( name='cred1', username='******', password='******', become_password=None, ssh_keyfile=None) self.cred_for_upload = self.cred.id self.source = Source( name='source1', source_type='network', port=22) self.source.save() self.source.credentials.add(self.cred) # Create scan configuration scan = Scan(name='scan_name', scan_type=ScanTask.SCAN_TYPE_CONNECT) scan.save() # Add source to scan scan.sources.add(self.source) # Create Job self.scan_job = ScanJob(scan=scan) self.scan_job.save()
def test_get_scan(self): user = User.objects.get(pk=1) house_objects_all = House.objects.all() house = random.choice(house_objects_all) #print 'picked house %i' % house.pk measure = random.choice(house.measures.all()) #print 'picked measure %i' % measure.pk Scan(user=user, text="%04d%04d" % (house.id, measure.measure.id), timestamp=time.time()).save() house = random.choice(house_objects_all) measure = random.choice(house.measures.all()) Scan(user=user, text="%04d%04d" % (house.id, measure.measure.id), timestamp=time.time()).save() report = ReportService().get_html_report(user=user) print report
def test_queue_task(self): """Test create queue state change.""" # Cannot use util because its testing queue # Create scan configuration scan = Scan(name='test', scan_type=ScanTask.SCAN_TYPE_INSPECT) scan.save() # Add source to scan scan.sources.add(self.source) options_to_use = ScanOptions() options_to_use.save() scan.options = options_to_use scan.save() # Create Job scan_job = ScanJob(scan=scan) scan_job.save() # Job in created state self.assertEqual(scan_job.status, ScanTask.CREATED) tasks = scan_job.tasks.all() self.assertEqual(len(tasks), 0) # Queue job to run scan_job.queue() # Job should be in pending state self.assertEqual(scan_job.status, ScanTask.PENDING) # Queue should have created scan tasks tasks = scan_job.tasks.all() self.assertEqual(len(tasks), 2) # Validate connect task created and correct connect_task = tasks[0] self.assertEqual(connect_task.scan_type, ScanTask.SCAN_TYPE_CONNECT) self.assertEqual(connect_task.status, ScanTask.PENDING) # Validate inspect task created and correct inspect_task = tasks[1] self.assertEqual(inspect_task.scan_type, ScanTask.SCAN_TYPE_INSPECT) self.assertEqual(inspect_task.status, ScanTask.PENDING)
def create_scan_job_two_tasks(source, source2, scan_type=ScanTask.SCAN_TYPE_CONNECT, scan_name='test', scan_options=None): """Create a new scan job with two sources. :param source: the source for the scan job :param sourc2: the second source for the scan job :param scan_type: Either connect or inspect :param scan_options: Job scan options :return: the scan job and task """ # Create scan configuration scan = Scan(name=scan_name, scan_type=scan_type) scan.save() # Add source to scan if source is not None: scan.sources.add(source) if source2 is not None: scan.sources.add(source2) # Add options to scan if scan_options is not None: scan.options = scan_options scan.save() # Create Job scan_job = ScanJob(scan=scan) scan_job.save() scan_job.queue() # grab the scan tasks scan_tasks = scan_job.tasks.all() if scan_type == ScanTask.SCAN_TYPE_INSPECT: for task in scan_tasks: task.complete() return scan_job, scan_tasks
def create_scan_job(source, scan_type=ScanTask.SCAN_TYPE_CONNECT, scan_name='test', scan_options=None): """Create a new scan job. :param source: the source for the scan job :param scan_type: Either connect or inspect :param scan_options: Job scan options :return: the scan job and task """ # Create scan configuration scan = Scan(name=scan_name, scan_type=scan_type) scan.save() # Add source to scan if source is not None: scan.sources.add(source) # Add options to scan if scan_options is not None: scan.options = scan_options scan.save() # Create Job scan_job = ScanJob(scan=scan) scan_job.save() scan_job.queue() # pylint: disable=no-member scan_task = scan_job.tasks.first() if scan_type == ScanTask.SCAN_TYPE_INSPECT: scan_task.complete() scan_task = scan_job.tasks.filter( scan_type=ScanTask.SCAN_TYPE_INSPECT).first() return scan_job, scan_task
def createScan(self, user, text): scan = Scan(text=text, user=user, timestamp=time.time()) scan.save() return scan
def setUp(self): """Create test setup.""" management.call_command('flush', '--no-input') self.cred = Credential.objects.create(name='cred1', username='******', password='******', become_password=None, ssh_keyfile=None) self.cred.save() self.cred_for_upload = self.cred.id self.source = Source(name='source1', source_type='network', port=22) self.source.save() self.source.credentials.add(self.cred) self.source.save() self.test1 = Scan(name='test1', scan_type=ScanTask.SCAN_TYPE_INSPECT) self.test1.save() self.test1.sources.add(self.source) self.test1.save() self.test2 = Scan(name='test2', scan_type=ScanTask.SCAN_TYPE_CONNECT) self.test2.save() self.test2.sources.add(self.source) self.test2.save() # self.test1 will not have a most_recent_scanjob, self.test2 # will. job = ScanJob(scan=self.test2) job.save() job.sources.add(self.source) job.save() self.test2.most_recent_scanjob = job self.test2.save() results1 = [{ 'id': 1, 'name': 'test1', 'sources': [{ 'id': 1, 'name': 'source1', 'source_type': 'network' }], 'scan_type': 'inspect' }, { 'id': 2, 'jobs': [{ 'id': 1 }], 'most_recent': { 'id': 1, 'scan_type': 'inspect', 'status': 'created', 'status_details': { 'job_status_message': 'Job is created.' } }, 'name': 'test2', 'sources': [{ 'id': 1, 'name': 'source1', 'source_type': 'network' }], 'scan_type': 'connect' }] self.expected = { 'count': 2, 'next': None, 'previous': None, 'results': results1 }