def test_log_cmd_with_retries(capsys): remove_handlers() capsys.readouterr() mock_options = MagicMock(koji=False) with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) logger = logging.getLogger('altsrc') handler = logging.StreamHandler() handler.setLevel('DEBUG') handler.setFormatter( logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')) logger.addHandler(handler) with patch('time.sleep') as mocked_sleep: with patch('subprocess.Popen.wait', side_effect=[3, 2, 1, 0]) as mocked_wait: assert_that( calling(processor.log_cmd).with_args(['echo', 'hello'], tries=4), exits(0)) assert len(mocked_wait.mock_calls) == 4 assert mocked_sleep.call_args_list == [ call(30), call(60), call(90) ] out, err = capsys.readouterr() # should fail three times and succeed in the forth time expected = \ ['[WARNING] Command echo hello failed, exit code: 3. Will retry in 30s [tried: 1/4]', '[WARNING] Command echo hello failed, exit code: 2. Will retry in 60s [tried: 2/4]', '[WARNING] Command echo hello failed, exit code: 1. Will retry in 90s [tried: 3/4]'] for expected_item in expected: assert expected_item in err
def test_get_state_with_error_other_than_enoent(tempdir): options = MagicMock(koji=None, source=tempfile.mkstemp(dir=tempdir)[1]) processor = BaseProcessor(options) processor.workdir = tempdir # attempting to open state file raises generic IOError with patch('six.moves.builtins.open', autospec=True, side_effect=IOError): # error is raised by method because only ENOENT is handled assert_that(calling(processor.get_state), raises(IOError))
def test_srpm_koji(mock_koji_session, mock_koji_pathinfo): mock_koji_session.return_value.getRPM.return_value = {'arch': 'src', 'build_id': 42} mock_koji_pathinfo.return_value.build.return_value = "test_build" mock_koji_pathinfo.return_value.rpm.return_value = "test_relpath" mock_options = MagicMock(koji=True, source="build_nvr.src.rpm") with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) assert_that(processor.source_file, equal_to("test_build/test_relpath"))
def test_read_srpm_input_error(mock_koji_session, mock_koji_pathinfo): mock_koji_session.return_value.getRPM.return_value = {'arch': 'src', 'build_id': 42} mock_koji_pathinfo.return_value.build.return_value = "test_build" mock_koji_pathinfo.return_value.rpm.return_value = "test_relpath" with patch('koji.get_rpm_header'): mock_options = MagicMock(koji=True, source="build_nvr.src.rpm") with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) assert_that(calling(processor.read_srpm), raises(InputError))
def test_module_src_koji(mock_koji_session, mock_koji_pathinfo): binfo = {'extra': {'typeinfo': {'module': {'modulemd_str': "foo_module_str"}}}} mock_koji_session.return_value.getBuild.return_value = binfo mock_koji_pathinfo.return_value.typedir.return_value = "test_build/files/module/" mock_options = MagicMock(koji=True, source="build_nvr:modulemd.src.txt") with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) assert_that(processor.source_file, equal_to("test_build/files/module/modulemd.src.txt")) assert_that(processor.mmd, equal_to("foo_module_str"))
def test_default_tries(cmd, expected): """ test different number of tries for different commands. If the command trys to communicate with remote server e.g. git clone, then the retries is set to 4, else it's defaulted to 1 """ mock_options = MagicMock(koji=False) with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) assert processor.default_tries(cmd) == expected
def test_read_mmd_str(mock_koji_session, mock_koji_pathinfo): mmd_str, mmd_dict = get_test_mmd_str_and_dict() binfo = {'extra': {'typeinfo': {'module': {'modulemd_str': mmd_str}}}} mock_koji_session.return_value.getBuild.return_value = binfo mock_options = MagicMock(koji=True, source="build_nvr:modulemd.src.txt") with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) processor.source_file = os.path.join(MODULES_PATH, "modulemd.src.txt") processor.read_source_file() assert_that(processor.package, equal_to(mmd_dict['name'])) assert_that(processor.version, equal_to(mmd_dict['stream'])) assert_that(processor.release, equal_to(str(mmd_dict['version']) + '.' + mmd_dict['context'])) assert_that(processor.summary, equal_to(mmd_dict['summary']))
def test_git_url_module(mock_koji_session, mock_koji_pathinfo): mmd_str, mmd_dict = get_test_mmd_str_and_dict() binfo = {'extra': {'typeinfo': {'module': {'modulemd_str': mmd_str}}}} mock_koji_session.return_value.getBuild.return_value = binfo mock_options = MagicMock(koji=True, source="build_nvr:modulemd.src.txt", config=CONFIG_DEFAULTS) with patch('os.path.isfile', return_value=True): processor = BaseProcessor(mock_options) processor.source_file = os.path.join(MODULES_PATH, "modulemd.src.txt") processor.read_source_file() git_push_url = processor.git_push_url() git_fetch_url = processor.git_fetch_url() assert_that(git_push_url, equal_to(processor.options.config['git_push_url_module'] % {'package': mmd_dict['name']})) assert_that(git_fetch_url, equal_to(processor.options.config['git_push_url_module'] % {'package': mmd_dict['name']}))