File: retrieve_test.go

package info (click to toggle)
golang-github-vmware-govmomi 0.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,844 kB
  • sloc: sh: 2,284; lisp: 1,560; ruby: 948; xml: 139; makefile: 52
file content (213 lines) | stat: -rw-r--r-- 5,306 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mo

import (
	"os"
	"testing"
	"time"

	"github.com/vmware/govmomi/vim25/soap"
	"github.com/vmware/govmomi/vim25/types"
	"github.com/vmware/govmomi/vim25/xml"
)

func load(name string) []types.ObjectContent {
	f, err := os.Open(name)
	if err != nil {
		panic(err)
	}

	defer f.Close()

	var b types.RetrievePropertiesResponse

	dec := xml.NewDecoder(f)
	dec.TypeFunc = types.TypeFunc()
	if err := dec.Decode(&b); err != nil {
		panic(err)
	}

	return b.Returnval
}

func TestNotAuthenticatedFault(t *testing.T) {
	var s SessionManager

	err := LoadObjectContent(load("fixtures/not_authenticated_fault.xml"), &s)
	if !soap.IsVimFault(err) {
		t.Errorf("Expected IsVimFault")
	}

	fault := soap.ToVimFault(err).(*types.NotAuthenticated)
	if fault.PrivilegeId != "System.View" {
		t.Errorf("Expected first fault to be returned")
	}
}

func TestNestedProperty(t *testing.T) {
	var vm VirtualMachine

	err := LoadObjectContent(load("fixtures/nested_property.xml"), &vm)
	if err != nil {
		t.Fatalf("Expected no error, got: %s", err)
	}

	self := types.ManagedObjectReference{
		Type:  "VirtualMachine",
		Value: "vm-411",
	}

	if vm.Self != self {
		t.Fatalf("Expected vm.Self to be set")
	}

	if vm.Config == nil {
		t.Fatalf("Expected vm.Config to be set")
	}

	if vm.Config.Name != "kubernetes-master" {
		t.Errorf("Got: %s", vm.Config.Name)
	}

	if vm.Config.Uuid != "422ec880-ab06-06b4-23f3-beb7a052a4c9" {
		t.Errorf("Got: %s", vm.Config.Uuid)
	}
}

func TestPointerProperty(t *testing.T) {
	var vm VirtualMachine

	err := LoadObjectContent(load("fixtures/pointer_property.xml"), &vm)
	if err != nil {
		t.Fatalf("Expected no error, got: %s", err)
	}

	if vm.Config == nil {
		t.Fatalf("Expected vm.Config to be set")
	}

	if vm.Config.BootOptions == nil {
		t.Fatalf("Expected vm.Config.BootOptions to be set")
	}
}

func TestEmbeddedTypeProperty(t *testing.T) {
	// Test that we avoid in this case:
	// panic: reflect.Set: value of type mo.ClusterComputeResource is not assignable to type mo.ComputeResource
	var cr ComputeResource

	err := LoadObjectContent(load("fixtures/cluster_host_property.xml"), &cr)
	if err != nil {
		t.Fatalf("Expected no error, got: %s", err)
	}

	if len(cr.Host) != 4 {
		t.Fatalf("Expected cr.Host to be set")
	}
}

func TestEmbeddedTypePropertySlice(t *testing.T) {
	var me []ManagedEntity

	err := LoadObjectContent(load("fixtures/hostsystem_list_name_property.xml"), &me)
	if err != nil {
		t.Fatalf("Expected no error, got: %s", err)
	}

	if len(me) != 2 {
		t.Fatalf("Expected 2 elements")
	}

	for _, m := range me {
		if m.Name == "" {
			t.Fatal("Expected Name field to be set")
		}
	}

	if me[0].Name == me[1].Name {
		t.Fatal("Name fields should not be the same")
	}
}

func TestReferences(t *testing.T) {
	var cr ComputeResource

	err := LoadObjectContent(load("fixtures/cluster_host_property.xml"), &cr)
	if err != nil {
		t.Fatalf("Expected no error, got: %s", err)
	}

	refs := References(cr)
	n := len(refs)
	if n != 5 {
		t.Errorf("%d refs", n)
	}
}

func TestEventReferences(t *testing.T) {
	event := &types.VmPoweredOnEvent{
		VmEvent: types.VmEvent{
			Event: types.Event{
				Key:         0,
				ChainId:     0,
				CreatedTime: time.Now(),
				UserName:    "",
				Datacenter: &types.DatacenterEventArgument{
					EntityEventArgument: types.EntityEventArgument{
						EventArgument: types.EventArgument{},
						Name:          "DC0",
					},
					Datacenter: types.ManagedObjectReference{Type: "Datacenter", Value: "datacenter-2"},
				},
				ComputeResource: &types.ComputeResourceEventArgument{
					EntityEventArgument: types.EntityEventArgument{
						EventArgument: types.EventArgument{},
						Name:          "DC0_C0",
					},
					ComputeResource: types.ManagedObjectReference{Type: "ClusterComputeResource", Value: "clustercomputeresource-26"},
				},
				Host: &types.HostEventArgument{
					EntityEventArgument: types.EntityEventArgument{
						EventArgument: types.EventArgument{},
						Name:          "DC0_C0_H0",
					},
					Host: types.ManagedObjectReference{Type: "HostSystem", Value: "host-32"},
				},
				Vm: &types.VmEventArgument{
					EntityEventArgument: types.EntityEventArgument{
						EventArgument: types.EventArgument{},
						Name:          "DC0_C0_RP0_VM1",
					},
					Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "vm-62"},
				},
				Ds:                   (*types.DatastoreEventArgument)(nil),
				Net:                  (*types.NetworkEventArgument)(nil),
				Dvs:                  (*types.DvsEventArgument)(nil),
				FullFormattedMessage: "",
				ChangeTag:            "",
			},
			Template: false,
		},
	}

	refs := References(event, true)
	n := len(refs)
	if n != 4 {
		t.Errorf("%d refs", n)
	}
}