github.com/vmware/govmomi@v0.43.0/object/virtual_machine_test.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package object
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/vmware/govmomi/vim25/types"
    24  )
    25  
    26  // VirtualMachine should implement the Reference interface.
    27  var _ Reference = VirtualMachine{}
    28  
    29  // pretty.Printf generated
    30  var snapshot = &types.VirtualMachineSnapshotInfo{
    31  	DynamicData:     types.DynamicData{},
    32  	CurrentSnapshot: &types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-11"},
    33  	RootSnapshotList: []types.VirtualMachineSnapshotTree{
    34  		{
    35  			DynamicData:    types.DynamicData{},
    36  			Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-1"},
    37  			Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
    38  			Name:           "root",
    39  			Description:    "",
    40  			Id:             1,
    41  			CreateTime:     time.Now(),
    42  			State:          "poweredOn",
    43  			Quiesced:       false,
    44  			BackupManifest: "",
    45  			ChildSnapshotList: []types.VirtualMachineSnapshotTree{
    46  				{
    47  					DynamicData:       types.DynamicData{},
    48  					Snapshot:          types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-2"},
    49  					Vm:                types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
    50  					Name:              "child",
    51  					Description:       "",
    52  					Id:                2,
    53  					CreateTime:        time.Now(),
    54  					State:             "poweredOn",
    55  					Quiesced:          false,
    56  					BackupManifest:    "",
    57  					ChildSnapshotList: nil,
    58  					ReplaySupported:   types.NewBool(false),
    59  				},
    60  				{
    61  					DynamicData:    types.DynamicData{},
    62  					Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-3"},
    63  					Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
    64  					Name:           "child",
    65  					Description:    "",
    66  					Id:             3,
    67  					CreateTime:     time.Now(),
    68  					State:          "poweredOn",
    69  					Quiesced:       false,
    70  					BackupManifest: "",
    71  					ChildSnapshotList: []types.VirtualMachineSnapshotTree{
    72  						{
    73  							DynamicData:    types.DynamicData{},
    74  							Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-9"},
    75  							Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
    76  							Name:           "grandkid",
    77  							Description:    "",
    78  							Id:             9,
    79  							CreateTime:     time.Now(),
    80  							State:          "poweredOn",
    81  							Quiesced:       false,
    82  							BackupManifest: "",
    83  							ChildSnapshotList: []types.VirtualMachineSnapshotTree{
    84  								{
    85  									DynamicData:       types.DynamicData{},
    86  									Snapshot:          types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-10"},
    87  									Vm:                types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
    88  									Name:              "great",
    89  									Description:       "",
    90  									Id:                10,
    91  									CreateTime:        time.Now(),
    92  									State:             "poweredOn",
    93  									Quiesced:          false,
    94  									BackupManifest:    "",
    95  									ChildSnapshotList: nil,
    96  									ReplaySupported:   types.NewBool(false),
    97  								},
    98  							},
    99  							ReplaySupported: types.NewBool(false),
   100  						},
   101  					},
   102  					ReplaySupported: types.NewBool(false),
   103  				},
   104  				{
   105  					DynamicData:    types.DynamicData{},
   106  					Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-5"},
   107  					Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
   108  					Name:           "voodoo",
   109  					Description:    "",
   110  					Id:             5,
   111  					CreateTime:     time.Now(),
   112  					State:          "poweredOn",
   113  					Quiesced:       false,
   114  					BackupManifest: "",
   115  					ChildSnapshotList: []types.VirtualMachineSnapshotTree{
   116  						{
   117  							DynamicData:       types.DynamicData{},
   118  							Snapshot:          types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-11"},
   119  							Vm:                types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
   120  							Name:              "child",
   121  							Description:       "",
   122  							Id:                11,
   123  							CreateTime:        time.Now(),
   124  							State:             "poweredOn",
   125  							Quiesced:          false,
   126  							BackupManifest:    "",
   127  							ChildSnapshotList: nil,
   128  							ReplaySupported:   types.NewBool(false),
   129  						},
   130  					},
   131  					ReplaySupported: types.NewBool(false),
   132  				},
   133  				{
   134  					DynamicData:    types.DynamicData{},
   135  					Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-6"},
   136  					Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
   137  					Name:           "better",
   138  					Description:    "",
   139  					Id:             6,
   140  					CreateTime:     time.Now(),
   141  					State:          "poweredOn",
   142  					Quiesced:       false,
   143  					BackupManifest: "",
   144  					ChildSnapshotList: []types.VirtualMachineSnapshotTree{
   145  						{
   146  							DynamicData:    types.DynamicData{},
   147  							Snapshot:       types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-7"},
   148  							Vm:             types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
   149  							Name:           "best",
   150  							Description:    "",
   151  							Id:             7,
   152  							CreateTime:     time.Now(),
   153  							State:          "poweredOn",
   154  							Quiesced:       false,
   155  							BackupManifest: "",
   156  							ChildSnapshotList: []types.VirtualMachineSnapshotTree{
   157  								{
   158  									DynamicData:       types.DynamicData{},
   159  									Snapshot:          types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-8"},
   160  									Vm:                types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"},
   161  									Name:              "betterer",
   162  									Description:       "",
   163  									Id:                8,
   164  									CreateTime:        time.Now(),
   165  									State:             "poweredOn",
   166  									Quiesced:          false,
   167  									BackupManifest:    "",
   168  									ChildSnapshotList: nil,
   169  									ReplaySupported:   types.NewBool(false),
   170  								},
   171  							},
   172  							ReplaySupported: types.NewBool(false),
   173  						},
   174  					},
   175  					ReplaySupported: types.NewBool(false),
   176  				},
   177  			},
   178  			ReplaySupported: types.NewBool(false),
   179  		},
   180  	},
   181  }
   182  
   183  func TestVirtualMachineSnapshotMap(t *testing.T) {
   184  	m := make(snapshotMap)
   185  	m.add("", snapshot.RootSnapshotList)
   186  
   187  	tests := []struct {
   188  		name   string
   189  		expect int
   190  	}{
   191  		{"enoent", 0},
   192  		{"root", 1},
   193  		{"child", 3},
   194  		{"root/child", 2},
   195  		{"root/voodoo/child", 1},
   196  		{"2-snapshot-6", 1},
   197  	}
   198  
   199  	for _, test := range tests {
   200  		s := m[test.name]
   201  
   202  		if len(s) != test.expect {
   203  			t.Errorf("%s: %d != %d", test.name, len(s), test.expect)
   204  		}
   205  	}
   206  }
   207  
   208  func TestDiskFileOperation(t *testing.T) {
   209  	backing := &types.VirtualDiskFlatVer2BackingInfo{
   210  		VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
   211  			FileName: "[datastore1] data/disk1.vmdk",
   212  		},
   213  		Parent: nil,
   214  	}
   215  
   216  	parent := &types.VirtualDiskFlatVer2BackingInfo{
   217  		VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
   218  			FileName: "[datastore1] data/parent.vmdk",
   219  		},
   220  	}
   221  
   222  	disk := &types.VirtualDisk{
   223  		VirtualDevice: types.VirtualDevice{
   224  			Backing: backing,
   225  		},
   226  	}
   227  
   228  	op := types.VirtualDeviceConfigSpecOperationAdd
   229  	fop := types.VirtualDeviceConfigSpecFileOperationCreate
   230  
   231  	res := diskFileOperation(op, fop, disk)
   232  	if res != "" {
   233  		t.Errorf("res=%s", res)
   234  	}
   235  
   236  	disk.CapacityInKB = 1
   237  	res = diskFileOperation(op, fop, disk)
   238  	if res != types.VirtualDeviceConfigSpecFileOperationCreate {
   239  		t.Errorf("res=%s", res)
   240  	}
   241  
   242  	disk.CapacityInKB = 0
   243  	disk.CapacityInBytes = 1
   244  	res = diskFileOperation(op, fop, disk)
   245  	if res != types.VirtualDeviceConfigSpecFileOperationCreate {
   246  		t.Errorf("res=%s", res)
   247  	}
   248  
   249  	disk.CapacityInBytes = 0
   250  	backing.Parent = parent
   251  	res = diskFileOperation(op, fop, disk)
   252  	if res != types.VirtualDeviceConfigSpecFileOperationCreate {
   253  		t.Errorf("res=%s", res)
   254  	}
   255  }