Esempio n. 1
0
def setup_test():
  """
  <Purpose>
    Prepare everything in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

  testlib.setup_test_db()

  # Create a user who has the donation key.
  maindb.create_user(mockutil.testusername, "password", "*****@*****.**", "affiliation", 
                    "10 11", "2 2 2", mockutil.donor_key_str)

  # Setup all the mock functions
  mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
  mockutil.mock_lockserver_calls()
  mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
  mockutil.mock_nodemanager_get_vesselresources()
  mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
  mockutil.mock_backend_set_vessel_owner_key()
  mockutil.mock_backend_split_vessel()
  mockutil.mock_backend_set_vessel_user_keylist([mockutil._mock_pubkey_to_string(
                                                node_transition_lib.transition_state_keys['canonical'])])
    def test_renew_vessels_dont_belong_to_user(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

        # Create a second user.
        user2 = maindb.create_user("user2", "password", "*****@*****.**",
                                   "affiliation", "1 2", "2 2 2", "3 4")

        vesselcount = 4

        # Have every vessel acquisition to the backend request succeed.
        calls_results = [True] * vesselcount
        mocklib.mock_backend_acquire_vessel(calls_results)

        testutil.create_nodes_on_different_subnets(vesselcount, [userport])

        # Acquire all of the vessels the user can acquire.
        vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')

        release_vessel = vessel_list[0]
        interface.release_vessels(user, [release_vessel])

        # Manually fiddle with one of the vessels to make it owned by user2.
        user2_vessel = vessel_list[1]
        user2_vessel.acquired_by_user = user2
        user2_vessel.save()

        # Try to renew all of the originally acquired vessels, including the ones
        # that were released. We expect these to just be ignored.
        interface.renew_vessels(user, vessel_list)

        # Get fresh vessel objects that reflect the renewal.
        remaining_vessels = interface.get_acquired_vessels(user)
        release_vessel = maindb.get_vessel(release_vessel.node.node_identifier,
                                           release_vessel.name)
        user2_vessel = maindb.get_vessel(user2_vessel.node.node_identifier,
                                         user2_vessel.name)

        now = datetime.datetime.now()
        timedelta_oneday = datetime.timedelta(days=1)

        # Ensure that the vessels the user still has were renewed but that the ones
        # the user released were ignored (not renewed).
        for vessel in remaining_vessels:
            self.assertTrue(vessel.date_expires - now > timedelta_oneday)

        self.assertTrue(user2_vessel.date_expires - now < timedelta_oneday)

        self.assertEqual(release_vessel.date_expires, None)
  def test_renew_vessels_dont_belong_to_user(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
    
    # Create a second user.
    user2 = maindb.create_user("user2", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    
    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])
    
    # Acquire all of the vessels the user can acquire.
    vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
    
    release_vessel = vessel_list[0]
    interface.release_vessels(user, [release_vessel])
    
    # Manually fiddle with one of the vessels to make it owned by user2.
    user2_vessel = vessel_list[1]
    user2_vessel.acquired_by_user = user2
    user2_vessel.save()
    
    # Try to renew all of the originally acquired vessels, including the ones
    # that were released. We expect these to just be ignored.
    interface.renew_vessels(user, vessel_list)
  
    # Get fresh vessel objects that reflect the renewal.
    remaining_vessels = interface.get_acquired_vessels(user)
    release_vessel = maindb.get_vessel(release_vessel.node.node_identifier, release_vessel.name)
    user2_vessel = maindb.get_vessel(user2_vessel.node.node_identifier, user2_vessel.name)
   
    now = datetime.datetime.now()
    timedelta_oneday = datetime.timedelta(days=1)
  
    # Ensure that the vessels the user still has were renewed but that the ones
    # the user released were ignored (not renewed).
    for vessel in remaining_vessels:
      self.assertTrue(vessel.date_expires - now > timedelta_oneday)

    self.assertTrue(user2_vessel.date_expires - now < timedelta_oneday)
    
    self.assertEqual(release_vessel.date_expires, None)
Esempio n. 4
0
  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))
Esempio n. 5
0
  def test_acquire_wan_vessels_some_vessels_fail(self):

    # Have every other vessel acquisition fail. We're going to acquire 50,
    # so we'll need 100 responses alternating between failure and success
    # (we're starting with failure, so 100, not 99).
    calls_results = [False, True] * 50
    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
    
    # We need to give the user some donations so they have enough credits.
    # We're assuming it's 10 credits per donation.
    self.assertEqual(10, maindb.VESSEL_CREDITS_FOR_DONATIONS_MULTIPLIER)
    
    # Also make sure the user started with 10 credits.
    self.assertEqual(10, maindb.get_user_free_vessel_credits(user))
    
    # We need 100 nodes the user can acquire vessels on as we're having half of
    # the node acquisitions fail.
    testutil.create_nodes_on_different_subnets(100, [userport])
    
    # Now credit the user for donations on 4 of these.
    for node in maindb.get_active_nodes()[:4]:
      maindb.create_donation(node, user, '')
    
    # Ok, the user now has 50 vessels the can acquire and there are 100 nodes
    # with vessels available for them. Let's try to acquire all 50 at once and
    # make sure this works even though we'll have to get through 100 requests
    # to the backend to make it happen.
    vessel_list = interface.acquire_vessels(user, 50, 'wan')
    
    self.assertEqual(50, len(vessel_list))
    def test_renew_some_of_users_vessel(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

        vesselcount = 4

        # Have every vessel acquisition to the backend request succeed.
        calls_results = [True] * vesselcount
        mocklib.mock_backend_acquire_vessel(calls_results)

        testutil.create_nodes_on_different_subnets(vesselcount, [userport])

        # Acquire all of the vessels the user can acquire.
        vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')

        renew_vessels_list = vessel_list[:2]
        not_renewed_vessels_list = vessel_list[2:]

        interface.renew_vessels(user, renew_vessels_list)

        now = datetime.datetime.now()
        timedelta_oneday = datetime.timedelta(days=1)

        for vessel in renew_vessels_list:
            self.assertTrue(vessel.date_expires - now > timedelta_oneday)

        for vessel in not_renewed_vessels_list:
            self.assertTrue(vessel.date_expires - now < timedelta_oneday)
    def test_renew_vessels_insufficient_vessel_credits(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

        vesselcount = 4

        # Have every vessel acquisition to the backend request succeed.
        calls_results = [True] * vesselcount
        mocklib.mock_backend_acquire_vessel(calls_results)

        testutil.create_nodes_on_different_subnets(vesselcount, [userport])

        # Acquire all of the vessels the user can acquire.
        vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')

        # Decrease the user's vessel credits to one less than the number of vessels
        # they have acquired.
        user.free_vessel_credits = 0
        user.save()

        func = interface.renew_vessels
        args = (user, vessel_list)
        self.assertRaises(InsufficientUserResourcesError, func, *args)

        func = interface.renew_all_vessels
        args = (user, )
        self.assertRaises(InsufficientUserResourcesError, func, *args)
  def test_renew_some_of_users_vessel(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
    
    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])
    
    # Acquire all of the vessels the user can acquire.
    vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
    
    renew_vessels_list = vessel_list[:2]
    not_renewed_vessels_list = vessel_list[2:]
    
    interface.renew_vessels(user, renew_vessels_list)
  
    now = datetime.datetime.now()
    timedelta_oneday = datetime.timedelta(days=1)
  
    for vessel in renew_vessels_list:
      self.assertTrue(vessel.date_expires - now > timedelta_oneday)

    for vessel in not_renewed_vessels_list:
      self.assertTrue(vessel.date_expires - now < timedelta_oneday)
Esempio n. 9
0
  def test_acquire_specific_vessels(self):
    # 8 vessels will ultimately be acquired.
    calls_results = [True] * 8
    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)
    
    # We use userport + 1 to make sure the user isn't being restricted to only
    # vessels that have their user port in their port list.
    testutil.create_nodes_on_different_subnets(vesselcount + 10, [userport + 1])
    
    vessels = list(maindb._get_queryset_of_all_available_vessels_for_a_port_include_nat_nodes(userport + 1))
    
    # Request the first 4 vessels in the list.
    first_vessel_list = interface.acquire_specific_vessels(user, vessels[:4])
    
    # Now request the first 6 vessels in the list. We should only get 2.
    second_vessel_list = interface.acquire_specific_vessels(user, vessels[:6])

    self.assertEqual(4, len(first_vessel_list))
    self.assertEqual(2, len(second_vessel_list))
    
    # Now ask for more vessels than the user has available, regardless of the
    # fact that some of the requested vessels aren't available.
    requestcount = vesselcount - 6 + 1
    
    func = interface.acquire_specific_vessels
    args = (user, vessels[:requestcount])
    self.assertRaises(InsufficientUserResourcesError, func, *args)
Esempio n. 10
0
 def test_renew_vessels_insufficient_vessel_credits(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
   
   vesselcount = 4
   
   # Have every vessel acquisition to the backend request succeed.
   calls_results = [True] * vesselcount
   mocklib.mock_backend_acquire_vessel(calls_results)
   
   testutil.create_nodes_on_different_subnets(vesselcount, [userport])
   
   # Acquire all of the vessels the user can acquire.
   vessel_list = interface.acquire_vessels(user, vesselcount, 'rand')
   
   # Decrease the user's vessel credits to one less than the number of vessels
   # they have acquired.
   user.free_vessel_credits = 0
   user.save()
   
   func = interface.renew_vessels
   args = (user, vessel_list)
   self.assertRaises(InsufficientUserResourcesError, func, *args)      
 
   func = interface.renew_all_vessels
   args = (user,)
   self.assertRaises(InsufficientUserResourcesError, func, *args)   
def setup_test():
    """
  <Purpose>
    Prepare everything in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

    testlib.setup_test_db()

    # Make sure all the variables are its default values
    mockutil.mockutil_cleanup_variables()

    # Create a user who has the donation key.
    user_object = maindb.create_user(
        mockutil.testusername,
        "password",
        "*****@*****.**",
        "affiliation",
        "10 11",
        "2 2 2",
        mockutil.donor_key_str,
    )

    # Create a database entry for the node
    node_object = maindb.create_node(
        mockutil.nodeid_key_str,
        mockutil.node_ip,
        mockutil.node_port,
        "10.0test",
        False,
        mockutil.per_node_key_str,
        mockutil.extra_vessel_name,
    )

    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")

    # Setup all the mock functions
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
    mockutil.mock_lockserver_calls()
    mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
    mockutil.mock_nodemanager_get_vesselresources()
    mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
    mockutil.mock_backend_set_vessel_owner_key()
    mockutil.mock_backend_split_vessel()
    mockutil.mock_backend_set_vessel_user_keylist(
        [mockutil._mock_pubkey_to_string(node_transition_lib.movingtoonepercentmanyeventspublickey)]
    )
  def test_regenerate_user_key(self):
    
    pubkey = "1 2"
    privkey = "3 4 5"
    donor_key = "6 7"
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", 
                              pubkey, privkey, donor_key)
    userport = user.usable_vessel_port

    vesselcount = 4
    
    # Have every vessel acquisition to the backend request succeed.
    calls_results = [True] * vesselcount
    mocklib.mock_backend_acquire_vessel(calls_results)
    
    testutil.create_nodes_on_different_subnets(vesselcount, [userport])

    # Acquire all vessels on behalf of this user.
    all_vessels_list = interface.acquire_vessels(user, vesselcount, 'rand')

    # Release 2 vessels.
    released_vessels_list = all_vessels_list[:2]
    kept_vessels_list = all_vessels_list[2:]
    interface.release_vessels(user, released_vessels_list)
    
    # Ensure all of the vessels are marked as having user keys in sync.
    for vessel in all_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertTrue(vessel.user_keys_in_sync)

    # We expect a single key to be generated through the keygen api (the new
    # user public key).
    mocklib.mock_keygen_generate_keypair([("55 66", "77 88 99")])
    
    interface.change_user_keys(user, pubkey=None)
    
    # Get a new user object from the database.
    user = maindb.get_user(user.username)
    
    # Make sure the user's key changed.
    self.assertEqual(user.user_pubkey, "55 66")
    self.assertEqual(user.user_privkey, "77 88 99")
    
    # Make sure that all of the vessels the user has access to (and no other
    # vessels) are marked as needing user keys to be sync'd.
    # Ensure all of the vessels are marked as having user keys in sync.
    for vessel in kept_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertFalse(vessel.user_keys_in_sync)

    for vessel in released_vessels_list:
      # Get a fresh vessel from the db.
      vessel = maindb.get_vessel(vessel.node.node_identifier, vessel.name)
      self.assertTrue(vessel.user_keys_in_sync)
 def test_get_available_lan_vessels_by_subnet(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(29, 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_lan_vessels_by_subnet, user, 0)
   
   # Request a negative number of vessels, make sure it raises a AssertionError.
   self.assertRaises(AssertionError, maindb.get_available_lan_vessels_by_subnet, user, -1)
   
   # We expect there to be 7 subnets with a single available vessel for the
   # user (the 7 on differnt subnets created above) and 1 subnet with 29
   # available vessels for the user (the 29 nodes on the same subnet created
   # above).
   
   # Request 1 vessel, make sure we get back a list of 8 subnets where one
   # subnet has more than one available vessel and the other 7 have only
   # one available vessel.
   subnet_vessel_list = maindb.get_available_lan_vessels_by_subnet(user, 1)
   self.assertEqual(8, len(subnet_vessel_list))
   
   vessel_list_sizes = []
   for vessel_list in subnet_vessel_list:
     vessel_list_sizes.append(len(vessel_list))
   vessel_list_sizes.sort()
   
   self.assertEqual([1, 1, 1, 1, 1, 1, 1], vessel_list_sizes[:7])
   self.assertTrue(vessel_list_sizes[7] > 1)
     
   # Request 5 vessels, make sure we get back a list that has one subnet and
   # in that subnet is a list of more than 5 potential vessels.
   subnet_vessel_list = maindb.get_available_lan_vessels_by_subnet(user, 5)
   self.assertEqual(1, len(subnet_vessel_list))
   self.assertTrue(len(subnet_vessel_list[0]) > 5)
     
   # Request 29 vessels, make sure we get back a list that has one subnet and
   # in that subnet is a list of 29 potential vessels.
   subnet_vessel_list = maindb.get_available_lan_vessels_by_subnet(user, 29)
   self.assertEqual(1, len(subnet_vessel_list))
   self.assertEqual(29, len(subnet_vessel_list[0]))
   
   # Request 30 vessels, make sure we get an exception.
   self.assertRaises(UnableToAcquireResourcesError, maindb.get_available_lan_vessels_by_subnet, user, 30)
def setup_test():
    """
  <Purpose>
    Prepare everything in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

    testlib.setup_test_db()

    # Create a user who has the donation key.
    user_object = maindb.create_user(
        mockutil.testusername,
        "password",
        "*****@*****.**",
        "affiliation",
        "10 11",
        "2 2 2",
        mockutil.donor_key_str,
    )

    # Create a database entry for the node
    node_object = maindb.create_node(
        mockutil.nodeid_key_str,
        mockutil.node_ip,
        mockutil.node_port,
        "10.0test",
        True,
        mockutil.per_node_key_str,
        mockutil.extra_vessel_name,
    )

    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")
    # Setup all the mock functions
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
    mockutil.mock_lockserver_calls()
    mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
    mockutil.mock_nodemanager_get_vesselresources()
    mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
    mockutil.mock_backend_set_vessel_owner_key()
    mockutil.mock_backend_split_vessel()
    # set_vessel_user_keylist_call_count won't be called because the
    # node_transition_lib will see that it's a state change to the same state.
    mockutil.mock_backend_set_vessel_user_keylist(None)
 def test_set_invalid_user_key(self):
   
   pubkey = "1 2"
   privkey = "3 4 5"
   donor_key = "6 7"
   
   # Create a user who will be doing the acquiring.
   user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", 
                             pubkey, privkey, donor_key)
   
   func = interface.change_user_keys
   args = (user, "abc")
   self.assertRaises(ValidationError, func, *args)
Esempio n. 16
0
def setup_test():
    """
  <Purpose>
    Prepare everything in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

    testlib.setup_test_db()

    # Make sure all the variables are its default values
    mockutil.mockutil_cleanup_variables()

    # Create a user who has the donation key.
    user_object = maindb.create_user(mockutil.testusername, "password",
                                     "*****@*****.**", "affiliation",
                                     "10 11", "2 2 2", mockutil.donor_key_str)

    # Create a database entry for the node
    node_object = maindb.create_node(mockutil.nodeid_key_str, mockutil.node_ip,
                                     mockutil.node_port, "10.0test", False,
                                     mockutil.per_node_key_str,
                                     mockutil.extra_vessel_name)

    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")

    # Setup all the mock functions
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test",
                                            vessels_dict)
    mockutil.mock_lockserver_calls()
    mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
    mockutil.mock_nodemanager_get_vesselresources()
    mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
    mockutil.mock_backend_set_vessel_owner_key()
    mockutil.mock_backend_split_vessel()
    mockutil.mock_backend_set_vessel_user_keylist([
        mockutil._mock_pubkey_to_string(
            node_transition_lib.
            transition_state_keys['movingto_onepercentmanyevents'])
    ])
    def test_get_queryset_vessel_acquired_by_user_changes(self):
        """
    This is ultimately testing
    maindb._get_queryset_of_all_available_vessels_for_a_port_include_nat_nodes()
    """

        # 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

        # Create two nodes that have three vessels but only one vessel on the user's port.
        portlist = [userport - 1, userport, userport + 1]
        ip = "127.0.0.1"
        create_node_and_vessels_with_one_port_each(ip, portlist)
        ip = "127.0.0.2"
        create_node_and_vessels_with_one_port_each(ip, portlist)

        # We expect two available vessels.
        queryset = _get_queryset_include_nat(userport)
        self.assertEqual(2, queryset.count())

        # Mark one of the vessels as acquired.
        vessel = queryset[0]
        maindb.record_acquired_vessel(user, vessel)

        # We expect one available vessel, and it shouldn't be the acquired one.
        queryset = _get_queryset_include_nat(userport)
        self.assertEqual(1, queryset.count())
        self.assertNotEqual(vessel, queryset[0])

        # Release the vessel. It should still be dirty.
        maindb.record_released_vessel(vessel)

        # We expect one available vessel, and it shouldn't be the acquired one.
        queryset = _get_queryset_include_nat(userport)
        self.assertEqual(1, queryset.count())
        self.assertNotEqual(vessel, queryset[0])

        # Mark the vessel as clean (as if the backend cleaned it up).
        maindb.mark_vessel_as_clean(vessel)

        # We expect two available vessels.
        queryset = _get_queryset_include_nat(userport)
        self.assertEqual(2, queryset.count())
    def test_get_available_nat_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_nat_vessels,
                          user, 0)

        # Request a negative number of vessels, make sure it raises a AssertionError.
        self.assertRaises(AssertionError, maindb.get_available_nat_vessels,
                          user, -1)

        # We expect there to be 13 available nat vessels (one vessel on each node
        # nat node).

        # Request 1 vessel, make sure we get back more than 1 potential vessels.
        vessel_list = maindb.get_available_nat_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_nat_vessels(user, 5)
        self.assertTrue(len(vessel_list) > 5)

        # Request 13 vessels, make sure we get back all 13 vessels we expect.
        vessel_list = maindb.get_available_nat_vessels(user, 13)
        self.assertEqual(13, len(vessel_list))

        # Request 14 vessels, make sure we get an exception.
        self.assertRaises(UnableToAcquireResourcesError,
                          maindb.get_available_nat_vessels, user, 14)
