summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSusmitha <susmitha@platform9.com>2016-06-29 13:32:19 -0700
committerSusmitha <susmitha@platform9.com>2016-06-29 13:36:41 -0700
commit141b307f68bf645c712128cd18b151e7a4c0770a (patch)
treeb2d55eb2302c9650ebbdfa974703f3df3ec65cad
parent2e3869feb25a1575447de186b9b39d2c8d8084bc (diff)
Fix issue with instance lease update when multiple rows presentrel_platform9-v2.2.1rel_platform9-v2.2.0platform9-v2.2.0
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.
-rw-r--r--mors/persistence.py7
-rw-r--r--test/test_persistence.py59
2 files changed, 50 insertions, 16 deletions
diff --git a/mors/persistence.py b/mors/persistence.py
index d7caa13..c439508 100644
--- a/mors/persistence.py
+++ b/mors/persistence.py
@@ -100,9 +100,10 @@ class DbPersistence:
100 @db_connect(transaction=True) 100 @db_connect(transaction=True)
101 def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at): 101 def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at):
102 logger.debug("Updating instance lease %s %s %s %s", instance_uuid, tenant_uuid, expiry, updated_by) 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, 103 conn.execute(self.instance_lease.update().\
104 expiry=expiry, 104 where(self.instance_lease.c.instance_uuid == instance_uuid).\
105 updated_at=updated_at, updated_by=updated_by) 105 values(tenant_uuid=tenant_uuid, expiry=expiry,
106 updated_at=updated_at, updated_by=updated_by))
106 107
107 @db_connect(transaction=True) 108 @db_connect(transaction=True)
108 def delete_instance_leases(self, conn, instance_uuids): 109 def delete_instance_leases(self, conn, instance_uuids):
diff --git a/test/test_persistence.py b/test/test_persistence.py
index 351e7c2..e1de110 100644
--- a/test/test_persistence.py
+++ b/test/test_persistence.py
@@ -3,7 +3,7 @@ from mors.persistence import DbPersistence
3import uuid 3import uuid
4import logging 4import logging
5from migrate.versioning.api import upgrade,create,version_control 5from migrate.versioning.api import upgrade,create,version_control
6from datetime import datetime 6from datetime import datetime, timedelta
7import os 7import os
8import shutil 8import shutil
9 9
@@ -39,6 +39,19 @@ def _verify_tenant_lease(tenants):
39 if "updated_at" in tenants[tenant].keys(): 39 if "updated_at" in tenants[tenant].keys():
40 assert (t_lease.updated_at == tenants[tenant]["updated_at"]) 40 assert (t_lease.updated_at == tenants[tenant]["updated_at"])
41 41
42def _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@test(depends_on=[setup_module]) 55@test(depends_on=[setup_module])
43def test_apis(): 56def test_apis():
44 tenants = {"tenant-1": { "user": "a@xyz.com", 57 tenants = {"tenant-1": { "user": "a@xyz.com",
@@ -63,22 +76,42 @@ def test_apis():
63 tenants[tenant]["updated_at"] = tenant_updated_date 76 tenants[tenant]["updated_at"] = tenant_updated_date
64 tenant_values = tenants[tenant] 77 tenant_values = tenants[tenant]
65 db_persistence.update_tenant_lease(tenant, tenant_values["expiry_mins"], 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 _verify_tenant_lease(tenants) 81 _verify_tenant_lease(tenants)
68 82
83 now = datetime.utcnow()
69 # Instance lease now 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 tenant_id = tenants.keys()[0] 95 tenant_id = tenants.keys()[0]
72 now = datetime.utcnow() 96 now = datetime.utcnow()
73 for instance_uuid in instance_uuids: 97 for instance_uuid in instances:
74 db_persistence.add_instance_lease(instance_uuid, tenant_id, now, tenants[tenant_id]["user"], now) 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: 111 for instance_uuid in instances:
77 logger.debug("1" + str(tenant_id)) 112 instance = instances[instance_uuid]
78 logger.debug("2"+ str(instance_uuid)) 113 db_persistence.update_instance_lease(instance['instance_uuid'], instance['tenant_uuid'],
79 i_lease = db_persistence.get_instance_lease(instance_uuid) 114 instance['expiry'], instance["created_by"],
80 assert (i_lease.instance_uuid == instance_uuid) 115 instance["updated_at"])
81 assert (i_lease.tenant_uuid == tenant_id)
82 assert (i_lease.expiry == now)
83 assert (i_lease.created_by == tenants[tenant_id]["user"])
84 116
117 _verify_instance_lease(instances)