def TestGetGroups(repManager): # This test needs to run when there are no groups in the server (i.e. before # other tests) """Test ReplicationManager groups property.""" # Check that we get no groups initially groups = repManager.groups if len(groups) != 0: raise TestFailedExc("Expected no groups, server reported " + str(groups)) # Add a group, check that we get it group1Id = CreateRandomId("getgroups1") group1Spec = CreateRandomizedGroupSpec(groupID=group1Id) group1 = ExpectNoException(repManager.CreateGroup, group1Spec) groups = repManager.groups if (len(groups) != 1) or (group1._moId != groups[0]._moId): raise TestFailedExc("Expected 1 group " + str(group1) + " received " + str(groups)) # Add another group, check that we get both group2Id = CreateRandomId("getgroups2") group2Spec = CreateRandomizedGroupSpec(groupID=group2Id) group2 = ExpectNoException(repManager.CreateGroup, group2Spec) groups = repManager.groups if len(groups) != 2 or not \ (((group1._moId == groups[0]._moId) and \ (group2._moId == groups[1]._moId)) \ or ((group1._moId == groups[1]._moId) and \ (group2._moId == groups[0]._moId))): raise TestFailedExc("Expected 2 groups " + str(group1) + " and " + str(group2) + " but received " + str(groups)) # Remove both groups, check that again we get no groups ExpectNoException(group1.Remove) ExpectNoException(group2.Remove) groups = repManager.groups if len(groups) != 0: raise TestFailedExc("Expected no groups, server reported " + str(groups))
def TestRemoveGroup(repManager): """Test Group Remove method""" gspec = CreateRandomizedGroupSpec() newGroup = ExpectNoException(repManager.CreateGroup, gspec) Log("Created valid group " + str(newGroup) + ". Will delete it now.") ExpectNoException(newGroup.Remove) Log("Deleted group " + str(newGroup) + ". Will delete it again.") ExpectManagedObjectNotFound(newGroup.Remove) Log("Re-add group " + str(gspec) + ".") ExpectNoException(repManager.CreateGroup, gspec)
def TestGetValidGroup(repManager): """Test ReplicationManager.GetGroup with a valid group.""" validID = CreateRandomId("valid-group") gspec = CreateRandomizedGroupSpec(groupID=validID) newGroup = ExpectNoException(repManager.CreateGroup, gspec) fetchGroup = ExpectNoException(repManager.GetGroup, validID) if fetchGroup._moId != newGroup._moId: raise TestFailedExc("New group '" + str(newGroup) + "' is not " "equivalent to fetched '" + str(fetchGroup) + "'.") Log("Created and looked up '" + str(newGroup) + "'.")
def TestUnconfigureGroup(repManager): """Test Group Unconfigure method""" # Create the group gspec = CreateRandomizedGroupSpec() newGroup = ExpectNoException(repManager.CreateGroup, gspec) # Unconfigure the group Log("Created valid group " + str(newGroup) + ". Will unconfigure it now.") ExpectNoException(newGroup.Unconfigure) Log("Unconfigured group " + str(newGroup) + ". Will unconfigure it again (error).") ExpectManagedObjectNotFound(newGroup.Unconfigure) Log("Re-add group " + str(gspec) + ".") ExpectNoException(repManager.CreateGroup, gspec)
def TestAddGroupErrorCleanup(repManager): """Test ReplicationManager cleanup on error in AddGroup""" global defaultDsMgr gspec1 = CreateRandomizedGroupSpec() gspec2 = CreateRandomizedGroupSpec() # Create a bogus disk disk = Hbr.Replica.DiskSpec() disk.diskIdent = Hbr.Replica.IdentSpec() disk.diskIdent.id = CreateRandomId("disk-id") disk.diskIdent.datastoreUUID = defaultDsMgr.DatastoreUUID() disk.diskIdent.pathname = "bogus/path/baby.vmdk" # Add disk to both groups gspec1.vms[0].replicatedDisks.append(disk) gspec2.vms[0].replicatedDisks.append(disk) Log("Adding group 1 with bogus disk") group1 = ExpectNoException(repManager.CreateGroup, gspec1) Log("Trying to add group 2 with same bogus disk") ExpectException(Hbr.Replica.Fault.HbrDiskAlreadyExists, repManager.CreateGroup, gspec2) # Add it again, shouldn't matter ExpectException(Hbr.Replica.Fault.HbrDiskAlreadyExists, repManager.CreateGroup, gspec2) Log("Removing group 1") ExpectNoException(group1.Remove) Log("Adding group 2") group2 = ExpectNoException(repManager.CreateGroup, gspec2) Log("Removing group 2") ExpectNoException(group2.Remove)
def TestRecoverGroup(repManager): """Test Group Recover method""" # Create the group gspec = CreateRandomizedGroupSpec() newGroup = ExpectNoException(repManager.CreateGroup, gspec) # Unconfigure the group Log("Created valid group " + str(newGroup) + ". Will unconfigure it now.") ExpectNoException(newGroup.Unconfigure) # Recover the group (equivalent to createGroup for now) Log("Recover unconfigured group " + str(newGroup) + ". Will delete it again.") ExpectManagedObjectNotFound(newGroup.Unconfigure) Log("Re-add group " + str(gspec) + ".") ExpectNoException(repManager.CreateGroup, gspec)
def TestHosts(repManager, datastoreConf): storageManager = repManager.GetStorageManager() # unpack the legitimate datastore configuration (host, user, password) = datastoreConf randHost = CreateRandomId("host-") randUser1 = CreateRandomId("user-") randUser2 = CreateRandomId("user1-") randPass = CreateRandomId("pass-") Log("Test EnableHost") # Test adding a new host hspec = CreateHostSpec(randHost, randUser1 + ":" + randPass) newHost1 = ExpectNoException(storageManager.EnableHost, hspec) print("Added host %s" % newHost1.GetId) # Test modifying the user of existing host hspec = CreateHostSpec(randHost, randUser2 + ":" + randPass) newHost2 = ExpectNoException(storageManager.EnableHost, hspec) # Test modifying password of existing host hspec = CreateHostSpec(randHost, randUser2 + ":") newHost2 = ExpectNoException(storageManager.EnableHost, hspec) # Since EnableHost is idempotent, get a reference to existing valid host hspec = CreateHostSpec(host, user + ":" + password) validHost = ExpectNoException(storageManager.EnableHost, hspec) # Test removing the host Log("Test removing hosts...") ExpectNoException(newHost1.Remove) ExpectNoException(validHost.Remove) # There should be no valid hosts existing and hence all configured # datastores should be marked inaccessible. dsList = storageManager.GetExpectedDatastores() for ds in dsList: if (ds.accessible): PrintDatastores(dsList) raise TestFailedExc( "Removing all hosts should mark all datastores inaccessible") Log("Test EnableHost with null configurations") # Try with .. # .. invalid host name ExpectException(Hbr.Replica.Fault.InvalidHostSpec, storageManager.EnableHost, CreateHostSpec("", randUser1 + ":" + randPass)) # .. invalid user/pass ExpectException(Hbr.Replica.Fault.InvalidHostSpec, storageManager.EnableHost, CreateHostSpec("valid", ":")) ExpectException(Hbr.Replica.Fault.InvalidHostSpec, storageManager.EnableHost, CreateHostSpec("valid", "@")) # .. invalid user ExpectException(Hbr.Replica.Fault.InvalidHostSpec, storageManager.EnableHost, CreateHostSpec("valid", ":" + randPass)) # Add the valid host with an invalid password, expect failures: validHost = ExpectNoException( storageManager.EnableHost, CreateHostSpec(host, user + ":" + password + "BORKED")) if validHost.accessible: raise TestFailedExc( "Valid host, with bad password, should not be accessible.") # Fixup the host (later tests expect it to be present in the server) validHost = ExpectNoException(storageManager.EnableHost, CreateHostSpec(host, user + ":" + password)) if not validHost.accessible: raise TestFailedExc("Valid host should be accessible.") return
def TestReplicaGroup(repManager): rpo = 42 retentionPolicy = AddTierToRetentionPolicy(None, 60, 4) gspec = CreateRandomizedGroupSpec(rpo=rpo, retentionPolicy=retentionPolicy) group = ExpectNoException(repManager.CreateGroup, gspec) Log("Created group for testing ReplicaGroup API: " + str(group)) TestGroupVMs(gspec, group) Log("Test GetRpo") returnRpo = group.GetRpo() if returnRpo != rpo: raise TestFailedExc("RPO should be " + str(rpo) + ", not " + str(returnRpo)) Log("Test UpdateRpo (valid RPO values)") for nRpo in [0, 1, 11, 13, 99, 100, 1000, 1440]: ExpectNoException(group.UpdateRpo, nRpo) returnRpo = group.GetRpo() if returnRpo != nRpo: raise TestFailedExc("RPO should be " + str(nRpo) + ", not " + str(returnRpo)) Log("Test UpdateRpo (invalid RPO values)") invalRpoFault = Vmodl.Fault.InvalidArgument for nRpo in [-1000, -1, 1441, 100 * 1000 * 1000]: ExpectException(invalRpoFault, group.UpdateRpo, nRpo) Log("Test GetRetentionPolicy") returnPolicy = group.GetRetentionPolicy() if RetentionPolicyToString(returnPolicy) != RetentionPolicyToString( retentionPolicy): raise TestFailedExc("Retention policy should be " + RetentionPolicyToString(retentionPolicy) + ", not " + RetentionPolicyToString(returnPolicy)) Log("Test UpdateRetentionPolicy (valid retention policy values)") testPolicy = AddTierToRetentionPolicy(None, 0, 8) testPolicy = AddTierToRetentionPolicy(testPolicy, 5, 14) testPolicy = AddTierToRetentionPolicy(testPolicy, 15, 3) testPolicy = AddTierToRetentionPolicy(testPolicy, 45, 16) testPolicy = AddTierToRetentionPolicy(testPolicy, 120, 8) testPolicy = AddTierToRetentionPolicy(testPolicy, 360, 10) testPolicy = AddTierToRetentionPolicy(testPolicy, 1140, 2) totalSlots = 0 retentionPolicy = None for tier in testPolicy.tiers: if totalSlots + tier.GetNumSlots() > 24: totalSlots = 0 retentionPolicy = None retentionPolicy = AddTierToRetentionPolicy( retentionPolicy, tier.GetGranularityMinutes(), tier.GetNumSlots()) totalSlots += tier.GetNumSlots() ExpectNoException(group.UpdateRetentionPolicy, retentionPolicy) returnPolicy = group.GetRetentionPolicy() if RetentionPolicyToString(returnPolicy) != RetentionPolicyToString( retentionPolicy): raise TestFailedExc("Retention policy should be " + RetentionPolicyToString(retentionPolicy) + ", not " + RetentionPolicyToString(returnPolicy)) Log("Test UpdateRetentionPolicy (invalid retention policy values)") testPolicy = AddTierToRetentionPolicy(None, -1, 8) testPolicy = AddTierToRetentionPolicy(testPolicy, 0, -1) testPolicy = AddTierToRetentionPolicy(testPolicy, 15, 0) testPolicy = AddTierToRetentionPolicy(testPolicy, 45, 36) invalidPolicyFault = Vmodl.Fault.InvalidArgument for tier in testPolicy.tiers: retentionPolicy = AddTierToRetentionPolicy( None, tier.GetGranularityMinutes(), tier.GetNumSlots()) ExpectException(invalidPolicyFault, group.UpdateRetentionPolicy, retentionPolicy) # Test repeating the same granularity mins testPolicy = AddTierToRetentionPolicy(None, 5, 2) testPolicy = AddTierToRetentionPolicy(testPolicy, 10, 6) testPolicy = AddTierToRetentionPolicy(testPolicy, 10, 12) testPolicy = AddTierToRetentionPolicy(testPolicy, 20, 1) ExpectException(invalidPolicyFault, group.UpdateRetentionPolicy, testPolicy) # Test total number of slots greater than default max of 24 testPolicy = AddTierToRetentionPolicy(None, 3, 9) testPolicy = AddTierToRetentionPolicy(testPolicy, 19, 8) testPolicy = AddTierToRetentionPolicy(testPolicy, 43, 6) testPolicy = AddTierToRetentionPolicy(testPolicy, 111, 7) ExpectException(invalidPolicyFault, group.UpdateRetentionPolicy, testPolicy) Log("Test GetState") st = group.GetState() if st != "passive": raise TestFailedExc("State should be 'passive'") else: Log("Group is 'passive'") Log("Test GetId") ExpectNoException(group.GetId) Log("Test GetCurrentRpoViolation") rpoViolation = group.GetCurrentRpoViolation() if (rpoViolation != -1): raise TestFailedExc("Rpo violation should initially be -1, not " + str(rpoViolation)) # group.Remove tested elsewhere # Can't test CommitToImage without an image ... #Log("Test CommitToImage"); #ExpectNotImplemented(group.CommitToImage, None) # Note that .Instances is a property, not a method Log("Test .instances property") insts = group.instances if len(insts) > 0: raise TestFailedExc("Groups should contain 0 instances!") else: Log("No instances.") # Note: that .latestInstance is a property, not a method inst = group.latestInstance Log("Got .latestInstance " + str(inst)) if inst: raise TestFailedExc("Group should not have a 'latest' instance!") else: Log("No latest instance.")
def TestAddRemoveDisk(repManager): """Test ReplicationManager RemoveGroup method""" groupID = CreateRandomId("addrm-group-id") disk1Spec = CreateRandomizedDiskSpec(datastoreMgr=None) disk2Spec = CreateRandomizedDiskSpec(datastoreMgr=None) disk3Spec = CreateRandomizedDiskSpec(datastoreMgr=None) vmSpec = Hbr.Replica.VirtualMachineSpec() vmSpec.virtualMachineIdent = CreateRandomizedVMIdent(datastoreMgr=None, vmID=groupID) vmSpec.replicatedDisks = [disk1Spec] gSpec = Hbr.Replica.GroupSpec() gSpec.id = groupID gSpec.rpo = 13 gSpec.vms = [vmSpec] Log("Creating group") g = ExpectNoException(repManager.CreateGroup, gSpec) vms = g.GetVms() ExpectCond(len(vms) == 1, "Group should contain 1 VM") vm = vms[0] Log("Adding disk 2") d2 = ExpectNoException(vm.AddDisk, disk2Spec) ExpectException(Hbr.Replica.Fault.HbrDiskAlreadyExists, vm.AddDisk, disk2Spec) ExpectCond(not d2.unconfigured, "Disk is unconfigured after add!") ExpectException(Hbr.Replica.Fault.HbrDiskNotUnconfigured, vm.RemoveDisk, d2) ExpectCond(not d2.unconfigured, "Disk became unconfigured!") ExpectNoException(vm.UnconfigureDisk, d2) ExpectCond(d2.unconfigured, "Disk is not unconfigured after unconfigure!") ExpectNoException(vm.UnconfigureDisk, d2) ExpectCond(d2.unconfigured, "Disk became configured!") ExpectNoException(vm.RemoveDisk, d2) ExpectManagedObjectNotFound(vm.RemoveDisk, d2) Log("Removing disk 1") d1 = ExpectNoException(vm.GetDisk, disk1Spec.diskIdent.id) ExpectCond(not d1.unconfigured, "Disk is already unconfigured!") ExpectException(Hbr.Replica.Fault.HbrDiskNotUnconfigured, vm.RemoveDisk, d1) ExpectCond(not d1.unconfigured, "Disk became configured!") ExpectNoException(vm.UnconfigureDisk, d1) ExpectCond(d1.unconfigured, "Disk is not unconfigured after unconfigure!") ExpectNoException(vm.RemoveDisk, d1) Log("Adding all disks and removing") d1 = ExpectNoException(vm.AddDisk, disk1Spec) d2 = ExpectNoException(vm.AddDisk, disk2Spec) d3 = ExpectNoException(vm.AddDisk, disk3Spec) ExpectNoException(vm.UnconfigureDisk, d1) ExpectNoException(vm.UnconfigureDisk, d2) ExpectNoException(vm.RemoveDisk, d2) ExpectNoException(vm.UnconfigureDisk, d3) ExpectNoException(vm.RemoveDisk, d1) ExpectNoException(vm.RemoveDisk, d3) ExpectNoException(g.Remove)
def TestCreateGroup(count, repManager): """Test ReplicationManager CreateGroup method.""" # Create 'count' random (valid) groups. for x in xrange(0, count): gspec = CreateRandomizedGroupSpec() Log("Created spec: " + str(gspec)) g = ExpectNoException(repManager.CreateGroup, gspec) Log("Got group: " + str(g)) # check idempotency g2 = ExpectNoException(repManager.CreateGroup, gspec) if g._moId != g2._moId: raise TestFailedExc( "Group returned to duplicate CreateGroup call '" + str(g2) + "' is not equivalent to existing '" + str(g) + "'.") Log("Valid group create tests complete.") # Create a valid spec (with at least one valid disk). To use for # causing duplicate-disk errors. while True: # Retry until we get at least one valid disk validSpec = CreateRandomizedGroupSpec() if len(validSpec.vms[0].replicatedDisks) != 0: break validDiskID = validSpec.vms[0].replicatedDisks[0].diskIdent.id invalidGroups = ( (Hbr.Replica.Fault.InvalidGroupSpec, "groupid-same disk twice", (("gr1-disk1", "valid-disk-1"), ("gr1-disk1", "valid-disk-1"))), (Hbr.Replica.Fault.InvalidGroupSpec, "groupid-same diskid twice", (("gr2-disk1", "valid-disk-1"), ("gr2-disk1", "valid-disk-2"))), (Hbr.Replica.Fault.HbrDiskAlreadyExists, "groupid-reused diskid", (("gr3-disk1", "valid-disk-1"), (validDiskID, "valid-disk-2"))), (Hbr.Replica.Fault.InvalidGroupSpec, "group-with-a-too-long-invalid-group-id-that-should-not-be-accepted-ever-no-matter-what-no-matter-how-superb-hbrsrv-ever-becomes", (("gr4-disk-1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidHbrDiskSpec, "groupid-too long diskid", (("disk-with-a-too-long-invalid-disk-id-that-should-not-be-accepted-ever-no-matter-what-no-matter-how-superb-hbrsrv-ever-becomes", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-period.", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-bang!", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-hash#", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-quote'", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-dblquote\"", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-nl\n", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-tab\t", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "id-invalid-dollar$", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "$#!@_&$**()", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidGroupSpec, "", (("gr1-d1", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidHbrDiskSpec, "valid-gr1", (("gr1-d1-invalid-period.", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidHbrDiskSpec, "valid-gr1", (("", "valid-disk-1"), )), (Hbr.Replica.Fault.InvalidHbrDiskSpec, "valid-gr1", (("_@$#!$**&&", "valid-disk-1"), )), ) # Create the valid group for 'groupid-reused diskid' to conflict with g = ExpectNoException(repManager.CreateGroup, validSpec) Log("Got 'valid' group (for reused id test): " + str(g)) for fault, groupID, disks in invalidGroups: Log("Creating group with invalid ID(s): " + str(groupID)) invalGr = CreateInvalidGroupSpec(groupID, disks) ExpectException(fault, repManager.CreateGroup, invalGr) Log("Try with mismatch between 'group' and 'vm' ids:") invalGr = CreateInvalidGroupSpec("group-id-different-from-vm", (("gr1-d1", "valid-disk-1"), )) invalGr.id = "group-id-VERY-DIFFERENT-from-vm" ExpectException(Hbr.Replica.Fault.InvalidVirtualMachineSpec, repManager.CreateGroup, invalGr) Log("Try with invalid vm datastore configuration:") baseSpec = CreateRandomizedGroupSpec() for ds in ("invalid/datastore", "/", "/invalid", "invalid/"): Log("Setting datastore to: '%s'" % ds) baseSpec.vms[0].virtualMachineIdent.datastoreUUID = ds ExpectException(Hbr.Replica.Fault.InvalidVirtualMachineSpec, repManager.CreateGroup, baseSpec) # Test that we don't accidentally allow duplicate calls when the spec changes Log("Negative idempotency tests") gspec = CreateRandomizedGroupSpec() g = ExpectNoException(repManager.CreateGroup, gspec) g2 = ExpectNoException(repManager.CreateGroup, gspec) if g._moId != g2._moId: raise TestFailedExc("Group returned to duplicate CreateGroup call '" + str(g2) + "' is not equivalent to existing '" + str(g) + "'.") # Test with completely different spec gspec2 = CreateRandomizedGroupSpec() gspec2.id = gspec.id gspec2.vms[0].virtualMachineIdent.id = gspec.id ExpectException(Hbr.Replica.Fault.GroupAlreadyExists, repManager.CreateGroup, gspec2) # Test with slightly different specs gpsec2 = copy.deepcopy(gspec) gspec2.rpo = gspec2.rpo + 1 ExpectException(Hbr.Replica.Fault.GroupAlreadyExists, repManager.CreateGroup, gspec2) # Test with different VM spec gpsec2 = copy.deepcopy(gspec) gspec2.vms = [CreateRandomizedVMSpec(None, vmID=gspec.id)] ExpectException(Hbr.Replica.Fault.GroupAlreadyExists, repManager.CreateGroup, gspec2) # Test with different disks gpsec2 = copy.deepcopy(gspec) gspec2.vms[0].replicatedDisks.append(CreateRandomizedDiskSpec(None)) ExpectException(Hbr.Replica.Fault.GroupAlreadyExists, repManager.CreateGroup, gspec2) Log("All invalid group create tests complete.")