Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite test_modify.py using pytest #333

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

git-hyagi
Copy link
Contributor

fixes: #326

@git-hyagi git-hyagi force-pushed the pytest-refactor-modify branch from f08d3f7 to b66dde9 Compare January 24, 2024 10:49
Copy link
Member

@lubosmj lubosmj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep on! Do you mind addressing the comments?

Comment on lines 21 to 38
@pytest.fixture
def synced_repo_version(
ostree_repositories_api_client,
ostree_repositories_versions_api_client,
ostree_repository_factory,
ostree_remote_factory,
monitor_task,
):
"""
Fixture that syncs a Remote ostree Repository,
waits until the sync task completes and returns the
created repository version object.
"""
repo = ostree_repository_factory()
remote = ostree_remote_factory(url=OSTREE_FIXTURE_URL, policy="immediate")
result = ostree_repositories_api_client.sync(repo.pulp_href, {"remote": remote.pulp_href})
repo_version_href = monitor_task(result.task).created_resources[0]
return ostree_repositories_versions_api_client.read(repo_version_href), remote, repo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a duplicate of

? If so, we can re-use that, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, kind of the same but instead of returning the repo we are now returning also the repo_version and remote.
At first, I thought that reusing it (by adding some conditionals or modifying the return) would compromise the readability/maintainability, but thinking again maybe not ... I'll check it!



@pytest.fixture
def modify_repo(bindings_cfg, monitor_task):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this fixture. Try using repository_api.modify() instead. It looks much more appealing!

https://github.com/pulp/pulpcore/blob/e2119307497f401de95e463e370787143c524016/pulpcore/tests/functional/api/test_repos.py#L39



@pytest.fixture
def get_content(bindings_cfg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can delete this.

In the past, we replaced get_content from pulp_smash by a plain call to content_api.list(repo.latest_version), see pulp/pulp_file@5c65d92#diff-082889e4107e892cad9e326317979e0a6e6b82a021fb85db8228af551797d6a0R99. Considering this, I propose adopting a similar strategy in the current context.

Furthermore, we will need to modify or remove the normalize_content function to parse results separately for each content type. Alternatively, we can make the function a fixture and fuse the content_api.list(repo.latest_version) logic into it, so we will have the downloading and normalizing at one place. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum... not sure if I understood it, but both options seem complicated. I'll do some tests and revisit/re-read the functions to have a better understanding of the proposals.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic idea is to use bindings to get things done. We should stray away from using requests to access API endpoints that are suited for administering. In this case, we want to get a list of content units from an API endpoint which is properly mapped by the bindings.

@git-hyagi git-hyagi force-pushed the pytest-refactor-modify branch 2 times, most recently from 86f50d6 to f027c34 Compare January 26, 2024 13:40
Copy link
Member

@lubosmj lubosmj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint errors should be fixed now. Please, rebase your commit on top of the main branch.

Comment on lines 20 to 24
commits = ostree_content_commits_api_client.list(repository_version=repo).results
configs = ostree_content_configs_api_client.list(repository_version=repo).results
objects = ostree_content_objects_api_client.list(repository_version=repo).results
refs = ostree_content_refs_api_client.list(repository_version=repo).results
summaries = ostree_content_summaries_api_client.list(repository_version=repo).results
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a guarantee that the results are always returned in the same order?

We used sorted in the past to enforce the order:

sorted(
        [remove_created_key(item) for item in repo_content[OSTREE_OBJECTS_NAME]],
        key=lambda item: item["pulp_href"],
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum... the only test that uses the normalize_content is the test_copy_whole_repository and, if I understood it right, the test will not create multiple contents (commits/configs/objects/refs/summaries), therefore the results will always be ordered (a list with 0 or one element is an ordered list), right?

Anyway, I'll order it so that we can use it in the future with other tests if necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The synced repository should contain 3 objects as far as I know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh ...my bad! (I still need to study and learn ostree to understand what is going on)

Comment on lines 256 to 277
response = ostree_repositories_api_client.modify(
ostree_ostree_repository_href=repo1.pulp_href,
repository_add_remove_content={"add_content_units": [obj["pulp_href"]]},
)
monitor_task(response.task)
version2 = repo1.latest_version_href
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary to re-load the version object by doing ostree_repositories_api_client.read(repo1.pulp_href).latest_version_href. We aim to verify if the repository version was really not changed.

repository_add_remove_content={"remove_content_units": [obj["pulp_href"]]},
)
monitor_task(response.task)
version2 = repo1.latest_version_href
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. The latest version is not automatically updated for repo1 since it represent an older state of the repository, before running the modify task.

result = ostree_repositories_api_client.sync(repo.pulp_href, {"remote": remote.pulp_href})
repo_version_href = monitor_task(result.task).created_resources[0]
repo = ostree_repositories_api_client.read(repo.pulp_href)
return ostree_repositories_versions_api_client.read(repo_version_href), remote, repo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth returning the updated repository, containing a new latest repository version after syncing.

repo = ostree_repositories_api_client.read(repo.pulp_href)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my bad. This one is correct. It went unnoticed.

@git-hyagi git-hyagi force-pushed the pytest-refactor-modify branch from f027c34 to e09f13c Compare January 29, 2024 12:28
@git-hyagi git-hyagi force-pushed the pytest-refactor-modify branch from e09f13c to 068d70d Compare January 30, 2024 10:25
@lubosmj lubosmj merged commit 978fac4 into pulp:main Jan 30, 2024
16 checks passed
@git-hyagi git-hyagi deleted the pytest-refactor-modify branch January 30, 2024 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite test_modify.py to pytest
2 participants