def remote_dir_exists(directory: pathlib.Path) -> bool: """Does |directory| exist in the CLOUD_EXPERIMENT_BUCKET.""" return gsutil.ls(exp_path.gcs(directory), must_exist=False)[0] == 0
def test_integration_runner(self, mocked_error, tmp_path, environ): """Test that runner can run libFuzzer and saves snapshots to GCS.""" # Switch cwd so that fuzzers don't create tons of files in the repo. os.chdir(tmp_path) # Set env variables that would be set by the Dockerfile. file_directory = pathlib.Path(__file__).parent root_dir = file_directory.parent os.environ['ROOT_DIR'] = str(root_dir) seed_corpus_dir = tmp_path / 'seeds' os.mkdir(seed_corpus_dir) os.environ['SEED_CORPUS_DIR'] = str(seed_corpus_dir) output_corpus_dir = tmp_path / 'corpus' os.mkdir(output_corpus_dir) os.environ['OUTPUT_CORPUS_DIR'] = str(output_corpus_dir) fuzzer = 'libfuzzer' fuzzer_variant = fuzzer + '_variant' fuzzer_parent_path = root_dir / 'fuzzers' / fuzzer benchmark = 'MultipleConstraintsOnSmallInputTest' test_experiment_bucket = os.environ['TEST_CLOUD_EXPERIMENT_BUCKET'] experiment = 'integration-test-experiment' gcs_directory = posixpath.join(test_experiment_bucket, experiment, 'experiment-folders', '%s-%s' % (benchmark, fuzzer_variant), 'trial-1') gsutil.rm(gcs_directory, force=True) # Add fuzzer directory to make it easy to run fuzzer.py in local # configuration. os.environ['PYTHONPATH'] = ':'.join( [str(root_dir), str(fuzzer_parent_path)]) # Set env variables that would set by the scheduler. os.environ['FUZZER'] = fuzzer os.environ['FUZZER_VARIANT_NAME'] = fuzzer_variant os.environ['BENCHMARK'] = benchmark os.environ['CLOUD_EXPERIMENT_BUCKET'] = test_experiment_bucket os.environ['EXPERIMENT'] = experiment os.environ['TRIAL_ID'] = str(TRIAL_NUM) max_total_time = 10 os.environ['MAX_TOTAL_TIME'] = str(max_total_time) target_binary_path = (file_directory / 'test_data' / 'test_runner' / benchmark) with mock.patch('common.fuzzer_utils.get_fuzz_target_binary', return_value=str(target_binary_path)): with mock.patch('common.experiment_utils.SNAPSHOT_PERIOD', max_total_time / 10): runner.main() gcs_corpus_directory = posixpath.join(gcs_directory, 'corpus') returncode, snapshots = gsutil.ls(gcs_corpus_directory) # Ensure that test works. assert returncode == 0, 'gsutil ls %s failed.' % gcs_corpus_directory assert len(snapshots) >= 2 # Check that the archives are deleted after being copied to GCS. assert not os.path.exists( tmp_path / 'corpus-archives' / 'corpus-archive-0001.tar.gz') local_gcs_corpus_dir_copy = tmp_path / 'gcs_corpus_dir' os.mkdir(local_gcs_corpus_dir_copy) gsutil.cp('-r', posixpath.join(gcs_corpus_directory, '*'), str(local_gcs_corpus_dir_copy)) archive_size = os.path.getsize(local_gcs_corpus_dir_copy / 'corpus-archive-0001.tar.gz') assert archive_size > 500 assert len(os.listdir(output_corpus_dir)) > 5 mocked_error.assert_not_called()