Esempio n. 19
0
  def test_acquire_vessels_insufficient_vessel_credits(self):
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    
    # TODO: need to test maindb.require_user_can_acquire_resources separately
    
    # The user doesn't have any donations, they shouldn't be able to acquire
    # more vessels than their free credits.
    credit_limit = maindb.get_user_free_vessel_credits(user)
    
    vesseltypelist = ['wan', 'lan', 'nat', 'rand']

    for vesseltype in vesseltypelist:
      func = interface.acquire_vessels
      args = (user, credit_limit + 1, vesseltype)
      self.assertRaises(InsufficientUserResourcesError, func, *args)      
Esempio n. 20
0
def setup_test():
    """
  <Purpose>
    Prepare everything in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

    testlib.setup_test_db()

    # Create a user who has the donation key.
    user_object = maindb.create_user(mockutil.testusername, "password",
                                     "*****@*****.**", "affiliation",
                                     "10 11", "2 2 2", mockutil.donor_key_str)

    # Create a database entry for the node
    node_object = maindb.create_node(mockutil.nodeid_key_str, mockutil.node_ip,
                                     mockutil.node_port, "10.0test", True,
                                     mockutil.per_node_key_str,
                                     mockutil.extra_vessel_name)

    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")
    # Setup all the mock functions
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test",
                                            vessels_dict)
    mockutil.mock_lockserver_calls()
    mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
    mockutil.mock_nodemanager_get_vesselresources()
    mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
    mockutil.mock_backend_set_vessel_owner_key()
    mockutil.mock_backend_split_vessel()
    # set_vessel_user_keylist_call_count won't be called because the
    # node_transition_lib will see that it's a state change to the same state.
    mockutil.mock_backend_set_vessel_user_keylist(None)
  def test_get_queryset_vessel_acquired_by_user_changes(self):
    """
    This is ultimately testing
    maindb._get_queryset_of_all_available_vessels_for_a_port_include_nat_nodes()
    """
    
    # 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
    
    # Create two nodes that have three vessels but only one vessel on the user's port.
    portlist = [userport - 1, userport, userport + 1]
    ip = "127.0.0.1"
    create_node_and_vessels_with_one_port_each(ip, portlist)
    ip = "127.0.0.2"
    create_node_and_vessels_with_one_port_each(ip, portlist)

    # We expect two available vessels.
    queryset = _get_queryset_include_nat(userport)
    self.assertEqual(2, queryset.count())
    
    # Mark one of the vessels as acquired.
    vessel = queryset[0]
    maindb.record_acquired_vessel(user, vessel)
    
    # We expect one available vessel, and it shouldn't be the acquired one.
    queryset = _get_queryset_include_nat(userport)
    self.assertEqual(1, queryset.count())
    self.assertNotEqual(vessel, queryset[0])
    
    # Release the vessel. It should still be dirty.
    maindb.record_released_vessel(vessel)
    
    # We expect one available vessel, and it shouldn't be the acquired one.
    queryset = _get_queryset_include_nat(userport)
    self.assertEqual(1, queryset.count())
    self.assertNotEqual(vessel, queryset[0])
    
    # Mark the vessel as clean (as if the backend cleaned it up).
    maindb.mark_vessel_as_clean(vessel)
    
    # We expect two available vessels.
    queryset = _get_queryset_include_nat(userport)
    self.assertEqual(2, queryset.count())
Esempio n. 22
0
 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))
Esempio n. 23
0
  def test_acquire_vessels_invalid_request(self):
    
    # Create a user who will be doing the acquiring.
    user = maindb.create_user("testuser", "password", "*****@*****.**", "affiliation", "1 2", "2 2 2", "3 4")
    
    func = interface.acquire_vessels
    
    # Negative vesselcount.
    args = (user, -1, 'wan')
    self.assertRaises(AssertionError, func, *args)   

    # Zero vesselcount.
    args = (user, 0, 'wan')
    self.assertRaises(AssertionError, func, *args)   

    # Unrecognized vessel type.
    args = (user, 1, 'x')
    self.assertRaises(ProgrammerError, func, *args)   
 def test_get_available_nat_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_nat_vessels, user, 0)
   
   # Request a negative number of vessels, make sure it raises a AssertionError.
   self.assertRaises(AssertionError, maindb.get_available_nat_vessels, user, -1)
   
   # We expect there to be 13 available nat vessels (one vessel on each node
   # nat node).
   
   # Request 1 vessel, make sure we get back more than 1 potential vessels.
   vessel_list = maindb.get_available_nat_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_nat_vessels(user, 5)
   self.assertTrue(len(vessel_list) > 5)
     
   # Request 13 vessels, make sure we get back all 13 vessels we expect.
   vessel_list = maindb.get_available_nat_vessels(user, 13)
   self.assertEqual(13, len(vessel_list))
   
   # Request 14 vessels, make sure we get an exception.
   self.assertRaises(UnableToAcquireResourcesError, maindb.get_available_nat_vessels, user, 14)
Esempio n. 25
0
def register_user(username, password, email, affiliation, pubkey=None):
  """
  <Purpose>
    Creates a user record with the specified information and sets any additional
    information necessary for the user record to be complete.
  <Arguments>
    username
    password
    email
    affiliation
    pubkey
      Optional. A string. If not provided, a key pair will be generated for this user.
  <Exceptions>
    UsernameAlreadyExistsError
      If there is already a user with the specified username.
    ValidationError
      If any of the arguments contains invalid values or if the username is the
      same as the password.
  <Side Effects>
    The user record in the django db is created as well as a user record in the
    corresponding user profile table that stores our custom information. A port
    will be assigned to the user and the user's donation keys will be set.
  <Returns>
    GeniUser instance (our GeniUser model, not the django User) corresponding to the
    newly registered user.
  """
  # If the frontend code that called this function wants to know which field
  # is invalid, it must call the validation functions itself before making the
  # call to register_user().
  # These will raise a ValidationError if any of the fields are invalid.
  # These ensure that the data is of the correct type (e.g. a string) as well as
  # that we like the content of the variable.
  validations.validate_username(username)
  validations.validate_password(password)
  validations.validate_username_and_password_different(username, password)
  validations.validate_email(email)
  validations.validate_affiliation(affiliation)
  if pubkey is not None:
    validations.validate_pubkey_string(pubkey)
  
  # Lock the user.
  lockserver_handle = lockserver.create_lockserver_handle()
  lockserver.lock_user(lockserver_handle, username)
  try:
    # Ensure there is not already a user with this username.
    try:
      # Raises a DoesNotExistError if the user doesn't exist.
      maindb.get_user(username)
      raise UsernameAlreadyExistsError
    except DoesNotExistError:
      # This is what we wanted: the username isn't already taken.
      pass
    
    # Get a key pair from the keygen api if the user didn't supply their own pubkey.
    if pubkey is None:
      (pubkey, privkey) = keygen.generate_keypair()
    else:
      privkey = None
    
    # Generate a donor key for this user. This is done through the backend
    # as the private key must be stored in the keydb, which the website cannot
    # directly access.
    keydescription = "donor:" + username
    donor_pubkey = backend.generate_key(keydescription)
    
    # Create the user record.
    geniuser = maindb.create_user(username, password, email, affiliation, pubkey, privkey, donor_pubkey)
    
  finally:
    # Unlock the user.
    lockserver.unlock_user(lockserver_handle, username)
    lockserver.destroy_lockserver_handle(lockserver_handle)
    
  return geniuser
Esempio n. 26
0
log.loglevel = log.LOG_LEVEL_ERROR

# Delete everything from the database. To be safe, this isn't enabled by
# default. You should probably use ./managepy --flush, instead.
#Donation.objects.all().delete()
#GeniUser.objects.all().delete()
#Node.objects.all().delete()
#Vessel.objects.all().delete()
#VesselPort.objects.all().delete()
#VesselUserAccessMap.objects.all().delete()

# Create GeniUser records
user_list = []
for i in range(10):
    username = '******' + str(i)
    user = maindb.create_user(username, 'mypassword', 'myemail', 'myaff',
                              'mypubkey', 'myprivkey', 'mydonorpubkey')
    user.save()
    user_list.append(user)

# Create Node records
node_list = []
for i in range(100):
    node_identifier = 'node' + str(i)
    extra_vessel_name = 'v2'
    ip = '127.0.' + str(i) + '.0'
    node = maindb.create_node(node_identifier, ip, 1234, '0.1a', True,
                              'the owner pubkey', extra_vessel_name)
    node_list.append(node)

# Create Donation records
donation_list = []
# Delete everything from the database. To be safe, this isn't enabled by
# default. You should probably use ./managepy --flush, instead.
#Donation.objects.all().delete()
#GeniUser.objects.all().delete()
#Node.objects.all().delete()
#Vessel.objects.all().delete()
#VesselPort.objects.all().delete()
#VesselUserAccessMap.objects.all().delete()


# Create GeniUser records
user_list = []
for i in range(10):
  username = '******' + str(i)
  user = maindb.create_user(username, 'mypassword', 'myemail', 'myaff', 'mypubkey', 'myprivkey', 'mydonorpubkey')
  user.save()
  user_list.append(user)

# Create Node records
node_list = []
for i in range(100):
  node_identifier = 'node' + str(i)
  extra_vessel_name = 'v2'
  ip = '127.0.' + str(i) + '.0'
  node = maindb.create_node(node_identifier, ip, 1234, '0.1a', True, 'the owner pubkey', extra_vessel_name)
  node_list.append(node)


# Create Donation records
donation_list = []
def setup_general():
  """
  <Purpose>
    Prepare all the general stuff in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

  testlib.setup_test_db()

  # Make sure all the variables are its default values
  mockutil.mockutil_cleanup_variables()

  # Create a user who has the donation key.
  user_object = maindb.create_user(mockutil.testusername, "password", "*****@*****.**", "affiliation",
                    "10 11", "2 2 2", mockutil.donor_key_str)

  # Create a database entry for the node
  node_object = maindb.create_node(mockutil.nodeid_key_str, mockutil.node_ip, mockutil.node_port, "10.0test",
                                  True, mockutil.per_node_key_str, mockutil.extra_vessel_name)


  # Create a donation for user
  maindb.create_donation(node_object, user_object, "Making a donation")

  testuser = maindb.get_user(mockutil.testusername)

  # Retrieve the info about the donation we just made.
  all_donations = maindb.get_donations_by_user(testuser, include_inactive_and_broken=True)
  node = all_donations[0].node

  global vessels_dict
  vessels_dict = vessels_dict_basic.copy()
  # Create 9 different vessels, assuming that the original vessel got split into 9.
  for i in range(9):
    vessels_dict["vessel"+str(i)]={}
    vessels_dict["vessel"+str(i)]["userkeys"] = []
    vessels_dict["vessel"+str(i)]["ownerkey"] = rsa_string_to_publickey(node.owner_pubkey)
    vessels_dict["vessel"+str(i)]["ownerinfo"] = ""
    vessels_dict["vessel"+str(i)]["status"] = ""
    vessels_dict["vessel"+str(i)]["advertise"] = True

    maindb.create_vessel(node, "vessel"+str(i))


  # Setup all the mock functions
  mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
  mockutil.mock_lockserver_calls()
  mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
  mockutil.mock_nodemanager_get_vesselresources()
  mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
  mockutil.mock_backend_set_vessel_owner_key()
