Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc features in vcsim #855

Merged
merged 3 commits into from
Oct 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions govc/test/snapshot.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env bats

load test_helper

test_vm_snapshot() {
vm=$1
id=$(new_id)

# No snapshots == no output
run govc snapshot.tree -vm "$vm"
assert_success ""

run govc snapshot.remove -vm "$vm" '*'
assert_success

run govc snapshot.revert -vm "$vm"
assert_failure

run govc snapshot.create -vm "$vm" "$id"
assert_success

run govc snapshot.revert -vm "$vm" enoent
assert_failure

run govc snapshot.revert -vm "$vm"
assert_success

run govc snapshot.remove -vm "$vm" "$id"
assert_success

run govc snapshot.create -vm "$vm" root
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

run govc snapshot.create -vm "$vm" grand
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

result=$(govc snapshot.tree -vm "$vm" -f | grep -c root/child/grand/child)
[ "$result" -eq 1 ]

run govc snapshot.revert -vm "$vm" root
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

# 3 snapshots named "child"
result=$(govc snapshot.tree -vm "$vm" | grep -c child)
[ "$result" -eq 3 ]

run govc snapshot.remove -vm "$vm" child
assert_failure

# 2 snapshots with path "root/child"
result=$(govc snapshot.tree -vm "$vm" -f | egrep -c 'root/child$')
[ "$result" -eq 2 ]

run govc snapshot.remove -vm "$vm" root/child
assert_failure

# path is unique
run govc snapshot.remove -vm "$vm" root/child/grand/child
assert_success

# set current to grand
run govc snapshot.revert -vm "$vm" grand

# name is unique
run govc snapshot.remove -vm "$vm" grand
assert_success

result=$(govc snapshot.tree -vm "$vm" -f | grep root/child/grand/child | wc -l)
[ "$result" -eq 0 ]

# current given to parent of previous current
result=$(govc snapshot.tree -vm "$vm" -f | grep '\.' | wc -l)
[ "$result" -eq 1 ]

id=$(govc snapshot.tree -vm "$vm" -f -i | egrep 'root/child$' | head -n1 | awk '{print $1}' | tr -d '[]')
# moid is unique
run govc snapshot.remove -vm "$vm" "$id"
assert_success

# now root/child is unique
run govc snapshot.remove -vm "$vm" root/child
assert_success

run govc snapshot.remove -vm "$vm" root
assert_success

# current is removed
result=$(govc snapshot.tree -vm "$vm" -f | grep '\.' | wc -l)
[ "$result" -eq 0 ]

# new root
run govc snapshot.create -vm "$vm" 2ndroot
assert_success

# new snapshot 2ndroot is current
result=$(govc snapshot.tree -vm "$vm" -f | grep '\.' | wc -l)
[ "$result" -eq 1 ]
}

@test "vm.snapshot vcsim" {
vcsim_env

vm=$(new_empty_vm)

test_vm_snapshot $vm
}

@test "vm.snapshot" {
esx_env

vm=$(new_ttylinux_vm)

test_vm_snapshot $vm
}
84 changes: 0 additions & 84 deletions govc/test/vm.bats
Original file line number Diff line number Diff line change
@@ -552,90 +552,6 @@ load test_helper
assert_success
}

@test "vm.snapshot" {
esx_env

vm=$(new_ttylinux_vm)
id=$(new_id)

# No snapshots == no output
run govc snapshot.tree -vm "$vm"
assert_success ""

run govc snapshot.remove -vm "$vm" '*'
assert_success

run govc snapshot.revert -vm "$vm"
assert_failure

run govc snapshot.create -vm "$vm" "$id"
assert_success

run govc snapshot.revert -vm "$vm" enoent
assert_failure

run govc snapshot.revert -vm "$vm"
assert_success

run govc snapshot.remove -vm "$vm" "$id"
assert_success

run govc snapshot.create -vm "$vm" root
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

run govc snapshot.create -vm "$vm" grand
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

result=$(govc snapshot.tree -vm "$vm" -f | grep -c root/child/grand/child)
[ "$result" -eq 1 ]

run govc snapshot.revert -vm "$vm" root
assert_success

run govc snapshot.create -vm "$vm" child
assert_success

# 3 snapshots named "child"
result=$(govc snapshot.tree -vm "$vm" | grep -c child)
[ "$result" -eq 3 ]

run govc snapshot.remove -vm "$vm" child
assert_failure

# 2 snapshots with path "root/child"
result=$(govc snapshot.tree -vm "$vm" -f | egrep -c 'root/child$')
[ "$result" -eq 2 ]

run govc snapshot.remove -vm "$vm" root/child
assert_failure

# path is unique
run govc snapshot.remove -vm "$vm" root/child/grand/child
assert_success

# name is unique
run govc snapshot.remove -vm "$vm" grand
assert_success

result=$(govc snapshot.tree -vm "$vm" -f | grep root/child/grand/child | wc -l)
[ "$result" -eq 0 ]

id=$(govc snapshot.tree -vm "$vm" -f -i | egrep 'root/child$' | head -n1 | awk '{print $1}' | tr -d '[]')
# moid is unique
run govc snapshot.remove -vm "$vm" "$id"
assert_success

# now root/child is unique
run govc snapshot.remove -vm "$vm" root/child
assert_success
}

