Skip to content

Commit

Permalink
vcsim: add AlarmManager
Browse files Browse the repository at this point in the history
Closes #3489
Closes #2476
  • Loading branch information
dougm committed Aug 9, 2024
1 parent d6a3b61 commit 9c28210
Show file tree
Hide file tree
Showing 7 changed files with 668 additions and 0 deletions.
21 changes: 21 additions & 0 deletions govc/object/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,26 @@ func saveHostSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObjec
return []saveMethod{{"QueryTpmAttestationReport", res}}, nil
}

func saveAlarmManager(ctx context.Context, c *vim25.Client, ref types.ManagedObjectReference) ([]saveMethod, error) {
res, err := methods.GetAlarm(ctx, c, &types.GetAlarm{This: ref})
if err != nil {
return nil, err
}
pc := property.DefaultCollector(c)
var content []types.ObjectContent
if err = pc.Retrieve(ctx, res.Returnval, nil, &content); err != nil {
return nil, err
}
return []saveMethod{{"GetAlarm", res}, {"", content}}, nil
}

// saveObjects maps object types to functions that can save data that isn't available via the PropertyCollector
var saveObjects = map[string]func(context.Context, *vim25.Client, types.ManagedObjectReference) ([]saveMethod, error){
"VmwareDistributedVirtualSwitch": saveDVS,
"EnvironmentBrowser": saveEnvironmentBrowser,
"HostNetworkSystem": saveHostNetworkSystem,
"HostSystem": saveHostSystem,
"AlarmManager": saveAlarmManager,
}

func isNotConnected(err error) bool {
Expand Down Expand Up @@ -204,6 +218,13 @@ func (cmd *save) save(content []types.ObjectContent) error {
return err
}
for _, obj := range objs {
if obj.Name == "" {
err = cmd.save(obj.Data.([]types.ObjectContent))
if err != nil {
return err
}
continue
}
err = cmd.write(filepath.Join(ref, obj.Name), obj.Data)
if err != nil {
return err
Expand Down
127 changes: 127 additions & 0 deletions govc/test/alarms.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env bats

load test_helper

@test "alarms" {
vcsim_env

run govc alarms
assert_success

run govc alarms -d
assert_success

vm=/DC0/vm/DC0_H0_VM0

run govc alarms $vm
assert_success

run govc object.collect -s $vm triggeredAlarmState
assert_success "" # empty

run env GOVC_SHOW_UNRELEASED=true govc event.post -s info -i vcsim.vm.success $vm
assert_success

run govc alarms $vm
assert_success
[ ${#lines[@]} -eq 1 ] # header only, no alarm triggered

run env GOVC_SHOW_UNRELEASED=true govc event.post -s warning -i vcsim.vm.failure $vm
assert_success

run govc alarms $vm
assert_success
assert_matches "Warning"

run govc alarms -l $vm
assert_success

run govc alarms -json $vm
assert_success
alarms="$output"
run jq -r .[].event.eventTypeId <<<"$alarms"
assert_success "vcsim.vm.failure"

run govc object.collect -json -s $vm triggeredAlarmState
assert_success
state="$output"
run jq -r .[].overallStatus <<<"$state"
assert_success "yellow"
run jq -r .[].acknowledged <<<"$state"
assert_success "false"

run govc alarms -ack
assert_success

run govc object.collect -json -s $vm triggeredAlarmState
assert_success
state="$output"
run jq -r .[].overallStatus <<<"$state"
assert_success "yellow"
run jq -r .[].acknowledged <<<"$state"
assert_success "true"

run env GOVC_SHOW_UNRELEASED=true govc event.post -s info -i vcsim.vm.success $vm
assert_success

run govc object.collect -s $vm triggeredAlarmState
assert_success "" # empty

run govc object.collect -s / triggeredAlarmState
assert_success "" # empty
}

@test "alarm.info" {
vcsim_env

run govc alarm.info
assert_success

run govc alarm.info -n alarm.VmErrorAlarm
assert_success
assert_matches "Virtual machine error"

run govc alarm.info -n invalid
assert_success ""
}

@test "alarm -esx" {
vcsim_env -esx

run govc object.collect -s - content.alarmManager
assert_success "" # empty, no AlarmManager

run govc alarm.info
assert_failure # not supported
}

@test "alarm.create" {
vcsim_env

export GOVC_SHOW_UNRELEASED=true

run govc alarm.create -n "My Alarm" -green my.alarm.success -yellow my.alarm.failure
assert_success
id="$output"

self=$(govc alarm.info -json -n "My Alarm" | jq -r .[].self.value)
assert_equal "$id" "$self"

run govc alarm.info -n "My Alarm"
assert_success

run govc alarm.create -n "My Alarm"
assert_failure # DuplicateName

run govc alarm.create -n "My Alarm" -r -d "This is my alarm description"
assert_success

run govc alarm.rm "$id"
assert_success

run govc alarm.rm "$id"
assert_failure

run govc alarm.info -n "My Alarm"
assert_success ""
}
Loading

0 comments on commit 9c28210

Please sign in to comment.