Skip to content

Conversation

@MoeSalah1999
Copy link
Contributor

@MoeSalah1999 MoeSalah1999 commented Dec 13, 2025

Proposed Commit Message

Converted test_cc_growpart.py from unittest to pytest:
-Removed all unittest and TestCase imports and inheritances.
-Used pytest fixtures.
-Converted all self.assert to plain assert.
-Made a couple of minor changes to avoid unusual behavior.

Related GH-6427 

Additional Context

Test Steps

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Let's please avoid propagating the use of setUp as a fixture if we can.

  • minimally let's make sure to avoid os.scandir("/tmp") to write files directly to the top-level of a host's /tmp directory. We should be using pytest's tmpdir or tmp_path instead.
  • I think we can avoid setUp entirely in TestDisabled case
  • Please review the approach taken in TestResizeZFS for distro setup which may be more useful that using that plain mock.Mock() seen in the modified setUp.

# this really only verifies that resizer_factory isn't called
config = {"growpart": {"mode": "off"}}

with mock.patch.object(cc_growpart, "resizer_factory") as mockobj:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We may be able to use pytest's mocker fixutre here instead of relying on mock.patch.object.


def test_mode_off(self):
# Test that nothing is done if mode is off.
def test_mode_off(disabled_context):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see the point of having a fixture at all here. This is dict is only used in one place. Let's just inline this response as part of the mocked resizer_factory.

Comment on lines 141 to 142
self.tmpdir = os.scandir("/tmp")
self.tmpfile = open(self.tmppath, "w")
Copy link
Collaborator

Choose a reason for hiding this comment

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

We really should use pytest's tmpdir fixture if we can here to avoid using test constructs which write files directly to the top-level of the host system's /tmp directory. In fact I'm not sure we need any of these operations in setUp anymore. They are antiquated and come from the python unittest framework and a misunderstanding of loggingsetup as well as temp file writing.

@blackboxsw blackboxsw self-assigned this Dec 17, 2025
@MoeSalah1999 MoeSalah1999 force-pushed the pytestify-test-growpart branch from 1a8b4aa to f4e2da4 Compare December 18, 2025 19:11
Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Something strange has occurred in this branch. I'm seeing 10 unrelated commits in this PR currently. Can you please rebase against tip of main with something like the following to resolve this patch delta and make sure it only includes your test_cc_growpart.py changes?

git fetch upstream
git rebase -i upstream/main
git push <your_remote_name> pytestify-test-growpath --force

@MoeSalah1999 MoeSalah1999 force-pushed the pytestify-test-growpart branch from a4580e4 to bda6619 Compare December 19, 2025 20:32
@MoeSalah1999
Copy link
Contributor Author

Rebased interactively onto upstream/main and cleaned history. The PR now contains only the test_cc_growpart.py changes.

@github-actions github-actions bot added the stale-pr Pull request is stale; will be auto-closed soon label Jan 3, 2026
@MoeSalah1999
Copy link
Contributor Author

@blackboxsw Could you please verify this so it doesn't get closed automatically by github?

@canonical canonical deleted a comment from github-actions bot Jan 6, 2026
@blackboxsw blackboxsw removed the stale-pr Pull request is stale; will be auto-closed soon label Jan 6, 2026
Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Thank you for the updates @MoeSalah1999. It's looking better. Let's get one more pass on this PR please where possible.

Comment on lines +381 to +383
assert cc_growpart.RESIZE.NOCHANGE, find("/dev/XXda1", resized)[1]
assert cc_growpart.RESIZE.CHANGED, find("/dev/YYda2", resized)[1]
assert cc_growpart.RESIZE.SKIPPED, find(enoent[0], resized)[1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are invalid asserts, they should be of the format:
assert cc_growpart.RESIZE.NOCHANGE == find("/dev/XXda1", resized)[1]

Comment on lines +535 to +538

# Instantiated MockDistro, otherwise we would be assigning
# the class not an instance when we pass self.distro
# into resize_devices, which expects a distro instance
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good fix/correction here, and you are correct for those reasons you mention. I don't think we need to persist this comment in the test though.

Suggested change
# Instantiated MockDistro, otherwise we would be assigning
# the class not an instance when we pass self.distro
# into resize_devices, which expects a distro instance

)
def test_mode_use_growfs_on_root(self):
with mock.patch.object(os.path, "isfile", return_value=True):
with mock.patch.object(
Copy link
Collaborator

Choose a reason for hiding this comment

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

In shifting to pytest it would be better to use pytest's mocker instead of mock.patch.object in tests you are touching in this PR.

I realize there are a number of pre-existing mock.patch.objects sprinkled throughout this file, so I'm not recommending that you change this in every existing test if you don't feel inclined. But, for tests you do touch, lets please use pytest's mocker.patch.object() instead of mock.patch.object

there is no need to change all existing uses of mock.patch.object in other tests

]
)
def test_mode_auto_falls_back_to_growfs(self):
with mock.patch.object(os.path, "isfile", return_value=True):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. Please use the mocker fixture and mocker.patch.object instead.

Comment on lines +322 to +327
@pytest.fixture(autouse=True)
def setUp(self):
super().setUp()
self.name = "growpart"
self.distro = MockDistro()
self.log = logging.getLogger("TestResize")
yield
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is nothing really needed in this setUp method anymore.

Nothing uses self.log, and in pytest we can use caplog or capsys fixtures to look through logs or stdout/error messages.

Let's just remove setUp altogether and create a local distro variable which is used within test_simple_devices.

Other things that look fairly painful in this test (not your fault) are that the test doesn't use mocker for os.stat and instead encloses the whole test a in a try/finally clause to attempt to reset original state of os.stat and the MockDistro class after the unittest completes. This is really bad practice that was in the original test. If possible, I'd like to see if we can avoid that try/finally stuff, drop the setUp fixture and just use mocker instead.

@MoeSalah1999 MoeSalah1999 force-pushed the pytestify-test-growpart branch from d26d733 to 51e9939 Compare January 8, 2026 14:41
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.

2 participants