@test "object name with slash" {
esx_env

10 changes: 10 additions & 0 deletions simulator/host_network_system.go
Original file line number Diff line number Diff line change
@@ -159,3 +159,13 @@ func (s *HostNetworkSystem) RemovePortGroup(c *types.RemovePortGroup) soap.HasFa

return r
}

func (s *HostNetworkSystem) UpdateNetworkConfig(req *types.UpdateNetworkConfig) soap.HasFault {
s.NetworkConfig = &req.Config

return &methods.UpdateNetworkConfigBody{
Res: &types.UpdateNetworkConfigResponse{
Returnval: types.HostNetworkConfigResult{},
},
}
}
1 change: 1 addition & 0 deletions simulator/service_instance.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ func NewServiceInstance(content types.ServiceContent, folder mo.Folder) *Service
NewSearchIndex(*s.Content.SearchIndex),
NewViewManager(*s.Content.ViewManager),
NewTaskManager(*s.Content.TaskManager),
NewUserDirectory(*s.Content.UserDirectory),
NewOptionManager(s.Content.Setting, setting),
}

72 changes: 72 additions & 0 deletions simulator/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright (c) 2017 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

import (
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

type VirtualMachineSnapshot struct {
mo.VirtualMachineSnapshot
}

func (v *VirtualMachineSnapshot) RemoveSnapshotTask(req *types.RemoveSnapshot_Task) soap.HasFault {
task := CreateTask(v, "removeSnapshot", func(t *Task) (types.AnyType, types.BaseMethodFault) {
Map.Remove(req.This)

vm := Map.Get(v.Vm).(*VirtualMachine)

if vm.Snapshot.CurrentSnapshot != nil && *vm.Snapshot.CurrentSnapshot == req.This {
parent := findParentSnapshotInTree(vm.Snapshot.RootSnapshotList, req.This)
vm.Snapshot.CurrentSnapshot = parent
}

vm.Snapshot.RootSnapshotList = removeSnapshotInTree(vm.Snapshot.RootSnapshotList, req.This, req.RemoveChildren)

return nil, nil
})

task.Run()

return &methods.RemoveSnapshot_TaskBody{
Res: &types.RemoveSnapshot_TaskResponse{
Returnval: task.Self,
},
}
}

func (v *VirtualMachineSnapshot) RevertToSnapshotTask(req *types.RevertToSnapshot_Task) soap.HasFault {
task := CreateTask(v, "revertToSnapshot", func(t *Task) (types.AnyType, types.BaseMethodFault) {
vm := Map.Get(v.Vm).(*VirtualMachine)

ref := v.Reference()
vm.Snapshot.CurrentSnapshot = &ref

return nil, nil
})

task.Run()

return &methods.RevertToSnapshot_TaskBody{
Res: &types.RevertToSnapshot_TaskResponse{
Returnval: task.Self,
},
}
}
78 changes: 78 additions & 0 deletions simulator/user_directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright (c) 2017 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

import (
"strings"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

var DefaultUserGroup = []*types.UserSearchResult{
{FullName: "root", Group: true, Principal: "root"},
{FullName: "root", Group: false, Principal: "root"},
{FullName: "administrator", Group: false, Principal: "admin"},
}

type UserDirectory struct {
mo.UserDirectory

userGroup []*types.UserSearchResult
}

func NewUserDirectory(ref types.ManagedObjectReference) object.Reference {
u := &UserDirectory{}

u.Self = ref
u.userGroup = DefaultUserGroup

return u
}

func (u *UserDirectory) RetrieveUserGroups(req *types.RetrieveUserGroups) soap.HasFault {
compare := compareFunc(req.SearchStr, req.ExactMatch)

var res []types.BaseUserSearchResult
for _, ug := range u.userGroup {
if req.FindUsers && !ug.Group || req.FindGroups && ug.Group {
if compare(ug.Principal) {
res = append(res, ug)
}
}
}

body := &methods.RetrieveUserGroupsBody{
Res: &types.RetrieveUserGroupsResponse{
Returnval: res,
},
}

return body
}

func compareFunc(compared string, exactly bool) func(string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of closures!

return func(s string) bool {
if exactly {
return s == compared
}
return strings.Contains(strings.ToLower(s), strings.ToLower(compared))
}
}
Loading