def setup_general():
    """
  <Purpose>
    Prepare all the general stuff in order to run the tests.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Efects>
    None

  <Return>
    None
  """

    testlib.setup_test_db()

    # Make sure all the variables are its default values
    mockutil.mockutil_cleanup_variables()

    # Create a user who has the donation key.
    user_object = maindb.create_user(mockutil.testusername, "password",
                                     "*****@*****.**", "affiliation",
                                     "10 11", "2 2 2", mockutil.donor_key_str)

    # Create a database entry for the node
    node_object = maindb.create_node(mockutil.nodeid_key_str, mockutil.node_ip,
                                     mockutil.node_port, "10.0test", True,
                                     mockutil.per_node_key_str,
                                     mockutil.extra_vessel_name)

    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")

    testuser = maindb.get_user(mockutil.testusername)

    # Retrieve the info about the donation we just made.
    all_donations = maindb.get_donations_by_user(
        testuser, include_inactive_and_broken=True)
    node = all_donations[0].node

    global vessels_dict
    vessels_dict = vessels_dict_basic.copy()
    # Create 9 different vessels, assuming that the original vessel got split into 9.
    for i in range(9):
        vessels_dict["vessel" + str(i)] = {}
        vessels_dict["vessel" + str(i)]["userkeys"] = []
        vessels_dict["vessel" + str(i)]["ownerkey"] = rsa_string_to_publickey(
            node.owner_pubkey)
        vessels_dict["vessel" + str(i)]["ownerinfo"] = ""
        vessels_dict["vessel" + str(i)]["status"] = ""
        vessels_dict["vessel" + str(i)]["advertise"] = True

        maindb.create_vessel(node, "vessel" + str(i))

    # Setup all the mock functions
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test",
                                            vessels_dict)
    mockutil.mock_lockserver_calls()
    mockutil.mock_backend_generate_key([mockutil.per_node_key_str])
    mockutil.mock_nodemanager_get_vesselresources()
    mockutil.mock_transitionlib_do_advertise_lookup([mockutil.node_address])
    mockutil.mock_backend_set_vessel_owner_key()
