def test_many_to_many_relations_with_nodes(self): """ It tests the many to many relations with Nodes """ node_1 = Node() node_1.name = "node1" node_1.information_retrieved = False node_2 = Node() node_2.name = "node2" node_2.information_retrieved = False db.session.add(node_1) db.session.add(node_2) execution_1 = Execution() execution_1.status = "x1" execution_2 = Execution() execution_2.status = "x2" db.session.add(execution_1) db.session.add(execution_2) db.session.commit() execution_1.nodes = [node_1, node_2] execution_2.nodes = [node_2, node_1] db.session.commit() execution = db.session.query(Execution).filter_by(status="x1").first() self.assertEquals(node_1, execution.nodes[0]) self.assertEquals(node_2, execution.nodes[1]) execution = db.session.query(Execution).filter_by(status="x2").first() self.assertEquals(node_2, execution.nodes[0]) self.assertEquals(node_1, execution.nodes[1])
def setUp(self): """ It creates the memory db """ db.create_all() # We store some Applications in the db for the tests application_1 = Application() application_1.name = 'AppName_1' application_2 = Application() application_2.name = 'AppName_2' # Adding executing scripts execution_script_1 = ExecutionConfiguration() execution_script_1.execution_type = "slurm:sbatch" execution_script_2 = ExecutionConfiguration() execution_script_2.execution_type = "slurm:sbatch2" application_2.execution_configurations = [ execution_script_1, execution_script_2 ] db.session.add(application_1) db.session.add(application_2) # We store some testbeds in the db for the tests testbed_1 = Testbed("name_1", True, "slurm", "ssh", "user@server", ['slurm']) testbed_2 = Testbed("name_2", False, "slurm", "ssh", "user@server", ['slurm']) testbed_3 = Testbed("name_3", True, "slurm", "ssh", "user@server", ['slurm', 'slurm:singularity']) db.session.add(testbed_1) db.session.add(testbed_2) db.session.add(testbed_3) db.session.commit() deployment = Deployment() deployment.executable_id = execution_script_1.id deployment.testbed_id = testbed_1.id db.session.add(deployment) # We store some nodes in the db for the tests node_1 = Node() node_1.name = "node_1" node_1.information_retrieved = True node_2 = Node() node_2.name = "node_2" node_2.information_retrieved = False db.session.add(node_1) db.session.add(node_2) execution = Execution() execution.execution_type = "execution_type" execution.status = "status" db.session.add(execution) db.session.commit()
def persist_graph(request, graph_id): import pygraphviz as P import networkx as N graph = Graph.objects.get(pk=graph_id) dot_path = graph.dot_file.storage.location + '/' + graph.dot_file.name G = P.AGraph() # init empty graph try: G.read(dot_path) #read file nodes = G.nodes() edges = G.edges() Node.objects.filter(graph__id=graph_id).delete() Edge.objects.filter(graph__id=graph_id).delete() for node in nodes: new_node = Node() new_node.graph = graph new_node.name = node new_node.label = node.attr['label'] new_node.save() for edge in edges: new_edge = Edge() new_edge.graph = graph new_edge.name = edge new_edge.save() except: return False return HttpResponse(pygraphviz_graph(request, G)) #response
def test_crud_node(self): """ It tests the basic CRUD operations of an Node class """ # We verify the object is not in the db after creating it node = Node() node.name = "node1" node.information_retrieved = True self.assertIsNone(node.id) # We store the object in the db db.session.add(node) # We recover the node from the db node = db.session.query(Node).filter_by(name='node1').first() self.assertIsNotNone(node.id) self.assertEquals("node1", node.name) self.assertTrue(node.information_retrieved) # We update the node node.information_retrieved = False db.session.commit() node = db.session.query(Node).filter_by(name='node1').first() self.assertFalse(node.information_retrieved) # We check the deletion db.session.delete(node) count = db.session.query(Node).filter_by(name='node1').count() self.assertEquals(0, count)
def test_node_cpu_relation(self): """ Unit test that verifies that the correct relation between the testbed class and the node class is built """ # We create a node node = Node() node.name = "node1" node.information_retrieved = True # We add several CPUs to it node.cpus = [ CPU("Intel", "Xeon", "x86_64", "e6333", "2600Mhz", True, 2, "cache", "111"), CPU("AMD", "Zen", "x86_64", "xxxxx", "2600Mhz", True, 2, "cache", "111") ] # We save everything to the db db.session.add(node) db.session.commit() # We retrived and verify that the cpus are there node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(2, len(node.cpus)) self.assertEquals("Intel", node.cpus[0].vendor_id) self.assertEquals("AMD", node.cpus[1].vendor_id) # Lets delete a cpu directly from the session db.session.delete(node.cpus[1]) db.session.commit() node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(1, len(node.cpus)) self.assertEquals("Intel", node.cpus[0].vendor_id)
def test_node_gpu_relation(self): """ Unit test that verifies that the correct relation between the GPU class and the node class is built """ # We create a node node = Node() node.name = "node1" node.information_retrieved = True # We add several CPUs to it node.gpus = [ GPU("Nvidia", "GeForce"), GPU("AMD", "Raedon") ] # We save everything to the db db.session.add(node) db.session.commit() # We retrived and verify that the gpus are there node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(2, len(node.gpus)) self.assertEquals("Nvidia", node.gpus[0].vendor_id) self.assertEquals("AMD", node.gpus[1].vendor_id) # Lets delete a gpu directly from the session db.session.delete(node.gpus[1]) db.session.commit() node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(1, len(node.gpus)) self.assertEquals("Nvidia", node.gpus[0].vendor_id)
def test_node_memory_relation(self): """ Unit test that verifies that the correct relation between the Memory class and the node class is built """ # We create a node node = Node() node.name = "node1" node.information_retrieved = True # We add several CPUs to it node.memories = [ Memory(11111, "bytes"), Memory(11211, "bytes") ] # We save everything to the db db.session.add(node) db.session.commit() # We retrived and verify that the gpus are there node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(2, len(node.memories)) self.assertEquals(11111, node.memories[0].size) self.assertEquals(11211, node.memories[1].size) # Lets delete a gpu directly from the session db.session.delete(node.memories[1]) db.session.commit() node = db.session.query(Node).filter_by(name='node1').first() self.assertEquals(1, len(node.memories)) self.assertEquals(11111, node.memories[0].size)
def test_remove_node(self): """Test that the method that deletes a node from the list of nodes of a testbed works as expected.""" # We create a testbed first testbed = Testbed("name", True, "slurm", "ssh", "user@server", ['slurm']) # We two nodes to the testbed node_1 = Node() node_1.name = "node_1" testbed.add_node(node_1) node_2 = Node() node_2.name = "node_2" testbed.add_node(node_2) self.assertEquals(2, len(testbed.nodes)) # We delete one of the nodes and check that the list only contains one node testbed.remove_node(node_1) self.assertEquals(1, len(testbed.nodes)) self.assertEquals(node_2, testbed.nodes[0]) testbed.remove_node("adasdfa")
def _create_initial_db_data(self): """ This method creates some initial data in the db that is employed by some of the tests here. """ # We add two testbeds to the db testbed = Testbed("name1", True, Testbed.slurm_category, "ssh", "user@server", ['slurm']) # We add some nodes to Testbed_1 node_1 = Node() node_1.name = "node_1" node_1.information_retrieved = True node_2 = Node() node_2.name = "node_2" node_2.information_retrieved = True testbed.nodes = [node_1, node_2] node_1.disabled = True db.session.add(testbed) db.session.commit() return testbed, node_1, node_2
def test_add_node(self): """Test that the method that add a node to the list of nodes works as expected""" # We create a testbed first testbed = Testbed("name", True, "slurm", "ssh", "user@server", ['slurm']) self.assertEquals(0, len(testbed.nodes)) # We add a node to the testbed and veriy it is added node_1 = Node() node_1.name = "node333" testbed.add_node(node_1) self.assertEquals(1, len(testbed.nodes)) self.assertEquals(node_1, testbed.nodes[0]) # We verify that it is not possible to add an object that it is not # from type Node no_node = "xxx" testbed.add_node(no_node) self.assertEquals(1, len(testbed.nodes)) self.assertEquals(node_1, testbed.nodes[0])
def run(): Session = database.get_db_session() session = Session() database.Base.query = Session.query_property() updates_comments_new = defaultdict(list) updates_topics_new = defaultdict(list) updated_topics = forum.get_updated_topics() for topic in updated_topics: # Find or create node for this topic node = Node.query.filter_by(id=topic['node_id']).first() if not node: node = Node(id=topic['node_id'], name=topic['name']) session.add(node) node.name = topic['name'] # find parent of this node parent_node = node.parent if not parent_node: if topic['section_node_id']: parent_node = Node.query.filter_by( id=topic['section_node_id']).first() if not parent_node: parent_node = Node(id=topic['section_node_id'], parent_id=NodeType.TOPIC.value, name=topic['section_name']) parent_node.name = topic['section_name'] else: parent_node = Node.query.filter_by( id=topic['type'].value).first() if not parent_node: raise Exception('Cannot find parent for node') node.parent = parent_node last_checked = node.last_checked node.last_checked = datetime.datetime.now() session.commit() current_node = node excepted = set() subscribed = {} while current_node: subscriptions = current_node.subscriptions for s in subscriptions: if s.exception: excepted.add(s.chat_id) else: if s.chat_id not in excepted and s.chat_id not in subscribed: subscribed[s.chat_id] = s current_node = current_node.parent print(subscribed) if topic['new_comments_link']: comments = forum.get_new_comments_in_topic( topic['new_comments_link']) else: comments = [] for chat_id, subscription in subscribed.items(): if topic['status'] == 'new' and not last_checked: updates_topics_new[chat_id].append(topic) if comments and not subscription.no_comments: if subscription.no_replies: sub_comments = [ comment for comment in comments if not comment['is_reply'] ] else: sub_comments = comments if sub_comments: updates_comments_new[chat_id].append((topic, sub_comments)) Session.remove() send_message_new_topics(updates_topics_new) send_message_new_comments(updates_comments_new)
def test_testbed_node_relation(self): """ Unit test that verifies that the correct relation between the testbed class and the node class is built """ # We create a testbed testbed = Testbed("name", True, "slurm", "ssh", "user@server", ['slurm']) # We create several nodes node_1 = Node() node_1.name = "node1" node_1.information_retrieved = True node_2 = Node() node_2.name = "node2" node_2.information_retrieved = False node_3 = Node() node_3.name = "node3" node_3.information_retrieved = True testbed.nodes = [ node_1, node_2, node_3 ] # We save everything to the db db.session.add(testbed) db.session.commit() # We retrieve and verify that the nodes are there testbed = db.session.query(Testbed).filter_by(name='name').first() self.assertEquals(3, len(testbed.nodes)) self.assertEquals("node1", testbed.nodes[0].name) self.assertTrue(testbed.nodes[0].information_retrieved) self.assertEquals("node2", testbed.nodes[1].name) self.assertFalse(testbed.nodes[1].information_retrieved) self.assertEquals("node3", testbed.nodes[2].name) self.assertTrue(testbed.nodes[2].information_retrieved) # lets remove a node from testbed testbed.remove_node(testbed.nodes[2]) # We commit the state and it should be updated db.session.commit() testbed = db.session.query(Testbed).filter_by(name='name').first() self.assertEquals(2, len(testbed.nodes)) self.assertEquals("node1", testbed.nodes[0].name) self.assertTrue(testbed.nodes[0].information_retrieved) self.assertEquals("node2", testbed.nodes[1].name) self.assertFalse(testbed.nodes[1].information_retrieved) # Lets delete the node directly from the session db.session.delete(testbed.nodes[1]) db.session.commit() testbed = db.session.query(Testbed).filter_by(name='name').first() self.assertEquals(1, len(testbed.nodes)) self.assertEquals("node1", testbed.nodes[0].name) self.assertTrue(testbed.nodes[0].information_retrieved) # It should be only one node in the db nodes = db.session.query(Node).all() self.assertEquals(2, len(nodes)) self.assertEquals("node1", nodes[0].name) self.assertTrue(nodes[0].information_retrieved) self.assertIsNotNone(nodes[0].testbed) self.assertEquals("node3", nodes[1].name) self.assertTrue(nodes[1].information_retrieved) self.assertIsNone(nodes[1].testbed)
def test_can_create_the_node(self): """ Checks the right behaviour that checks if it is possible to add a node """ # Node not associated to any db node = Node() node.name = "xxx" node.information_retrieved = True is_possible = alde.can_create_the_node(node) self.assertEquals(True, is_possible['create']) self.assertEquals('', is_possible['reason']) # Node has an ivalid testbed id node.testbed_id = 100000 is_possible = alde.can_create_the_node(node) self.assertEquals(False, is_possible['create']) self.assertEquals('Testbed does not exist', is_possible['reason']) # Testbed exists but information is retrieved automatically node.testbed_id = 1 is_possible = alde.can_create_the_node(node) self.assertEquals(False, is_possible['create']) self.assertEquals('Testbed is configured to automatically retrieve information of nodes', is_possible['reason']) # Testeb exists but information is not retrieved automatically node.testbed_id = 2 is_possible = alde.can_create_the_node(node) self.assertEquals(True, is_possible['create']) self.assertEquals('', is_possible['reason']) # Testbed info is passed by the url: node = Node() node.name = "xxx" node.information_retrieved = True ## Testbed does not allow it is_possible = alde.can_create_the_node(node, testbed_id=1) self.assertEquals(False, is_possible['create']) self.assertEquals('Testbed is configured to automatically retrieve information of nodes', is_possible['reason']) ## Testbed does allow it is_possible = alde.can_create_the_node(node, testbed_id=2) self.assertEquals(True, is_possible['create']) self.assertEquals('', is_possible['reason']) # Testbed and nodes are created at the same time testbed_1 = { 'name' : 'name_3', 'on_line' : True } testbed_2 = { 'name' : 'name_3', 'on_line' : False } ## Testbed does not allow it is_possible = alde.can_create_the_node(node, testbed=testbed_1) self.assertEquals(False, is_possible['create']) self.assertEquals('Testbed is configured to automatically retrieve information of nodes', is_possible['reason']) ## Testbed does allow it is_possible = alde.can_create_the_node(node, testbed=testbed_2) self.assertEquals(True, is_possible['create']) self.assertEquals('', is_possible['reason'])
def addEditNode(node_id): node = None # get choices for node leaders leader_choices = [(0, 'Self')] for x in Node.query.filter_by(leader_id=0): # @UndefinedVariable leader_choices.append((x.id,x.name)) form = NodeForm() form.leader.choices = leader_choices form.leader.default = 0 if node_id is not None: node = Node.query.get(node_id) # @UndefinedVariable if request.method == 'GET': if node is None: form.new.data = True else: form.new.data = False form.id.data = node.id form.name.data = node.name form.rid.data = node.rid form.ip.data = node.ip form.location.lat.data = node.location.lat form.location.lon.data = node.location.lon form.leader.data = node.leader_id jumppoints = [] for jp in node.jumppoints: form.jumppoints.append_entry({"jp_id": jp.id, "lat": jp.location.lat, "lon":jp.location.lon }) goals = [] for goal in node.goals: form.goals.append_entry({"goal_id": goal.id, "lat": goal.location.lat, "lon":goal.location.lon }) elif request.method == 'POST' and form.validate(): # @UndefinedVariable if node is None: #new node has passed validation, add to db location = Location(lat=form.location.lat.data, lon=form.location.lon.data) db.session.add(location) # @UndefinedVariable node = Node(name=form.name.data, leader_id=form.leader.data, location=location, rid=form.rid.data, ip=form.ip.data) db.session.add(node) # @UndefinedVariable db.session.commit() # @UndefinedVariable for index, point in enumerate(form.jumppoints.data): jp = JumpPoint() location = Location(lat=point['lat'], lon=point['lon']) db.session.add(location) # @UndefinedVariable jp.location = location jp.position = int(point['pos']) + 1 db.session.add(jp) node.jumppoints.append(jp) for index, point in enumerate(form.goals.data): goal = Goal() location = Location(lat=point['lat'], lon=point['lon']) db.session.add(location) # @UndefinedVariable goal.location = location goal.position = int(point['pos']) + 1 db.session.add(goal) node.goals.append(goal) db.session.commit() # @UndefinedVariable flash("Node has been created") else: #node has been updated. save updates node.name = form.name.data node.rid = form.rid.data node.ip = form.ip.data location = Location.query.get(node.loc_id) # @UndefinedVariable location.lat = form.location.lat.data location.lon = form.location.lon.data node.location = location node.leader_id = form.leader.data # create a list of all points already included on this path. will be used to determine if # any points were deleted from the list. deleteList = [] for jp in node.jumppoints: deleteList.append(jp.id) for index, jp in enumerate(form.jumppoints.data): if int(jp['jp_id']) == 0: newjp = JumpPoint() location = Location(lat=jp['lat'], lon=jp['lon']) db.session.add(location) # @UndefinedVariable newjp.location = location newjp.position = int(jp['pos']) + 1 db.session.add(newjp) node.jumppoints.append(newjp) else: # found existing point. update and remove from delete list savedjp = JumpPoint.query.get(jp['jp_id']) # @UndefinedVariable savedjp.position = int(jp['pos']) + 1 savedLoc = Location.query.get(savedjp.loc_id) # @UndefinedVariable savedLoc.lat = jp['lat'] savedLoc.lon = jp['lon'] deleteList.remove(int(jp['jp_id'])) for id in deleteList: jp= JumpPoint.query.get(id) # @UndefinedVariable db.session.delete(jp) deleteList = [] for goal in node.goals: deleteList.append(goal.id) for index, goal in enumerate(form.goals.data): if int(goal['goal_id']) == 0: newgoal = Goal() location = Location(lat=goal['lat'], lon=goal['lon']) db.session.add(location) # @UndefinedVariable newgoal.location = location newgoal.position = int(goal['pos']) + 1 db.session.add(newgoal) node.goals.append(newgoal) else: # found existing point. update and remove from delete list savedGoal = Goal.query.get(goal['goal_id']) # @UndefinedVariable savedGoal.position = int(goal['pos']) + 1 savedLoc = Location.query.get(savedGoal.loc_id) # @UndefinedVariable savedLoc.lat = goal['lat'] savedLoc.lon = goal['lon'] deleteList.remove(int(goal['goal_id'])) for id in deleteList: goal= Goal.query.get(id) # @UndefinedVariable db.session.delete(goal) db.session.commit() flash("Node has been updated") # after creating the new state, redirect them back to dce config page return redirect(url_for("nodePage")) return render_template("nodeForm.html", form=form)
def test_update_node_information(self, mock_shell): """ Test that the correct work of this function """ l = LogCapture() # we capture the logger command = "pbsnodes" params = ["-x"] # We store some data in the db for the test. # We add a testbed to the db testbed = Testbed("name1", True, Testbed.torque_category, Testbed.protocol_ssh, "user@server", ['torque']) # We add some nodes to Testbed_1 node_1 = Node() node_1.name = "node-1.novalocal" node_1.information_retrieved = True node_2 = Node() node_2.name = "node-2.novalocal" node_2.information_retrieved = True testbed.nodes = [node_1, node_2] node_1.disabled = True db.session.add(testbed) db.session.commit() # We mock the command call mock_shell.return_value = self.command_pbsnodes_output testbeds.facade.update_testbed_node_information(testbed) # We verify the results node_1 = db.session.query(Node).filter_by( name='node-1.novalocal').first() node_2 = db.session.query(Node).filter_by( name='node-2.novalocal').first() self.assertEqual('free', node_1.state) self.assertEqual(1, len(node_1.memories)) self.assertEqual(Memory.KILOBYTE, node_1.memories[0].units) self.assertEqual(131583196, node_1.memories[0].size) self.assertEqual(1, len(node_1.gpus)) self.assertEqual('free', node_2.state) self.assertEqual(1, len(node_2.memories)) self.assertEqual(Memory.KILOBYTE, node_2.memories[0].units) self.assertEqual(131583197, node_2.memories[0].size) self.assertEqual(0, len(node_2.gpus)) mock_shell.assert_called_with(command=command, server="user@server", params=params) self.assertEqual(1, mock_shell.call_count) # Checking that we are logging the correct messages l.check( ('root', 'INFO', 'Updating information for node: node-1.novalocal if necessary'), ('root', 'INFO', 'Updating memory information for node: node-1.novalocal'), ('root', 'INFO', 'Updating gpu information for node: node-1.novalocal'), ('root', 'INFO', 'Updating information for node: node-2.novalocal if necessary'), ('root', 'INFO', 'Updating memory information for node: node-2.novalocal'), ) l.uninstall() # We uninstall the capture of the logger
def test_update_node_information(self, mock_shell): """ Test that the correct work of this function """ l = LogCapture() # we cature the logger command = "scontrol" params = ["-o", "--all", "show", "node"] # We store some data in the db for the test. # We add two testbeds to the db testbed = Testbed("name1", True, Testbed.slurm_category, Testbed.protocol_ssh, "user@server", ['slurm']) # We add some nodes to Testbed_1 node_1 = Node() node_1.name = "nd80" node_1.information_retrieved = True node_2 = Node() node_2.name = "nd23" node_2.information_retrieved = True testbed.nodes = [node_1, node_2] node_1.disabled = True db.session.add(testbed) db.session.commit() # We mock the command call mock_shell.return_value = self.command_scontrol_output slurm.update_node_information() # We verify the results node_80 = db.session.query(Node).filter_by(name='nd80').first() node_23 = db.session.query(Node).filter_by(name='nd23').first() self.assertEquals('ALLOCATED', node_80.state) self.assertEquals(1, len(node_80.memories)) self.assertEquals(Memory.MEGABYTE, node_80.memories[0].units) self.assertEquals(6850663, node_80.memories[0].size) self.assertEquals(0, len(node_80.gpus)) self.assertEquals('MAINT', node_23.state) self.assertEquals(1, len(node_23.memories)) self.assertEquals(Memory.MEGABYTE, node_23.memories[0].units) self.assertEquals(24018, node_23.memories[0].size) self.assertEquals(2, len(node_23.gpus)) self.assertEquals('Nvidia', node_23.gpus[0].vendor_id) self.assertEquals('Nvidia TESLA C2075', node_23.gpus[0].model_name) self.assertEquals('Nvidia', node_23.gpus[1].vendor_id) self.assertEquals('Nvidia TESLA C2075', node_23.gpus[1].model_name) mock_shell.assert_called_with(command=command, server="user@server", params=params) self.assertEquals(1, mock_shell.call_count) # Checking that we are logging the correct messages l.check(('root', 'INFO', 'Updating information for node: nd80 if necessary'), ('root', 'INFO', 'Updating memory information for node: nd80'), ('root', 'INFO', 'Updating information for node: nd23 if necessary'), ('root', 'INFO', 'Updating memory information for node: nd23'), ('root', 'INFO', 'Updating gpu information for node: nd23')) l.uninstall() # We uninstall the capture of the logger
def test_update_cpu_node_information(self, mock_parser): """ Test that the correct work of this function """ l = LogCapture() # we cature the logger # We store some data in the db for the test. testbed, node_1, node_2 = self._create_initial_db_data() node_3 = Node() node_3.name = "node_3" node_3.information_retrieved = True testbed.nodes.append(node_3) node_3.cpus = [ CPU("Intel", "Xeon", "x86_64", "e6333", "2600Mhz", True, 2, "cache", "111") ] # So, testbed has 3 nodes, one disabled and the other ones enabled db.session.commit() cpus_result = [ CPU("Intel2", "Xeon2", "x86_64", "e6333", "2600Mhz", True, 2, "cache", "111"), CPU("Intel3", "Xeon3", "x86_64", "e6333", "2600Mhz", True, 2, "cache", "111") ] mock_parser.return_value = cpus_result slurm.update_cpu_node_information() # We verify the results self.assertEquals( 0, len(db.session.query(CPU).filter_by(vendor_id="Intel").all())) self.assertEquals( 1, len(db.session.query(CPU).filter_by(vendor_id="Intel2").all())) self.assertEquals( 1, len(db.session.query(CPU).filter_by(vendor_id="Intel3").all())) calls = [mock.call(testbed, node_2), mock.call(testbed, node_3)] mock_parser.assert_has_calls(calls) self.assertEquals(2, mock_parser.call_count) # In case an error occours retrieving the information mock_parser.return_value = [] slurm.update_cpu_node_information() calls = [mock.call(testbed, node_2), mock.call(testbed, node_3)] mock_parser.assert_has_calls(calls) self.assertEquals(4, mock_parser.call_count) # Checking that we are logging the correct messages l.check(('root', 'INFO', 'Updating CPU info for node: node_2'), ('root', 'INFO', 'Updating CPU info for node: node_3'), ('root', 'ERROR', 'Impossible to update CPU info for node: node_2'), ('root', 'ERROR', 'Impossible to update CPU info for node: node_3')) l.uninstall() # We uninstall the capture of the logger
def test_get_cpuinfo_node(self, mock_shell): """ It verfies that given a testbed it is possible to get the cpuinfo information of the node. """ l = LogCapture() # we cature the logger # When the testbed is local testbed = Testbed("name1", True, Testbed.slurm_category, Testbed.protocol_local, "user@server", ['slurm']) node_1 = Node() # We add some nodes to Testbed_1 node_1.name = "node_1" node_1.information_retrieved = True node_2 = Node() node_2.name = "node_2" node_2.information_retrieved = True testbed.nodes = [ node_1, node_2] node_1.disabled = True node_3 = Node() node_3.name = "node_3" node_3.information_retrieved = True # When the node does not belong to the testbed it should return empty list cpus = parser.get_cpuinfo_node(testbed, node_3) self.assertEquals(0, len(cpus)) # When the node is there, we have to get double CPU info mock_shell.return_value = self.command_output cpus = parser.get_cpuinfo_node(testbed, node_2) self.assertEquals(8, len(cpus)) mock_shell.assert_called_with(command="ssh", params=["node_2", "'cat", "/proc/cpuinfo'"]) self.assertEqual(mock_shell.call_count, 1) # When the node is dissabled it should return an empty list cpus = parser.get_cpuinfo_node(testbed, node_1) self.assertEquals(0, len(cpus)) self.assertEqual(mock_shell.call_count, 1) # When the testbed is using ssh protocol testbed = Testbed("name1", True, Testbed.slurm_category, Testbed.protocol_ssh, "user@server", ['slurm']) testbed.nodes = [node_2] mock_shell.return_value = self.command_output cpus = parser.get_cpuinfo_node(testbed, node_2) self.assertEquals(8, len(cpus)) mock_shell.assert_called_with(command="ssh", server=testbed.endpoint, params=["node_2", "'cat", "/proc/cpuinfo'"]) self.assertEqual(mock_shell.call_count, 2) # We simulate what happens if we get an exception executing the command error = subprocess.CalledProcessError(returncode=255, cmd="ls") mock_shell.side_effect = error cpus = parser.get_cpuinfo_node(testbed, node_2) self.assertEquals(0, len(cpus)) self.assertEqual(mock_shell.call_count, 3) # When the testbed has an unknown protocol testbed = Testbed("name1", True, Testbed.slurm_category, "xxx", "user@server", ['slurm']) testbed.nodes = [node_2] cpus = parser.get_cpuinfo_node(testbed, node_2) self.assertEquals(0, len(cpus)) self.assertEqual(mock_shell.call_count, 3) # We verify that we raised the right errors # Checking that we are logging the correct messages l.check( ('root', 'ERROR', 'Exception trying to get the node cpu info'), ('root', 'INFO', 'Tesbed protocol: xxx not supported to get node information') ) l.uninstall() # We uninstall the capture of the logger