def test_api_representation_matches_expected(self): job_config_command = 'fake command' subjob = Subjob( build_id=12, subjob_id=34, project_type=Mock(spec_set=ProjectType), job_config=Mock(spec=JobConfig, command=job_config_command), atoms=[ Atom('BREAKFAST', 'pancakes', expected_time=23.4, actual_time=56.7), Atom('BREAKFAST', 'cereal', expected_time=89.0, actual_time=24.6), ], ) actual_api_repr = subjob.api_representation() expected_api_repr = { 'id': 34, 'command': job_config_command, 'atoms': [ { 'id': 0, 'atom': get_environment_variable_setter_command('BREAKFAST', 'pancakes'), 'expected_time': 23.4, 'actual_time': 56.7, }, { 'id': 1, 'atom': get_environment_variable_setter_command('BREAKFAST', 'cereal'), 'expected_time': 89.0, 'actual_time': 24.6, }, ] } self.assertEqual(actual_api_repr, expected_api_repr, 'Actual api representation should match expected.')
def test_api_representation_matches_expected(self): job_config_command = 'fake command' subjob = Subjob( build_id=12, subjob_id=34, project_type=Mock(spec_set=ProjectType), job_config=Mock(spec=JobConfig, command=job_config_command), atoms=[ Atom('BREAKFAST', 'pancakes', expected_time=23.4, actual_time=56.7), Atom('BREAKFAST', 'cereal', expected_time=89.0, actual_time=24.6), ], ) actual_api_repr = subjob.api_representation() expected_api_repr = { 'id': 34, 'command': job_config_command, 'atoms': [ { 'id': 0, 'atom': get_environment_variable_setter_command( 'BREAKFAST', 'pancakes'), 'expected_time': 23.4, 'actual_time': 56.7, }, { 'id': 1, 'atom': get_environment_variable_setter_command( 'BREAKFAST', 'cereal'), 'expected_time': 89.0, 'actual_time': 24.6, }, ] } self.assertEqual(actual_api_repr, expected_api_repr, 'Actual api representation should match expected.')
def atomize_in_project(self, project_type): """ Translate the atomizer dicts that this instance was initialized with into a list of actual atom commands. This executes atomizer commands inside the given project in order to generate the atoms. :param project_type: The ProjectType instance in which to execute the atomizer commands :type project_type: ProjectType :return: The list of environment variable "export" atom commands :rtype: list[app.master.atom.Atom] """ atoms_list = [] for atomizer_dict in self._atomizer_dicts: for atomizer_var_name, atomizer_command in atomizer_dict.items(): atomizer_output, exit_code = project_type.execute_command_in_project(atomizer_command) if exit_code != 0: self._logger.error('Atomizer command "{}" for variable "{}" failed with exit code: {} and output:' '\n{}', atomizer_command, atomizer_var_name, exit_code, atomizer_output) internal_errors.labels(ErrorType.AtomizerFailure).inc() # pylint: disable=no-member raise AtomizerError('Atomizer command failed!') new_atoms = [] for atom_value in atomizer_output.strip().splitlines(): # For purposes of matching atom string values across builds, we must replace the generated/unique # project directory with its corresponding universal environment variable: '$PROJECT_DIR'. atom_value = atom_value.replace(project_type.project_directory, '$PROJECT_DIR') new_atoms.append(Atom(get_environment_variable_setter_command(atomizer_var_name, atom_value))) atoms_list.extend(new_atoms) return atoms_list
def atomize_in_project(self, project_type): """ Translate the atomizer dicts that this instance was initialized with into a list of actual atom commands. This executes atomizer commands inside the given project in order to generate the atoms. :param project_type: The ProjectType instance in which to execute the atomizer commands :type project_type: ProjectType :return: The list of environment variable "export" atom commands :rtype: list[app.master.atom.Atom] """ atoms_list = [] for atomizer_dict in self._atomizer_dicts: for atomizer_var_name, atomizer_command in atomizer_dict.items(): atomizer_output, exit_code = project_type.execute_command_in_project( atomizer_command) if exit_code != 0: self._logger.error( 'Atomizer command "{}" for variable "{}" failed with exit code: {} and output:' '\n{}', atomizer_command, atomizer_var_name, exit_code, atomizer_output) raise AtomizerError('Atomizer command failed!') new_atoms = [] for atom_value in atomizer_output.strip().splitlines(): # For purposes of matching atom string values across builds, we must replace the generated/unique # project directory with its corresponding universal environment variable: '$PROJECT_DIR'. atom_value = atom_value.replace( project_type.project_directory, '$PROJECT_DIR') new_atoms.append( Atom( get_environment_variable_setter_command( atomizer_var_name, atom_value))) atoms_list.extend(new_atoms) return atoms_list
def test_atomizer_returns_expected_atom_list(self): mock_project = MagicMock(spec_set=ProjectType) mock_project.execute_command_in_project.return_value = (_FAKE_ATOMIZER_COMMAND_OUTPUT, _SUCCESSFUL_EXIT_CODE) atomizer = Atomizer([{'TEST_FILE': _FAKE_ATOMIZER_COMMAND}]) actual_atoms = atomizer.atomize_in_project(mock_project) actual_atom_commands = [atom.command_string for atom in actual_atoms] expected_atom_commands = [ get_environment_variable_setter_command('TEST_FILE', 'test_a.py'), get_environment_variable_setter_command('TEST_FILE', 'test_b.py'), get_environment_variable_setter_command('TEST_FILE', 'test_c.py'), ] self.assertListEqual(expected_atom_commands, actual_atom_commands, 'List of actual atoms should match list of expected atoms.') mock_project.execute_command_in_project.assert_called_once_with(_FAKE_ATOMIZER_COMMAND)
def test_atomizer_returns_expected_atom_list(self): mock_project = MagicMock(spec_set=ProjectType) mock_project.execute_command_in_project.return_value = ( _FAKE_ATOMIZER_COMMAND_OUTPUT, _SUCCESSFUL_EXIT_CODE) atomizer = Atomizer([{'TEST_FILE': _FAKE_ATOMIZER_COMMAND}]) actual_atoms = atomizer.atomize_in_project(mock_project) actual_atom_commands = [atom.command_string for atom in actual_atoms] expected_atom_commands = [ get_environment_variable_setter_command('TEST_FILE', 'test_a.py'), get_environment_variable_setter_command('TEST_FILE', 'test_b.py'), get_environment_variable_setter_command('TEST_FILE', 'test_c.py'), ] self.assertListEqual( expected_atom_commands, actual_atom_commands, 'List of actual atoms should match list of expected atoms.') mock_project.execute_command_in_project.assert_called_once_with( _FAKE_ATOMIZER_COMMAND)
def test_get_environment_variable_setter_command(self, name, value, os_name, expected_command): # Arrange mock_os = self.patch('app.util.process_utils.os') mock_os.name = os_name # Act command = get_environment_variable_setter_command(name, value) # Assert self.assertEqual(command, expected_command)
def shell_environment_command(self, extra_environment_vars=None): """ Turn a dict of environment vars into a shell string :param extra_environment_vars: :type extra_environment_vars: dict [string, string] :return: shell command for setting the environment :rtype: string """ environment_vars = self._get_environment_vars() environment_vars.update(extra_environment_vars or {}) commands = [get_environment_variable_setter_command(key, value) for key, value in environment_vars.items()] return ' '.join(commands)
def __init__(self, env_var_name, atom_value, expected_time=None, actual_time=None): """ :type env_var_name: str :type atom_value: str :type expected_time: float :type actual_time: float """ self._env_var_name = env_var_name self._atom_value = atom_value self.expected_time = expected_time self.actual_time = actual_time # Convert atomizer command output into environment variable export commands. self.command_string = get_environment_variable_setter_command(self._env_var_name, self._atom_value)
def shell_environment_command(self, extra_environment_vars=None): """ Turn a dict of environment vars into a shell string :param extra_environment_vars: :type extra_environment_vars: dict [string, string] :return: shell command for setting the environment :rtype: string """ environment_vars = self._get_environment_vars() environment_vars.update(extra_environment_vars or {}) commands = [ get_environment_variable_setter_command(key, value) for key, value in environment_vars.items() ] return ' '.join(commands)
def test_execute_command_in_project_type_specifies_cwd_if_doesnt_exist(self): project_type_popen_patch = self._patch_popen() fake_project_directory = 'proj_dir' fake_command = 'some_command' git_env = Git("ssh://scm.dev.box.net/box/www/current", 'origin', 'refs/changes/78/151978/27') git_env.project_directory = fake_project_directory git_env.execute_command_in_project(fake_command) env_setter = get_environment_variable_setter_command('PROJECT_DIR', fake_project_directory) project_type_popen_patch.assert_called_once_with( '{} {}'.format(env_setter, fake_command), cwd=None, shell=ANY, stdout=ANY, stderr=ANY, start_new_session=ANY, )
def __init__(self, env_var_name, atom_value, expected_time=None, actual_time=None): """ :type env_var_name: str :type atom_value: str :type expected_time: float :type actual_time: float """ self._env_var_name = env_var_name self._atom_value = atom_value self.expected_time = expected_time self.actual_time = actual_time # Convert atomizer command output into environment variable export commands. self.command_string = get_environment_variable_setter_command( self._env_var_name, self._atom_value)