Skip to content

Add unit tests for VM Driver Registry package #3735

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

Merged
merged 2 commits into from
Jul 17, 2025

Conversation

unsuman
Copy link
Contributor

@unsuman unsuman commented Jul 15, 2025

This PR adds unit tests for the registry package.

Changes

  • Added pkg/registry/registry_test.go with unit tests covering core functions like registering internal and external drivers, registering external driver executables
  • Changed a line from pkg/registry/registry.go, which was causing .exe files to be registered on non-Windows platform.

Closes #3718

@AkihiroSuda AkihiroSuda added this to the v2.0.0 milestone Jul 16, 2025
@AkihiroSuda AkihiroSuda added the gsoc/2025 Google Summer of Code 2025 label Jul 16, 2025
@unsuman unsuman force-pushed the test/registry branch 2 times, most recently from 00bce5e to f7dd1a4 Compare July 16, 2025 10:22
@@ -177,7 +177,7 @@ func registerDriverFile(path string) {
name = strings.TrimSuffix(strings.TrimPrefix(base, "lima-driver-"), ".exe")
}
} else {
if strings.HasPrefix(base, "lima-driver-") {
if strings.HasPrefix(base, "lima-driver-") && !strings.HasSuffix(base, ".exe") {
Copy link
Member

Choose a reason for hiding this comment

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

May I suggest creating a separate PR for this fix?

Copy link
Member

Choose a reason for hiding this comment

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

Can be also a separate commit in the same PR

@alexandear alexandear requested review from alexandear and Copilot July 16, 2025 11:51
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds comprehensive unit tests for the VM driver registry package and fixes a platform-specific bug. The tests validate the core functionality of driver registration and discovery, while the bug fix ensures that .exe files are properly filtered on non-Windows platforms.

  • Added comprehensive unit tests covering internal driver registration, external driver discovery, and driver retrieval functions
  • Fixed bug preventing .exe files from being incorrectly registered as drivers on non-Windows platforms

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/registry/registry_test.go Added comprehensive unit tests for registry functionality including mock driver implementation and test cases for all major functions
pkg/registry/registry.go Fixed platform-specific bug to prevent .exe files from being registered as drivers on non-Windows systems
Comments suppressed due to low confidence (3)

pkg/registry/registry_test.go:55

  • The test directly accesses the global internalDrivers map but doesn't clean it up between tests. This could cause test interdependencies and flaky behavior. Consider resetting the global state in each test or using a setup/teardown mechanism.
	assert.Equal(t, len(internalDrivers), 2)

pkg/registry/registry_test.go:103

  • The test accesses the global ExternalDrivers map without cleaning it up. This could cause test pollution and unpredictable results when tests run in different orders. The cleanup should reset ExternalDrivers to its initial state.
	assert.Equal(t, len(ExternalDrivers), 1)

pkg/registry/registry_test.go:177

  • Global state is being reset in TestGet but not cleaned up afterwards. This could affect other tests that run after this one. Add t.Cleanup() to restore the original state or use a test setup/teardown pattern.
	internalDrivers = make(map[string]driver.Driver)


for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ExternalDrivers = make(map[string]*ExternalDriver)
Copy link
Preview

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

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

[nitpick] Resetting global state within individual test cases can make tests harder to understand and maintain. Consider moving this setup to a helper function or using t.Cleanup() to ensure proper teardown regardless of test outcome.

Suggested change
ExternalDrivers = make(map[string]*ExternalDriver)
setupExternalDrivers(t)

Copilot uses AI. Check for mistakes.

@alexandear
Copy link
Member

Looks like these Copilot comments makes sense.


pkg/registry/registry_test.go:55

The test directly accesses the global internalDrivers map but doesn't clean it up between tests. This could cause test interdependencies and flaky behavior. Consider resetting the global state in each test or using a setup/teardown mechanism.

	assert.Equal(t, len(internalDrivers), 2)

pkg/registry/registry_test.go:103

The test accesses the global ExternalDrivers map without cleaning it up. This could cause test pollution and unpredictable results when tests run in different orders. The cleanup should reset ExternalDrivers to its initial state.

	assert.Equal(t, len(ExternalDrivers), 1)

pkg/registry/registry_test.go:177

Global state is being reset in TestGet but not cleaned up afterwards. This could affect other tests that run after this one. Add t.Cleanup() to restore the original state or use a test setup/teardown pattern.

	internalDrivers = make(map[string]driver.Driver)

Comment on lines 105 to 111
t.Cleanup(func() {
err := os.Remove(driverPath)
assert.NilError(t, err)

_, err = os.Stat(driverPath)
assert.Assert(t, os.IsNotExist(err))
})
Copy link
Member

Choose a reason for hiding this comment

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

This is not needed since t.TempDir() automatically removes a directory on Cleanup.

Comment on lines 195 to 200
originalExternalDrivers := ExternalDrivers
originalInternalDrivers := internalDrivers
t.Cleanup(func() {
ExternalDrivers = originalExternalDrivers
internalDrivers = originalInternalDrivers
})
Copy link
Member

Choose a reason for hiding this comment

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

I propose creating a helper function and use it in all tests.


"gotest.tools/v3/assert"

"github.com/lima-vm/lima/pkg/driver"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/v2/pkg/driver"

unsuman added 2 commits July 16, 2025 22:02
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit b1216d8 into lima-vm:master Jul 17, 2025
114 of 117 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gsoc/2025 Google Summer of Code 2025
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unit tests for VM Driver Registry Package
3 participants