crc
crc copied to clipboard
refactor (crc/machine) : Provide a dummy implementation for virtualMachine object for writing unit tests (#4407)
Description
Fixes: Issue #4407
Relates to: Issue #4407, PR https://github.com/crc-org/crc/pull/4400
Type of change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change
- [x] Chore (non-breaking change which doesn't affect codebase; test, version modification, documentation, etc.)
Checklist
- [x] I have read the contributing guidelines
- [x] My code follows the style guidelines of this project
- [x] I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
- [x] I use conventional commits in my commit messages
- [x] I have performed a self-review of my code
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I tested my code on specified platforms
- [x] Linux
- [ ] Windows
- [ ] MacOS
Solution/Idea
- Add a VirtualMachine interface and make the CRC
machinepackage client use the VirtualMachine interface instead of a concrete implementation. This way we can inject a dummy test FakeVirtualMachine implementation into client tests that can ease writing tests for this package.- Add some additional methods in VirtualMachine interface so that we can replace direct usage of struct fields with interface methods
Bundle()Driver()API()GetHost()
- Add some additional methods in VirtualMachine interface so that we can replace direct usage of struct fields with interface methods
- Add a new field
VirtualMachinein the client so we can avoid creating it if it's already created. - Introduce a new constructor method
newClientWithVirtualMachinein machine client that would have an additional VirtualMachine argument, this would be kept package private so that it's used only by tests in the same package. - Add FakeVirtualMachine sturct in
fakemachinefor adding dummy implementation forVirtualMachineinterface. Currently, I've only completed methods used bystop_test.go, I'll add more in small increments as I get more familiar with the project.
Proposed changes
- Add new
VirtualMachineinterface and make client member functions use it instead ofvirtualMachinestruct. - Add new constructor in machine client to be able to override
VirtualMachineimplementation from tests - These files have been modified to use
VirtualMachineinterface instead ofvirtualMachinestruct (these changes are only related to changing use ofvirtualMachinestruct toVirtualMachineinterface:pkg/crc/machine/ip.gopkg/crc/machine/poweroff.gopkg/crc/machine/start.gopkg/crc/machine/status.gopkg/crc/machine/stop.go
Testing
It's a small refactor. I've only run E2E tests locally to verify if these changes don't introduce any kind of regression.
Hi @rohanKanojia. Thanks for your PR.
I'm waiting for a crc-org member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.
Once the patch is verified, the new status will be reflected by the ok-to-test label.
I understand the commands that are listed here.
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by: Once this PR has been reviewed and has the lgtm label, please assign cfergeau for approval. For more information see the Code Review Process.
The full list of commands accepted by this bot can be found here.
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment