1307
1307
def test_aggregate_get_by_host(self):
1308
1308
ctxt = context.get_admin_context()
1309
values = {'name': 'fake_aggregate2'}
1309
values2 = {'name': 'fake_aggregate2'}
1310
values3 = {'name': 'fake_aggregate3'}
1311
values4 = {'name': 'fake_aggregate4'}
1312
values5 = {'name': 'fake_aggregate5'}
1310
1313
a1 = _create_aggregate_with_hosts(context=ctxt)
1311
a2 = _create_aggregate_with_hosts(context=ctxt, values=values)
1314
a2 = _create_aggregate_with_hosts(context=ctxt, values=values2)
1315
# a3 has no hosts and should not be in the results.
1316
a3 = _create_aggregate(context=ctxt, values=values3)
1317
# a4 has no matching hosts.
1318
a4 = _create_aggregate_with_hosts(context=ctxt, values=values4,
1319
hosts=['foo4.openstack.org'])
1320
# a5 has no matching hosts after deleting the only matching host.
1321
a5 = _create_aggregate_with_hosts(context=ctxt, values=values5,
1322
hosts=['foo5.openstack.org', 'foo.openstack.org'])
1323
db.aggregate_host_delete(ctxt, a5['id'],
1324
'foo.openstack.org')
1312
1325
r1 = db.aggregate_get_by_host(ctxt, 'foo.openstack.org')
1313
1326
self.assertEqual([a1['id'], a2['id']], [x['id'] for x in r1])
1315
1328
def test_aggregate_get_by_host_with_key(self):
1316
1329
ctxt = context.get_admin_context()
1317
values = {'name': 'fake_aggregate2'}
1330
values2 = {'name': 'fake_aggregate2'}
1331
values3 = {'name': 'fake_aggregate3'}
1332
values4 = {'name': 'fake_aggregate4'}
1318
1333
a1 = _create_aggregate_with_hosts(context=ctxt,
1319
1334
metadata={'goodkey': 'good'})
1320
a2 = _create_aggregate_with_hosts(context=ctxt, values=values)
1335
_create_aggregate_with_hosts(context=ctxt, values=values2)
1336
_create_aggregate(context=ctxt, values=values3)
1337
_create_aggregate_with_hosts(context=ctxt, values=values4,
1338
hosts=['foo4.openstack.org'], metadata={'goodkey': 'bad'})
1321
1339
# filter result by key
1322
1340
r1 = db.aggregate_get_by_host(ctxt, 'foo.openstack.org', key='goodkey')
1323
1341
self.assertEqual([a1['id']], [x['id'] for x in r1])
1337
1355
def test_aggregate_metadata_get_by_host_with_key(self):
1338
1356
ctxt = context.get_admin_context()
1339
values = {'name': 'fake_aggregate2'}
1340
values2 = {'name': 'fake_aggregate3'}
1357
values2 = {'name': 'fake_aggregate12'}
1358
values3 = {'name': 'fake_aggregate23'}
1359
a2_hosts = ['foo1.openstack.org', 'foo2.openstack.org']
1360
a2_metadata = {'good': 'value12', 'bad': 'badvalue12'}
1361
a3_hosts = ['foo2.openstack.org', 'foo3.openstack.org']
1362
a3_metadata = {'good': 'value23', 'bad': 'badvalue23'}
1341
1363
a1 = _create_aggregate_with_hosts(context=ctxt)
1342
a2 = _create_aggregate_with_hosts(context=ctxt, values=values)
1343
a3 = _create_aggregate_with_hosts(context=ctxt, values=values2,
1344
hosts=['foo.openstack.org'], metadata={'good': 'value'})
1345
r1 = db.aggregate_metadata_get_by_host(ctxt, 'foo.openstack.org',
1364
a2 = _create_aggregate_with_hosts(context=ctxt, values=values2,
1365
hosts=a2_hosts, metadata=a2_metadata)
1366
a3 = _create_aggregate_with_hosts(context=ctxt, values=values3,
1367
hosts=a3_hosts, metadata=a3_metadata)
1368
r1 = db.aggregate_metadata_get_by_host(ctxt, 'foo2.openstack.org',
1347
self.assertEqual(r1['good'], set(['value']))
1370
self.assertEqual(r1['good'], set(['value12', 'value23']))
1348
1371
self.assertFalse('fake_key1' in r1)
1372
self.assertFalse('bad' in r1)
1349
1373
# Delete metadata
1350
1374
db.aggregate_metadata_delete(ctxt, a3['id'], 'good')
1351
1375
r2 = db.aggregate_metadata_get_by_host(ctxt, 'foo.openstack.org',
1355
1379
def test_aggregate_host_get_by_metadata_key(self):
1356
1380
ctxt = context.get_admin_context()
1357
values = {'name': 'fake_aggregate2'}
1358
values2 = {'name': 'fake_aggregate3'}
1381
values2 = {'name': 'fake_aggregate12'}
1382
values3 = {'name': 'fake_aggregate23'}
1383
a2_hosts = ['foo1.openstack.org', 'foo2.openstack.org']
1384
a2_metadata = {'good': 'value12', 'bad': 'badvalue12'}
1385
a3_hosts = ['foo2.openstack.org', 'foo3.openstack.org']
1386
a3_metadata = {'good': 'value23', 'bad': 'badvalue23'}
1359
1387
a1 = _create_aggregate_with_hosts(context=ctxt)
1360
a2 = _create_aggregate_with_hosts(context=ctxt, values=values)
1361
a3 = _create_aggregate_with_hosts(context=ctxt, values=values2,
1362
hosts=['foo.openstack.org'], metadata={'good': 'value'})
1388
a2 = _create_aggregate_with_hosts(context=ctxt, values=values2,
1389
hosts=a2_hosts, metadata=a2_metadata)
1390
a3 = _create_aggregate_with_hosts(context=ctxt, values=values3,
1391
hosts=a3_hosts, metadata=a3_metadata)
1363
1392
r1 = db.aggregate_host_get_by_metadata_key(ctxt, key='good')
1364
self.assertEqual(r1, {'foo.openstack.org': set(['value'])})
1394
'foo1.openstack.org': set(['value12']),
1395
'foo2.openstack.org': set(['value12', 'value23']),
1396
'foo3.openstack.org': set(['value23']),
1365
1398
self.assertFalse('fake_key1' in r1)
1367
1400
def test_aggregate_get_by_host_not_found(self):
2092
2135
self.assertEqual(len(vol_usages), 2)
2093
2136
_compare(vol_usages[0], expected_vol_usages[0])
2094
2137
_compare(vol_usages[1], expected_vol_usages[1])
2095
timeutils.clear_time_override()
2097
2139
def test_vol_usage_update_totals_update(self):
2098
2140
ctxt = context.get_admin_context()
2099
2141
now = timeutils.utcnow()
2100
timeutils.set_time_override(now)
2101
2142
start_time = now - datetime.timedelta(seconds=10)
2144
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=100, rd_bytes=200,
2145
wr_req=300, wr_bytes=400,
2147
current_usage = db.vol_get_usage_by_time(ctxt, start_time)[0]
2148
self.assertEqual(current_usage['tot_reads'], 0)
2149
self.assertEqual(current_usage['curr_reads'], 100)
2151
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=200, rd_bytes=300,
2152
wr_req=400, wr_bytes=500,
2155
current_usage = db.vol_get_usage_by_time(ctxt, start_time)[0]
2156
self.assertEqual(current_usage['tot_reads'], 200)
2157
self.assertEqual(current_usage['curr_reads'], 0)
2159
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=300, rd_bytes=400,
2160
wr_req=500, wr_bytes=600,
2162
current_usage = db.vol_get_usage_by_time(ctxt, start_time)[0]
2163
self.assertEqual(current_usage['tot_reads'], 200)
2164
self.assertEqual(current_usage['curr_reads'], 300)
2166
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=400, rd_bytes=500,
2167
wr_req=600, wr_bytes=700,
2171
vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
2102
2173
expected_vol_usages = {'volume_id': u'1',
2103
2174
'tot_reads': 600,
2104
2175
'tot_read_bytes': 800,
2109
2180
'curr_writes': 0,
2110
2181
'curr_write_bytes': 0}
2112
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=100, rd_bytes=200,
2113
wr_req=300, wr_bytes=400,
2115
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=200, rd_bytes=300,
2116
wr_req=400, wr_bytes=500,
2119
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=300, rd_bytes=400,
2120
wr_req=500, wr_bytes=600,
2122
vol_usage = db.vol_usage_update(ctxt, 1, rd_req=400, rd_bytes=500,
2123
wr_req=600, wr_bytes=700,
2127
vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
2129
2183
self.assertEquals(1, len(vol_usages))
2130
2184
for key, value in expected_vol_usages.items():
2131
2185
self.assertEqual(vol_usages[0][key], value)
2132
timeutils.clear_time_override()
2187
def test_vol_usage_update_when_blockdevicestats_reset(self):
2188
ctxt = context.get_admin_context()
2189
now = timeutils.utcnow()
2190
start_time = now - datetime.timedelta(seconds=10)
2192
vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
2193
self.assertEqual(len(vol_usages), 0)
2195
db.vol_usage_update(ctxt, 1,
2196
rd_req=10000, rd_bytes=20000,
2197
wr_req=30000, wr_bytes=40000,
2200
# Instance rebooted or crashed. block device stats were reset and are
2201
# less then the previous values
2202
db.vol_usage_update(ctxt, 1,
2203
rd_req=100, rd_bytes=200,
2204
wr_req=300, wr_bytes=400,
2207
db.vol_usage_update(ctxt, 1,
2208
rd_req=200, rd_bytes=300,
2209
wr_req=400, wr_bytes=500,
2212
vol_usage = db.vol_get_usage_by_time(ctxt, start_time)[0]
2213
expected_vol_usage = {'volume_id': u'1',
2215
'curr_read_bytes': 300,
2217
'curr_write_bytes': 500,
2219
'tot_read_bytes': 20000,
2220
'tot_writes': 30000,
2221
'tot_write_bytes': 40000}
2222
for key, value in expected_vol_usage.items():
2223
self.assertEqual(vol_usage[key], value, key)
2225
def test_vol_usage_update_totals_update_when_blockdevicestats_reset(self):
2226
# This is unlikely to happen, but could when a volume is detached
2227
# right after a instance has rebooted / recovered and before
2228
# the system polled and updated the volume usage cache table.
2229
ctxt = context.get_admin_context()
2230
now = timeutils.utcnow()
2231
start_time = now - datetime.timedelta(seconds=10)
2233
vol_usages = db.vol_get_usage_by_time(ctxt, start_time)
2234
self.assertEqual(len(vol_usages), 0)
2236
db.vol_usage_update(ctxt, 1,
2237
rd_req=10000, rd_bytes=20000,
2238
wr_req=30000, wr_bytes=40000,
2241
# Instance rebooted or crashed. block device stats were reset and are
2242
# less then the previous values
2243
db.vol_usage_update(ctxt, 1,
2244
rd_req=100, rd_bytes=200,
2245
wr_req=300, wr_bytes=400,
2249
vol_usage = db.vol_get_usage_by_time(ctxt, start_time)[0]
2250
expected_vol_usage = {'volume_id': u'1',
2252
'curr_read_bytes': 0,
2254
'curr_write_bytes': 0,
2256
'tot_read_bytes': 20200,
2257
'tot_writes': 30300,
2258
'tot_write_bytes': 40400}
2259
for key, value in expected_vol_usage.items():
2260
self.assertEqual(vol_usage[key], value, key)
2135
2263
class TaskLogTestCase(test.TestCase):