Пример #1
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 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)]
    )
Пример #3
0
def run_database_stituation_three_test():
    """
  <Purpose>
    The purpose of this test is to test the case where a record
    for the node has been registered in the database and a 
    record for the donation has been registered, but the
    owner key on the node has not been changed.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Effects>
    None

  <Return>
    None
  """

    # 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,
    )

    user_object = maindb.get_user(mockutil.testusername)

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

    transitionlist = []

    transitionlist.append(
        (
            ("donation_state", node_transition_lib.acceptdonationpublickey),
            ("canonical_state", node_transition_lib.canonicalpublickey),
            node_transition_lib.noop,
            node_transition_lib.noop,
        )
    )

    print "Running test where database has record of node, has record of donation and node has donor key"

    (success_count, failure_count) = node_transition_lib.do_one_processnode_run(transitionlist, "startstatename", 1)[0]

    assert success_count == 1
    assert failure_count == 0

    assert_database_info()

    assert mockutil.set_vessel_owner_key_call_count == 1
    assert mockutil.set_vessel_user_keylist_call_count == 1
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 run_database_stituation_three_test():
    """
  <Purpose>
    The purpose of this test is to test the case where a record
    for the node has been registered in the database and a 
    record for the donation has been registered, but the
    owner key on the node has not been changed.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Effects>
    None

  <Return>
    None
  """

    # 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)

    user_object = maindb.get_user(mockutil.testusername)

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

    transitionlist = []

    transitionlist.append(
        (("donation_state", node_transition_lib.acceptdonationpublickey),
         ("canonical_state", node_transition_lib.canonicalpublickey),
         node_transition_lib.noop, node_transition_lib.noop))

    print "Running test where database has record of node, has record of donation and node has donor key"

    (success_count,
     failure_count) = node_transition_lib.do_one_processnode_run(
         transitionlist, "startstatename", 1)[0]

    assert (success_count == 1)
    assert (failure_count == 0)

    assert_database_info()

    assert (mockutil.set_vessel_owner_key_call_count == 1)
    assert (mockutil.set_vessel_user_keylist_call_count == 1)
Пример #6
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'])
    ])
Пример #7
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)
Пример #8
0
def create_donation_record(database_nodeobject, donor_key):
  """
  <Purpose>
    To create a donation record for a user in the database

  <Arguments>
    database_nodeobject - a nodeobject that was retrieved from
      the database

  <Exceptions>
    DatabaseError - raised if problem accessing the database

  <Side Effects>
    Database will get modified

  <Return>
    None
  """

  # Retrieve the user object in order to give them credit
  # for their donation using the donor key
  try:
    database_userobject = maindb.get_donor(rsa_publickey_to_string(donor_key))
    log("Retrieved the userobject of the donor from database: " +
        str(database_userobject))
  except:
    raise DatabaseError("Failed to retrieve the userobject from the database. " + 
                        traceback.format_exc())

  donation_description = "Crediting user " + str(database_userobject)+ " for donation of node " + str(database_nodeobject)
      
  # Attempt to give the user credit for their donation
  try:
    log("Attempting to credit donation for the node " + database_nodeobject.node_identifier)
    maindb.create_donation(database_nodeobject, database_userobject, 
                           donation_description)
  except:
    raise DatabaseError("Failed to credit user for donation for user: "******"Registered the donation for the user with the donor_key: " + 
      str(donor_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()
def run_multiple_donation_test():
    """
  <Purpose>
    The purpose of this test is to see how the transtion script would
    react if we provide a dictionary where a vessel is in the acceptdonation
    state but the node has already in the canonical state. This is possible
    if the node had two vessel in the acceptdonation state to start with.
    This is essentially if the node has two donations. Currently we don't handle
    multiple donations from the second node, so when we run the test, the success
    count should be 0 and the failure count should be 1. Also the name of the extra
    vessel should not change.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Effects>
    None

  <Return>
    None
  """

    # Have the extra vessel in canonical form
    vessels_dict[mockutil.extra_vessel_name]["userkeys"] = [
        node_transition_lib.canonicalpublickey
    ]

    # Have a second vessel in acceptdonation state. Essentially a second donation.
    vessels_dict["second_donation"] = {
        "userkeys": [node_transition_lib.acceptdonationpublickey],
        "ownerkey": mockutil.donor_key,
        "ownerinfo": "",
        "status": "",
        "advertise": True
    }

    # 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)
    user_object = maindb.get_user(mockutil.testusername)
    # Create a donation for user
    maindb.create_donation(node_object, user_object, "Making a donation")

    # Register the new vesseldict so get_info returns this new modified vesseldict
    mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test",
                                            vessels_dict)

    transitionlist = []

    transitionlist.append(
        (("donation_state", node_transition_lib.acceptdonationpublickey),
         ("canonical_state", node_transition_lib.canonicalpublickey),
         node_transition_lib.noop, node_transition_lib.noop))

    print "Running test where database has record of node, no record of donation and node has donor key"

    (success_count,
     failure_count) = node_transition_lib.do_one_processnode_run(
         transitionlist, "startstatename", 1)[0]

    assert (success_count == 0)
    assert (failure_count == 1)

    assert_database_info()

    assert (mockutil.set_vessel_owner_key_call_count == 0)
    assert (mockutil.set_vessel_user_keylist_call_count == 0)
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 run_multiple_donation_test():
  """
  <Purpose>
    The purpose of this test is to see how the transtion script would
    react if we provide a dictionary where a vessel is in the acceptdonation
    state but the node has already in the canonical state. This is possible
    if the node had two vessel in the acceptdonation state to start with.
    This is essentially if the node has two donations. Currently we don't handle
    multiple donations from the second node, so when we run the test, the success
    count should be 0 and the failure count should be 1. Also the name of the extra
    vessel should not change.

  <Arguments>
    None

  <Exceptions>
    None

  <Side Effects>
    None

  <Return>
    None
  """
 
  # Have the extra vessel in canonical form
  vessels_dict[mockutil.extra_vessel_name]["userkeys"] = [node_transition_lib.transition_state_keys['canonical']]

  # Have a second vessel in acceptdonation state. Essentially a second donation.
  vessels_dict["second_donation"] = {"userkeys" : [node_transition_lib.transition_state_keys['acceptdonation']],
                                   "ownerkey" : mockutil.donor_key,
                                   "ownerinfo" : "",
                                   "status" : "",
                                   "advertise" : True}

  # 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)
  user_object = maindb.get_user(mockutil.testusername)
  # Create a donation for user
  maindb.create_donation(node_object, user_object, "Making a donation")
  
  # Register the new vesseldict so get_info returns this new modified vesseldict
  mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)




  transitionlist = []

  transitionlist.append(("acceptdonation", "canonical", 
                         node_transition_lib.noop,
                         node_transition_lib.noop, False))

  print "Running test where database has record of node, no record of donation and node has donor key"

  (success_count, failure_count) = node_transition_lib.do_one_processnode_run(transitionlist, "startstatename", 1)[0]

  assert(success_count == 0)
  assert(failure_count == 1)

  assert_database_info()

  assert(mockutil.set_vessel_owner_key_call_count == 0)
  assert(mockutil.set_vessel_user_keylist_call_count == 0)
