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 18e51b8  Replaced CLI test helper function 'running_tasks' by 
'wait_for_task'.
18e51b8 is described below

commit 18e51b86fac848330dea640d7b7b7bf2d6584fe5
Author: Armand Grillet <agril...@mesosphere.io>
AuthorDate: Mon Nov 26 08:47:25 2018 -0500

    Replaced CLI test helper function 'running_tasks' by 'wait_for_task'.
    
    Replaces 'running_tasks(master)', a function that was not generic nor
    explicit, by 'wait_for_task(master, name, state, delay)'. This helper
    function waits a 'delay' for a task with a given 'name' to be in a
    certain 'state'.
    
    All uses of 'running_tasks' have been replaced by the new function.
    
    Review: https://reviews.apache.org/r/69426/
---
 src/python/cli_new/lib/cli/tests/base.py | 19 ++++----
 src/python/cli_new/lib/cli/tests/task.py | 84 +++++++++++++++++++++++++-------
 2 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/src/python/cli_new/lib/cli/tests/base.py 
b/src/python/cli_new/lib/cli/tests/base.py
index 58c96d7..e3104fe 100644
--- a/src/python/cli_new/lib/cli/tests/base.py
+++ b/src/python/cli_new/lib/cli/tests/base.py
@@ -31,6 +31,7 @@ import parse
 
 from tenacity import retry
 from tenacity import stop_after_delay
+from tenacity import wait_fixed
 
 from cli import http
 
@@ -503,19 +504,19 @@ def popen_tty(cmd, shell=True):
     return (proc, master)
 
 
-def running_tasks(master):
+def wait_for_task(master, name, state, delay=1):
     """
-    Open the master's `/tasks` endpoint and read the task information.
-    Retry for up to 1 second before giving up.
+    Wait for a task with a certain name to be in a given state.
     """
-    @retry(stop=stop_after_delay(1))
-    def _running_tasks():
+    @retry(wait=wait_fixed(0.2), stop=stop_after_delay(delay))
+    def _wait_for_task():
         tasks = http.get_json(master.addr, "tasks")["tasks"]
-        if tasks[0]["state"] == "TASK_RUNNING":
-            return tasks
+        for task in tasks:
+            if task["name"] == name and task["state"] == state:
+                return task
         raise Exception()
 
     try:
-        return _running_tasks()
+        return _wait_for_task()
     except Exception:
-        return []
+        raise CLIException("Timeout waiting for task expired")
diff --git a/src/python/cli_new/lib/cli/tests/task.py 
b/src/python/cli_new/lib/cli/tests/task.py
index eb01bf5..69ed823 100644
--- a/src/python/cli_new/lib/cli/tests/task.py
+++ b/src/python/cli_new/lib/cli/tests/task.py
@@ -21,6 +21,7 @@ Task plugin tests.
 import os
 
 from cli import config
+from cli import http
 
 from cli.exceptions import CLIException
 
@@ -28,7 +29,7 @@ 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 wait_for_task
 from cli.tests import CLITestCase
 from cli.tests import Agent
 from cli.tests import Master
@@ -62,10 +63,23 @@ class TestTaskPlugin(CLITestCase):
         task = Task({"command": command})
         task.launch()
 
-        tasks = running_tasks(master)
-        if not tasks:
-            raise CLIException("Unable to find running tasks on master"
-                               " '{master}'".format(master=master.addr))
+        try:
+            wait_for_task(master, task.name, "TASK_RUNNING")
+        except Exception as exception:
+            raise CLIException(
+                "Error waiting for task '{name}' to"
+                " reach state '{state}': {error}"
+                .format(name=task.name, state="TASK_RUNNING", error=exception))
+
+        try:
+            tasks = http.get_json(master.addr, "tasks")["tasks"]
+        except Exception as exception:
+            raise CLIException(
+                "Could not get tasks from '/{endpoint}' on master: {error}"
+                .format(endpoint="tasks", error=exception))
+
+        self.assertEqual(type(tasks), list)
+        self.assertEqual(len(tasks), 1)
 
         returncode, stdout, stderr = exec_command(
             ["mesos", "task", "exec", tasks[0]["id"], "cat", "a.txt"])
@@ -93,10 +107,23 @@ class TestTaskPlugin(CLITestCase):
         task = Task({"command": "sleep 1000"})
         task.launch()
 
-        tasks = running_tasks(master)
-        if not tasks:
-            raise CLIException("Unable to find running tasks on master"
-                               " '{master}'".format(master=master.addr))
+        try:
+            wait_for_task(master, task.name, "TASK_RUNNING")
+        except Exception as exception:
+            raise CLIException(
+                "Error waiting for task '{name}' to"
+                " reach state '{state}': {error}"
+                .format(name=task.name, state="TASK_RUNNING", error=exception))
+
+        try:
+            tasks = http.get_json(master.addr, "tasks")["tasks"]
+        except Exception as exception:
+            raise CLIException(
+                "Could not get tasks from '/{endpoint}' on master: {error}"
+                .format(endpoint="tasks", error=exception))
+
+        self.assertEqual(type(tasks), list)
+        self.assertEqual(len(tasks), 1)
 
         returncode, _, _ = exec_command(
             ["mesos", "task", "exec", tasks[0]["id"], "true"])
@@ -126,10 +153,23 @@ class TestTaskPlugin(CLITestCase):
         task = Task({"command": "sleep 1000"})
         task.launch()
 
-        tasks = running_tasks(master)
-        if not tasks:
-            raise CLIException("Unable to find running tasks on master"
-                               " '{master}'".format(master=master.addr))
+        try:
+            wait_for_task(master, task.name, "TASK_RUNNING")
+        except Exception as exception:
+            raise CLIException(
+                "Error waiting for task '{name}' to"
+                " reach state '{state}': {error}"
+                .format(name=task.name, state="TASK_RUNNING", error=exception))
+
+        try:
+            tasks = http.get_json(master.addr, "tasks")["tasks"]
+        except Exception as exception:
+            raise CLIException(
+                "Could not get tasks from '/{endpoint}' on master: {error}"
+                .format(endpoint="tasks", error=exception))
+
+        self.assertEqual(type(tasks), list)
+        self.assertEqual(len(tasks), 1)
 
         with open(LOREM_IPSUM) as text:
             returncode, stdout, stderr = exec_command(
@@ -161,10 +201,20 @@ class TestTaskPlugin(CLITestCase):
         task = Task({"command": "sleep 1000"})
         task.launch()
 
-        tasks = running_tasks(master)
-        if not tasks:
-            raise CLIException("Unable to find running tasks on master"
-                               " '{master}'".format(master=master.addr))
+        try:
+            wait_for_task(master, task.name, "TASK_RUNNING")
+        except Exception as exception:
+            raise CLIException(
+                "Error waiting for task '{name}' to"
+                " reach state '{state}': {error}"
+                .format(name=task.name, state="TASK_RUNNING", error=exception))
+
+        try:
+            tasks = http.get_json(master.addr, "tasks")["tasks"]
+        except Exception as exception:
+            raise CLIException(
+                "Could not get tasks from '/{endpoint}' on master: {error}"
+                .format(endpoint="tasks", error=exception))
 
         self.assertEqual(type(tasks), list)
         self.assertEqual(len(tasks), 1)

Reply via email to