def run_movingto_twopercent_to_canonical_test():

  transitionlist = []


  # Change the vessel_dict and reset all the mock functions in order to have the appropriate info
  testuser = maindb.get_user(mockutil.testusername)

  all_donations = maindb.get_donations_by_user(testuser, include_inactive_and_broken=True)
  node = all_donations[0].node

  # Create 9 vessels for this node
  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))



  vessels_dict[mockutil.extra_vessel_name]["userkeys"] = [node_transition_lib.transition_state_keys['movingto_twopercent']]
  vessels_dict[mockutil.extra_vessel_name]["ownerkey"] = rsa_string_to_publickey(node.owner_pubkey)

  mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
  mockutil.mock_backend_set_vessel_owner_key()
  mockutil.mock_backend_join_vessels()
  mockutil.mock_backend_set_vessel_user_keylist([mockutil._mock_pubkey_to_string(
                                                node_transition_lib.transition_state_keys['canonical'])])


  transitionlist.append(("movingto_twopercent", "canonical",
                         node_transition_lib.combine_vessels,
                         node_transition_lib.noop, False))

  print "Starting movingto_twopercent to canonical test....."

  (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_before_completed()

  assert(mockutil.set_vessel_owner_key_call_count == 0)

  assert(mockutil.set_vessel_user_keylist_call_count == 1)

  all_donations = maindb.get_donations_by_user(testuser, include_inactive_and_broken=True)
  node = all_donations[0].node
  
  vessel_list_per_node = maindb.get_vessels_on_node(node)

  #testing to see if the vessels were deleted after join_vessels was called
  assert(mockutil.join_vessels_call_count == 9)
  assert(len(vessel_list_per_node) == 0)
  assert(node.extra_vessel_name == "extra_vessel_join9")
Exemplo n.º 2
0
def run_moving2onepercent_to_canonical_test():

  transitionlist = []


  # Change the vessel_dict and reset all the mock functions in order to have the appropriate info
  testuser = maindb.get_user(mockutil.testusername)

  all_donations = maindb.get_donations_by_user(testuser, include_inactive_and_broken=True)
  node = all_donations[0].node

  # Create 9 vessels for this node
  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))



  vessels_dict[mockutil.extra_vessel_name]["userkeys"] = [node_transition_lib.transition_state_keys['movingto_onepercentmanyevents']]
  vessels_dict[mockutil.extra_vessel_name]["ownerkey"] = rsa_string_to_publickey(node.owner_pubkey)

  mockutil.mock_nodemanager_get_node_info(mockutil.nodeid_key, "10.0test", vessels_dict)
  mockutil.mock_backend_set_vessel_owner_key()
  mockutil.mock_backend_join_vessels()
  mockutil.mock_backend_set_vessel_user_keylist([mockutil._mock_pubkey_to_string(
                                                node_transition_lib.transition_state_keys['canonical'])])


  transitionlist.append(("movingto_onepercentmanyevents", "canonical",
                         node_transition_lib.combine_vessels,
                         node_transition_lib.noop, False))

  print "Starting canonical to movingtoonepercentmanyevents test....."

  (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_before_completed()

  assert(mockutil.set_vessel_owner_key_call_count == 0)

  assert(mockutil.set_vessel_user_keylist_call_count == 1)

  all_donations = maindb.get_donations_by_user(testuser, include_inactive_and_broken=True)
  node = all_donations[0].node
  
  vessel_list_per_node = maindb.get_vessels_on_node(node)

  #testing to see if the vessels were deleted after join_vessels was called
  assert(mockutil.join_vessels_call_count == 9)
  assert(len(vessel_list_per_node) == 0)
  assert(node.extra_vessel_name == "extra_vessel_join9")
Exemplo n.º 3
0
def create_node_and_vessels_with_one_port_each(ip, portlist, is_active=True):
  
  global next_nodeid_number
  next_nodeid_number += 1
  
  nodeid = "node" + str(next_nodeid_number)
  port = 1234
  version = "10.0test"
  owner_pubkey = "1 2"
  extra_vessel_name = "v1"
  
  node = maindb.create_node(nodeid, ip, port, version, is_active, owner_pubkey, extra_vessel_name)

  single_vessel_number = 2

  for vesselport in portlist:
    single_vessel_name = "v" + str(single_vessel_number)
    single_vessel_number += 1
    vessel = maindb.create_vessel(node, single_vessel_name)
    maindb.set_vessel_ports(vessel, [vesselport])
  
  return node
def create_node_and_vessels_with_one_port_each(ip, portlist):
  
  global next_nodeid_number
  next_nodeid_number += 1
  
  nodeid = "node" + str(next_nodeid_number)
  port = 1234
  version = "10.0test"
  is_active = True
  owner_pubkey = "1 2"
  extra_vessel_name = "v1"
  
  node = maindb.create_node(nodeid, ip, port, version, is_active, owner_pubkey, extra_vessel_name)

  single_vessel_number = 2

  for vesselport in portlist:
    single_vessel_name = "v" + str(single_vessel_number)
    single_vessel_number += 1
    vessel = maindb.create_vessel(node, single_vessel_name)
    maindb.set_vessel_ports(vessel, [vesselport])
  
  return node
Exemplo n.º 5
0
def split_vessels (node_string, node_info, database_nodeobject, resourcetemplate):
  """
  <Purpose>
    The purpose of this function is to take a node has an extra vessel that is
    big enough to be split into two or more vessels. The resources on a vessel
    is determined by the resourcetemplate that is provided as well as the usable
    ports on the node.

  <Arguments>
    node_string - the name of the node. ip:port or NAT:port

    node_info - a dictionary containing information about the node

    database_nodeobject - a database object for the node

    resourcetemplate - the file that has information about resources

  <Exceptions>
    NodeError - Error raised if node is not in the right state

    NodemanagerCommunicationError - raised if we cannot retrieve the usable ports for a node

    NodeProcessError - raised if unable to split vessels properly

    DatabaseError - raised if unable to modify the database

  <Side Effects>
    Database gets modified.

  <Return>
    None
  """

  log("Beginning to divide vessel on node: "+node_string)

  # Extract the ip/NAT and the port.
  # Note that the first portion of the node might be an ip or a NAT string.
  (ip_or_nat_string, port_num) = split_node_string(node_string)

  donated_vesselname = database_nodeobject.extra_vessel_name

  # Retrieve the usable ports list for the node and then shuffle
  # the ports so each vessel gets a random subset of the ports
  usable_ports_list = nodemanager.get_vessel_resources(ip_or_nat_string, port_num, donated_vesselname)['usableports']
  log("List of usable ports in node: "+node_string+". "+str(usable_ports_list))
  random.shuffle(usable_ports_list)

  #the vessel that we start with
  current_vessel = donated_vesselname
  log("Name of starting vessel: "+current_vessel)

  # Keep splittiing the vessel until we run out of resources.
  # Note that when split_vessel is called the left vessel
  # has the leftover (extra vessel)and the right vessel has
  # the vessel with the exact resources.
  while len(usable_ports_list) >= 10:
    desired_resourcedata = get_resource_data(resourcetemplate, usable_ports_list)

    #use the first 10 ports so remove them from the list of usable_ports_list
    used_ports_list = usable_ports_list[:10]
    usable_ports_list = usable_ports_list[10:]

    log("Ports we are going to use for the new vessel: "+str(used_ports_list))
    log("Starting to split vessel: "+current_vessel)

    # Split the current vessel. The exact vessel is the right vessel
    # and the extra vessel is the left vessel.
    try:
      leftover_vessel, new_vessel = backend.split_vessel(database_nodeobject, current_vessel, desired_resourcedata)
    except NodemanagerCommunicationError, e:
      # The object 'e' will already include traceback info that has the actual node error.
      # If the failure is due to inability to split further, that's ok.
      if 'Insufficient quantity:' in str(e):
        log("Could not split " + current_vessel + " any further due to insufficient resource/quantity. " + str(e))
        # We must break out of the while loop here. If we let the exception get,
        # raised, it will look like the transition failed.
        break
      raise

    log("Successfully split vessel: "+current_vessel+" into vessels: "+leftover_vessel+" and "+new_vessel)
    current_vessel = leftover_vessel

    # Make sure to update the database and record the new
    # name of the extra vessel as when backend.split_vessels()
    # is called, the old vessel does not exist anymore.
    # Instead two new vessels are created, where the first
    # vessel is the extra vessel with leftover resources
    # and the second vessel has the actual amount of resources
    maindb.set_node_extra_vessel_name(database_nodeobject, current_vessel)

    # Set the user_list for the new vesel to be empty. Remember that user_list is what determines
    # the transition state, and only the extra vessel should have this set.
    backend.set_vessel_user_keylist(database_nodeobject, new_vessel, [])
    log("Changed the userkeys for the vessel "+new_vessel+" to []")

    # Add the newly created vessel to the database and then add the ports associated with
    # the vessel to the database also.
    log("Creating a vessel record in the database for vessel "+new_vessel+" for node "+node_string)
    try:
      vessel_object = maindb.create_vessel(database_nodeobject, new_vessel)
      log("Setting the vessel ports in the database for vessel "+new_vessel+" with port list: "+str(used_ports_list))
      maindb.set_vessel_ports(vessel_object, used_ports_list)
    except:
      raise DatabaseError("Failed to create vessel entry or change vessel entry for vessel: " +
                          new_vessel + ". " + traceback.format_exc())
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 onepercentmanyevents_divide(node_string, node_info, database_nodeobject,
                                onepercent_resourcetemplate):
    """
  <Purpose>
    The purpose of this function is to take a node thats in canonical state
    with one vessel, and split it into the 1% vessels so the vessels can
    be acquired by users.

  <Arguments>
    node_string - the name of the node. ip:port or NAT:port

    node_info - a dictionary containing information about the node

    database_nodeobject - a database object for the node
 
    onepercent_resourcetemplate - the file that has information about resources

  <Exceptions>
    NodeError - Error raised if node is not in the right state 

    NodemanagerCommunicationError - raised if we cannot retrieve the usable ports for a node

    NodeProcessError - raised if unable to split vessels properly

    DatabaseError - raised if unable to modify the database    

  <Side Effects>
    Database gets modified.        

  <Return>
    None
  """

    node_transition_lib.log("Beginning onepercentmanyevents_divide on node: " +
                            node_string)

    # Extract the ip/NAT and the port.
    # Note that the first portion of the node might be an ip or a NAT string.
    (ip_or_nat_string,
     port_num) = node_transition_lib.split_node_string(node_string)

    donated_vesselname = database_nodeobject.extra_vessel_name

    # Retrieve the usable ports list for the node and then shuffle
    # the ports so each vessel gets a random subset of the ports
    usable_ports_list = nodemanager.get_vessel_resources(
        ip_or_nat_string, port_num, donated_vesselname)['usableports']
    node_transition_lib.log("List of usable ports in node: " + node_string +
                            ". " + str(usable_ports_list))
    random.shuffle(usable_ports_list)

    #the vessel that we start with
    current_vessel = donated_vesselname
    node_transition_lib.log("Name of starting vessel: " + current_vessel)

    # Keep splittiing the vessel until we run out of resources.
    # Note that when split_vessel is called the left vessel
    # has the leftover (extra vessel)and the right vessel has
    # the vessel with the exact resources.
    while len(usable_ports_list) >= 10:
        desired_resourcedata = get_resource_data(onepercent_resourcetemplate,
                                                 usable_ports_list)

        #use the first 10 ports so remove them from the list of usable_ports_list
        used_ports_list = usable_ports_list[:10]
        usable_ports_list = usable_ports_list[10:]

        node_transition_lib.log(
            "Ports we are going to use for the new vessel: " +
            str(used_ports_list))
        node_transition_lib.log("Starting to split vessel: " + current_vessel)

        # Split the current vessel. The exact vessel is the right vessel
        # and the extra vessel is the left vessel.
        try:
            leftover_vessel, new_vessel = backend.split_vessel(
                database_nodeobject, current_vessel, desired_resourcedata)
        except NodemanagerCommunicationError, e:
            # The object 'e' will already include traceback info that has the actual node error.
            # If the failure is due to inability to split further, that's ok.
            if 'Insufficient quantity:' in str(e):
                node_transition_lib.log(
                    "Could not split " + current_vessel +
                    " any further due to insufficient resource/quantity. " +
                    str(e))
                # We must break out of the while loop here. If we let the exception get,
                # raised, it will look like the transition failed.
                break
            raise

        node_transition_lib.log("Successfully split vessel: " +
                                current_vessel + " into vessels: " +
                                leftover_vessel + " and " + new_vessel)
        current_vessel = leftover_vessel

        # Make sure to update the database and record the new
        # name of the extra vessel as when backend.split_vessels()
        # is called, the old vessel does not exist anymore.
        # Instead two new vessels are created, where the first
        # vessel is the extra vessel with leftover resources
        # and the second vessel has the actual amount of resources
        maindb.set_node_extra_vessel_name(database_nodeobject, current_vessel)

        #set the user_list for the new vesel to be empty. Remember that user_list is what determines
        #the transition state, and only the extra vessel should have this set.
        backend.set_vessel_user_keylist(database_nodeobject, new_vessel, [])
        node_transition_lib.log("Changed the userkeys for the vessel " +
                                new_vessel + " to []")

        # Add the newly created vessel to the database and then add the ports associated with
        # the vessel to the database also.
        try:
            node_transition_lib.log(
                "Creating a vessel record in the database for vessel " +
                new_vessel + " for node " + node_string)
            vessel_object = maindb.create_vessel(database_nodeobject,
                                                 new_vessel)
            node_transition_lib.log(
                "Setting the vessel ports in the database for vessel " +
                new_vessel + " with port list: " + str(used_ports_list))
            maindb.set_vessel_ports(vessel_object, used_ports_list)
        except:
            raise node_transition_lib.DatabaseError(
                "Failed to create vessel entry or change vessel entry for vessel: "
                + new_vessel + ". " + traceback.format_exc())
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()
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)
  

Exemplo n.º 10
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)