def test_search_distributor_with_relative_url(): controller = FakeController() dist1 = Distributor( id="yum_distributor", type_id="yum_distributor", repo_id="repo1", relative_url="relative/path", ) dist2 = Distributor( id="cdn_distributor", type_id="rpm_rsync_distributor", repo_id="repo1", relative_url="relative/path", ) repo1 = Repository(id="repo1", distributors=(dist1, dist2)) dist3 = Distributor( id="yum_distributor", type_id="yum_distributor", repo_id="repo2", relative_url="another/path", ) repo2 = Repository(id="repo2", distributors=(dist3, )) controller.insert_repository(repo1) controller.insert_repository(repo2) client = controller.client crit = Criteria.with_field("relative_url", Matcher.regex("relative/path")) found = client.search_distributor(crit).result().data assert sorted(found) == [dist2, dist1]
def test_search_content_unsupported_operator(populated_repo): """search_content using unsupported operators on content_type_id raises""" with pytest.raises(ValueError) as e: populated_repo.search_content( Criteria.with_field("content_type_id", Matcher.regex("foobar"))) assert "unsupported expression for content_type_id" in str(e.value)
def test_type(): """type is mapped correctly""" crit = Criteria.with_field("type", Matcher.regex("foobar")) assert filters_for_criteria(crit, Repository) == { "notes._repo-type": { "$regex": "foobar" } }
def test_field_regex_criteria(): """with_field with regex is translated to a mongo fragment as expected.""" assert filters_for_criteria( Criteria.with_field("some.field", Matcher.regex("abc"))) == { "some.field": { "$regex": "abc" } }
def test_complex_type_ids(client): """content searches raise if using criteria with unsupported operators on content_type_id""" repo = Repository(id="some-repo") repo.__dict__["_client"] = client with pytest.raises(ValueError) as e: repo.search_content( Criteria.with_field("content_type_id", Matcher.regex("foobar"))) assert "unsupported expression for content_type_id" in str(e.value)
def test_stringify_complex_criteria(): crit = Criteria.and_( Criteria.with_field("must-exist", Matcher.exists()), Criteria.with_field("foo", Matcher.equals("bar")), Criteria.true(), Criteria.or_( Criteria.with_field("foo", Matcher.regex("quux")), Criteria.with_field("other", Matcher.in_(["x", "y", "z"])), Criteria.with_field("num", Matcher.less_than(9000)), ), Criteria.with_unit_type(FileUnit), ) assert (str(crit) == "((must-exist EXISTS) AND foo=='bar' AND TRUE " "AND (foo=~/quux/ OR (other IN ['x', 'y', 'z']) OR num<9000) " "AND (content_type_id IN ['iso']))")
def test_search_mapped_field_regex(): """Can do regex search with fields subject to Python<=>Pulp conversion.""" controller = FakeController() repo1 = Repository(id="repo1", type="foobar") repo2 = Repository(id="repo2", type="foobaz") repo3 = Repository(id="repo3", type="quux") controller.insert_repository(repo1) controller.insert_repository(repo2) controller.insert_repository(repo3) client = controller.client crit = Criteria.with_field("type", Matcher.regex("fooba[rz]")) found = client.search_repository(crit).data assert sorted(found) == [repo1, repo2]
def _filtered_repo_distributors(self): published_before = self.args.published_before url_regex = self.args.repo_url_regex # define the criteria on available filters crit = [Criteria.true()] if published_before: crit.append( Criteria.with_field("last_publish", Matcher.less_than(published_before)) ) if url_regex: crit.append( Criteria.with_field("relative_url", Matcher.regex(url_regex.pattern)) ) crit = Criteria.and_(*crit) return self.pulp_client.search_distributor(crit)
def test_can_search_distributors_with_relative_url(client, requests_mocker): requests_mocker.post( "https://pulp.example.com/pulp/api/v2/distributors/search/", json=[ { "id": "yum_distributor", "distributor_type_id": "yum_distributor", "repo_id": "test_rpm", "config": { "relative_url": "relative/path" }, }, { "id": "cdn_distributor", "distributor_type_id": "rpm_rsync_distributor", "config": { "relative_url": "relative/path" }, }, ], ) crit = Criteria.with_field("relative_url", Matcher.regex("relative/path")) distributors_f = client.search_distributor(crit) distributors = [dist for dist in distributors_f.result()] # distributor objects are returned assert sorted(distributors) == [ Distributor( id="cdn_distributor", type_id="rpm_rsync_distributor", relative_url="relative/path", ), Distributor( id="yum_distributor", type_id="yum_distributor", repo_id="test_rpm", relative_url="relative/path", ), ] # api is called once assert requests_mocker.call_count == 1
def test_search_created_regex(): """Can search using regular expressions.""" controller = FakeController() when1 = datetime.datetime(2019, 6, 11, 14, 47, 0, tzinfo=None) when2 = datetime.datetime(2019, 3, 1, 1, 1, 0, tzinfo=None) when3 = datetime.datetime(2019, 6, 1, 1, 1, 0, tzinfo=None) repo1 = Repository(id="repo1", created=when1) repo2 = Repository(id="repo2", created=when2) repo3 = Repository(id="repo3", created=when3) repo4 = Repository(id="repo4") controller.insert_repository(repo1) controller.insert_repository(repo2) controller.insert_repository(repo3) controller.insert_repository(repo4) client = controller.client crit = Criteria.with_field("notes.created", Matcher.regex("19-06")) found = client.search_repository(crit) assert sorted(found) == [repo1, repo3]
def adjust_maintenance_report(self, report): to_add = [] if self.args.repo_ids: found_ids = self._ensure_repos_exist(self.args.repo_ids) to_add.extend(found_ids) if self.args.repo_url_regex: # search distributors with relative_url, get the repo id from distributors crit = Criteria.with_field( "relative_url", Matcher.regex(self.args.repo_url_regex.pattern) ) dists = self.pulp_client.search_distributor(crit).result() to_add.extend(set([dist.repo_id for dist in dists])) if to_add: LOG.info("Setting following repos to maintenance mode:") for repo_id in to_add: LOG.info(" - %s", repo_id) report = report.add( to_add, owner=self.args.owner, message=self.args.message ) return report
def test_matcher_regex_invalid(): """Matcher.regex raises if passed value is not a valid regular expression.""" with pytest.raises(re.error): Matcher.regex("foo [bar")
def test_matcher_regex_compiled(): """Matcher.regex raises if passed value is a compiled regex.""" with pytest.raises(TypeError): Matcher.regex(re.compile("foobar"))