def test_read(self): """Test _read of engine.CgroupReader""" engine_obj = engine.CgroupReader(self.root, 0) self.assertEqual( set(engine_obj.cache['treadmill'].keys()), set(['core', 'apps', 'treadmill']) ) self.assertEqual( set(engine_obj.cache['core'].keys()), set(['fw', 'eventd']) ) self.assertEqual( set(engine_obj.cache['app'].keys()), set(['foo']) ) self.assertEqual( set(engine_obj.list()), set(['treadmill.core', 'treadmill.apps', 'treadmill.treadmill', 'core.eventd', 'core.fw', 'app.foo']) )
def __init__(self, app_root=None, cgroup_prefix=None): reader = engine.CgroupReader(app_root, cgroup_prefix) def system(path, *paths): """Get aggregated system-level cgroup value.""" return reader.read_system(path, *paths) def service(svc): """Get treadmill core service cgroup value.""" return reader.read_service(svc) def services(detail=False): """Get all treadmill core service cgroup names along with values. """ return reader.read_services(detail=detail) def app(name): """Get treadmill app cgroup value.""" return reader.read_app(name) def apps(detail=False): """Get all treadmill app cgroup names along with values.""" return reader.read_apps(detail=detail) self.system = system self.service = service self.services = services self.app = app self.apps = apps
def test_read_service(self): """Test read_service of engine.CgroupReader""" for cgroup_prefix in ['treadmill', 'tmprumsnf_y']: self.app_metrics.reset_mock() reader = engine.CgroupReader(self.root, cgroup_prefix) reader.read_service('fw') self.app_metrics.assert_called_once_with( '{0}/core/fw'.format(cgroup_prefix), None )
def test_read_app(self): """Test read_app of engine.CgroupReader""" for cgroup_prefix in ['treadmill', 'tmp5s8o17g5']: self.app_metrics.reset_mock() reader = engine.CgroupReader(self.root, cgroup_prefix) names = reader.read_app('foo') self.app_metrics.assert_called_once_with( '{0}/apps/foo'.format(cgroup_prefix), '/dev/foo' )
def test_read_apps(self): """Test read_apps of engine.CgroupReader""" for cgroup_prefix in ['treadmill', 'tmphqy2ut38']: self.app_metrics.reset_mock() reader = engine.CgroupReader(self.root, cgroup_prefix) snapshot = reader.read_apps(detail=True) self.assertListEqual(list(snapshot), ['foo']) self.app_metrics.assert_called_once_with( '{0}/apps/foo'.format(cgroup_prefix), '/dev/foo' )
def init(api, cors, **kwargs): """Init cgroup API""" app_root = os.environ['TREADMILL_APPROOT'] # default interval is 60 interval = kwargs['interval'] _ENGINE['cgroup'] = engine.CgroupReader(app_root, interval) namespace = api.namespace( 'cgroup', description='Cgroup fetching REST API' ) @namespace.route('/') class _CgroupList(restplus.Resource): """Treadmill cgroup API""" @webutils.get_api( api, cors, marshal=api.marshal_list_with, ) def get(self): """Get all cgroups in the list""" # get all cgroups values return _ENGINE['cgroup'].list() @namespace.route('/_bulk') class _CgroupBulk(restplus.Resource): """Treadmill cgroup bulk API""" @webutils.get_api( api, cors, marshal=api.marshal_with, ) def get(self): """Get the whole cgroups values""" return _ENGINE['cgroup'].snapshot() @namespace.route('/<cgroup>') @api.doc(params={'cgroup': 'Cgroup name'}) class _CgroupResource(restplus.Resource): """Treadmill cgroup resource API""" @webutils.get_api( api, cors, marshal=api.marshal_with, ) def get(self, cgroup): """Get cgroups values.""" return _ENGINE['cgroup'].get(cgroup) return 'cgroup'
def test_read_services(self): """Test read_services of engine.CgroupReader""" for cgroup_prefix in ['treadmill', 'tmp9c2q8lj2']: self.app_metrics.reset_mock() reader = engine.CgroupReader(self.root, cgroup_prefix) snapshot = reader.read_services(detail=True) self.assertSetEqual(set(snapshot.keys()), {'eventd', 'fw'}) self.app_metrics.assert_has_calls( [ mock.call('{0}/core/fw'.format(cgroup_prefix), None), mock.call('{0}/core/eventd'.format(cgroup_prefix), None), ], any_order=True )
def test_read_system(self): """Test read_system of engine.CgroupReader""" # default & configurable cgroup prefixes for cgroup_prefix in ['treadmill', 'tmpcz4qr_nl']: self.app_metrics.reset_mock() reader = engine.CgroupReader(self.root, cgroup_prefix) reader.read_system('treadmill') self.app_metrics.assert_called_once_with( cgroup_prefix, '/dev/sda3' ) self.app_metrics.reset_mock() reader.read_system('system.slice') self.app_metrics.assert_called_once_with( 'system.slice', '/dev/sda3' )