# 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 = []
for i in range(100):
  node = node_list[i]
  donor = user_list[i % len(user_list)]
  donation = maindb.create_donation(node, donor, 'some resource description text')
  donation_list.append(donation)


# Create Vessel records
vessel_list = []
for i in range(100):
  node = node_list[i]
  name = 'v' + str(i)
  vessel = maindb.create_vessel(node, name)
  vessel_list.append(vessel)
  # Set the vessel ports.
  ports = range(1000, 1010)
  ports.append(user_list[0].usable_vessel_port)
  maindb.set_vessel_ports(vessel, ports)
  
Пример #14
0
# 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 = []
for i in range(100):
    node = node_list[i]
    donor = user_list[i % len(user_list)]
    donation = maindb.create_donation(node, donor,
                                      'some resource description text')
    donation_list.append(donation)

# Create Vessel records
vessel_list = []
for i in range(100):
    node = node_list[i]
    name = 'v' + str(i)
    vessel = maindb.create_vessel(node, name)
    vessel_list.append(vessel)
    # Set the vessel ports.
    ports = range(1000, 1010)
    ports.append(user_list[0].usable_vessel_port)
    maindb.set_vessel_ports(vessel, ports)
Пример #15
0
  testfailed("private key wasn't deleted: " + str(privkey))

# Make sure they don't have any donations initially.
donations = interface.get_donations(geniuser)
if len(donations) > 0:
  testfailed("The user shouldn't have any donations.")

INITIAL_DONATION_COUNT = 6

# Create a few nodes and donations by this user.
for i in range(INITIAL_DONATION_COUNT):
  node_identifier = 'testnode' + username + 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)
  maindb.create_donation(node, geniuser, 'some resource description text')
  # Create the vessels on these nodes.
  name = 'testvessel' + username + str(i)
  vessel = maindb.create_vessel(node, name)
  # Set the vessel ports.
  ports = range(1000, 1010)
  ports.append(geniuser.usable_vessel_port)
  maindb.set_vessel_ports(vessel, ports)

donations = interface.get_donations(geniuser)
if len(donations) != INITIAL_DONATION_COUNT:
  testfailed("The user's donation count doesn't equal their number of donations.")

# Make sure they don't have any acquired vessels initially.
acquired_vessels = interface.get_acquired_vessels(geniuser)
if len(acquired_vessels) > 0: