def acquire_rand_vessels(lockserver_handle, geniuser, vesselcount): """ <Purpose> Acquire 'rand' vessels for a geniuser. <Arguments> lockserver_handle The lockserver handle to be used for obtaining node locks. geniuser The GeniUser the vessels should be acquired for. vesselcount The number of vessels to acquire. <Exceptions> UnableToAcquireResourcesError If either the user does not not have enough vessel credits to acquire the number of vessels they requested or if there are not enough vessels available to fulfill the request. <Side Effects> The vessels are acquired for the user. The database has been updated to reflect the acquisition. <Returns> A list of the vessels that were acquired. """ # Get a randomized list of vessels where there are no guarantees about whether # the list includes wan vessels, lan vessels, vessels on the same subnet, etc. vessel_list = maindb.get_available_rand_vessels(geniuser, vesselcount) return _acquire_vessels_from_list(lockserver_handle, geniuser, vesselcount, vessel_list)
def test_get_available_rand_vessels(self): # Create a user who will be doing the acquiring. user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4") userport = user.usable_vessel_port # We choose the numbers of each type of node in a way that helps ensure # that we don't accidentally pass the test if something is going wrong. # We will get one vessel on each created node for each port in portlist # and there will be only that one port on the vessel. portlist = [userport - 1, userport, userport + 1] create_nodes_on_same_subnet(3, portlist) create_nodes_on_different_subnets(7, portlist) create_nat_nodes(13, portlist) # Request 0 vessels, make sure it raises a AssertionError. self.assertRaises(AssertionError, maindb.get_available_rand_vessels, user, 0) # Request a negative number of vessels, make sure it raises a AssertionError. self.assertRaises(AssertionError, maindb.get_available_rand_vessels, user, -1) # We expect there to be 23 available rand vessels (one vessel on each node # including the nat nodes). # Request 1 vessel, make sure we get back more than 1 potential vessels. vessel_list = maindb.get_available_rand_vessels(user, 1) self.assertTrue(len(vessel_list) > 1) # Request 5 vessels, make sure we get back more than 5 potential vessels. vessel_list = maindb.get_available_rand_vessels(user, 5) self.assertTrue(len(vessel_list) > 5) # Request 23 vessels, make sure we get back all 23 vessels we expect. vessel_list = maindb.get_available_rand_vessels(user, 23) self.assertEqual(23, len(vessel_list)) # Request 24 vessels, make sure we get an exception. self.assertRaises(UnableToAcquireResourcesError, maindb.get_available_rand_vessels, user, 24)