@@ -10,9 +10,12 @@ import (
10
10
11
11
"github.com/ory/dockertest/v3"
12
12
"github.com/stretchr/testify/require"
13
+ "github.com/vmware/govmomi/find"
13
14
"github.com/vmware/govmomi/vim25/mo"
15
+ "github.com/vmware/govmomi/vim25/types"
14
16
corev1 "k8s.io/api/core/v1"
15
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
+ "k8s.io/utils/pointer"
16
19
17
20
migration "github.com/harvester/vm-import-controller/pkg/apis/migration.harvesterhci.io/v1beta1"
18
21
"github.com/harvester/vm-import-controller/pkg/server"
@@ -27,10 +30,11 @@ func TestMain(t *testing.M) {
27
30
log .Fatalf ("error connecting to dockerd: %v" , err )
28
31
}
29
32
33
+ // https://hub.docker.com/r/vmware/vcsim
30
34
runOpts := & dockertest.RunOptions {
31
35
Name : "vcsim" ,
32
36
Repository : "vmware/vcsim" ,
33
- Tag : "v0.29 .0" ,
37
+ Tag : "v0.49 .0" ,
34
38
}
35
39
36
40
vcsimMock , err := pool .RunWithOptions (runOpts )
@@ -202,7 +206,7 @@ func Test_ExportVirtualMachine(t *testing.T) {
202
206
}
203
207
204
208
err = c .ExportVirtualMachine (vm )
205
- assert .NoError (err , "expected no error during vm export" )
209
+ assert .NoError (err , "expected no error during VM export" )
206
210
t .Log (vm .Status )
207
211
}
208
212
@@ -247,14 +251,91 @@ func Test_GenerateVirtualMachine(t *testing.T) {
247
251
}
248
252
249
253
newVM , err := c .GenerateVirtualMachine (vm )
250
- assert .NoError (err , "expected no error during vm export " )
254
+ assert .NoError (err , "expected no error during VM CR generation " )
251
255
assert .Len (newVM .Spec .Template .Spec .Networks , 1 , "should have found the default pod network" )
252
256
assert .Len (newVM .Spec .Template .Spec .Domain .Devices .Interfaces , 1 , "should have found a network map" )
253
257
assert .Equal (newVM .Spec .Template .Spec .Domain .Memory .Guest .String (), "32M" , "expected VM to have 32M memory" )
254
258
assert .NotEmpty (newVM .Spec .Template .Spec .Domain .Resources .Limits , "expect to find resource requests to be present" )
255
259
256
260
}
257
261
262
+ func Test_GenerateVirtualMachine_secureboot (t * testing.T ) {
263
+ assert := require .New (t )
264
+ ctx := context .TODO ()
265
+ endpoint := fmt .Sprintf ("https://localhost:%s/sdk" , vcsimPort )
266
+ secret := & corev1.Secret {
267
+ ObjectMeta : metav1.ObjectMeta {
268
+ Name : "test" ,
269
+ Namespace : "default" ,
270
+ },
271
+ Data : map [string ][]byte {
272
+ "username" : []byte ("user" ),
273
+ "password" : []byte ("pass" ),
274
+ },
275
+ }
276
+ vm := & migration.VirtualMachineImport {
277
+ ObjectMeta : metav1.ObjectMeta {
278
+ Name : "demo" ,
279
+ Namespace : "default" ,
280
+ },
281
+ Spec : migration.VirtualMachineImportSpec {
282
+ SourceCluster : corev1.ObjectReference {},
283
+ VirtualMachineName : "test01" ,
284
+ Mapping : []migration.NetworkMapping {
285
+ {
286
+ SourceNetwork : "DVSwitch: fea97929-4b2d-5972-b146-930c6d0b4014" ,
287
+ DestinationNetwork : "default/vlan" ,
288
+ },
289
+ },
290
+ },
291
+ }
292
+
293
+ c , err := NewClient (ctx , endpoint , "DC0" , secret )
294
+ assert .NoError (err , "expected no error during creation of client" )
295
+ err = c .Verify ()
296
+ assert .NoError (err , "expected no error during verification of client" )
297
+
298
+ // https://github.com/vmware/govmomi/blob/main/vcsim/README.md#default-vcenter-inventory
299
+ f := find .NewFinder (c .Client .Client , true )
300
+
301
+ dc , err := f .Datacenter (ctx , c .dc )
302
+ assert .NoError (err , "expected no error during datacenter lookup" )
303
+
304
+ f .SetDatacenter (dc )
305
+
306
+ ds , err := f .DefaultDatastore (ctx )
307
+ assert .NoError (err , "expected no error during datastore lookup" )
308
+
309
+ pool , err := f .ResourcePool (ctx , "DC0_H0/Resources" )
310
+ assert .NoError (err , "expected no error during resource pool lookup" )
311
+
312
+ folder , err := dc .Folders (ctx )
313
+ assert .NoError (err , "expected no error during folder lookup" )
314
+
315
+ vmConfigSpec := types.VirtualMachineConfigSpec {
316
+ Name : vm .Spec .VirtualMachineName ,
317
+ GuestId : string (types .VirtualMachineGuestOsIdentifierOtherGuest64 ),
318
+ Firmware : string (types .GuestOsDescriptorFirmwareTypeEfi ),
319
+ BootOptions : & types.VirtualMachineBootOptions {
320
+ EfiSecureBootEnabled : pointer .Bool (true ),
321
+ },
322
+ Files : & types.VirtualMachineFileInfo {
323
+ VmPathName : fmt .Sprintf ("[%s] %s" , ds .Name (), vm .Spec .VirtualMachineName ),
324
+ },
325
+ }
326
+
327
+ task , err := folder .VmFolder .CreateVM (ctx , vmConfigSpec , pool , nil )
328
+ assert .NoError (err , "expected no error when creating VM" )
329
+
330
+ _ , err = task .WaitForResult (ctx , nil )
331
+ assert .NoError (err , "expected no error when waiting for task to complete: %v" )
332
+
333
+ newVM , err := c .GenerateVirtualMachine (vm )
334
+ assert .NoError (err , "expected no error during VM CR generation" )
335
+ assert .True (* newVM .Spec .Template .Spec .Domain .Firmware .Bootloader .EFI .SecureBoot , "expected VM to have secure boot enabled" )
336
+ assert .True (* newVM .Spec .Template .Spec .Domain .Features .SMM .Enabled , "expected VM to have SMM enabled" )
337
+ }
338
+
258
339
func Test_identifyNetworkCards (t * testing.T ) {
259
340
ctx := context .TODO ()
260
341
endpoint := fmt .Sprintf ("https://localhost:%s/sdk" , vcsimPort )
@@ -277,7 +358,7 @@ func Test_identifyNetworkCards(t *testing.T) {
277
358
assert .NoError (err , "expected no error during verification of client" )
278
359
279
360
vmObj , err := c .findVM ("" , "DC0_H0_VM0" )
280
- assert .NoError (err , "expected no error during vm lookup" )
361
+ assert .NoError (err , "expected no error during VM lookup" )
281
362
282
363
var o mo.VirtualMachine
283
364
0 commit comments