This is an automated email from the ASF dual-hosted git repository. klueska pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mesos.git
The following commit(s) were added to refs/heads/master by this push: new 6b7b7e6 Refactored 'running_tasks()' call for new CLI tests. 6b7b7e6 is described below commit 6b7b7e6f68ef891febcce2a38077a847288c1c10 Author: Armand Grillet <agril...@mesosphere.io> AuthorDate: Fri Nov 2 19:17:56 2018 -0400 Refactored 'running_tasks()' call for new CLI tests. Review: https://reviews.apache.org/r/69207/ --- src/python/cli_new/lib/cli/tests/base.py | 21 ++++++++++++++++ src/python/cli_new/lib/cli/tests/task.py | 42 ++++++++++---------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/python/cli_new/lib/cli/tests/base.py b/src/python/cli_new/lib/cli/tests/base.py index 5d9cf01..c28e2a6 100644 --- a/src/python/cli_new/lib/cli/tests/base.py +++ b/src/python/cli_new/lib/cli/tests/base.py @@ -29,6 +29,9 @@ import unittest import parse +from tenacity import retry +from tenacity import stop_after_delay + from cli import http from cli.tests.constants import TEST_AGENT_IP @@ -498,3 +501,21 @@ def popen_tty(cmd, shell=True): os.close(slave) return (proc, master) + + +def running_tasks(master): + """ + Open the master's `/tasks` endpoint and read the task information. + Retry for up to 1 second before giving up. + """ + @retry(stop=stop_after_delay(1)) + def _running_tasks(): + tasks = http.get_json(master.addr, "tasks")["tasks"] + if tasks[0]["state"] == "TASK_RUNNING": + return tasks + raise Exception() + + try: + return _running_tasks() + except Exception: + return [] diff --git a/src/python/cli_new/lib/cli/tests/task.py b/src/python/cli_new/lib/cli/tests/task.py index ce3a55d..6022003 100644 --- a/src/python/cli_new/lib/cli/tests/task.py +++ b/src/python/cli_new/lib/cli/tests/task.py @@ -20,18 +20,16 @@ Task plugin tests. import os -from tenacity import retry -from tenacity import stop_after_delay - from cli import config from cli import http -from cli.plugins.task.main import Task as TaskPlugin - from cli.exceptions import CLIException +from cli.plugins.task.main import Task as TaskPlugin + from cli.tests import capture_output from cli.tests import exec_command +from cli.tests import running_tasks from cli.tests import CLITestCase from cli.tests import Agent from cli.tests import Master @@ -65,18 +63,11 @@ class TestTaskPlugin(CLITestCase): task = Task({"command": command}) task.launch() - # We wait (max 1 second) for the task to be in a running state. - @retry(stop=stop_after_delay(1)) - def updated_tasks(): - # Open the master's `/tasks` endpoint and - # read the task information ourselves. - tasks = http.get_json(master.addr, "tasks")["tasks"] - if tasks[0]["state"] == "TASK_RUNNING": - return tasks - raise CLIException("Unable to find running task in master state" - " '{master}'".format(master=master)) - - tasks = updated_tasks() + tasks = running_tasks(master) + if not tasks: + raise CLIException("Unable to find running tasks on master" + " '{master}'".format(master=master.addr)) + returncode, stdout, stderr = exec_command( ["mesos", "task", "exec", tasks[0]["id"], "cat", "a.txt"]) @@ -104,18 +95,11 @@ class TestTaskPlugin(CLITestCase): task = Task({"command": "sleep 1000"}) task.launch() - # We wait (max 1 second) for the task to be in a running state. - @retry(stop=stop_after_delay(1)) - def updated_tasks(): - # Open the master's `/tasks` endpoint and - # read the task information ourselves. - tasks = http.get_json(master.addr, "tasks")["tasks"] - if tasks[0]["state"] == "TASK_RUNNING": - return tasks - raise CLIException("Unable to find running task in master state" - " '{master}'".format(master=master)) - - tasks = updated_tasks() + tasks = running_tasks(master) + if not tasks: + raise CLIException("Unable to find running tasks on master" + " '{master}'".format(master=master.addr)) + with open(LOREM_IPSUM) as text: returncode, stdout, stderr = exec_command( ["mesos", "task", "exec", "-i", tasks[0]["id"], "cat"],