def test_source_tree_object(): """Verify that we throw an exception if not all required data is present.""" source = source_tree.SourceTree(proto_source.SourceRepository()) with pytest.raises(source_tree.SourceTreeError) as pull_exception: source.get_head_hash() assert "No origin is defined or can be" in str(pull_exception.value)
def _generate_source_tree_from_path(path: str) -> source_tree.SourceTree: """Build a source tree from a remote url.""" source_repository = proto_source.SourceRepository( identity=proto_source.SourceRepository.SourceIdentity.SRCID_ENVOY, source_path=path, ) return source_tree.SourceTree(source_repository)
def test_git_with_origin(): """Verify that at a minimum, we can work with a remote origin url specified. """ source_repository = proto_source.SourceRepository( source_url='somewhere_in_github') source = source_tree.SourceTree(source_repository) assert source.get_origin()
def test_get_identity(): """Verify we can retrieve the identity out of a source repository object.""" for source_id in [ proto_source.SourceRepository.SourceIdentity.SRCID_UNSPECIFIED, proto_source.SourceRepository.SourceIdentity.SRCID_ENVOY, proto_source.SourceRepository.SourceIdentity.SRCID_NIGHTHAWK, ]: source_repository = proto_source.SourceRepository(identity=source_id) tree = source_tree.SourceTree(source_repository) identity = tree.get_identity() assert identity == source_id
def test_build_envoy_image_from_source_fail(mock_get_source_tree): """Verify an exception is raised if the source identity is invalid""" nighthawk_source_repo = proto_source.SourceRepository( identity=proto_source.SourceRepository.SourceIdentity.SRCID_NIGHTHAWK, source_path='/some_random_not_envoy_directory') mock_get_source_tree.return_value = nighthawk_source_repo manager = _generate_default_source_manager() builder = envoy_builder.EnvoyBuilder(manager) with pytest.raises(envoy_builder.EnvoyBuilderError) as builder_error: builder.build_envoy_image_from_source() assert str(builder_error.value) == "This class builds Envoy only."
def test_source_tree_with_local_workdir(): """Verify that we can work with a source location on disk.""" source_repository = proto_source.SourceRepository( source_path='/some_source_path') source = source_tree.SourceTree(source_repository) remote_string = 'origin [email protected]:username/reponame.git (fetch)' with mock.patch('src.lib.cmd_exec.run_command', mock.MagicMock(return_value=remote_string)) as magic_mock: origin = source.get_origin() assert origin == "[email protected]:username/reponame.git" cmd_params = cmd_exec.CommandParameters(cwd=mock.ANY) magic_mock.assert_called_once_with("git remote -v", cmd_params)
def get_source_repository(self, \ source_id: proto_source.SourceRepository.SourceIdentity) \ -> proto_source.SourceRepository: """Find and return the source repository object with the specified id Args: source_id: The identity of the source object we seek (eg. SRCID_NIGHTHAWK or SRCID_ENVOY) Return: a Source repository matching the specified source_id Raises: SourceManagerError: If no source exists matching the specified source_id """ return proto_source.SourceRepository()
def _create_new_source_job_control(self, nh_source, envoy_source, envoy_hash) \ -> proto_control.JobControl: """Duplicate the job control for a specific benchmark run. This method creates a new job control object for a single binary benchmark Args: nh_source: the nighthawk source to run envoy_source: the envoy source to test Returns: A job control document containing the Nighthawk and Envoy source specified """ # Generate a unique identifier for the individual run # 1. Specifies whether source is provided via source_path or source_url # 2. Specifies the primary commit hash or tag, if provided # 3. Specifies the branch, if provided # Ex: source_url__v1.16.0__branch_name identifier = str(envoy_source.WhichOneof('source_location')) if envoy_hash: identifier += '__' identifier += envoy_hash if envoy_source.branch: identifier += '__' identifier += envoy_source.branch # Create a new SourceRepository representing just one Envoy build target new_envoy_source = proto_source.SourceRepository() new_envoy_source.CopyFrom(envoy_source) new_envoy_source.commit_hash = envoy_hash del new_envoy_source.additional_hashes[:] output_dir = os.path.join(self._control.environment.output_dir, identifier) new_job_control = proto_control.JobControl() new_job_control.CopyFrom(self._control) del new_job_control.source[:] new_job_control.source.extend([nh_source, new_envoy_source]) new_job_control.environment.output_dir = output_dir log.debug(f"Creating new Binary job for {new_envoy_source}") log.debug(f"Job:\n{new_job_control}") self._create_symlink_for_test_artifacts(output_dir, identifier) return new_job_control
def test_get_origin_fail(mock_get_source_directory, mock_run_command): """Verify that we raise an exception if we are unable to determine the source tree origin """ mock_get_source_directory.return_value = '/some_temp_directory' mock_run_command.return_value = 'definitely not any origin data we want' source_repository = proto_source.SourceRepository(source_path='/tmp') source = source_tree.SourceTree(source_repository) origin = '' with pytest.raises(source_tree.SourceTreeError) as source_error: origin = source.get_origin() assert not origin assert str(source_error.value) == \ "Unable to determine the origin url from /some_temp_directory"
def test_prepare_nighthawk_source_fail(mock_get_source_tree): """Verify an exception is raised if the source identity is invalid""" envoy_source_repo = proto_source.SourceRepository( identity=proto_source.SourceRepository.SourceIdentity.SRCID_ENVOY, source_path='/some_random_envoy_directory', commit_hash='doesnt_matter' ) mock_get_source_tree.return_value = envoy_source_repo manager = _generate_default_source_manager() builder = nighthawk_builder.NightHawkBuilder(manager) with pytest.raises(nighthawk_builder.NightHawkBuilderError) as builder_error: builder.prepare_nighthawk_source() assert str(builder_error.value) == \ "This module supports building NightHawk Only"
def _generate_job_control_for_binaries(self) \ -> List[proto_control.JobControl]: """Determine the required source configurations needed for a binary benchmark. Find the specified version of Nighthawk, Envoy, and all additional Envoy commits to be tested. These will be spawned into multiple Binary Benchmark jobs. Raises: BenchmarkRunnerError: when multiple competing repositories of Envoy/Nighthawk are specified. """ # Find the specified version of Nighthawk and use it for all job controls nighthawk_source = None for source_item in self._control.source: if source_item.identity == proto_source.SourceRepository.SourceIdentity.SRCID_NIGHTHAWK: if nighthawk_source: raise BenchmarkRunnerError( "Multiple Nighthawk sources specified." "Please specify only one.") nighthawk_source = source_item if not nighthawk_source: raise BenchmarkRunnerError("No Nighthawk sources specified") log.info("Using Nighthawk sources at " \ + getattr(nighthawk_source, nighthawk_source.WhichOneof('source_location'))) base_envoy_source = self._source_manager.get_source_repository( proto_source.SourceRepository.SourceIdentity.SRCID_ENVOY) envoy_hashes = self._source_manager.get_envoy_hashes_for_benchmark() jobs = [] for envoy_version in envoy_hashes: envoy_source = proto_source.SourceRepository() envoy_source.CopyFrom(base_envoy_source) envoy_source.commit_hash = envoy_version jobs.append( self._create_new_source_job_control(nighthawk_source, envoy_source, envoy_version)) return jobs
def test_source_tree_with_disk_files(): """Verify that we can get hash data from a source tree on disk.""" source = source_tree.SourceTree( proto_source.SourceRepository( identity=proto_source.SourceRepository.SRCID_ENVOY, source_path='/tmp', commit_hash='fake_commit_hash')) git_output = 'origin [email protected]:username/reponame.git (fetch)' git_cmd = "git remote -v" with mock.patch('src.lib.cmd_exec.run_command', mock.MagicMock(return_value=git_output)) as magic_mock: origin = source.get_origin() assert origin cmd_params = cmd_exec.CommandParameters(cwd=mock.ANY) magic_mock.assert_called_once_with(git_cmd, cmd_params)
def get_source_repository(self, \ source_id: proto_source.SourceRepository.SourceIdentity) \ -> proto_source.SourceRepository: """Find and return the source repository object with the specified id Args: source_id: The identity of the source object we seek (eg. SRCID_NIGHTHAWK or SRCID_ENVOY) Return: a Source repository matching the specified source_id Raises: SourceManagerError: If no source exists matching the specified source_id """ source_name = proto_source.SourceRepository.SourceIdentity.Name( source_id) # Filter source objects that do not match the source_id and return the # first remaining object. We expect one source repository defined for # NightHawk and Envoy, so one object is filtered out and one should remain source = next( filter(lambda s: s.identity == source_id, self._control.source), None) # See if any of the known sources can work for the ID if none was specified if not source and source_id in _KNOWN_REPOSITORIES: log.debug(f"Using default location for {source_name}") source = proto_source.SourceRepository( identity=source_id, source_url=_KNOWN_REPOSITORIES[source_id]) log.debug(f"{source_name} configured with:\n{source}") if not source: raise SourceManagerError( f"Unable to find a source with the requested ID: {source_name}" ) return source
def test_get_origin_ssh(mock_get_source_directory): """Verify that we can determine the origin for a local repository. In this instance the repo was cloned via ssh. """ mock_get_source_directory.return_value = '/some_temp_directory' remote_string = 'origin [email protected]:username/reponame.git (fetch)' git_cmd = "git remote -v" source_repository = proto_source.SourceRepository(source_path='/tmp') source = source_tree.SourceTree(source_repository) with mock.patch('src.lib.cmd_exec.run_command', mock.MagicMock(return_value=remote_string)) as magic_mock: origin_url = source.get_origin() cmd_params = cmd_exec.CommandParameters(cwd='/some_temp_directory') magic_mock.assert_called_once_with(git_cmd, cmd_params) assert origin_url == '[email protected]:username/reponame.git'