def test_acquire_wan_vessels_multiple_calls_rand(self):

    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * 10
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # 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
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # Create vesselcount nodes split between the different types.
    testutil.create_nodes_on_different_subnets(4, [userport])
    testutil.create_nodes_on_same_subnet(4, [userport])
    testutil.create_nat_nodes(vesselcount - 8, [userport])
    
    # First request a single vessel.
    first_vessel_list = interface.acquire_vessels(user, 1, 'rand')
    
    # Now acquire all of the rest that the user can acquire.
    second_vessel_list = interface.acquire_vessels(user, vesselcount - 1, 'rand')

    self.assertEqual(1, len(first_vessel_list))
    self.assertEqual(vesselcount - 1, len(second_vessel_list))
  def test_acquire_wan_vessels_multiple_calls_rand(self):

    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * 10
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    # 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
    
    vesselcount = maindb.get_user_free_vessel_credits(user)
    
    # Create vesselcount nodes split between the different types.
    testutil.create_nodes_on_different_subnets(4, [userport])
    testutil.create_nodes_on_same_subnet(4, [userport])
    testutil.create_nat_nodes(vesselcount - 8, [userport])
    
    # First request a single vessel.
    first_vessel_list = interface.acquire_vessels(user, 1, 'rand')
    
    # Now acquire all of the rest that the user can acquire.
    second_vessel_list = interface.acquire_vessels(user, vesselcount - 1, 'rand')

    self.assertEqual(1, len(first_vessel_list))
    self.assertEqual(vesselcount - 1, len(second_vessel_list))
    def test_acquire_lan_vessels_some_subnets_have_too_many_vessels_fail(self):
        """
    We're going to be trying to acquire 4 vessels in a single subnet. We'll
    make it so that there are 3 subnets to choose from. The first two will
    potentially have enough vessels for the user to satisfy their acquisition
    request, but too many vessels in each will fail. The third subnet tried
    will have enough succeed.
    """

        # We're going to have three subnets with 5 potential vessels each. We
        # want the first two subnets to fail. Choosing the values creatively
        # here is helped by knowing the logic of the function
        # vessels._acquire_vessels_from_list(). Basically, it won't try to
        # acquire more than the minumum needed at a time, so the first time
        # it loops it will try to acquire all 4, then the next time it loops
        # it will try to acquire however many of the 4 failed the first time.
        # It won't bother trying again if there are too few left in the list.
        results_1 = [False, True, True, True, False]
        results_2 = [False, False, True, True]
        results_3 = [False, True, True, True, True]
        calls_results = results_1 + results_2 + results_3
        mocklib.mock_backend_acquire_vessel(calls_results)

        # 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

        # Make sure the user starts with 10 credits.
        self.assertEqual(10, maindb.get_user_free_vessel_credits(user))

        # Create three subnets with 5 nodes each. We need to keep them all the same
        # size, otherwise we need to change the test to patch maindb so that the
        # subnets will be tried in the order we expect.
        testutil.create_nodes_on_same_subnet(5, [userport],
                                             ip_prefix="127.0.0.")
        testutil.create_nodes_on_same_subnet(5, [userport],
                                             ip_prefix="127.0.1.")
        testutil.create_nodes_on_same_subnet(5, [userport],
                                             ip_prefix="127.0.2.")

        # Now try to acquire 8 lan nodes on a single subnet.
        vessel_list = interface.acquire_vessels(user, 4, 'lan')

        self.assertEqual(4, len(vessel_list))

        # Make sure backend.acquire_vessel() got called the correct number of
        # times (that is, the call_results list got pop(0)'d enough times).
        self.assertEqual(0, len(calls_results))
 def test_acquire_lan_vessels_some_subnets_have_too_many_vessels_fail(self):
   """
   We're going to be trying to acquire 4 vessels in a single subnet. We'll
   make it so that there are 3 subnets to choose from. The first two will
   potentially have enough vessels for the user to satisfy their acquisition
   request, but too many vessels in each will fail. The third subnet tried
   will have enough succeed.
   """
   
   # We're going to have three subnets with 5 potential vessels each. We
   # want the first two subnets to fail. Choosing the values creatively
   # here is helped by knowing the logic of the function
   # vessels._acquire_vessels_from_list(). Basically, it won't try to
   # acquire more than the minumum needed at a time, so the first time
   # it loops it will try to acquire all 4, then the next time it loops
   # it will try to acquire however many of the 4 failed the first time.
   # It won't bother trying again if there are too few left in the list.
   results_1 = [False, True, True, True, False]
   results_2 = [False, False, True, True]
   results_3 = [False, True, True, True, True]
   calls_results = results_1 + results_2 + results_3
   mocklib.mock_backend_acquire_vessel(calls_results)
   
   # 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
   
   # Make sure the user starts with 10 credits.
   self.assertEqual(10, maindb.get_user_free_vessel_credits(user))
   
   # Create three subnets with 5 nodes each. We need to keep them all the same
   # size, otherwise we need to change the test to patch maindb so that the
   # subnets will be tried in the order we expect.
   testutil.create_nodes_on_same_subnet(5, [userport], ip_prefix="127.0.0.")
   testutil.create_nodes_on_same_subnet(5, [userport], ip_prefix="127.0.1.")
   testutil.create_nodes_on_same_subnet(5, [userport], ip_prefix="127.0.2.")
   
   # Now try to acquire 8 lan nodes on a single subnet.
   vessel_list = interface.acquire_vessels(user, 4, 'lan')
   
   self.assertEqual(4, len(vessel_list))
   
   # Make sure backend.acquire_vessel() got called the correct number of
   # times (that is, the call_results list got pop(0)'d enough times).
   self.assertEqual(0, len(calls_results))