def test_dispatch_returns_error_if_bad_request(self): manager = ClaraManager(clara) msg = 'clara:start' res = manager.dispatch_request(msg) self.assertSequenceEqual(res, ['ERROR', 'Bad request: "clara:start"'])
def test_dispatch_returns_error_if_bad_action(self): manager = ClaraManager(clara) msg = 'clara:launch:python:dpe' res = manager.dispatch_request(msg) self.assertSequenceEqual(res, ['ERROR', 'Unsupported action: launch'])
def test_dispatch_returns_error_if_empty_request(self): manager = ClaraManager(clara) msg = '' res = manager.dispatch_request(msg) self.assertSequenceEqual(res, ['ERROR', 'Empty request'])
def test_dispatch_returns_error_if_standard_request_failed(self, mock_sr): manager = ClaraManager(clara) msg = 'clara:request:java:list-dpes' mock_sr.return_value = ['OUT1', 'OUT2'], ['ERR'], 1 res = manager.dispatch_request(msg) self.assertSequenceEqual(res, ['ERROR', 'OUT1', 'OUT2', 'ERR'])
def test_dispatch_successful_stop_request(self, mock_sc): manager = ClaraManager(clara) msg = 'clara:stop:python:dpe' res = manager.dispatch_request(msg) mock_sc.assert_called_once_with('python', 'dpe') self.assertSequenceEqual(res, ['SUCCESS', ''])
def test_dispatch_returns_error_if_request_failed(self, mock_sc): manager = ClaraManager(clara) msg = 'clara:start:python:monitor' mock_sc.side_effect = ClaraManagerError('Bad instance: monitor') res = manager.dispatch_request(msg) self.assertSequenceEqual(res, ['ERROR', 'Bad instance: monitor'])
def test_dispatch_returns_error_on_request_error(self, mock_sc): manager = ClaraManager(clara) msg = 'clara:start:python:dpe' mock_sc.side_effect = OSError('Popen error') res = manager.dispatch_request(msg) self.assertSequenceEqual( res, ['ERROR', 'Unexpected exception: Popen error'])
def test_dispatch_sucessful_stop_all_request(self, mock_sa): manager = ClaraManager(clara) msg = 'clara:stop:all:all' res = manager.dispatch_request(msg) mock_sa.assert_called_once_with(manager) self.assertSequenceEqual(res, ['SUCCESS', '']) self.assertTrue(not manager.instances)
def test_dispatch_successful_standard_request(self, mock_sr): manager = ClaraManager(clara) msg = 'clara:request:java:list-dpes' mock_sr.return_value = ['OK'], [''], 0 res = manager.dispatch_request(msg) mock_sr.assert_called_once_with('java', 'list-dpes') self.assertSequenceEqual(res, ['SUCCESS', 'OK'])
def test_stop_all_processes(self, mock_sp): manager = ClaraManager(clara) manager.instances = {'p/d': 'p1', 'p/p': 'p2', 'j/d': 'p3'} stop_all(manager) mock_sp.assert_any_call('p1') mock_sp.assert_any_call('p2') mock_sp.assert_any_call('p3') self.assertEqual(mock_sp.call_count, 3) self.assertTrue(not manager.instances)
def setUp(self): self.manager = ClaraManager(clara) self.mock_t = patch_on_setup(self, 'time.sleep') self.mock_po = patch_on_setup(self, 'subprocess.Popen') self.mock_cc = patch_on_setup(self, 'clara_manager.ClaraProcessConfig') self.ps = self.mock_po.return_value self.cc = self.mock_cc.return_value self.ps.poll.return_value = None
def test_stop_all_processes_with_forced_kill(self, mock_sp): manager = ClaraManager(clara) manager.instances = {'p/d': 'p1', 'p/p': 'p2', 'j/d': 'p3'} mock_sp.side_effect = OSError stop_all(manager) mock_sp.assert_any_call('p1') mock_sp.assert_any_call('p2') mock_sp.assert_any_call('p3') self.assertEqual(mock_sp.call_count, 3) self.assertTrue(not manager.instances)
def test_stop_clara_stops_process(self, mock_stop): run1 = ClaraProcessConfig(clara) run2 = ClaraProcessConfig(clara) run3 = ClaraProcessConfig(clara) manager = ClaraManager(clara) manager.instances = { 'python/dpe': run1, 'python/platform': run2, 'java/dpe': run3 } result = {'python/platform': run2, 'java/dpe': run3} manager.stop_clara('python', 'dpe') mock_stop.assert_called_once_with(run1) self.assertDictEqual(manager.instances, result)
class TestClaraManagerStandardRequest(unittest.TestCase): def setUp(self): self.manager = ClaraManager(clara) def test_standard_request_raises_on_bad_language(self): self.assertRaisesRegexp(ClaraManagerError, 'Bad language: erlang', self.manager.standard_request, 'erlang', 'list-dpes') @mock.patch('subprocess.Popen') @mock.patch('clara_manager.ClaraProcessConfig') def test_standard_request_creates_config(self, mock_cc, mock_po): mock_po.return_value.communicate.return_value = '', '' self.manager.standard_request('java', 'list-dpes') mock_cc.assert_called_once_with(clara, 'java', 'orchestrator') @mock.patch('subprocess.Popen') @mock.patch('clara_manager.ClaraProcessConfig') def test_start_clara_run_process(self, mock_cc, mock_po): mock_po.return_value.communicate.return_value = '', '' cc = mock_cc.return_value cc.cmd = ['./bin/std-orch'] self.manager.standard_request('java', 'list-dpes') mock_po.assert_called_once_with(['./bin/std-orch', 'list-dpes'], env=cc.env, cwd=cc.cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @mock.patch('subprocess.Popen') @mock.patch('clara_manager.ClaraProcessConfig') def test_start_clara_gets_process_ouput(self, mock_cc, mock_po): mock_po.return_value.communicate.return_value = 'OUT\nALL\n', '' mock_po.return_value.returncode = 0 out, err, rc = self.manager.standard_request('java', 'list-dpes') self.assertEqual(out, ['OUT', 'ALL']) self.assertEqual(err, []) self.assertEqual(rc, 0)
def test_zmq_server_is_up(self, mock_t, mock_ctx, mock_sck): manager = ClaraManager(clara) ctx = mock_ctx.return_value sck = mock_sck.return_value ctx.socket.return_value = sck sck.recv.side_effect = NotImplementedError self.assertRaises(NotImplementedError, manager.run) ctx.socket.assert_called_once_with(zmq.REP) sck.bind.assert_called_once_with("tcp://*:7788")
def test_zmq_server_reply(self, mock_t, mock_ctx, mock_sck, mock_dr): manager = ClaraManager(clara) msg = 'clara:start:python:dpe' res = ["SUCCESS", ""] sck = mock_sck.return_value mock_ctx.return_value.socket.return_value = sck sck.recv.return_value = msg mock_dr.return_value = res sck.send_multipart.side_effect = NotImplementedError self.assertRaises(NotImplementedError, manager.run) sck.recv.assert_called_once_with() mock_dr.assert_called_once_with(msg) sck.send_multipart.assert_called_once_with(res)
def test_stop_clara_raises_on_bad_instance(self): manager = ClaraManager(clara) self.assertRaisesRegexp(ClaraManagerError, 'Bad instance: monitor', manager.stop_clara, 'python', 'monitor')
def test_stop_clara_raises_on_bad_lang(self): manager = ClaraManager(clara) self.assertRaisesRegexp(ClaraManagerError, 'Bad language: erlang', manager.stop_clara, 'erlang', 'dpe')
def test_stop_clara_raises_if_not_running(self): manager = ClaraManager(clara) self.assertRaisesRegexp(ClaraManagerError, 'python/dpe is not run', manager.stop_clara, 'python', 'dpe')
def setUp(self): self.manager = ClaraManager(clara)
class TestClaraManagerStart(unittest.TestCase): def setUp(self): self.manager = ClaraManager(clara) self.mock_t = patch_on_setup(self, 'time.sleep') self.mock_po = patch_on_setup(self, 'subprocess.Popen') self.mock_cc = patch_on_setup(self, 'clara_manager.ClaraProcessConfig') self.ps = self.mock_po.return_value self.cc = self.mock_cc.return_value self.ps.poll.return_value = None def test_start_clara_raises_on_bad_instance(self): self.assertRaisesRegexp(ClaraManagerError, 'Bad instance: monitor', self.manager.start_clara, 'python', 'monitor') def test_start_clara_raises_on_bad_lang(self): self.assertRaisesRegexp(ClaraManagerError, 'Bad language: erlang', self.manager.start_clara, 'erlang', 'dpe') def test_start_clara_raises_if_already_running(self): self.manager.instances = {'python/dpe': None} self.assertRaisesRegexp(ClaraManagerError, 'python/dpe already run', self.manager.start_clara, 'python', 'dpe') def test_start_clara_creates_config(self): self.manager.start_clara('python', 'dpe') self.mock_cc.assert_called_once_with(clara, 'python', 'dpe') def test_start_clara_open_logs_before_running_process(self): def assert_open_logs(*args, **kwargs): self.cc.open_logs.assert_called_once_with() return mock.DEFAULT self.mock_po.side_effect = assert_open_logs self.manager.start_clara('python', 'dpe') self.assertFalse(self.cc.close_logs.called) def test_start_clara_run_process(self): self.manager.start_clara('python', 'dpe') self.mock_po.assert_called_once_with(self.cc.cmd, env=self.cc.env, cwd=self.cc.cwd, preexec_fn=os.setsid, stdout=self.cc.out, stderr=self.cc.err) def test_start_clara_store_process(self): self.manager.start_clara('python', 'platform') self.cc.set_proc.assert_called_once_with(self.ps) def test_start_clara_store_config(self): self.manager.start_clara('python', 'platform') key = 'python/platform' self.assertIn(key, self.manager.instances) clara_run = self.manager.instances[key] self.assertEquals(clara_run, self.cc) def test_start_clara_raises_on_process_error(self): self.ps.poll.return_value = 1 self.assertRaisesRegexp(ClaraManagerError, 'Could not start python/platform', self.manager.start_clara, 'python', 'platform') def test_start_clara_dont_store_config_on_process_error(self): self.ps.poll.return_value = 1 try: self.manager.start_clara('python', 'platform') except: pass self.assertNotIn('python/platform', self.manager.instances) def test_start_clara_close_logs_on_process_error(self): self.ps.poll.return_value = 1 try: self.manager.start_clara('python', 'platform') except: pass self.cc.close_logs.assert_called_once_with()