|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/containers/podman/v6/pkg/machine/define" |
| 9 | + "github.com/containers/podman/v6/pkg/machine/hyperv" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// helper to setup mocks and ensure cleanup |
| 15 | +func mockPermissions(t *testing.T, admin, hyperV bool) { |
| 16 | + origAdmin, origHyperV := hasAdminRightsFunc, hasHyperVAdminRightsFunc |
| 17 | + t.Cleanup(func() { |
| 18 | + hasAdminRightsFunc = origAdmin |
| 19 | + hasHyperVAdminRightsFunc = origHyperV |
| 20 | + }) |
| 21 | + |
| 22 | + hasAdminRightsFunc = func() bool { return admin } |
| 23 | + hasHyperVAdminRightsFunc = func() bool { return hyperV } |
| 24 | +} |
| 25 | + |
| 26 | +func TestGetByVMType_HyperV(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + isAdmin bool |
| 30 | + isHyperV bool |
| 31 | + expectError bool |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "WithAdminRights", |
| 35 | + isAdmin: true, |
| 36 | + isHyperV: false, |
| 37 | + expectError: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "WithHyperVAdminRights", |
| 41 | + isAdmin: false, |
| 42 | + isHyperV: true, |
| 43 | + expectError: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "WithBothRights", |
| 47 | + isAdmin: true, |
| 48 | + isHyperV: true, |
| 49 | + expectError: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "WithoutPermissions", |
| 53 | + isAdmin: false, |
| 54 | + isHyperV: false, |
| 55 | + expectError: true, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + mockPermissions(t, tt.isAdmin, tt.isHyperV) |
| 62 | + |
| 63 | + provider, err := GetByVMType(define.HyperVVirt) |
| 64 | + |
| 65 | + if tt.expectError { |
| 66 | + assert.Error(t, err) |
| 67 | + assert.Equal(t, err.Error(), hyperv.ErrHypervUserNotInAdminGroup.Error()) |
| 68 | + assert.Nil(t, provider) |
| 69 | + } else { |
| 70 | + require.NoError(t, err) |
| 71 | + assert.NotNil(t, provider) |
| 72 | + assert.Equal(t, define.HyperVVirt, provider.VMType()) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestGetAll_HyperV_Inclusion(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + isAdmin bool |
| 82 | + isHyperV bool |
| 83 | + expectHyperV bool |
| 84 | + }{ |
| 85 | + {"WithAdminRights", true, false, true}, |
| 86 | + {"WithHyperVRights", false, true, true}, |
| 87 | + {"NoRights", false, false, false}, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tt := range tests { |
| 91 | + t.Run(tt.name, func(t *testing.T) { |
| 92 | + mockPermissions(t, tt.isAdmin, tt.isHyperV) |
| 93 | + |
| 94 | + providers := GetAll() |
| 95 | + |
| 96 | + // Check for HyperV presence |
| 97 | + hasHyperV := false |
| 98 | + for _, p := range providers { |
| 99 | + if p.VMType() == define.HyperVVirt { |
| 100 | + hasHyperV = true |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + assert.Equal(t, tt.expectHyperV, hasHyperV, "Hyper-V provider presence mismatch") |
| 106 | + |
| 107 | + // WSL should always be present in these scenarios |
| 108 | + hasWSL := false |
| 109 | + for _, p := range providers { |
| 110 | + if p.VMType() == define.WSLVirt { |
| 111 | + hasWSL = true |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + assert.True(t, hasWSL, "GetAll should always include WSL provider") |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestGetByVMType_WSL_AlwaysWorks(t *testing.T) { |
| 121 | + provider, err := GetByVMType(define.WSLVirt) |
| 122 | + require.NoError(t, err) |
| 123 | + assert.NotNil(t, provider) |
| 124 | + assert.Equal(t, define.WSLVirt, provider.VMType()) |
| 125 | +} |
| 126 | + |
| 127 | +func TestGetByVMType_UnsupportedProvider(t *testing.T) { |
| 128 | + provider, err := GetByVMType(define.QemuVirt) |
| 129 | + assert.Error(t, err) |
| 130 | + assert.Contains(t, err.Error(), "unsupported virtualization provider") |
| 131 | + assert.Nil(t, provider) |
| 132 | +} |
0 commit comments