Browse Source

Fix issue with instance lease update when multiple rows present

Added a functional test for the scenario. When more than two rows
exist in instance_lease, the update to one VM's instance lease
updates all rows instead of just one row.
Susmitha 2 years ago
parent
commit
141b307f68
2 changed files with 50 additions and 16 deletions
  1. 4
    3
      mors/persistence.py
  2. 46
    13
      test/test_persistence.py

+ 4
- 3
mors/persistence.py View File

@@ -100,9 +100,10 @@ class DbPersistence:
100 100
     @db_connect(transaction=True)
101 101
     def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at):
102 102
         logger.debug("Updating instance lease %s %s %s %s", instance_uuid, tenant_uuid, expiry, updated_by)
103
-        conn.execute(self.instance_lease.update(instance_uuid == instance_uuid), tenant_uuid=tenant_uuid,
104
-                     expiry=expiry,
105
-                     updated_at=updated_at, updated_by=updated_by)
103
+        conn.execute(self.instance_lease.update().\
104
+                        where(self.instance_lease.c.instance_uuid == instance_uuid).\
105
+                        values(tenant_uuid=tenant_uuid, expiry=expiry,
106
+                               updated_at=updated_at, updated_by=updated_by))
106 107
 
107 108
     @db_connect(transaction=True)
108 109
     def delete_instance_leases(self, conn, instance_uuids):

+ 46
- 13
test/test_persistence.py View File

@@ -3,7 +3,7 @@ from mors.persistence import DbPersistence
3 3
 import uuid
4 4
 import logging
5 5
 from migrate.versioning.api import upgrade,create,version_control
6
-from datetime import datetime
6
+from datetime import datetime, timedelta
7 7
 import os
8 8
 import shutil
9 9
 
@@ -39,6 +39,19 @@ def _verify_tenant_lease(tenants):
39 39
         if "updated_at" in tenants[tenant].keys():
40 40
             assert (t_lease.updated_at == tenants[tenant]["updated_at"])
41 41
 
42
+def _verify_instance_lease(instances):
43
+    for instance_uuid in instances:
44
+        instance = instances[instance_uuid]
45
+        i_lease = db_persistence.get_instance_lease(instance['instance_uuid'])
46
+        assert (i_lease.instance_uuid == instance['instance_uuid'])
47
+        assert (i_lease.tenant_uuid == instance['tenant_uuid'])
48
+        assert (i_lease.expiry == instance['expiry'])
49
+        assert (i_lease.created_by == instance["created_by"])
50
+        if "updated_by" in instance.keys():
51
+            assert (i_lease.updated_by == instance["updated_by"])
52
+        if "updated_at" in instance.keys():
53
+            assert (i_lease.updated_at == instance["updated_at"])
54
+
42 55
 @test(depends_on=[setup_module])
43 56
 def test_apis():
44 57
     tenants = {"tenant-1": { "user": "a@xyz.com",
@@ -63,22 +76,42 @@ def test_apis():
63 76
         tenants[tenant]["updated_at"] = tenant_updated_date
64 77
         tenant_values = tenants[tenant]
65 78
         db_persistence.update_tenant_lease(tenant, tenant_values["expiry_mins"],
66
-                                       tenant_values["updated_by"], tenant_values["updated_at"])
79
+                                           tenant_values["updated_by"],
80
+                                           tenant_values["updated_at"])
67 81
     _verify_tenant_lease(tenants)
68 82
 
83
+    now = datetime.utcnow()
69 84
     # Instance lease now
70
-    instance_uuids = [str(uuid.uuid4()), str(uuid.uuid4())]
85
+    instances = {"instance-1": {"instance_uuid": "instance-1",
86
+                                "tenant_uuid": "tenant-1",
87
+                                "expiry": now,
88
+                                "created_at": now,
89
+                                "created_by": "d@xyz.com"},
90
+                 "instance-2": {"instance_uuid": "instance-2",
91
+                                "tenant_uuid": "tenant-2",
92
+                                "expiry": now + timedelta(seconds=60),
93
+                                "created_at": now + timedelta(seconds=60),
94
+                                "created_by": "e@xyz.com"}}
71 95
     tenant_id = tenants.keys()[0]
72 96
     now = datetime.utcnow()
73
-    for instance_uuid in instance_uuids:
74
-        db_persistence.add_instance_lease(instance_uuid, tenant_id, now, tenants[tenant_id]["user"], now)
97
+    for instance_uuid in instances:
98
+        instance = instances[instance_uuid]
99
+        db_persistence.add_instance_lease(instance['instance_uuid'], instance['tenant_uuid'],
100
+                                          instance['expiry'], instance["created_by"],
101
+                                          instance["created_at"])
102
+    _verify_instance_lease(instances)
103
+
104
+    newtime1 = datetime.utcnow() + timedelta(seconds=240)
105
+    newtime2 = datetime.utcnow() + timedelta(seconds=120)
106
+    instances['instance-1']['expiry'] = newtime1
107
+    instances['instance-1']['updated_at'] = newtime1
108
+    instances['instance-2']['expiry'] = newtime2
109
+    instances['instance-2']['updated_at'] = newtime2
75 110
 
76
-    for instance_uuid in instance_uuids:
77
-        logger.debug("1" + str(tenant_id))
78
-        logger.debug("2"+ str(instance_uuid)) 
79
-        i_lease = db_persistence.get_instance_lease(instance_uuid)
80
-        assert (i_lease.instance_uuid == instance_uuid)
81
-        assert (i_lease.tenant_uuid == tenant_id)
82
-        assert (i_lease.expiry == now)
83
-        assert (i_lease.created_by == tenants[tenant_id]["user"])
111
+    for instance_uuid in instances:
112
+        instance = instances[instance_uuid]
113
+        db_persistence.update_instance_lease(instance['instance_uuid'], instance['tenant_uuid'],
114
+                                             instance['expiry'], instance["created_by"],
115
+                                             instance["updated_at"])
84 116
 
117
+    _verify_instance_lease(instances)

Loading…
Cancel
Save