/
simulator_test.go
450 lines (393 loc) · 10.6 KB
/
simulator_test.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
Copyright (c) 2021 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 simulator_test
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"sync"
"testing"
"time"
"github.com/vmware/govmomi/eam"
"github.com/vmware/govmomi/eam/object"
"github.com/vmware/govmomi/eam/types"
"github.com/vmware/govmomi/find"
vimobject "github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/session"
vcsim "github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/soap"
vim "github.com/vmware/govmomi/vim25/types"
// making sure the SDK endpoint is registered
_ "github.com/vmware/govmomi/eam/simulator"
)
const waitLoopMessage = `
################################################################################
# When executed with the flags -powerOnVMs and -waitForExit, this test will #
# pause here and update the screen with the status of the agent VMs until #
# SIGINT is sent to this process. #
################################################################################
`
var (
flagPowerOnVMs = flag.Bool("powerOnVMs", false, "Powers on the VMs in the test with Docker")
flagWaitToExit = flag.Bool("waitToExit", false, "Waits for user input to exit the test")
)
func TestSimulator(t *testing.T) {
vcsim.Test(func(ctx context.Context, vimClient *vim25.Client) {
// Create a finder that sets the default datacenter.
finder := find.NewFinder(vimClient, true)
// Get the datacenter to use when creating the agency.
datacenter, err := finder.DefaultDatacenter(ctx)
if err != nil {
t.Fatal(err)
}
finder.SetDatacenter(datacenter)
// Get the "vm" folder.
folder, err := finder.DefaultFolder(ctx)
if err != nil {
t.Fatal(err)
}
// Get the cluster to use when creating the agency.
computeResource, err := finder.ClusterComputeResourceOrDefault(ctx, "")
if err != nil {
t.Fatal(err)
}
// Get the resource pool to use when creating the agency.
pool, err := computeResource.ResourcePool(ctx)
if err != nil {
t.Fatal(err)
}
// Get the datastore to use when creating the agency.
datastore, err := finder.DatastoreOrDefault(ctx, "")
if err != nil {
t.Fatal(err)
}
// Get the network to use when creating the agency.
network, err := finder.NetworkOrDefault(ctx, "DVS0")
if err != nil {
t.Fatal(err)
}
// Get an EAM client.
eamClient := eam.NewClient(vimClient)
// Get the EAM root object.
mgr := object.NewEsxAgentManager(eamClient, eam.EsxAgentManager)
// Define a function that will list and print the agency MoRefs.
listAgencies := func() int {
t.Log("listing agencies")
agencies, err := mgr.Agencies(ctx)
if err != nil {
t.Fatal(err)
}
if len(agencies) == 0 {
t.Log("no agencies")
return 0
}
for _, obj := range agencies {
t.Logf("agency: %v", obj.Reference())
config, err := obj.Config(ctx)
if err != nil {
t.Fatal(err)
}
t.Logf("agency config: %+v", config)
runtime, err := obj.Runtime(ctx)
if err != nil {
t.Fatal(err)
}
t.Logf("agency runtime: %+v", runtime)
agents, err := obj.Agents(ctx)
if err != nil {
t.Fatal(err)
}
if len(agents) == 0 {
t.Log("no agents")
} else {
for _, a := range agents {
t.Logf("agent: %v", a.Reference())
config, err := a.Config(ctx)
if err != nil {
t.Fatal(err)
}
t.Logf("agent config: %+v", config)
runtime, err := a.Runtime(ctx)
if err != nil {
t.Fatal(err)
}
t.Logf("agent runtime: %+v", runtime)
}
}
}
return len(agencies)
}
// List and print the agency MoRefs. There are none.
if listAgencies() > 0 {
t.Fatal("no agencies expected")
}
// Create a new agency.
t.Log("creating a new agency")
agency, err := mgr.CreateAgency(
ctx,
&types.AgencyConfigInfo{
AgencyName: "nginx",
AgentVmDatastore: []vim.ManagedObjectReference{
datastore.Reference(),
},
Folders: []types.AgencyVMFolder{
{
FolderId: folder.Reference(),
DatacenterId: datacenter.Reference(),
},
},
ResourcePools: []types.AgencyVMResourcePool{
{
ResourcePoolId: pool.Reference(),
ComputeResourceId: computeResource.Reference(),
},
},
AgentVmNetwork: []vim.ManagedObjectReference{
network.Reference(),
},
AgentConfig: []types.AgentConfigInfo{
{
OvfPackageUrl: "nginx",
},
{
OvfPackageUrl: "nginx",
},
},
},
string(types.EamObjectRuntimeInfoGoalStateEnabled),
)
if err != nil {
if soap.IsSoapFault(err) {
fault := soap.ToSoapFault(err).VimFault()
t.Fatalf("%[1]T %[1]v", fault)
} else {
t.Fatalf("%[1]T %[1]v", err)
}
}
t.Logf("created agency: %v", agency.Reference())
// List the agencies again, and this time the newly created agency will be
// printed to the console.
if listAgencies() != 1 {
t.Fatal("one agency expected")
}
// Check whether or not we want to power on the VMs with Docker.
if *flagPowerOnVMs {
agencies, err := mgr.Agencies(ctx)
if err != nil {
t.Fatal(err)
}
if len(agencies) == 0 {
t.Fatal("no agencies")
}
agency := agencies[0]
// Wait for the agent VMs to have IP addresses
{
agents, err := agency.Agents(ctx)
if err != nil {
t.Fatal(err)
}
if len(agents) == 0 {
t.Fatal("no agents")
}
wait := make(chan struct{})
done := make(chan struct{})
errs := make(chan error)
msgs := make(chan string)
var wg sync.WaitGroup
wg.Add(len(agents))
for _, agent := range agents {
agent := agent
go func() {
var (
vmPowerState string
vmIp string
once sync.Once
vmon = map[vim.ManagedObjectReference]struct{}{}
)
for {
runtime, err := agent.Runtime(ctx)
if err != nil {
errs <- err
return
}
if runtime.Vm == nil {
errs <- fmt.Errorf("vm is nil for agent %s", agent.Reference())
return
}
if _, ok := vmon[*runtime.Vm]; !ok {
vm := vimobject.NewVirtualMachine(vimClient, *runtime.Vm)
if _, err := vm.PowerOn(ctx); err != nil {
errs <- err
return
}
vmon[*runtime.Vm] = struct{}{}
}
if vmIp != runtime.VmIp || vmPowerState != string(runtime.VmPowerState) {
vmIp = runtime.VmIp
vmPowerState = string(runtime.VmPowerState)
msgs <- fmt.Sprintf(
"%v: name=%s, powerState=%s, ipAddr=%s",
*runtime.Vm,
runtime.VmName,
vmPowerState,
vmIp)
}
if vmIp != "" {
once.Do(func() {
wg.Done()
})
}
select {
case <-time.After(1 * time.Second):
case <-done:
return
}
}
}()
}
go func() {
wg.Wait()
if *flagWaitToExit {
<-wait
}
close(done)
}()
go func() {
defer close(wait)
if !*flagWaitToExit {
return
}
t.Log(waitLoopMessage)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for {
select {
case <-time.After(1 * time.Second):
case <-c:
return
}
}
}()
t.Log("waiting for the agent VMs to power on and get IP addresses")
timeout := time.After(10 * time.Minute)
func() {
for {
select {
case msg := <-msgs:
t.Log(msg)
case err := <-errs:
t.Fatal(err)
case <-done:
return
case <-timeout:
t.Fatal("timed out waiting for agent VMs to power on and get IP addresses")
return
}
}
}()
}
}
// Destroy the agency.
t.Log("destroying agency")
if err := agency.Destroy(ctx); err != nil {
t.Fatal(err)
}
if listAgencies() != 0 {
t.Fatal("no agencies expected")
}
})
}
func TestNotAuthenticated(t *testing.T) {
vcsim.Test(func(ctx context.Context, vimClient *vim25.Client) {
isNotAuthenticatedFault := func(fault vim.AnyType) bool {
switch fault.(type) {
case vim.NotAuthenticated, *vim.NotAuthenticated:
return true
default:
return false
}
}
isEamInvalidLoginFault := func(fault vim.AnyType) bool {
switch fault.(type) {
case types.EamInvalidLogin, *types.EamInvalidLogin:
return true
default:
return false
}
}
validateFault := func(
err error,
faultType string,
faultIsTypeFn func(vim.AnyType) bool) error {
if err == nil {
return fmt.Errorf("%s expected", faultType)
}
validateVimFault := func(vimFault vim.AnyType) error {
if !faultIsTypeFn(vimFault) {
return fmt.Errorf(
"err is not %[1]s: %[2]T %[2]v", faultType, vimFault)
}
return nil
}
if soap.IsSoapFault(err) {
return validateVimFault(soap.ToSoapFault(err).VimFault())
}
if soap.IsVimFault(err) {
return validateVimFault(soap.ToVimFault(err))
}
return fmt.Errorf(
"err is not a Soap or Vim fault: %[1]T %[1]v", err)
}
t.Run("TerminateSession", func(t *testing.T) {
// Terminate the session.
sessionManager := session.NewManager(vimClient)
if err := sessionManager.Logout(ctx); err != nil {
t.Fatalf("logout failed: %v", err)
}
})
t.Run("ValidateFaults", func(t *testing.T) {
t.Run("vim.NotAuthenticated", func(t *testing.T) {
t.Parallel()
// Create a finder to get the default datacenter.
finder := find.NewFinder(vimClient, true)
// Try to get the default datacenter, but receive a NotAuthenticated
// error.
_, err := finder.DefaultDatacenter(ctx)
if err := validateFault(
err,
"NotAuthenticated",
isNotAuthenticatedFault); err != nil {
t.Fatal(err)
}
})
t.Run("eam.EamInvalidLogin", func(t *testing.T) {
t.Parallel()
// Get an EAM client.
eamClient := eam.NewClient(vimClient)
// Get the EAM root object.
mgr := object.NewEsxAgentManager(eamClient, eam.EsxAgentManager)
// Try to list the agencies, but receive an EamInvalidLogin error.
_, err := mgr.Agencies(ctx)
if err := validateFault(
err,
"EamInvalidLogin",
isEamInvalidLoginFault); err != nil {
t.Fatal(err)
}
})
})
})
}