[LNT] r308709 - api: Add machine merge operation

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 20 18:46:17 PDT 2017


Author: matze
Date: Thu Jul 20 18:46:17 2017
New Revision: 308709

URL: http://llvm.org/viewvc/llvm-project?rev=308709&view=rev
Log:
api: Add machine merge operation

Add api operation to merge 1 machine into another.

Duplicated machines are a very common occurence with the current
behaviour where we create a new one when parameters don't match, so it
is very helpful to have a way to clean this up by merging two machines
(typically with the same name).

Modified:
    lnt/trunk/lnt/server/ui/api.py
    lnt/trunk/tests/server/ui/test_api_modify.py

Modified: lnt/trunk/lnt/server/ui/api.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/ui/api.py?rev=308709&r1=308708&r2=308709&view=diff
==============================================================================
--- lnt/trunk/lnt/server/ui/api.py (original)
+++ lnt/trunk/lnt/server/ui/api.py Thu Jul 20 18:46:17 2017
@@ -154,10 +154,10 @@ class Machine(Resource):
                     ts.session.delete(run)
                 ts.commit()
 
-            machine_name = machine.name
+            machine_name = "%s:%s" % (machine.name, machine.id)
             ts.session.delete(machine)
             ts.commit()
-            msg = "Deleted machine %s:%s" % (machine_name, machine_id)
+            msg = "Deleted machine %s" % machine_name
             logger.info(msg)
             yield msg + '\n'
 
@@ -170,7 +170,7 @@ class Machine(Resource):
     def post(machine_id):
         ts = request.get_testsuite()
         machine = Machine._get_machine(machine_id)
-        previous_name = machine.name
+        machine_name = "%s:%s" % (machine.name, machine.id)
 
         action = request.values.get('action', None)
         if action is None:
@@ -185,8 +185,25 @@ class Machine(Resource):
                 abort(400, msg="Machine with name '%s' already exists" % name)
             machine.name = name
             ts.session.commit()
-            logger.info("Renamed machine %s:%s to %s" %
-                        (previous_name, machine_id, name))
+            logger.info("Renamed machine %s to %s" % (machine_name, name))
+        elif action == 'merge':
+            into_id = request.values.get('into', None)
+            if into_id is None:
+                abort(400, msg="Expected 'into' for merge request")
+            into = Machine._get_machine(into_id)
+            into_name = "%s:%s" % (into.name, into.id)
+            ts.query(ts.Run) \
+                .filter(ts.Run.machine_id == machine.id) \
+                .update({ts.Run.machine_id: into.id},
+                        synchronize_session=False)
+            ts.session.expire_all()  # be safe after synchronize_session==False
+            # re-query Machine so we can delete it.
+            machine = Machine._get_machine(machine_id)
+            ts.delete(machine)
+            ts.session.commit()
+            logger.info("Merged machine %s into %s" %
+                        (machine_name, into_name))
+            logger.info("Deleted machine %s" % machine_name)
         else:
             abort(400, msg="Unknown action '%s'" % action)
 

Modified: lnt/trunk/tests/server/ui/test_api_modify.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/test_api_modify.py?rev=308709&r1=308708&r2=308709&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/test_api_modify.py (original)
+++ lnt/trunk/tests/server/ui/test_api_modify.py Thu Jul 20 18:46:17 2017
@@ -16,7 +16,13 @@ import lnt.server.db.migrate
 import lnt.server.ui.app
 from V4Pages import check_json
 
-logging.basicConfig(level=logging.DEBUG)
+logging.basicConfig(level=logging.INFO)
+
+
+class _hashabledict(dict):
+    """See https://stackoverflow.com/questions/1151658."""
+    def __hash__(self):
+        return hash(tuple(sorted(self.items())))
 
 
 class JSONAPIDeleteTester(unittest.TestCase):
@@ -147,6 +153,37 @@ Deleted machine machine2:2
         resp_json = json.loads(resp.data)
         self.assertEqual(resp_json['run_id'], 5)
 
+    def test_04_merge_into(self):
+        """Check POST/merge into request for /machines."""
+        client = self.client
+
+        # Download existing machines.
+        machine_1 = check_json(client, 'api/db_default/v4/nts/machines/1')
+        machine_3 = check_json(client, 'api/db_default/v4/nts/machines/3')
+        # The test is boring if we don't have at least 1 run in each machine.
+        self.assertTrue(len(machine_1['runs']) > 0)
+        self.assertTrue(len(machine_3['runs']) > 0)
+
+        data = {
+            'action': 'merge',
+            'into': '3',
+        }
+        resp = client.post('api/db_default/v4/nts/machines/1', data=data,
+                           headers={'AuthToken': 'test_token'})
+        self.assertEqual(resp.status_code, 200)
+
+        # Old machine should have disappeared.
+        resp_2 = client.get('api/db_default/v4/nts/machines/1')
+        self.assertEqual(resp_2.status_code, 404)
+
+        # The other machine should have the union of all runs.
+        machine_1['runs'] = [_hashabledict(run) for run in machine_1['runs']]
+        machine_3['runs'] = [_hashabledict(run) for run in machine_3['runs']]
+        allruns = set(machine_1['runs']).union(machine_3['runs'])
+        resp_3 = check_json(client, 'api/db_default/v4/nts/machines/3')
+        resp_3['runs'] = [_hashabledict(run) for run in resp_3['runs']]
+        self.assertEqual(set(resp_3['runs']), allruns)
+
 
 if __name__ == '__main__':
     unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(x, y)




More information about the llvm-commits mailing list