def test_create_instance_not_preemptible(): """Tests create_instance doesn't specify preemptible when it isn't supposed to.""" with test_utils.mock_popen_ctx_mgr(returncode=1) as mocked_popen: gcloud.create_instance(INSTANCE_NAME, gcloud.InstanceType.RUNNER, CONFIG) assert mocked_popen.commands == [ _get_expected_create_runner_command(False) ]
def start(self): """Start the experiment on the dispatcher.""" logs.info('Started dispatcher with instance name: %s', self.instance_name) with tempfile.NamedTemporaryFile(dir=os.getcwd(), mode='w') as startup_script: self.write_startup_script(startup_script) gcloud.create_instance(self.instance_name, gcloud.InstanceType.DISPATCHER, self.config, startup_script=startup_script.name)
def test_create_instance(): """Tests create_instance creates an instance.""" with test_utils.mock_popen_ctx_mgr(returncode=1) as mocked_popen: gcloud.create_instance(INSTANCE_NAME, gcloud.InstanceType.DISPATCHER, CONFIG) assert mocked_popen.commands == [[ 'gcloud', 'compute', 'instances', 'create', 'instance-a', '--image-family=cos-stable', '--image-project=cos-cloud', '--zone=zone-a', '--scopes=cloud-platform', '--machine-type=n1-standard-96', '--boot-disk-size=4TB' ]]
def test_create_instance_preemptible(): """Tests create_instance doesn't specify preemptible when it isn't supposed to.""" config = CONFIG.copy() config['preemptible_runners'] = True with test_utils.mock_popen_ctx_mgr(returncode=1) as mocked_popen: gcloud.create_instance(INSTANCE_NAME, gcloud.InstanceType.RUNNER, config) assert mocked_popen.commands == [ _get_expected_create_runner_command(True) ]
def test_create_instance_failed_create(mocked_execute): """Tests create_instance creates an instance if it doesn't already exist.""" mocked_execute.return_value = new_process.ProcessResult(1, '', False) # We shouldn't exception here. assert not gcloud.create_instance(INSTANCE_NAME, gcloud.InstanceType.DISPATCHER, CONFIG) # Check that the first call is to create the instance. assert 'create' in mocked_execute.call_args_list[0][0][0]
def create_trial_instance(benchmark: str, fuzzer: str, trial_id: int, experiment_config: dict) -> bool: """Create or start a trial instance for a specific trial_id,fuzzer,benchmark.""" instance_name = experiment_utils.get_trial_instance_name( experiment_config['experiment'], trial_id) startup_script = render_startup_script_template(instance_name, benchmark, fuzzer, trial_id, experiment_config) startup_script_path = '/tmp/%s-start-docker.sh' % instance_name with open(startup_script_path, 'w') as file_handle: file_handle.write(startup_script) return gcloud.create_instance(instance_name, gcloud.InstanceType.RUNNER, experiment_config, startup_script=startup_script_path)
def create_trial_instance(benchmark: str, fuzzer: str, trial_id: int, experiment_config: dict) -> bool: """Create or start a trial instance for a specific trial_id,fuzzer,benchmark.""" instance_name = experiment_utils.get_trial_instance_name( experiment_config['experiment'], trial_id) fuzzer_config = fuzzer_config_utils.get_by_variant_name(fuzzer) underlying_fuzzer_name = fuzzer_config['fuzzer'] docker_image_url = benchmark_utils.get_runner_image_url( benchmark, underlying_fuzzer_name, experiment_config['cloud_project']) fuzz_target = benchmark_utils.get_fuzz_target(benchmark) # Convert additional environment variables from configuration to arguments # that will be passed to docker. additional_env = '' if 'env' in fuzzer_config: additional_env = ' '.join([ '-e {k}={v}'.format(k=k, v=shlex.quote(v)) for k, v in fuzzer_config['env'].items() ]) startup_script = '''#!/bin/bash echo 0 > /proc/sys/kernel/yama/ptrace_scope echo core >/proc/sys/kernel/core_pattern while ! docker pull {docker_image_url} do echo 'Error pulling image, retrying...' done docker run --privileged --cpuset-cpus=0 --rm \ -e INSTANCE_NAME={instance_name} -e FUZZER={fuzzer} -e BENCHMARK={benchmark} \ -e FUZZER_VARIANT_NAME={fuzzer_variant_name} -e EXPERIMENT={experiment} \ -e TRIAL_ID={trial_id} -e MAX_TOTAL_TIME={max_total_time} \ -e CLOUD_PROJECT={cloud_project} -e CLOUD_COMPUTE_ZONE={cloud_compute_zone} \ -e CLOUD_EXPERIMENT_BUCKET={cloud_experiment_bucket} \ -e FUZZ_TARGET={fuzz_target} {additional_env} \ --cap-add SYS_NICE --cap-add SYS_PTRACE --name=runner-container \ {docker_image_url} 2>&1 | tee /tmp/runner-log.txt'''.format( instance_name=instance_name, benchmark=benchmark, experiment=experiment_config['experiment'], fuzzer=underlying_fuzzer_name, fuzzer_variant_name=fuzzer, trial_id=trial_id, max_total_time=experiment_config['max_total_time'], cloud_project=experiment_config['cloud_project'], cloud_compute_zone=experiment_config['cloud_compute_zone'], cloud_experiment_bucket=experiment_config['cloud_experiment_bucket'], fuzz_target=fuzz_target, docker_image_url=docker_image_url, additional_env=additional_env) startup_script_path = '/tmp/%s-start-docker.sh' % instance_name with open(startup_script_path, 'w') as file_handle: file_handle.write(startup_script) return gcloud.create_instance(instance_name, gcloud.InstanceType.RUNNER, experiment_config, startup_script=startup_script_path, write_to_stdout=False)