Esempio n. 30
0
def register_user(username, password, email, affiliation, pubkey=None):
  """
  <Purpose>
    Creates a user record with the specified information and sets any additional
    information necessary for the user record to be complete.
  <Arguments>
    username
    password
    email
    affiliation
    pubkey
      Optional. A string. If not provided, a key pair will be generated for this user.
  <Exceptions>
    UsernameAlreadyExistsError
      If there is already a user with the specified username.
    ValidationError
      If any of the arguments contains invalid values or if the username is the
      same as the password.
  <Side Effects>
    The user record in the django db is created as well as a user record in the
    corresponding user profile table that stores our custom information. A port
    will be assigned to the user and the user's donation keys will be set.
  <Returns>
    GeniUser instance (our GeniUser model, not the django User) corresponding to the
    newly registered user.
  """
  # If the frontend code that called this function wants to know which field
  # is invalid, it must call the validation functions itself before making the
  # call to register_user().
  # These will raise a ValidationError if any of the fields are invalid.
  # These ensure that the data is of the correct type (e.g. a string) as well as
  # that we like the content of the variable.
  validations.validate_username(username)
  validations.validate_password(password)
  validations.validate_username_and_password_different(username, password)
  validations.validate_email(email)
  validations.validate_affiliation(affiliation)
  if pubkey is not None:
    validations.validate_pubkey_string(pubkey)
  
  # Lock the user.
  lockserver_handle = lockserver.create_lockserver_handle()
  lockserver.lock_user(lockserver_handle, username)
  try:
    # Ensure there is not already a user with this username.
    try:
      # Raises a DoesNotExistError if the user doesn't exist.
      maindb.get_user(username)
      raise UsernameAlreadyExistsError
    except DoesNotExistError:
      # This is what we wanted: the username isn't already taken.
      pass
    
    # Get a key pair from the keygen api if the user didn't supply their own pubkey.
    if pubkey is None:
      (pubkey, privkey) = keygen.generate_keypair()
    else:
      privkey = None
    
    # Generate a donor key for this user. This is done through the backend
    # as the private key must be stored in the keydb, which the website cannot
    # directly access.
    keydescription = "donor:" + username
    donor_pubkey = backend.generate_key(keydescription)
    
    # Create the user record.
    geniuser = maindb.create_user(username, password, email, affiliation, pubkey, privkey, donor_pubkey)
    
  finally:
    # Unlock the user.
    lockserver.unlock_user(lockserver_handle, username)
    lockserver.destroy_lockserver_handle(lockserver_handle)
    
  return geniuser