def  test_post_task_creates_correct_task_msg(self):
        ls_task = ['ls', '-la']
        bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
        msg = bashtasks.post_task(ls_task)

        self.assertEqual(msg['command'], ls_task)
        self.assertEqual(msg['reply_to'], TASK_RESPONSES_POOL)
        self.assertTrue('correlation_id' in msg)
        self.assertTrue('request_ts' in msg)
    def test_execute_task_returns_correct_response_msg(self):
        try:
            p = start_executor_process()

            bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
            ls_task = ['ls', '-la']
            response_msg = bashtasks.execute_task(ls_task)

            self.assertTrue('returncode' in response_msg)
            self.assertTrue('request_ts' in response_msg)
            self.assertEqual(response_msg['command'], ls_task)
            self.assertEqual(response_msg['reply_to'], TASK_RESPONSES_POOL)
            self.assertTrue('correlation_id' in response_msg)
        finally:
            kill_executor_process(p)
            time.sleep(0.2)
    def test_subscribe(self):
        ls_task = ['ls', '-la']
        response_msg = {}

        def start_subscriber():
            def on_response_received(msg):
                body = json.loads(msg.body.decode('utf-8'))
                response_msg.update(body)
                msg.ack()
            subscriber = bashtasks_mod.init_subscriber(host=rabbit_host,
                                                       port=rabbit_port,
                                                       usr=rabbit_user, pas=rabbit_pass)
            subscriber.subscribe(on_response_received)

        try:
            # subscribe to responses.
            # prepare executor, and send task.
            # subscribe to responses and check response arrives
            p = start_executor_process()

            subscriber_th = threading.Thread(target=start_subscriber,
                                             args=(),
                                             name='subscriber_th')
            subscriber_th.daemon = True
            subscriber_th.start()

            bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
            posted_msg = bashtasks.post_task(ls_task)
            start_time = time.time()
            # give rabbit and subscriber time to work
            while 'command' not in response_msg and time.time() - start_time < 10:
                sleep(0.1)
            self.assertTrue('command' in response_msg, 'expected response_msg to include command')
            self.assertEqual(ls_task, response_msg['command'])
        except Exception as e:
            print('Got exception in test:', repr(e))
            import traceback
            print(traceback.format_exc())
            print('response_msg::::::', repr(response_msg))

        finally:
            kill_executor_process(p)
            time.sleep(0.2)
    def test_execute_task_KO_with_nonretries(self):
        try:
            max_retries = 2
            expected_returncode = 2
            p = start_executor_process(tasks_nr=max_retries+1)
            bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
            task = ['grep', 'bye', 'bye']
            response_msg = bashtasks.execute_task(task, max_retries=max_retries,
                                                  non_retriable=[expected_returncode])

            self.assertEqual(response_msg['retries'], 0)
            self.assertEqual(response_msg['max_retries'], max_retries)
            self.assertEqual(response_msg['returncode'], expected_returncode)
            self.assertEqual(response_msg['non_retriable'], [expected_returncode])
        except Exception as e:
            print 'test_execute_task_KO_with_nonretries', e
            raise e
        finally:
            kill_executor_process(p)
            time.sleep(0.2)
    def test_execute_task_KO_returns_max_retries(self):
        try:
            max_retries = 2
            p = start_executor_process(tasks_nr=max_retries+1)
            bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
            ls_task = ['non-existent-command']
            response_msg = bashtasks.execute_task(ls_task, max_retries=max_retries)

            expect_retries = 'response.retries should be {}'.format(max_retries)
            self.assertEqual(response_msg['retries'], max_retries, expect_retries)

            expect_max_retries = 'response.max_retries should be {}'.format(max_retries)
            self.assertEqual(response_msg['max_retries'], max_retries, expect_max_retries)

            expect_returncode = 'response.returncode should be different to {}'.format(max_retries)
            self.assertNotEqual(response_msg['returncode'], 0, expect_returncode)
        except Exception as e:
            print 'test_execute_task_KO_returns_max_retries', e
            raise e
        finally:
            kill_executor_process(p)
            time.sleep(0.2)
    def test_execute_task_OK_returns_zero_retries(self):
        try:
            p = start_executor_process()
            max_retries = 5
            bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
            ls_task = ['ls', '-la']
            response_msg = bashtasks.execute_task(ls_task, max_retries=max_retries)

            expect_retries = 'response.retries should be {}'.format(0)
            self.assertEqual(response_msg['retries'], 0, expect_retries)

            expect_max_retries = 'response.max_retries should be {}'.format(max_retries)
            self.assertEqual(response_msg['max_retries'], max_retries, expect_max_retries)

            expect_returncode = 'response.returncode should be {}'.format(0)
            self.assertEqual(response_msg['returncode'], 0, expect_returncode)
        except Exception as e:
            print 'test_execute_task_OK_returns_zero_retries', e
            raise e

        finally:
            kill_executor_process(p)
            time.sleep(0.2)
    def test_post_task_sends_message(self):
        bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
        bashtasks.post_task(['ls', '-la'])

        body = assertMessageInQueue(TASK_REQUESTS_POOL, host=rabbit_host,port=rabbit_port,
                                    usr=rabbit_user, pas=rabbit_pass)
 def test_init_returns_bashtasks(self):
     bashtask = bashtasks_mod.init(channel=FakeChannel())
     isBashTask = hasattr(bashtask, 'post_task')
     self.assertTrue(isBashTask)
 def test_execute_task_raises_on_timeout(self):
     ls_task = ['ls', '-la']
     bashtasks = bashtasks_mod.init(host=rabbit_host, port=rabbit_port, usr=rabbit_user, pas=rabbit_pass)
     with self.assertRaises(Exception):
         response_msg = bashtasks.execute_task(ls_task, timeout=0.3)
                    metavar='"destination" (exchange->queue) to send the message to.')

if len(sys.argv) == 1:
    parser.print_help()
    sys.exit(1)

args = parser.parse_args()
args.command = args.command.split()


def currtimemillis():
    return int(round(time.time() * 1000))

start_ts = currtimemillis()

bashtasks = bashtasks_mod.init(host=args.host, port=args.port, usr=args.usr, pas=args.pas)

if args.fire_and_forget:
    bashtasks.post_task(args.command, max_retries=args.max_retries, destination=args.destination)
    sys.exit(0)

response_msg = bashtasks.execute_task(args.command, max_retries=args.max_retries,
                                      destination=args.destination)

total_time = currtimemillis() - response_msg['request_ts']
command_time = response_msg['post_command_ts'] - response_msg['pre_command_ts']
print('=======================================================================================')
print("received response: returncode : " + str(response_msg['returncode']))
print("                   command    : " + ' '.join(response_msg['command']))
print("                   executor   : " + response_msg['executor_name'])
print("                   retries    : " + str(response_msg['retries']))