diff --git a/tests/constants/admincli/cmd.go b/tests/constants/admincli/cmd.go index a729f8163..3d4d9de03 100644 --- a/tests/constants/admincli/cmd.go +++ b/tests/constants/admincli/cmd.go @@ -56,14 +56,17 @@ const ( GetAccessForVMgroup = vmdkopsAdmin + "vmgroup access ls --name " // ListVMgroups list vm groups - ListVMgroups = vmdkopsAdmin + "vmgroup ls" + ListVMgroups = vmdkopsAdmin + "vmgroup ls " // DefaultVMgroup referring name of default vmgroup - DefaultVMgroup = "_DEFAULT " + DefaultVMgroup = "_DEFAULT" // VMHomeDatastore referring datastore where the docker host vm is created VMHomeDatastore = "_VM_DS" + // AllDatastore access to all datastore for vms in vmgroup + AllDatastore = "_ALL_DS" + // InitLocalConfigDb referring to Initialize (local) Single Node Config DB InitLocalConfigDb = vmdkopsAdmin + "config init --local" @@ -82,4 +85,16 @@ const ( // ReadWriteAccess read-write rights for the volume ReadWriteAccess = "read-write" + + // ListVmgroupVMs referring to vmdkops_admin vmgroups vm ls + ListVmgroupVMs = vmdkopsAdmin + "vmgroup vm ls --name=" + + // VMlist VM of the vmgroup + VMlist = " --vm-list=" + + // AddDatastoreAccess adds datastore to vmgroup + AddDatastoreAccess = vmdkopsAdmin + "vmgroup access add --name=" + + // GetDBMode get current DB config + GetDBMode = vmdkopsAdmin + "config status" ) diff --git a/tests/constants/admincli/vmdkops.go b/tests/constants/admincli/vmdkops.go index 718ecef15..a911951e7 100644 --- a/tests/constants/admincli/vmdkops.go +++ b/tests/constants/admincli/vmdkops.go @@ -31,4 +31,10 @@ const ( // StartService starts vmdkops service StartService = vmdkopsd + "start" + + // DBNotConfigured - DB mode not configured + DBNotConfigured = "NotConfigured" + + // DBSingleNode - DB mode SingleNode + DBSingleNode = "SingleNode" ) diff --git a/tests/e2e/basic_test.go b/tests/e2e/basic_test.go index 8f47ae9b6..d3bfbfa05 100644 --- a/tests/e2e/basic_test.go +++ b/tests/e2e/basic_test.go @@ -20,13 +20,13 @@ package e2e import ( - . "gopkg.in/check.v1" - + admincliconst "github.com/vmware/docker-volume-vsphere/tests/constants/admincli" "github.com/vmware/docker-volume-vsphere/tests/utils/admincli" "github.com/vmware/docker-volume-vsphere/tests/utils/dockercli" "github.com/vmware/docker-volume-vsphere/tests/utils/inputparams" "github.com/vmware/docker-volume-vsphere/tests/utils/misc" "github.com/vmware/docker-volume-vsphere/tests/utils/verification" + . "gopkg.in/check.v1" ) type BasicTestSuite struct { @@ -173,7 +173,7 @@ func (s *BasicTestSuite) TestVmGroupVolumeIsolation(c *C) { c.Assert(accessible, Equals, true, Commentf("Volume %s is not available on [%s]", s.volName1, s.vm2)) const vmgroup = "T1" - out, err = admincli.CreateVMgroup(s.esx, vmgroup, s.vm1Name) + out, err = admincli.CreateVMgroup(s.esx, vmgroup, s.vm1Name, admincliconst.VMHomeDatastore) c.Assert(err, IsNil, Commentf(out)) accessible = verification.CheckVolumeAvailability(s.vm1, s.volName1) diff --git a/tests/e2e/vmgroupmisc_test.go b/tests/e2e/vmgroupmisc_test.go new file mode 100644 index 000000000..ddc5147fa --- /dev/null +++ b/tests/e2e/vmgroupmisc_test.go @@ -0,0 +1,269 @@ +// Copyright 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. + +// This test suite contains miscellaneous tests to verify behavior of non-default vmgroup + +// +build unstable + +package e2e + +import ( + "log" + + admincliconst "github.com/vmware/docker-volume-vsphere/tests/constants/admincli" + "github.com/vmware/docker-volume-vsphere/tests/constants/properties" + "github.com/vmware/docker-volume-vsphere/tests/utils/admincli" + "github.com/vmware/docker-volume-vsphere/tests/utils/dockercli" + "github.com/vmware/docker-volume-vsphere/tests/utils/esx" + "github.com/vmware/docker-volume-vsphere/tests/utils/inputparams" + "github.com/vmware/docker-volume-vsphere/tests/utils/misc" + "github.com/vmware/docker-volume-vsphere/tests/utils/verification" + . "gopkg.in/check.v1" +) + +const ( + vmGroupName = "vg_basictest" +) + +type vgBasicSuite struct { + config *inputparams.TestConfig + volumeNames []string +} + +func (s *vgBasicSuite) SetUpSuite(c *C) { + basicVG := "basicVGTest" + s.config = inputparams.GetTestConfig() + if s.config == nil { + c.Skip("Unable to retrieve test config, skipping vmgroupbasic tests.") + } + admincli.ConfigInit(s.config.EsxHost) + + // Verify DB successfully initialized + c.Assert(admincli.GetDBmode(s.config.EsxHost), Equals, admincliconst.DBSingleNode, Commentf("Failed to init the DB mode on ESX - .", s.config.EsxHost)) + s.volumeNames = []string{inputparams.GetUniqueVolumeName(basicVG), inputparams.GetUniqueVolumeName(basicVG)} +} + +func (s *vgBasicSuite) SetUpTest(c *C) { + // Creating non-default vmgroup only if it does not exists + if admincli.IsVmgroupPresent(s.config.EsxHost, vmGroupName) { + return + } + admincli.CreateVMgroup(s.config.EsxHost, vmGroupName, s.config.DockerHostNames[0], s.config.Datastores[0]) + + // Verify if vmgroup exists + isVmgroupAvailable := admincli.IsVmgroupPresent(s.config.EsxHost, vmGroupName) + c.Assert(isVmgroupAvailable, Equals, true, Commentf("vmgroup ls command does not lists the vmgroup %s .", vmGroupName)) + + // Verify vm belongs to vmgroup + isVMPartofVg := admincli.IsVMInVmgroup(s.config.EsxHost, s.config.DockerHostNames[0], vmGroupName) + c.Assert(isVMPartofVg, Equals, true, Commentf("VM %s does not belong to vmgroup %s .", s.config.DockerHostNames[0], vmGroupName)) +} + +func (s *vgBasicSuite) TearDownSuite(c *C) { + admincli.RemoveVMFromVMgroup(s.config.EsxHost, vmGroupName, s.config.DockerHostNames[0]) + + // Verify vm does not belongs to vmgroup + isVMPartofVg := admincli.IsVMInVmgroup(s.config.EsxHost, s.config.DockerHostNames[0], vmGroupName) + c.Assert(isVMPartofVg, Equals, false, Commentf("Unexpected Behavior: VM %s belong to vmgroup %s .", s.config.DockerHostNames[0], vmGroupName)) + + admincli.DeleteVMgroup(s.config.EsxHost, vmGroupName) + // Verify vmgroup does not exist + isVmgroupAvailable := admincli.IsVmgroupPresent(s.config.EsxHost, vmGroupName) + c.Assert(isVmgroupAvailable, Equals, false, Commentf("Failed to delete the vmgroup [%s] .", vmGroupName)) + + // Removing the DB at the end of suite + admincli.ConfigRemove(s.config.EsxHost) + + // Verifying DB successfully removed + c.Assert(admincli.GetDBmode(s.config.EsxHost), Equals, admincliconst.DBNotConfigured, Commentf("Failed to remove the DB mode on ESX - .", s.config.EsxHost)) +} + +var _ = Suite(&vgBasicSuite{}) + +/* +//TO DO: Please reenable or remove this test based on the conclusion of the Issue # 1469 + +// Verify vmgroup for orphaned volumes is specified as N/A +// 1. Create a vmgroup and add vm to it. +// 2. Verify vmgroup was successfully created. +// 3. Create a volume from vm-2. +// 4. Verify volumes vmgroup from esx. +// 5. Remove vm-2 from vmgroup. +// 6. Verify volume is not visible from vm-2. +// 7. Delete the tenant. +// 8. Again verify volumes vmgroup is listed as N/A. + +func (s *vgBasicSuite) TestVGNameForOrphanedVolumes(c *C) { + misc.LogTestStart(c.TestName()) + nullVmgroup := "N/A" + + // Create a volume from vmgroup's vm + out, err := dockercli.CreateVolume(s.config.DockerHosts[0], s.volumeNames[0]) + c.Assert(err, IsNil, Commentf(out)) + + // Check if volume was successfully created + isAvailable := verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]) + c.Assert(isAvailable, Equals, true, Commentf("Volume %s is not available after creation", s.volumeNames[0])) + + // Verify if volume exists in vmgroup + isVolInVmgroup := admincli.IsVolumeExistInVmgroup(s.config.EsxHost, vmGroupName, s.volumeNames[0]) + c.Assert(isVolInVmgroup, Equals, true, Commentf("Volume [%s] does not belong to vmgroup [%s]", s.volumeNames[0], vmGroupName)) + + admincli.RemoveVMFromVMgroup(s.config.EsxHost, vmGroupName, s.config.DockerHostNames[0]) + + // Check if volume was not visible from vm since it does not belong to vmgroup + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]) + c.Assert(isAvailable, Equals, false, Commentf("Unexpected Behavior: Volume %s belonging to "+ + "vmgroup [%s] is visible from host [%s] which does not belong to the same vmgroup", s.volumeNames[0], vmGroupName, s.config.DockerHosts[0])) + + // Now delete the vmgroup + admincli.DeleteVMgroup(s.config.EsxHost, vmGroupName) + + // Verify vmgroup does not exist + isVmgroupAvailable := admincli.IsVmgroupPresent(s.config.EsxHost, vmGroupName) + c.Assert(isVmgroupAvailable, Equals, false, Commentf("Failed to delete the vmgroup [%s] .", vmGroupName)) + + // Verify vmgroup for volume is N/A + isVolInVmgroup = admincli.IsVolumeExistInVmgroup(s.config.EsxHost, nullVmgroup, s.volumeNames[0]) + c.Assert(isVolInVmgroup, Equals, true, Commentf("Unexpected Behavior: Vmgroup [%s] for volume [%s] is not N/A. ", s.volumeNames[0], vmGroupName)) + + // verify orpahned volume is not visible from host + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]) + c.Assert(isAvailable, Equals, false, Commentf("Unexpected Behavior: Orphaned volume %s "+ + "is visible from host [%s] ", s.volumeNames[0], s.config.DockerHosts[0])) + + misc.LogTestEnd(c.TestName()) +} +*/ + +// TestUserVGDatastoreAccessPrivilege - Verify volumes can be +// created by a VM as long as the non-default vmgroup has "allow_create" right given +// 1. Create vmgroup VG1 and add vm VM2 to it +// 2. Add datastore to VG1 - By default no "allow_create" right is given +// 3. Create volume in VG1 and expect error as allow-create is false +// 4. Create volume creation from VM1 (In _DEFAULT vmgroup) and expects successful volume creation +// 5. Change allow-create access to true +// 6. Repeat step-3 and this time volume creation should succeed +// 7. Update datastore access for VG1 (remove --allow-create rule) +// 8. Repeat step#5 and this time expect the error +func (s *vgBasicSuite) TestDSAccessPrivilegeForUserVG(c *C) { + misc.LogTestStart(c.TestName()) + + // Add another datastore to vmgroup + admincli.AddDatastoreToVmgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + + // Verify if vmgroup does not have access-rights for DS + isDatastoreAccessible := admincli.IsDSAccessibleForVMgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + c.Assert(isDatastoreAccessible, Equals, false, Commentf("Unexpected Behavior: Datastore %s is accessible for vmgroup %s .", s.config.Datastores[1], vmGroupName)) + + // Create a volume from _DEFAULT vmgroup's vm - operation should succeed + out, err := dockercli.CreateVolume(s.config.DockerHosts[1], s.volumeNames[1]) + c.Assert(err, IsNil, Commentf(out)) + + // Check if volume was successfully created + isAvailable := verification.CheckVolumeAvailability(s.config.DockerHosts[1], s.volumeNames[1]) + c.Assert(isAvailable, Equals, true, Commentf("Volume %s is not available after creation", s.volumeNames[1])) + + // This volume create will fail because of trying to create volume on the non-default DS. + out, err = dockercli.CreateVolume(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(err, Not(IsNil), Commentf(out)) + + // Check volume was not created + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(isAvailable, Equals, false, Commentf("Unexpected behavior: Volume %s is successfully created "+ + " even though vmgroup [%s] does not have access rights for the datastore %s", s.volumeNames[1], vmGroupName, s.config.Datastores[1])) + + // Set the create privilege on the vmgroup for specified datastore + out, _ = admincli.AddCreateAccessForVMgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + isDatastoreAccessible = admincli.IsDSAccessibleForVMgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + c.Assert(isDatastoreAccessible, Equals, true, Commentf("Datstore %s is not accessible for vmgroup %s .", s.config.Datastores[1], vmGroupName)) + + // Create a volume from non-default vmgroup's vm - operation should succeed this time + // as access-rights have been changed to True + out, err = dockercli.CreateVolume(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(err, IsNil, Commentf(out)) + + // Check if volume was successfully created + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(isAvailable, Equals, true, Commentf("Volume %s is not available after creation", s.volumeNames[1])) + + // verify able to delete volume + out, err = dockercli.DeleteVolume(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(err, IsNil, Commentf(out)) + + // Check if volume was successfully deleted + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], s.volumeNames[0]+"@"+s.config.Datastores[1]) + c.Assert(isAvailable, Equals, false, Commentf("Failed to delete volume %s", s.volumeNames[0])) + + // Remove the create privilege on the non-default vmgroup for specified datastore + admincli.RemoveCreateAccessForVMgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + + // Verify if vmgroup does not have access-rights for DS + isDatastoreAccessible = admincli.IsDSAccessibleForVMgroup(s.config.EsxHost, vmGroupName, s.config.Datastores[1]) + c.Assert(isDatastoreAccessible, Equals, false, Commentf("Datastore %s is accessible for vmgroup %s .", s.config.Datastores[1], vmGroupName)) + + // This volume create will fail because of trying to create volume on ds with no create privilege. + volumeName := inputparams.GetUniqueVolumeName(vmGroupName) + out, err = dockercli.CreateVolume(s.config.DockerHostNames[0], volumeName+"@"+s.config.Datastores[1]) + c.Assert(err, Not(IsNil), Commentf(out)) + + // Check if volume was not created + isAvailable = verification.CheckVolumeAvailability(s.config.DockerHosts[0], volumeName+"@"+s.config.Datastores[1]) + c.Assert(isAvailable, Equals, false, Commentf("Unexpected behavior: Volume %s is successfully created "+ + " even though vmgroup [%s] does not have access rights for the datastore %s ", s.volumeNames[1], vmGroupName, s.config.Datastores[1])) + + out, err = dockercli.DeleteVolume(s.config.DockerHosts[1], s.volumeNames[1]) + c.Assert(err, IsNil, Commentf(out)) + + misc.LogTestEnd(c.TestName()) +} + +// Test steps: +// 1. Create a vmgroup and associate a vm to it. +// 2. Execute vmgroup ls command. +// 3. Kill the vm that was added to the tenant. +// 4. Again execute vmgroup ls command to verify command works fine. +func (s *vgBasicSuite) TestVmgroupLsPostVMKill(c *C) { + misc.LogTestStart(c.TestName()) + + powerState := esx.GetVMPowerState(s.config.DockerHostNames[0]) + log.Printf("VM[%s]'s current power state is [%s]", s.config.DockerHostNames[0], properties.PowerOnState) + c.Assert(powerState, Equals, properties.PowerOnState, Commentf("VM [%s] should be powered on state", s.config.DockerHostNames[0])) + + vmProcessID := esx.GetVMProcessID(s.config.EsxHost, s.config.DockerHostNames[0]) + log.Printf("VM's process ID is: %s", vmProcessID) + + // Kill the VM + isVMKilled := esx.KillVM(s.config.EsxHost, vmProcessID) + c.Assert(isVMKilled, Equals, true, Commentf("Unable to kill VM %s ...", s.config.DockerHostNames[0])) + + isStatusChanged := esx.WaitForExpectedState(esx.GetVMPowerState, s.config.DockerHostNames[0], properties.PowerOffState) + c.Assert(isStatusChanged, Equals, true, Commentf("VM [%s] should be powered off state", s.config.DockerHostNames[0])) + + // Check vmgroup ls + isVmgroupAvailable := admincli.IsVmgroupPresent(s.config.EsxHost, vmGroupName) + c.Assert(isVmgroupAvailable, Equals, true, Commentf("vmgroup ls command does not lists the vmgroup %s .", vmGroupName)) + + // Power-on the VM + esx.PowerOnVM(s.config.DockerHostNames[0]) + + isStatusChanged = esx.WaitForExpectedState(esx.GetVMPowerState, s.config.DockerHostNames[0], properties.PowerOnState) + c.Assert(isStatusChanged, Equals, true, Commentf("Failed to power-on the VM [%s]", s.config.DockerHostNames[0])) + + // Verify vDVS successfully started + isVDVSRunning := verification.IsVDVSIsRunning(s.config.DockerHosts[0]) + c.Assert(isVDVSRunning, Equals, true, Commentf("Failed to start docker volume vsphere on the VM [%s]", s.config.DockerHostNames[0])) + + misc.LogTestEnd(c.TestName()) +} diff --git a/tests/utils/admincli/vmgroupmgmt.go b/tests/utils/admincli/vmgroupmgmt.go index 69a45de80..3677ad68c 100644 --- a/tests/utils/admincli/vmgroupmgmt.go +++ b/tests/utils/admincli/vmgroupmgmt.go @@ -19,51 +19,58 @@ package admincli import ( "log" + "strings" "github.com/vmware/docker-volume-vsphere/tests/constants/admincli" "github.com/vmware/docker-volume-vsphere/tests/utils/ssh" ) // CreateVMgroup method is going to create a vmgroup and adds vm to it. -func CreateVMgroup(ip, name, vmName string) (string, error) { +func CreateVMgroup(ip, name, vmName, dsName string) (string, error) { log.Printf("Creating a vmgroup [%s] on esx [%s]\n", name, ip) - return ssh.InvokeCommand(ip, admincli.CreateVMgroup+name+" --default-datastore="+admincli.VMHomeDatastore+" --vm-list="+vmName) + return ssh.InvokeCommand(ip, admincli.CreateVMgroup+name+" --default-datastore="+dsName+admincli.VMlist+vmName) } -// DeleteVMgroup method deletes a vmgroup and removes its volumes as well +// DeleteVMgroup method deletes a vmgroup func DeleteVMgroup(ip, name string) (string, error) { log.Printf("Deleting a vmgroup [%s] on esx [%s]\n", name, ip) + return ssh.InvokeCommand(ip, admincli.RemoveVMgroup+name) +} + +// DeleteVMgroupAndItsVolumes method deletes a vmgroup and removes its volumes +func DeleteVMgroupAndItsVolumes(ip, name string) (string, error) { + log.Printf("Deleting a vmgroup and its corresponding volumes [%s] on esx [%s]\n", name, ip) return ssh.InvokeCommand(ip, admincli.RemoveVMgroup+name+admincli.RemoveVolumes) } // AddVMToVMgroup - Adds vm to vmgroup func AddVMToVMgroup(ip, name, vmName string) (string, error) { log.Printf("Adding VM [%s] to a vmgroup [%s] on esx [%s] \n", vmName, name, ip) - return ssh.InvokeCommand(ip, admincli.AddVMToVMgroup+name+" --vm-list="+vmName) + return ssh.InvokeCommand(ip, admincli.AddVMToVMgroup+name+admincli.VMlist+vmName) } // RemoveVMFromVMgroup - Removes a vm from vmgroup func RemoveVMFromVMgroup(ip, name, vmName string) (string, error) { log.Printf("Removing VM [%s] from a vmgroup [%s] on esx [%s] \n", vmName, name, ip) - return ssh.InvokeCommand(ip, admincli.RemoveVMFromVMgroup+name+" --vm-list="+vmName) + return ssh.InvokeCommand(ip, admincli.RemoveVMFromVMgroup+name+admincli.VMlist+vmName) } // ReplaceVMFromVMgroup - Replaces a vm from vmgroup func ReplaceVMFromVMgroup(ip, name, vmName string) (string, error) { log.Printf("Replacing VM [%s] from a vmgroup [%s] on esx [%s] \n", vmName, name, ip) - return ssh.InvokeCommand(ip, admincli.ReplaceVMFromVMgroup+name+" --vm-list="+vmName) + return ssh.InvokeCommand(ip, admincli.ReplaceVMFromVMgroup+name+admincli.VMlist+vmName) } // AddCreateAccessForVMgroup - set allow-create access on the vmgroup func AddCreateAccessForVMgroup(ip, name, datastore string) (string, error) { log.Printf("Enabling create access for vmgroup %s, datastore %s on esx [%s] \n", name, datastore, ip) - return ssh.InvokeCommand(ip, admincli.SetAccessForVMgroup + name + " --allow-create True --datastore " + datastore) + return ssh.InvokeCommand(ip, admincli.SetAccessForVMgroup+name+" --allow-create True --datastore "+datastore) } -// RemoveCreateAccessForVMgroup - remove ellow-create access on the vmgroup +// RemoveCreateAccessForVMgroup - remove allow-create access on the vmgroup func RemoveCreateAccessForVMgroup(ip, name, datastore string) (string, error) { log.Printf("Removing create access for vmgroup %s, datastore %s on esx [%s] \n", name, datastore, ip) - return ssh.InvokeCommand(ip, admincli.SetAccessForVMgroup + name + " --allow-create False --datastore " + datastore) + return ssh.InvokeCommand(ip, admincli.SetAccessForVMgroup+name+" --allow-create False --datastore "+datastore) } // SetVolumeSizeForVMgroup - set max and total volume size for vmgroup @@ -84,3 +91,101 @@ func ConfigRemove(ip string) (string, error) { log.Printf("Removing the SingleNode Config DB on esx [%s] \n", ip) return ssh.InvokeCommand(ip, admincli.RemoveLocalConfigDb) } + +// IsVmgroupPresent - checks for a vmgroup in list of vmgroups +func IsVmgroupPresent(esxIP, vmgroupName string) bool { + log.Printf("Checking if vmgroup [%s] exists from esx [%s] \n", vmgroupName, esxIP) + cmd := admincli.ListVMgroups + "2>/dev/null | grep " + vmgroupName + out, _ := ssh.InvokeCommand(esxIP, cmd) + return strings.Contains(out, vmgroupName) +} + +// IsVolumeExistInVmgroup returns true if the given volume is available +// from the specified vmgroup; false otherwise. +func IsVolumeExistInVmgroup(esxIP, vmgroupName, volName string) bool { + return IsVolumeListExistInVmgroup(esxIP, vmgroupName, []string{volName}) +} + +// IsVolumeListExistInVmgroup returns true if the given volumes specified in list are +// from the specified vmgroup; false otherwise. +func IsVolumeListExistInVmgroup(esxIP, vmgroupName string, volList []string) bool { + log.Printf("Checking if volumes [%s] belongs to vmgroup [%s] from esx [%s] \n", volList, vmgroupName, esxIP) + cmd := admincli.ListVolumes + " -c volume,vmgroup 2>/dev/null | awk -v OFS='\t' '{print $1, $2}' | sed '1,2d' " + out, _ := ssh.InvokeCommand(esxIP, cmd) + + vgValues := strings.Fields(out) + VolumeToVgMap := make(map[string]string) + for i := 0; i < len(vgValues); { + VolumeToVgMap[vgValues[i]] = vgValues[i+1] + i = i + 2 + } + for _, volName := range volList { + if val, ok := VolumeToVgMap[volName]; !ok || val != vmgroupName { + log.Printf("Volume %s does not belongs to vmgroup - %s", volName, vmgroupName) + return false + } + } + return true +} + +// IsVMInVmgroup - verify vm's vmgroup is same as expected +func IsVMInVmgroup(esxIP, vmName, vmgroupName string) bool { + log.Printf("Checking if vm [%s] belongs to vmgroup [%s] from esx [%s] \n", vmName, vmgroupName, esxIP) + cmd := admincli.ListVmgroupVMs + vmgroupName + " 2>/dev/null | cut -d ' ' -f3 " + out, _ := ssh.InvokeCommand(esxIP, cmd) + return strings.Contains(out, vmName) +} + +// AddDatastoreToVmgroup - Grants datastore to a vmgroup whose access is controlled +func AddDatastoreToVmgroup(ip, name, datastore string) (string, error) { + log.Printf("Adding datastore %s to vmgroup %s, on esx [%s] \n", datastore, name, ip) + return ssh.InvokeCommand(ip, admincli.AddDatastoreAccess+name+" --datastore="+datastore) +} + +// IsDSAccessibleForVMgroup - Verifies if vmgroup has access rights to a datastore +func IsDSAccessibleForVMgroup(ip, name, datastore string) bool { + log.Printf("Getting vmgroup's [%s] access rights to datastore %s , on esx [%s] \n", name, datastore, ip) + cmd := admincli.GetAccessForVMgroup + name + " 2>/dev/null | awk -v OFS='\t' '{print $1, $2}' | grep " + datastore + out, _ := ssh.InvokeCommand(ip, cmd) + accessRights := strings.Fields(out) + log.Printf("Access rights to datastore %s for vmgroup - %s are: %s . ", datastore, name, accessRights) + if len(accessRights) > 1 && accessRights[1] == "True" { + return true + } + return false + +} + +// CreateDefaultVmgroup - Create the default Vmgroup +func CreateDefaultVmgroup(ip string) bool { + log.Printf("Creating default vmgroup %s on esx [%s] ", admincli.DefaultVMgroup, ip) + cmd := admincli.CreateVMgroup + admincli.DefaultVMgroup + " --default-datastore " + admincli.VMHomeDatastore + + // Create a default vmgroup with default datastore set to _VM_DS + ssh.InvokeCommand(ip, cmd) + + // Verify default vmgroup exists + isVmgroupAvailable := IsVmgroupPresent(ip, admincli.DefaultVMgroup) + if !isVmgroupAvailable { + log.Println("Failed to create default vmgroup") + return false + } + + // Set datastore to _ALL_DS to allow access to all datastore for default vmgroup + ssh.InvokeCommand(ip, admincli.AddDatastoreAccess+admincli.DefaultVMgroup+" --datastore="+admincli.AllDatastore+" --allow-create ") + return IsDSAccessibleForVMgroup(ip, admincli.DefaultVMgroup, admincli.AllDatastore) +} + +// GetDBmode - Returns current DB mode +func GetDBmode(esxIP string) string { + log.Printf("Get db mode from esx [%s] \n", esxIP) + cmd := admincli.GetDBMode + "| grep DB_Mode" + out, _ := ssh.InvokeCommand(esxIP, cmd) + if strings.Contains(out, admincli.DBNotConfigured) { + return admincli.DBNotConfigured + } else if strings.Contains(out, admincli.DBSingleNode) { + return admincli.DBSingleNode + } else { + return "" + } +} diff --git a/tests/utils/verification/vDVSUtil.go b/tests/utils/verification/vDVSUtil.go new file mode 100644 index 000000000..3d1841ff6 --- /dev/null +++ b/tests/utils/verification/vDVSUtil.go @@ -0,0 +1,44 @@ +// Copyright 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. + +// This util provides various helper methods that can be used by different tests to +// fetch information related to vSphere docker-volume-service. + +package verification + +import ( + "log" + + "github.com/vmware/docker-volume-vsphere/tests/utils/misc" + "github.com/vmware/docker-volume-vsphere/tests/utils/ssh" +) + +// IsVDVSIsRunning - finds out if vDVS is running. This util can be useful +// in scenarios where vm is powered-on and user wants to find out if +// vm is fully up to be able to run docker volume commands. +func IsVDVSIsRunning(ip string) bool { + log.Printf("Verifying if vDVS is running on vm : %s", ip) + maxAttempt := 30 + waitTime := 3 + for attempt := 0; attempt < maxAttempt; attempt++ { + misc.SleepForSec(waitTime) + pid, _ := ssh.InvokeCommand(ip, "pidof docker-volume-vsphere") + if pid != "" { + log.Printf("Process ID of docker-volume-vsphere is : %s", pid) + return true + } + } + log.Printf("vDVS failed to start.\n") + return false +}