class WhenTestingWorkerUtil(unittest.TestCase): def setUp(self): self.system_info = SystemInfo().format() self.worker = Worker( **{ "hostname": "worker-01", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "worker", "status": "online", "system_info": self.system_info }) def test_create_worker_calls_db_put(self): put_method = MagicMock() with patch('meniscus.data.model.worker_util._db_handler.put', put_method): worker_util.create_worker(self.worker) put_method.assert_called_once_with('worker', self.worker.format()) def test_find_worker_returns_worker(self): find_one_method = MagicMock(return_value=self.worker.format()) with patch('meniscus.data.model.worker_util._db_handler.find_one', find_one_method): worker = worker_util.find_worker(self.worker.hostname) find_one_method.assert_called_once_with( 'worker', {'hostname': self.worker.hostname}) self.assertIsInstance(worker, Worker) def test_find_worker_returns_none(self): find_one_method = MagicMock(return_value=None) with patch('meniscus.data.model.worker_util._db_handler.find_one', find_one_method): worker = worker_util.find_worker(self.worker.hostname) find_one_method.assert_called_once_with( 'worker', {'hostname': self.worker.hostname}) self.assertIsNone(worker) def test_save_worker(self): update_method = MagicMock(return_value=None) with patch('meniscus.data.model.worker_util._db_handler.update', update_method): worker_util.save_worker(self.worker) update_method.assert_called_once_with( 'worker', self.worker.format_for_save()) def test_retrieve_all_workers(self): find_method = MagicMock(return_value=[self.worker.format_for_save()]) with patch('meniscus.data.model.worker_util._db_handler.find', find_method): workers = worker_util.retrieve_all_workers() find_method.assert_called_once_with('worker') for worker in workers: self.assertIsInstance(worker, Worker)
class WhenTestingWorkerUtil(unittest.TestCase): def setUp(self): self.system_info = SystemInfo().format() self.worker = Worker(**{"hostname": "worker-01", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "worker", "status": "online", "system_info": self.system_info}) def test_create_worker_calls_db_put(self): put_method = MagicMock() with patch('meniscus.data.model.worker_util._db_handler.put', put_method): worker_util.create_worker(self.worker) put_method.assert_called_once_with('worker', self.worker.format()) def test_find_worker_returns_worker(self): find_one_method = MagicMock(return_value=self.worker.format()) with patch('meniscus.data.model.worker_util._db_handler.find_one', find_one_method): worker = worker_util.find_worker(self.worker.hostname) find_one_method.assert_called_once_with( 'worker', {'hostname': self.worker.hostname}) self.assertIsInstance(worker, Worker) def test_find_worker_returns_none(self): find_one_method = MagicMock(return_value=None) with patch('meniscus.data.model.worker_util._db_handler.find_one', find_one_method): worker = worker_util.find_worker(self.worker.hostname) find_one_method.assert_called_once_with( 'worker', {'hostname': self.worker.hostname}) self.assertIsNone(worker) def test_save_worker(self): update_method = MagicMock(return_value=None) with patch('meniscus.data.model.worker_util._db_handler.update', update_method): worker_util.save_worker(self.worker) update_method.assert_called_once_with( 'worker', self.worker.format_for_save()) def test_retrieve_all_workers(self): find_method = MagicMock(return_value=[self.worker.format_for_save()]) with patch('meniscus.data.model.worker_util._db_handler.find', find_method): workers = worker_util.retrieve_all_workers() find_method.assert_called_once_with('worker') for worker in workers: self.assertIsInstance(worker, Worker)
def on_put(self, req, resp, worker_id): """ updates a worker's status """ #load json payload in body body = load_body(req) self._validate_req_body_on_put(body) #find the worker in db worker_dict = self.db.find_one('worker', {'worker_id': worker_id}) if not worker_dict: _worker_not_found() worker = Worker(**worker_dict) if 'worker_status' in body: worker.status = body['worker_status'] if 'disk_usage' in body: worker.system_info.disk_usage = body['disk_usage'] if 'load_average' in body: worker.system_info.load_average = body['load_average'] self.db.update('worker', worker.format_for_save()) resp.status = falcon.HTTP_200
class WhenTestingWorkerObject(unittest.TestCase): def setUp(self): self.system_info = SystemInfo() self.test_worker = Worker(_id='010101', worker_token='9876543210', hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='worker', status='new', system_info=self.system_info.format()) self.test_worker_lite = Worker(hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='worker', status='new', system_info=self.system_info.format()) self.worker_status = self.test_worker.get_status() def test_new_worker_format_for_save(self): self.assertTrue('_id' in self.test_worker.format_for_save()) def test_get_status(self): self.assertEqual(self.worker_status['hostname'], 'worker01') self.assertEqual(self.worker_status['personality'], 'worker') self.assertEqual(self.worker_status['status'], 'new') self.assertEqual(self.worker_status['system_info'], self.system_info.format())
class WhenTestingWorkerObject(unittest.TestCase): def setUp(self): self.system_info = SystemInfo() self.test_worker = Worker(_id='010101', worker_token='9876543210', hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='worker', status='new', system_info=self.system_info.format()) self.test_worker_lite = Worker(hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='worker', status='new', system_info=self.system_info.format()) self.worker_status = self.test_worker.get_status() def test_new_worker_format_for_save(self): self.assertTrue('_id' in self.test_worker.format_for_save()) def test_get_status(self): self.assertEqual(self.worker_status['hostname'], 'worker01') self.assertEqual(self.worker_status['personality'], 'worker') self.assertEqual(self.worker_status['status'], 'new') self.assertEqual(self.worker_status['system_info'], self.system_info.format())
class WhenTestingWorkerObject(unittest.TestCase): def setUp(self): self.system_info = SystemInfo() self.test_worker = Worker(_id='010101', worker_id='0123456789', worker_token='9876543210', hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='correlation', status='new', system_info=self.system_info.format()) self.test_worker_lite = Worker(hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='correlation', status='new', system_info=self.system_info.format()) self.worker_route = self.test_worker.get_route_info() self.worker_status = self.test_worker.get_status() def test_new_worker_no_ids(self): worker_dict = self.test_worker_lite.format() self.assertTrue('worker_id' in worker_dict) self.assertTrue('worker_token' in worker_dict) def test_new_worker_format_for_save(self): self.assertTrue('_id' in self.test_worker.format_for_save()) def test_new_worker_get_registration_identity(self): self.assertTrue('personality_module' in self.test_worker.get_registration_identity()) def test_new_worker_routes(self): self.assertEqual(self.worker_route['worker_id'], '0123456789') self.assertEqual(self.worker_route['ip_address_v4'], '172.23.1.100') self.assertEqual(self.worker_route['ip_address_v6'], '::1') def test_get_status(self): self.assertEqual(self.worker_status['hostname'], 'worker01') self.assertEqual(self.worker_status['worker_id'], '0123456789') self.assertEqual(self.worker_status['personality'], 'correlation') self.assertEqual(self.worker_status['status'], 'new') self.assertEqual(self.worker_status['system_info'], self.system_info.format()) self.assertEqual(self.worker_route['ip_address_v4'], '172.23.1.100') self.assertEqual(self.worker_route['ip_address_v6'], '::1')
class WhenTestingWorkerObject(unittest.TestCase): def setUp(self): self.system_info = SystemInfo() self.test_worker = Worker(_id='010101', worker_id='0123456789', worker_token='9876543210', hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='correlation', status='new', system_info=self.system_info.format()) self.test_worker_lite = Worker(hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='correlation', status='new', system_info=self.system_info.format()) self.worker_route = self.test_worker.get_route_info() self.worker_status = self.test_worker.get_status() def test_new_worker_no_ids(self): worker_dict = self.test_worker_lite.format() self.assertTrue('worker_id' in worker_dict) self.assertTrue('worker_token' in worker_dict) def test_new_worker_format_for_save(self): self.assertTrue('_id' in self.test_worker.format_for_save()) def test_new_worker_get_registration_identity(self): self.assertTrue('personality_module' in self.test_worker.get_registration_identity()) def test_new_worker_routes(self): self.assertEqual(self.worker_route['worker_id'], '0123456789') self.assertEqual(self.worker_route['ip_address_v4'], '172.23.1.100') self.assertEqual(self.worker_route['ip_address_v6'], '::1') def test_get_status(self): self.assertEqual(self.worker_status['hostname'], 'worker01') self.assertEqual(self.worker_status['worker_id'], '0123456789') self.assertEqual(self.worker_status['personality'], 'correlation') self.assertEqual(self.worker_status['status'], 'new') self.assertEqual(self.worker_status['system_info'], self.system_info.format()) self.assertEqual(self.worker_route['ip_address_v4'], '172.23.1.100') self.assertEqual(self.worker_route['ip_address_v6'], '::1')
def on_put(self, req, resp, worker_id, validated_body): """ updates a worker's status """ #load validated json payload in body body = validated_body['worker_status'] #find the worker in db worker_dict = self.db.find_one('worker', {'worker_id': worker_id}) if not worker_dict: _worker_not_found() worker = Worker(**worker_dict) if 'status' in body: worker.status = body['status'] if 'system_info' in body: worker.system_info = SystemInfo(**body['system_info']) self.db.update('worker', worker.format_for_save()) resp.status = falcon.HTTP_200
def on_put(self, req, resp, worker_id, validated_body): """ updates a worker's status """ #load validated json payload in body body = validated_body['worker_status'] #find the worker in db worker_dict = self.db.find_one('worker', {'worker_id': worker_id}) if not worker_dict: _worker_not_found() worker = Worker(**worker_dict) if 'status' in body: worker.status = body['status'] if 'system_info' in body: worker.system_info = SystemInfo(**body['system_info']) self.db.update('worker', worker.format_for_save()) resp.status = falcon.HTTP_200
class WhenTestingCoordinatorFlow(unittest.TestCase): def setUp(self): self.db_handler = MagicMock() self.worker_id = "0123456789" self.req = MagicMock() self.resp = MagicMock() self.body_bad_header = {'worker': WorkerRegistration('correlation').format()} self.body_bad_personality = {'worker_registration': WorkerRegistration( 'bad_personality').format()} self.new_status = 'offline' self.new_bad_status = 'bad_status' self.system_info = SystemInfo() self.worker_dict = {"worker_id": self.worker_id, "hostname": "worker-01", "callback": "192.168.100.101:8080/v1/callback/", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "worker.storage", "status": "draining", "system_info": { "os_type": "Darwin-11.4.2-x86_64-i386-64bit", "memory_mb": "1024", "architecture": "", "cpu_cores": "4", "load_average": "0.234353456", "disk_usage": { "/dev/sda1": { "total": 313764528, "used": 112512436 } } } } self.worker = Worker(_id='010101', worker_id=self.worker_id, worker_token='9876543210', hostname='worker01', callback='172.22.15.25:8080/v1/config/', ip_address_v4='172.23.1.100', ip_address_v6='::1', personality='correlation', status='online', system_info=self.system_info.format()) self.worker_list = [ {"hostname": "worker-01", "callback": "192.168.100.101:8080/v1/callback/", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "storage", "status": "draining", "system_info": { "os_type": "Darwin-11.4.2-x86_64-i386-64bit", "memory_mb": "1024", "architecture": "", "cpu_cores": "4", "load_average": "0.234353456", "disk_usage": { "/dev/sda1": { "total": 313764528, "used": 112512436 }}}}, {"hostname": "worker-01", "callback": "192.168.100.101:8080/v1/callback/", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "storage", "status": "online", "system_info": { "os_type": "Darwin-11.4.2-x86_64-i386-64bit", "memory_mb": "1024", "architecture": "", "cpu_cores": "4", "load_average": "0.234353456", "disk_usage": { "/dev/sda1": { "total": 313764528, "used": 112512436 }}}}, {'hostname': "worker-01", "callback": "192.168.100.101:8080/v1/callback/", "ip_address_v4": "192.168.100.101", "ip_address_v6": "::1", "personality": "normalization", "status": "draining", "system_info": { "os_type": "Darwin-11.4.2-x86_64-i386-64bit", "memory_mb": "1024", "architecture": "", "cpu_cores": "4", "load_average": "0.234353456", "disk_usage": { "/dev/sda1": { "total": 313764528, "used": 112512436 }}}}] def test_add_worker(self): db_handler = MagicMock() db_handler.put.return_value = self.worker.format() coordinator_flow.add_worker(db_handler, self.worker_id) def test_find_worker(self): db_handler = MagicMock() db_handler.find_one.return_value = self.worker.format_for_save() self.db_handler.find_one.return_value = self.worker.format() self.assertIsInstance(coordinator_flow.find_worker(self.db_handler, self.worker_id), Worker) def test_find_worker_empty(self): db_handler = MagicMock() db_handler.find_one.return_value = None with self.assertRaises(falcon.HTTPError): coordinator_flow.find_worker(db_handler, self.worker_id)