ceph/qa/tasks/mgr/test_crash.py
Kefu Chai d7258ea7fd qa/tasks: use next(iter(..)) for accessing first element in a view
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer index.

there are three use cases when we access these views in python3:

1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
   the view
4. iterate thru the view

in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.

in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.

in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.

in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.

Signed-off-by: Kefu Chai <kchai@redhat.com>
2020-04-07 20:33:47 +08:00

109 lines
3.3 KiB
Python

import json
import logging
import datetime
from .mgr_test_case import MgrTestCase
log = logging.getLogger(__name__)
UUID = 'd5775432-0742-44a3-a435-45095e32e6b1'
DATEFMT = '%Y-%m-%d %H:%M:%S.%f'
class TestCrash(MgrTestCase):
def setUp(self):
super(TestCrash, self).setUp()
self.setup_mgrs()
self._load_module('crash')
# Whip up some crash data
self.crashes = dict()
now = datetime.datetime.utcnow()
for i in (0, 1, 3, 4, 8):
timestamp = now - datetime.timedelta(days=i)
timestamp = timestamp.strftime(DATEFMT) + 'Z'
crash_id = '_'.join((timestamp, UUID)).replace(' ', '_')
self.crashes[crash_id] = {
'crash_id': crash_id, 'timestamp': timestamp,
}
self.assertEqual(
0,
self.mgr_cluster.mon_manager.raw_cluster_cmd_result(
'crash', 'post', '-i', '-',
stdin=json.dumps(self.crashes[crash_id]),
)
)
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'ls',
)
log.warning("setUp: crash ls returns %s" % retstr)
self.oldest_crashid = crash_id
def tearDown(self):
for crash in self.crashes.values():
self.mgr_cluster.mon_manager.raw_cluster_cmd_result(
'crash', 'rm', crash['crash_id']
)
def test_info(self):
for crash in self.crashes.values():
log.warning('test_info: crash %s' % crash)
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'ls'
)
log.warning('ls output: %s' % retstr)
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'info', crash['crash_id'],
)
log.warning('crash info output: %s' % retstr)
crashinfo = json.loads(retstr)
self.assertIn('crash_id', crashinfo)
self.assertIn('timestamp', crashinfo)
def test_ls(self):
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'ls',
)
for crash in self.crashes.values():
self.assertIn(crash['crash_id'], retstr)
def test_rm(self):
crashid = next(iter(self.crashes.keys()))
self.assertEqual(
0,
self.mgr_cluster.mon_manager.raw_cluster_cmd_result(
'crash', 'rm', crashid,
)
)
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'ls',
)
self.assertNotIn(crashid, retstr)
def test_stat(self):
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'stat',
)
self.assertIn('5 crashes recorded', retstr)
self.assertIn('4 older than 1 days old:', retstr)
self.assertIn('3 older than 3 days old:', retstr)
self.assertIn('1 older than 7 days old:', retstr)
def test_prune(self):
self.assertEqual(
0,
self.mgr_cluster.mon_manager.raw_cluster_cmd_result(
'crash', 'prune', '5'
)
)
retstr = self.mgr_cluster.mon_manager.raw_cluster_cmd(
'crash', 'ls',
)
self.assertNotIn(self.oldest_crashid, retstr)