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

refactor (crc/machine) : Provide a dummy implementation for virtualMachine object for writing unit tests (#4407) #4423

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rohanKanojia
Copy link
Contributor

Description

Fixes: Issue #4407

Relates to: Issue #4407, PR #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
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • I have read the contributing guidelines
  • My code follows the style guidelines of this project
  • I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
  • I use conventional commits in my commit messages
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I tested my code on specified platforms
    • Linux
    • Windows
    • MacOS

Solution/Idea

  • Add a VirtualMachine interface and make the CRC machine package 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 a new field VirtualMachine in the client so we can avoid creating it if it's already created.
  • Introduce a new constructor method newClientWithVirtualMachine in 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 fakemachine for adding dummy implementation for VirtualMachine interface. Currently, I've only completed methods used by stop_test.go, I'll add more in small increments as I get more familiar with the project.

Proposed changes

  • Add new VirtualMachine interface and make client member functions use it instead of virtualMachine struct.
  • Add new constructor in machine client to be able to override VirtualMachine implementation from tests
  • These files have been modified to use VirtualMachine interface instead of virtualMachine struct (these changes are only related to changing use of virtualMachine struct to VirtualMachine interface:
    • pkg/crc/machine/ip.go
    • pkg/crc/machine/poweroff.go
    • pkg/crc/machine/start.go
    • pkg/crc/machine/status.go
    • pkg/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.

Copy link

openshift-ci bot commented Oct 29, 2024

[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 Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

openshift-ci bot commented Oct 29, 2024

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.

@rohanKanojia rohanKanojia force-pushed the pr/issue4407 branch 5 times, most recently from 0e36ad5 to 988b812 Compare November 5, 2024 11:32
config: config,
diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute),
ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute),
virtualMachine: vm,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd separate the commit adding this virtualMachine: vm field from the commit introducing the VirtualMachine interface. This commit is huge because of the introduction of this interface, but the changes needed for the interface changes are very mechanical (vm.bundle -> vm.Bundle) and such. If they are put in a commit doing only this, it's easier to review as we can focus on one thing, these mechanical changes.
Then in a 2nd commit come the "logic" changes which add a way to mock/test the virtual machine, and it's going to be a lot smaller, and easier to understand/review.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have split this PR into multiple commits, please review if it's as per your expectations. I can adjust if additional changes are needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm https://github.com/cfergeau/crc/commits/pr/issue4407/ is closer to what I had in mind. One commit doing only the VirtualMachine interface introduction and making use of its methods (could be 2 commits if you prefer, but I think it's fine as one). I added 2 follow-up commits which are meant to be squashed in the first one.
Then one commit adds newClientWithVirtualMachine, loadVirtualMachineLazily so that we can override it, and the rest of your series, I haven't touched it.

Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, the content in Solutions/Idea and Proposed Changes would be nice to have in the individual commit logs, as one line commit logs are very terse when you'll get back to them in 6 months trying to remember what the commit was about, but also too long for a short log, they overflow quite a bit in github UI, git log, ... They typically should be about 60 characters in length.

Better to have a short 1line log, followed by more details in the log explaining why you are making the change, and giving a high level overview of what you are doing if it's a big change.

pkg/crc/machine/vsock.go Outdated Show resolved Hide resolved
@@ -43,7 +43,7 @@ func NewClient(name string, debug bool, config crcConfig.Storage) Client {
return newClientWithVirtualMachine(name, debug, config, nil)
}

// newClientWithVirtualMachine creates a Client instance with a overridden VirtualMachine implementation.
// newClientWithVirtualMachine creates a Client instance with an overridden VirtualMachine implementation.
Copy link
Contributor

Choose a reason for hiding this comment

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

You could squash this in the commit where it was added.

config: config,
diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute),
ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute),
virtualMachine: vm,
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm https://github.com/cfergeau/crc/commits/pr/issue4407/ is closer to what I had in mind. One commit doing only the VirtualMachine interface introduction and making use of its methods (could be 2 commits if you prefer, but I think it's fine as one). I added 2 follow-up commits which are meant to be squashed in the first one.
Then one commit adds newClientWithVirtualMachine, loadVirtualMachineLazily so that we can override it, and the rest of your series, I haven't touched it.

config: config,
diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute),
ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute),
virtualMachine: vm,
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, the content in Solutions/Idea and Proposed Changes would be nice to have in the individual commit logs, as one line commit logs are very terse when you'll get back to them in 6 months trying to remember what the commit was about, but also too long for a short log, they overflow quite a bit in github UI, git log, ... They typically should be about 60 characters in length.

Better to have a short 1line log, followed by more details in the log explaining why you are making the change, and giving a high level overview of what you are doing if it's a big change.

rohanKanojia and others added 2 commits November 7, 2024 10:39
…raction

Add a VirtualMachine interface and make the CRC `machine` package 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()`
    - `Host()`
    - `Kill()`
Introduce a new constructor method `newClientWithVirtualMachine` in 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 `fakemachine` for adding dummy
implementation for `VirtualMachine` interface. Currently, I've
only completed methods used by `stop_test.go`, I'll add more in
small increments as we implement more unit tests using this
implementation.

Signed-off-by: Rohan Kumar <[email protected]>
…rtualMachine implementation

Add additional tests in `stop_test.go` to verify that client.Stop()
updates the state of virtual machine and unexposes exposed ports
as expected. Use the fake vm implementation added previously to
test vm state modification behavior on stop.

Signed-off-by: Rohan Kumar <[email protected]>
…rface

Make VirtualMachine implement vsock methods so that vsock interaction
is also done via VirtualMachine interface. This would help in writing
tests, we can override the behavior of expose/unexpose in fake vm
implementation.

Signed-off-by: Rohan Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants