def test_pull_base_base_parse(reactor_config_map, inspect_only): # noqa flexmock(ImageName).should_receive('parse').and_raise(AttributeError) with pytest.raises(AttributeError): test_pull_base_image_plugin(LOCALHOST_REGISTRY, BASE_IMAGE, [BASE_IMAGE_W_REGISTRY], [BASE_IMAGE_W_LIB_REG], reactor_config_map=reactor_config_map, inspect_only=inspect_only)
def test_create_simple_session(self): url = 'https://koji-hub-url.com' session = flexmock() (flexmock(koji_util.koji) .should_receive('ClientSession').with_args(url).and_return(session)) assert create_koji_session(url) == session
def prepare(self, workflow): # Setup expected platforms workflow.buildstep_plugins_conf[0]['args']['platforms'] = ['x86_64', 'ppc64le'] workflow.prebuild_results[PLUGIN_CHECK_AND_SET_PLATFORMS_KEY] = set(['x86_64', 'ppc64le']) # Setup platform descriptors workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({ 'version': 1, 'source_registry': {'url': 'registry.example.com', 'insecure': True}, 'platform_descriptors': [{'platform': 'x86_64', 'architecture': 'amd64'}], }) # Setup multi-arch manifest list manifest_list = { 'manifests': [ {'platform': {'architecture': 'amd64'}, 'digest': 'sha256:123456'}, {'platform': {'architecture': 'ppc64le'}, 'digest': 'sha256:654321'}, ] } (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .and_return(flexmock(json=lambda: manifest_list, content=json.dumps(manifest_list).encode('utf-8')))) return workflow
def test_empty_barcode(self): catalog = flexmock() display = flexmock() sale_controller = SaleController(catalog, display) display.should_receive("display_empty_barcode_message").once sale_controller.on_barcode("")
def test_unknown_product(self): catalog = flexmock(find_price = lambda _: None) display = flexmock() sale_controller = SaleController(catalog, display) display.should_receive("display_product_not_found_message").with_args("12345").once sale_controller.on_barcode("12345")
def test_create_authenticated_session(self): url = 'https://koji-hub-url.com' session = flexmock() session.should_receive('krb_login').once().and_return(True) (flexmock(koji_util.koji) .should_receive('ClientSession').with_args(url).and_return(session)) assert create_koji_session(url, {}) == session
def getOpenMock(): """ Mocks FileUtils to return a mock, and returns that mock from this function so you can run expectations on it, etc. """ fileMock = flexmock() flexmock(FileUtils).should_receive("open").and_return(fileMock) return fileMock
def test_context_manager_on_instance(self): class CM(object): def __enter__(self): pass def __exit__(self, *_): pass cm = CM() flexmock(cm).should_call('__enter__').once flexmock(cm).should_call('__exit__').once with cm: pass self._tear_down()
def test_context_manager_on_class(self): class CM(object): def __enter__(self): pass def __exit__(self, *_): pass cm = CM() flexmock(CM).should_receive('__enter__').once flexmock(CM).should_receive('__exit__').once with cm: pass self._tear_down()
def test_known_product(self): price = Price.euro(12) catalog = flexmock(find_price = lambda _: price) display = flexmock() sale_controller = SaleController(catalog, display) display.should_receive("display_price").with_args(price).once sale_controller.on_barcode("12345")
def test_builtin_open(self): if sys.version_info < (3, 0): mock = flexmock(sys.modules['__builtin__']) else: mock = flexmock(sys.modules['builtins']) fake_fd = flexmock(read=lambda: 'some data') mock.should_receive('open').once.with_args('file_name').and_return(fake_fd) with open('file_name') as f: data = f.read() self.assertEqual('some data', data)
def workflow_callback(workflow): workflow = self.prepare(workflow) manifest_list = { 'manifests': [ {'platform': {'architecture': 'amd64'}, 'digest': 'sha256:123456'}, ] } (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .and_return(flexmock(json=lambda: manifest_list))) return workflow
def test_daemon(config_dir): args = flexmock(config_dir=config_dir.strpath, foreground=True, verbose=True) d = flexmock(daemon) d.should_receive('_parse_arguments').and_return(args) d.should_receive('_check_privileges') k = flexmock(KnockWatcher) k.should_receive('tail_and_process') d.main()
def test_log_encoding(self, caplog, monkeypatch): if MOCK: mock_docker() (flexmock(InputPluginsRunner) .should_receive('run') .and_raise(RuntimeError)) monkeypatch.setenv('LC_ALL', 'en_US.UTF-8') command = [ "main.py", "--verbose", "inside-build", ] with caplog.atLevel(logging.INFO): with pytest.raises(RuntimeError): self.exec_cli(command) # first message should be 'log encoding: <encoding>' match = caplog.records()[0].message.split(':') if not match: raise RuntimeError encoding = codecs.getreader(match[1]) assert encoding == encodings.utf_8.StreamReader
def test_tagging(self, task_state, failure): session = flexmock() task_id = 9876 build_id = 1234 target_name = 'target' tag_name = 'images-candidate' target_info = {'dest_tag_name': tag_name} task_info = {'state': koji.TASK_STATES[task_state]} (session .should_receive('getBuildTarget') .with_args(target_name) .and_return(target_info)) (session .should_receive('tagBuild') .with_args(tag_name, build_id) .and_return(task_id)) (session .should_receive('taskFinished') .with_args(task_id) .and_return(True)) (session .should_receive('getTaskInfo') .with_args(task_id, request=True) .and_return(task_info)) if failure: with pytest.raises(RuntimeError): tag_koji_build(session, build_id, target_name) else: build_tag = tag_koji_build(session, build_id, target_name) assert build_tag == tag_name
def test_retry_pull_base_image(exc, failures, should_succeed): if MOCK: mock_docker(remember_images=True) tasker = DockerTasker() workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image') workflow.builder = MockBuilder() workflow.builder.base_image = ImageName.parse('parent-image') class MockResponse(object): content = '' expectation = flexmock(tasker).should_receive('tag_image') for _ in range(failures): expectation = expectation.and_raise(exc('', MockResponse())) expectation.and_return('foo') expectation.and_return('parent-image') runner = PreBuildPluginsRunner( tasker, workflow, [{ 'name': PullBaseImagePlugin.key, 'args': {'parent_registry': 'registry.example.com', 'parent_registry_insecure': True}, }], ) if should_succeed: runner.run() else: with pytest.raises(Exception): runner.run()
def test_wait_for_compose(odcs_client, final_state_id, final_state_name, expect_exc, state_reason): state = {'count': 1} def handle_composes_get(request): assert_request_token(request, odcs_client.session) if state['count'] == 1: response_json = compose_json(1, 'generating') else: response_json = compose_json(final_state_id, final_state_name, state_reason=state_reason) state['count'] += 1 return (200, {}, response_json) responses.add_callback(responses.GET, '{}composes/{}'.format(ODCS_URL, COMPOSE_ID), content_type='application/json', callback=handle_composes_get) (flexmock(time) .should_receive('sleep') .and_return(None)) if expect_exc: with pytest.raises(RuntimeError) as exc_info: odcs_client.wait_for_compose(COMPOSE_ID) assert expect_exc in str(exc_info.value) else: odcs_client.wait_for_compose(COMPOSE_ID)
def workflow_callback(workflow): workflow = self.prepare(workflow) (flexmock(atomic_reactor.util) .should_receive('get_config_from_registry') .and_raise(exception('', response=MockResponse())) .once()) manifest_tag = 'registry.example.com' + '/' + BASE_IMAGE_W_SHA base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .with_args(image=manifest_image, registry=manifest_image.registry, insecure=True, dockercfg_path=None) .and_return(None) .once()) return workflow
def test_try_with_library_pull_base_image(library, reactor_config_map): if MOCK: mock_docker(remember_images=True) tasker = DockerTasker(retry_times=0) workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image') workflow.builder = MockBuilder() if library: base_image = 'library/parent-image' else: base_image = 'parent-image' workflow.builder.base_image = ImageName.parse(base_image) workflow.builder.parent_images = {ImageName.parse(base_image): None} class MockResponse(object): content = '' cr = CommandResult() cr._error = "cmd_error" cr._error_detail = {"message": "error_detail"} if library: call_wait = 1 else: call_wait = 2 (flexmock(atomic_reactor.util) .should_receive('wait_for_command') .times(call_wait) .and_return(cr)) error_message = 'registry.example.com/' + base_image if reactor_config_map: workflow.plugin_workspace[ReactorConfigPlugin.key] = {} workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({'version': 1, 'source_registry': {'url': 'registry.example.com', 'insecure': True}}) runner = PreBuildPluginsRunner( tasker, workflow, [{ 'name': PullBaseImagePlugin.key, 'args': {'parent_registry': 'registry.example.com', 'parent_registry_insecure': True}, }], ) with pytest.raises(PluginFailedException) as exc: runner.run() assert error_message in exc.value.args[0]
def test_koji_login_krb_keyring(self, proxyuser): session = flexmock() expectation = session.should_receive('krb_login').once().and_return(True) kwargs = {} if proxyuser is not None: expectation.with_args(proxyuser=proxyuser) kwargs['proxyuser'] = proxyuser else: expectation.with_args() koji_login(session, **kwargs)
def workflow_callback(workflow): workflow = self.prepare(workflow) if fail: # fail to provide x86_64 platform specific digest manifest_list = { 'manifests': [] } (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .and_return(flexmock(json=lambda: manifest_list, content=json.dumps(manifest_list).encode('utf-8'))) ) # platform validation will fail if manifest is missing # setting only one platform to skip platform validation and test negative case workflow.buildstep_plugins_conf[0]['args']['platforms'] = ['x86_64'] workflow.prebuild_results[PLUGIN_CHECK_AND_SET_PLATFORMS_KEY] = set(['x86_64']) test_vals['workflow'] = workflow return workflow
def test_cancel(self): session = flexmock() task_id = 1234 (session .should_receive('taskFinished') .with_args(task_id) .and_raise(BuildCanceledException)) task = TaskWatcher(session, task_id, poll_interval=0) with pytest.raises(BuildCanceledException): task.wait() assert task.failed()
def test_remove_built_image_plugin(self, remove_base, deferred, expected): tasker, workflow = mock_environment() runner = PostBuildPluginsRunner( tasker, workflow, [{ 'name': GarbageCollectionPlugin.key, 'args': {'remove_pulled_base_image': remove_base}, }] ) removed_images = [] def spy_remove_image(image_id, force=None): removed_images.append(image_id) flexmock(tasker, remove_image=spy_remove_image) for image in deferred: defer_removal(workflow, image) output = runner.run() image_set = set(removed_images) assert len(image_set) == len(removed_images) assert image_set == expected
def test_output_as_generator(self): contents = 'this is the simulated file contents' session = flexmock() expectation = session.should_receive('downloadTaskOutput') for chunk in contents: expectation = expectation.and_return(chunk) # Empty content to simulate end of stream. expectation.and_return('') streamer = koji_util.stream_task_output(session, 123, 'file.ext') assert ''.join(list(streamer)) == contents
def test_koji_login_ssl(self, proxyuser): session = flexmock() expectation = session.should_receive('ssl_login').once().and_return(True) call_kwargs = { 'ssl_certs_dir': '/certs', } exp_kwargs = {} if proxyuser: call_kwargs['proxyuser'] = proxyuser exp_kwargs['proxyuser'] = proxyuser expectation.with_args('/certs/cert', '/certs/ca', '/certs/serverca', **exp_kwargs) koji_login(session, **call_kwargs)
def workflow_callback(workflow): workflow = self.prepare(workflow) workflow.prebuild_results[PLUGIN_CHECK_AND_SET_PLATFORMS_KEY] = set(['ppc64le']) release = 'rel1' version = 'ver1' config_blob = {'config': {'Labels': {'release': release, 'version': version}}} (flexmock(atomic_reactor.util) .should_receive('get_config_from_registry') .and_return(config_blob) .times(0)) manifest_tag = 'registry.example.com' + '/' + BASE_IMAGE_W_SHA base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .with_args(image=manifest_image, registry=manifest_image.registry, insecure=True, dockercfg_path=None) .and_return(flexmock(json=lambda: manifest_list, content=json.dumps(manifest_list).encode('utf-8'))) .once()) return workflow
def test_result_backend_issue(self): # # flow1: # # Task1 # # Checks that if there is an issue with result backend, dispatcher retries as there is no info on how # to continue edge_table = { 'flow1': [{'from': [], 'to': ['Task1'], 'condition': self.cond_true}] } self.init( edge_table, storage_mapping={'Storage1': self.MyStorage()}, task2storage_mapping={'Task1': 'Storage1'} ) node_args = {'Foo': 'bar'} system_state = SystemState(id(self), 'flow1', node_args=node_args) retry = system_state.update() state_dict = system_state.to_dict() assert retry is not None task1 = self.get_task('Task1') self.set_finished(task1, "some result") flexmock(AsyncResult).\ should_receive('successful').\ and_raise(ValueError, "Some error raised due to result backed issues") with pytest.raises(DispatcherRetry) as exc_info: SystemState(id(self), 'flow1', state=state_dict, node_args=system_state.node_args).update() assert exc_info.value.keep_state is True assert exc_info.value.adjust_retry_count is False
def test_wait(self, finished, info, exp_state, exp_failed): session = flexmock() task_id = 1234 task_finished = (session.should_receive('taskFinished') .with_args(task_id)) for finished_value in finished: task_finished = task_finished.and_return(finished_value) (session.should_receive('getTaskInfo') .with_args(task_id, request=True) .once() .and_return(info)) task = TaskWatcher(session, task_id, poll_interval=0) assert task.wait() == exp_state assert task.failed() == exp_failed
def workflow_callback(workflow): workflow = self.prepare(workflow) workflow.prebuild_results[PLUGIN_CHECK_AND_SET_PLATFORMS_KEY] = set(['x86_64']) if not has_manifest_list: resp = {'v2': StubResponse()} if has_v2s2_manifest else {} flexmock(atomic_reactor.util).should_receive('get_manifest_list').and_return(None) flexmock(atomic_reactor.util).should_receive('get_all_manifests').and_return(resp) if has_v2s2_manifest: dgst = {'sha256sum': 'stubDigest'} flexmock(atomic_reactor.util).should_receive('get_checksums').and_return(dgst) return workflow
def workflow_callback(workflow): workflow = self.prepare(workflow) release = 'rel1' version = 'ver1' config_blob = {'config': {'Labels': {'release': release, 'version': version}}} (flexmock(atomic_reactor.util) .should_receive('get_config_from_registry') .and_return(config_blob) .times(0 if sha_is_manifest_list else 1)) manifest_list = { 'manifests': [ {'platform': {'architecture': 'amd64'}, 'digest': 'sha256:123456'}, {'platform': {'architecture': 'ppc64le'}, 'digest': 'sha256:654321'}, ] } manifest_tag = 'registry.example.com' + '/' + BASE_IMAGE_W_SHA base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() if sha_is_manifest_list: (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .with_args(image=manifest_image, registry=manifest_image.registry, insecure=True, dockercfg_path=None) .and_return(flexmock(json=lambda: manifest_list, content=json.dumps(manifest_list).encode('utf-8'))) .once()) else: (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .with_args(image=manifest_image, registry=manifest_image.registry, insecure=True, dockercfg_path=None) .and_return(None) .once() .ordered()) docker_tag = "%s-%s" % (version, release) manifest_tag = 'registry.example.com' + '/' +\ BASE_IMAGE_W_SHA[:BASE_IMAGE_W_SHA.find('@sha256')] +\ ':' + docker_tag base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .with_args(image=manifest_image, registry=manifest_image.registry, insecure=True, dockercfg_path=None) .and_return(flexmock(json=lambda: manifest_list)) .once() .ordered()) return workflow
def test_push_with_io_error(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) # preventing tests from executing real commands flexmock( distgit.exectools).should_receive("cmd_gather").and_return(None) # pretending cmd_assert raised an IOError flexmock(distgit.exectools).should_receive("cmd_assert").and_raise( IOError("io-error")) metadata = flexmock( runtime=flexmock( global_opts={"rhpkg_push_timeout": "_irrelevant_"}, branch="_irrelevant_"), config=flexmock(distgit=flexmock(branch="_irrelevant_")), name="_irrelevant_", logger=flexmock(info=lambda *_: None)) expected = (metadata, "IOError('io-error',)") actual = distgit.ImageDistGitRepo(metadata, autoclone=False).push() self.assertEqual(expected, actual)
def predictor_mock_class() -> type: """Return a predictor mock class.""" flexmock(PredictorMock) return PredictorMock
def __setDirListReturn(self, generatedList): flexmock(Directory).should_receive('getDirList').and_return( generatedList).and_return(self.DEFAULT_LIST)
def test_verify_wrong_signature(capsys): req = flexmock(requests.Request(), headers={'Signature': 'sha1=8dd7186bd499f38cfe5f3b829e9f931f105e760f'}) req.data=b'neplatne' assert not verify_signature(req)
def test_push_image_insecure_source(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) flexmock(distgit.os.path).should_receive("isdir").and_return(True) flexmock(distgit.os.path).should_receive("isfile").and_return(True) flexmock( distgit.os).should_receive("remove").replace_with(lambda _: None) (flexmock( sys.modules["__builtin__"]).should_receive("open").and_return( flexmock(write=lambda *_: None, readlines=lambda *_: []))) expected_cmd = "oc image mirror --filter-by-os=amd64 --insecure=true --filename=some-workdir/push/my-distgit-key" (flexmock(distgit.exectools).should_receive("cmd_gather").with_args( expected_cmd).once().and_return((0, "", ""))) metadata = flexmock( config=flexmock(push=flexmock(late=distgit.Missing), name="my-name", namespace="my-namespace", distgit=flexmock(branch="_irrelevant_")), runtime=self.mock_runtime(), distgit_key="my-distgit-key", name="my-name", get_default_push_names=lambda *_: ["my-default-name"], get_additional_push_names=lambda *_: [], logger=flexmock(info=lambda *_: None), namespace="_irrelevant_") metadata.runtime.working_dir = "some-workdir" metadata.runtime.group_config.insecure_source = True repo = distgit.ImageDistGitRepo(metadata, autoclone=False) tag_list = ["tag-a", "tag-b"] push_to_defaults = True expected = ("my-distgit-key", True) actual = repo.push_image(tag_list, push_to_defaults, version_release_tuple=("version", "release")) self.assertEqual(expected, actual)
def test_commit_without_source_sha(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) expected_1st_cmd = ["git", "add", "-A", "."] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_1st_cmd).once().ordered()) expected_2nd_cmd = [ "git", "commit", "--allow-empty", "-m", "commit msg\n- MaxFileSize: 50000000" ] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_2nd_cmd).once().ordered()) (flexmock(distgit.exectools).should_receive("cmd_gather").with_args( ["git", "rev-parse", "HEAD"]).and_return( (0, "sha-from-stdout", ""))) metadata = flexmock( distgit_key="my-distgit-key", runtime=flexmock(local=False, branch="_irrelevant_", add_distgits_diff=lambda: None), config=flexmock(distgit=flexmock(branch="_irrelevant_")), logger=flexmock(info=lambda _: None), name="_irrelevant_") repo = distgit.DistGitRepo(metadata, autoclone=False) self.assertEqual("sha-from-stdout", repo.commit("commit msg"))
def test_init_with_autoclone(self): flexmock(distgit.DistGitRepo).should_receive("clone").once() distgit.DistGitRepo(self.md)
def test_source_path_without_sub_path(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock(distgit.os.path).should_receive("isdir").and_return(True) metadata = flexmock( runtime=flexmock(resolve_source=lambda *_: "source-root", branch="_irrelevant_"), config=flexmock( content=flexmock(source=flexmock(path=distgit.Missing)), distgit=flexmock(branch="_irrelevant_")), logger=flexmock(info=lambda _: None), config_filename="_irrelevant_", name="_irrelevant_") repo = distgit.DistGitRepo(metadata, autoclone=False) self.assertEqual("source-root", repo.source_path())
def test_merge_branch_allow_overwrite(self): # pretenting there is no Dockerfile nor .oit directory flexmock(distgit.os.path).should_receive("isfile").and_return(False) flexmock(distgit.os.path).should_receive("isdir").and_return(False) expected_1st_log_msg = "Switching to branch: my-target" logger = flexmock() logger.should_receive("info").with_args( expected_1st_log_msg).once().ordered() expected_1st_cmd = ["rhpkg", "switch-branch", "my-target"] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_1st_cmd, retries=3).once().ordered()) expected_2nd_log_msg = "Merging source branch history over current branch" logger.should_receive("info").with_args( expected_2nd_log_msg).once().ordered() expected_2nd_cmd = [ "git", "merge", "--allow-unrelated-histories", "-m", "Merge branch my-branch into my-target", "my-branch" ] expected_on_retry = ["git", "reset", "--hard", "my-target"] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_2nd_cmd, retries=3, on_retry=expected_on_retry).once().ordered()) metadata = flexmock( config=flexmock(distgit=flexmock(branch="my-branch")), logger=logger, runtime=flexmock(branch="_irrelevant_"), name="_irrelevant_") (distgit.DistGitRepo(metadata, autoclone=False).merge_branch( "my-target", allow_overwrite=True))
def testGenerateTwoConf(self): generatedList = ["general.ini"] self.__setDirListReturn(generatedList) flexmock(shutil).should_receive('copy').times(2) self.configGenerator.generateMissing()
def test_clone_images_build_command(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) # pretenting the directory doesn't exist (not yet cloned) flexmock(distgit.os.path).should_receive("isdir").and_return(False) expected_log_msg = ("Cloning distgit repository [branch:my-branch] " "into: my-root-dir/my-namespace/my-distgit-key") logger = flexmock() logger.should_receive("info").with_args(expected_log_msg).once() expected_cmd = [ "timeout", "999", "rhpkg", "clone", "my-qualified-name", "my-root-dir/my-namespace/my-distgit-key", "--branch", "my-branch" ] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_cmd, retries=3).once().and_return(None)) expected_warning = ("Warning: images:rebase was skipped and " "therefore your local build will be sourced " "from the current dist-git contents and not " "the typical GitHub source. ") (flexmock(distgit).should_receive("yellow_print").with_args( expected_warning).once()) metadata = flexmock(config=MockConfig(content="_irrelevant_"), runtime=flexmock( local=False, command="images:build", global_opts={"rhpkg_clone_timeout": 999}, user=None, branch="_irrelevant_"), namespace="my-namespace", distgit_key="my-distgit-key", qualified_name="my-qualified-name", logger=logger, name="_irrelevant_") distgit.DistGitRepo(metadata, autoclone=False).clone("my-root-dir", "my-branch")
def setUp(self): (flexmock(github.support) .should_receive('resource_exists') .and_return(True))
def test_clone_with_fake_distgit(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) # pretenting the directory doesn't exist (not yet cloned) flexmock(distgit.os.path).should_receive("isdir").and_return(False) expected_log_msg = ("Creating local build dir: " "my-root-dir/my-namespace/my-distgit-key") logger = flexmock() logger.should_receive("info").with_args(expected_log_msg).once() expected_cmd = [ "mkdir", "-p", "my-root-dir/my-namespace/my-distgit-key" ] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_cmd).once()) metadata = flexmock(config=MockConfig(content="_irrelevant_"), runtime=flexmock(local=True, command="images:rebase", branch="_irrelevant_"), namespace="my-namespace", distgit_key="my-distgit-key", logger=logger, name="_irrelevant_") distgit.DistGitRepo(metadata, autoclone=False).clone("my-root-dir", "my-branch")
def workflow_callback(workflow): workflow = self.prepare(workflow) release = 'rel1' version = 'ver1' config_blob = { 'config': { 'Labels': { 'release': release, 'version': version } } } (flexmock( atomic_reactor.util).should_receive('get_config_from_registry') .and_return(config_blob).times(0 if sha_is_manifest_list else 1)) manifest_list = { 'manifests': [ { 'platform': { 'architecture': 'amd64' }, 'digest': 'sha256:123456' }, { 'platform': { 'architecture': 'ppc64le' }, 'digest': 'sha256:654321' }, ] } manifest_tag = 'registry.example.com' + '/' + BASE_IMAGE_W_SHA base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() if sha_is_manifest_list: (flexmock(atomic_reactor.util).should_receive( 'get_manifest_list').with_args( image=manifest_image, registry=manifest_image.registry, insecure=True).and_return( flexmock(json=lambda: manifest_list)).once()) else: (flexmock(atomic_reactor.util).should_receive( 'get_manifest_list').with_args( image=manifest_image, registry=manifest_image.registry, insecure=True).and_return(None).once().ordered()) docker_tag = "%s-%s" % (version, release) manifest_tag = 'registry.example.com' + '/' +\ BASE_IMAGE_W_SHA[:BASE_IMAGE_W_SHA.find('@sha256')] +\ ':' + docker_tag base_image_result = ImageName.parse(manifest_tag) manifest_image = base_image_result.copy() (flexmock(atomic_reactor.util).should_receive( 'get_manifest_list').with_args( image=manifest_image, registry=manifest_image.registry, insecure=True).and_return( flexmock( json=lambda: manifest_list)).once().ordered()) return workflow
def test_push_image_without_version_release_tuple(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) flexmock(distgit.os.path).should_receive("isdir").and_return(True) flexmock(distgit.os.path).should_receive("isfile").and_return(True) flexmock( distgit.os).should_receive("remove").replace_with(lambda _: None) (flexmock( sys.modules["__builtin__"]).should_receive("open").and_return( flexmock(write=lambda *_: None, readlines=lambda *_: []))) # preventing tests from executing real commands flexmock(distgit.exectools).should_receive("cmd_gather").and_return( (0, "", "")) metadata = flexmock( config=flexmock(push=flexmock(late=distgit.Missing), name="my-name", namespace="my-namespace", distgit=flexmock(branch="_irrelevant_")), runtime=self.mock_runtime(), distgit_key="my-distgit-key", name="my-name", get_latest_build_info=lambda *_: ("_", "my-version", "my-release"), get_default_push_names=lambda *_: ["my-default-name"], get_additional_push_names=lambda *_: [], logger=flexmock(info=lambda *_: None), namespace="_irrelevant_") repo = distgit.ImageDistGitRepo(metadata, autoclone=False) tag_list = ["tag-a", "tag-b"] push_to_defaults = True expected = ("my-distgit-key", True) actual = repo.push_image(tag_list, push_to_defaults) self.assertEqual(expected, actual)
def github(): return flexmock( token='XXX', login='******', create_repo=blank_fn )
def test_verify_right_signature(capsys): req = flexmock(requests.Request(), headers={'Signature': 'sha1=aa6be43a6f16941bc73885ac233bc5c2227eb6f7'}) req.data=b'spravne' assert verify_signature(req)
def test_main_install_params_default(): """Test running install from main with default parameters set.""" flexmock(micropipenv).should_receive("install").with_args( deploy=True, dev=True, pip_args=[], method=None).once() micropipenv.main(argv=["install", "--deploy", "--dev"])
def pipeline_config() -> PipelineConfig: """A fixture for a pipeline configuration with few representatives of each pipeline unit type.""" flexmock(PipelineConfig) flexmock(Boot1) flexmock(Sieve1) flexmock(Step1) flexmock(Stride1) flexmock(Wrap1) return PipelineConfig( boots=[Boot1()], sieves=[Sieve1()], steps=[Step1()], strides=[Stride1()], wraps=[Wrap1()], )
def test_push_zipfile_with_package_name_suffix( mocked_org_manager, client, valid_manifests_archive, endpoint_push_zipfile, mocked_quay_io, mocked_op_courier_push, auth_header, suffix_func, expected_pkg_name_func): """Test REST API for pushing operators form zipfile with package_name_suffix""" original_pkg_name = valid_manifests_archive.pkg_name expected_pkg_name = expected_pkg_name_func(original_pkg_name) mocked_org_manager.get_org.return_value = QuayOrganization( endpoint_push_zipfile.org, 'cnr_token', package_name_suffix=suffix_func(original_pkg_name)) def verify_modified_package_name(repo, version, source_dir): # The modified YAML file should retain comments and defined order. The # only modification should be the package name. if valid_manifests_archive.pkg_name == 'marketplace': pkg_manifest = ('deploy/chart/catalog_resources/rh-operators/' 'marketplace.v0.0.1.clusterserviceversion.yaml') expected_packages_yaml = dedent("""\ #! package-manifest: {pkg_manifest} packageName: {pkg_name} channels: - name: alpha currentCSV: marketplace-operator.v0.0.1 """) packages_yaml_path = os.path.join(source_dir, 'packages.yaml') elif valid_manifests_archive.pkg_name == 'etcd': pkg_manifest = ('./deploy/chart/catalog_resources/rh-operators/' 'etcdoperator.v0.9.2.clusterserviceversion.yaml') expected_packages_yaml = dedent("""\ #! package-manifest: {pkg_manifest} packageName: {pkg_name} channels: - name: alpha currentCSV: etcdoperator.v0.9.2 """) packages_yaml_path = os.path.join(source_dir, 'etcd.package.yaml') else: raise ValueError( f'Unsupported manifests archive, {valid_manifests_archive}') expected_packages_yaml = expected_packages_yaml.format( pkg_manifest=pkg_manifest, pkg_name=expected_pkg_name) with open(packages_yaml_path) as f: assert f.read() == expected_packages_yaml flexmock(QuayOrganization)\ .should_receive('push_operator_manifest')\ .replace_with(verify_modified_package_name)\ .once() with open(valid_manifests_archive.path, 'rb') as f: rv = client.post( endpoint_push_zipfile.url_path, headers=auth_header, data={'file': (f, f.name)}, content_type='multipart/form-data', ) assert rv.status_code == 200, rv.get_json() # In V2, package_name_suffix also modifies the repository assert rv.get_json()['repo'] == expected_pkg_name
def predictor_mock() -> Predictor: """Return a mock for predictor.""" flexmock(PredictorMock) return PredictorMock()
def test_get_filtered_list_fail(self): """Ensure we notice invalid erratum lists""" (flexmock(errata.requests).should_receive("get").and_return( flexmock(status_code=404, text="_irrelevant_"))) self.assertRaises(exceptions.ErrataToolError, errata.get_filtered_list)
def mock_builtin_open(file, contents): mock = flexmock(get_builtin_module()) mock.should_call('open') (mock.should_receive('open') .with_args(file) .and_return(flexmock(read=lambda: contents)))
def test_edux_page_prev(rev_fixture, challenge, expected): session = flexmock( get=flexmock(raise_for_status=lambda: None, text=rev_fixture)) result = edux_page_prev('whatever', session, challenge) assert result == expected
def workflow_callback(workflow): workflow = self.prepare(workflow) (flexmock(atomic_reactor.util) .should_receive('get_manifest_list') .and_return(None)) return workflow
def test_push_image_fail_to_create_a_push_config_dir(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock(distgit.os.path).should_receive("isdir").and_return(False) flexmock(distgit.os.path).should_receive("isfile").and_return(True) flexmock( distgit.os).should_receive("remove").replace_with(lambda _: None) (flexmock( sys.modules["__builtin__"]).should_receive("open").and_return( flexmock(write=lambda *_: None, readlines=lambda *_: []))) # simulating an error on mkdir attempt flexmock(distgit.os).should_receive("mkdir").and_raise(OSError) # preventing tests from executing real commands flexmock(distgit.exectools).should_receive("cmd_gather").and_return( (0, "", "")) metadata = flexmock( config=flexmock(push=flexmock(late=distgit.Missing), name="my-name", namespace="my-namespace", distgit=flexmock(branch="_irrelevant_")), runtime=self.mock_runtime(), distgit_key="my-distgit-key", name="my-name", namespace="my-namespace", get_default_push_names=lambda *_: ["my-default-name"], get_additional_push_names=lambda *_: [], logger=flexmock(info=lambda *_: None)) repo = distgit.ImageDistGitRepo(metadata, autoclone=False) tag_list = ["tag-a", "tag-b"] push_to_defaults = True version_release_tuple = ("version", "release") self.assertRaises(OSError, repo.push_image, tag_list, push_to_defaults, version_release_tuple=version_release_tuple)
def setUp(self): flexmock(os).should_receive('listdir').and_return(self.DEFAULT_LIST) flexmock(os.path).should_receive('isfile').and_return(True) self.dirUnderTest = Directory(self.DIR_ABS_PATH)
def test_push_image_to_defaults_fail_mirroring_with_lstate(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) flexmock(distgit.os.path).should_receive("isdir").and_return(True) flexmock(distgit.os.path).should_receive("isfile").and_return(True) flexmock( distgit.os).should_receive("remove").replace_with(lambda _: None) (flexmock( sys.modules["__builtin__"]).should_receive("open").and_return( flexmock(write=lambda *_: None, readlines=lambda *_: []))) # simulating an error while executing cmd_gather (flexmock(distgit.exectools).should_receive("cmd_gather").and_return( (1, "", "stderr"))) # a failed cmd_gather will try again in X seconds, we don't want to wait flexmock( distgit.time).should_receive("sleep").replace_with(lambda *_: None) logger = flexmock(info=lambda *_: None) metadata = flexmock( config=flexmock(push=flexmock(late=distgit.Missing), name="my-name", namespace="my-namespace", distgit=flexmock(branch="_irrelevant_")), runtime=self.mock_runtime(), distgit_key="my-distgit-key", name="my-name", namespace="my-namespace", get_default_push_names=lambda *_: ["my-default-name"], get_additional_push_names=lambda *_: [], logger=logger) metadata.runtime.state = {"images:push": "my-runtime-state"} metadata.runtime.command = "images:push" metadata.runtime.logger = logger (flexmock(distgit.state).should_receive("record_image_fail").with_args( "my-runtime-state", metadata, "Build failure", logger).once().ordered().and_return(None)) expected_msg = "Error pushing image: stderr" (flexmock(distgit.state).should_receive("record_image_fail").with_args( "my-runtime-state", metadata, expected_msg, logger).once().ordered().and_return(None)) repo = distgit.ImageDistGitRepo(metadata, autoclone=False) tag_list = ["tag-a", "tag-b"] push_to_defaults = True version_release_tuple = ("version", "release") try: repo.push_image(tag_list, push_to_defaults, version_release_tuple=version_release_tuple) self.fail("Should have raised an exception, but didn't") except IOError as e: self.assertEqual(expected_msg, e.message)
def test_clone_cmd_with_user(self): # preventing tests from interacting with the real filesystem flexmock(distgit).should_receive("Dir").and_return( flexmock(__exit__=None)) flexmock( distgit.os).should_receive("mkdir").replace_with(lambda _: None) # pretenting the directory doesn't exist (not yet cloned) flexmock(distgit.os.path).should_receive("isdir").and_return(False) # avoid warning print in the middle of the test progress report flexmock(distgit).should_receive("yellow_print").replace_with( lambda _: None) expected_cmd = [ "timeout", "999", "rhpkg", "--user=my-user", "clone", "my-qualified-name", "my-root-dir/my-namespace/my-distgit-key", "--branch", "my-branch" ] (flexmock(distgit.exectools).should_receive("cmd_assert").with_args( expected_cmd, retries=3).once().and_return(None)) metadata = flexmock(config=MockConfig(content="_irrelevant_"), runtime=flexmock( local=False, command="images:build", global_opts={"rhpkg_clone_timeout": 999}, user="******", branch="_irrelevant_"), namespace="my-namespace", distgit_key="my-distgit-key", qualified_name="my-qualified-name", logger=flexmock(info=lambda _: None), name="_irrelevant_") distgit.DistGitRepo(metadata, autoclone=False).clone("my-root-dir", "my-branch")
def copr_helper(copr_url): """Create a mock CoprHelper, with a copr_client configured with 'copr_url'.""" helper = CoprHelper(flexmock()) helper._copr_client = flexmock(config={"copr_url": copr_url}) return helper