def update_fuzzer_jobs(fuzzer_entities, job_names): """Update fuzzer job mappings.""" to_delete = {} for fuzzer_entity_key in fuzzer_entities: fuzzer_entity = fuzzer_entity_key.get() for job in data_types.Job.query(): if not job.environment_string: continue job_environment = job.get_environment() if not utils.string_is_true(job_environment.get( 'MANAGED', 'False')): continue if job.name in job_names: continue logs.log('Deleting job %s' % job.name) to_delete[job.name] = job.key try: fuzzer_entity.jobs.remove(job.name) except ValueError: pass fuzzer_entity.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer_entity) if to_delete: ndb_utils.delete_multi(to_delete.values())
def test_no_mappings(self): """Ensure that we do nothing if a fuzzer has no mappings.""" fuzzer = data_types.Fuzzer() fuzzer.name = 'no_mappings' fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer) self.assertEqual(_get_job_list_for_fuzzer(fuzzer), [])
def test_mapping_removed(self): """Ensure that mappings are removed properly.""" fuzzer = data_types.Fuzzer() fuzzer.name = 'adding_jobs' fuzzer.jobs = ['job_1'] fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer, []) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertEqual([], mappings)
def test_new_addition(self): """Ensure that we add mappings for a new fuzzer.""" fuzzer = data_types.Fuzzer() fuzzer.name = 'new_addition' fuzzer.jobs = ['job_1', 'job_2'] fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertIn('job_1', mappings) self.assertIn('job_2', mappings)
def post(self): """Handle a post request.""" key = helpers.get_integer_key(request) fuzzer = ndb.Key(data_types.Fuzzer, key).get() if not fuzzer: raise helpers.EarlyExitException('Fuzzer not found.', 400) fuzzer_selection.update_mappings_for_fuzzer(fuzzer, mappings=[]) fuzzer.key.delete() helpers.log('Deleted fuzzer %s' % fuzzer.name, helpers.MODIFY_OPERATION) return self.redirect('/fuzzers')
def test_mapping_subsituted(self): """Ensure that mappings are subsituted properly.""" fuzzer = data_types.Fuzzer() fuzzer.name = 'adding_jobs' fuzzer.jobs = ['job_1'] fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertIn('job_1', mappings) self.assertNotIn('job_2', mappings) fuzzer.jobs = ['job_2'] fuzzer_selection.update_mappings_for_fuzzer(fuzzer) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertNotIn('job_1', mappings) self.assertIn('job_2', mappings)
def test_mapping_added(self): """Ensure that we properly add mappings for existing fuzzers.""" fuzzer = data_types.Fuzzer() fuzzer.name = 'adding_jobs' fuzzer.jobs = ['job_1'] fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertIn('job_1', mappings) self.assertNotIn('job_2', mappings) fuzzer.jobs += ['job_2'] fuzzer_selection.update_mappings_for_fuzzer(fuzzer) mappings = _get_job_list_for_fuzzer(fuzzer) self.assertIn('job_1', mappings) self.assertIn('job_2', mappings)
def apply_fuzzer_changes(self, fuzzer, upload_info): """Apply changes to a fuzzer.""" if upload_info and not archive.is_archive(upload_info.filename): raise helpers.EarlyExitException( 'Sorry, only zip, tgz, tar.gz, tbz, and tar.bz2 archives are ' 'allowed!', 400) if fuzzer.builtin: executable_path = launcher_script = None else: executable_path = self._get_executable_path(upload_info) launcher_script = self._get_launcher_script(upload_info) # Executable path is required for non-builtin fuzzers and if it is not # already set. if not fuzzer.executable_path and not executable_path: raise helpers.EarlyExitException( 'Please enter the path to the executable, or if the archive you ' 'uploaded is less than 16MB, ensure that the executable file has ' '"run" in its name.', 400) jobs = request.get('jobs', []) timeout = self._get_integer_value('timeout') max_testcases = self._get_integer_value('max_testcases') external_contribution = request.get('external_contribution', False) differential = request.get('differential', False) environment_string = request.get('additional_environment_string') data_bundle_name = request.get('data_bundle_name') # Save the fuzzer file metadata. if upload_info: fuzzer.filename = upload_info.filename fuzzer.blobstore_key = str(upload_info.key()) fuzzer.file_size = utils.get_size_string(upload_info.size) fuzzer.jobs = jobs fuzzer.revision = fuzzer.revision + 1 fuzzer.source = helpers.get_user_email() fuzzer.timeout = timeout fuzzer.max_testcases = max_testcases fuzzer.result = None fuzzer.sample_testcase = None fuzzer.console_output = None fuzzer.external_contribution = bool(external_contribution) fuzzer.differential = bool(differential) fuzzer.additional_environment_string = environment_string fuzzer.timestamp = datetime.datetime.utcnow() fuzzer.data_bundle_name = data_bundle_name # Update only if a new archive is provided. if executable_path: fuzzer.executable_path = executable_path # Optional. Also, update only if a new archive is provided and contains a # launcher script. if launcher_script: fuzzer.launcher_script = launcher_script fuzzer.put() fuzzer_selection.update_mappings_for_fuzzer(fuzzer) helpers.log('Uploaded fuzzer %s.' % fuzzer.name, helpers.MODIFY_OPERATION) return self.redirect('/fuzzers')