def test_custom_io(self, tmpdir): """ It is possible to create a new process with custom IO. """ stdout_file = tmpdir.join("output.txt") stderr_file = tmpdir.join("err.txt") with open(stdout_file.strpath, 'w') as stdout: with open(stderr_file.strpath, 'w') as stderr: Task( image_url=ROOTFS_URI, args=['echo', '1'], download_path=pathlib.Path(tmpdir.strpath), stdout=stdout, stderr=stderr, ) Task( image_url=ROOTFS_URI, args=['sleep', 'a'], download_path=pathlib.Path(tmpdir.strpath), stdout=stdout, stderr=stderr, ) assert stdout_file.read() == '1\n' assert stderr_file.read() == "sleep: invalid number 'a'\n"
def send_signal(task_id, signal): """ Send a signal to a process started by an existing task. :param str task_id: The id of an existing task. :param str signal: The signal to send. """ task = Task(existing_task=int(task_id)) task.send_signal(Signals[signal])
def health_check(task_id): """ Check the health of a task. :param str task_id: The id of an existing task. """ task = Task(existing_task=int(task_id)) health = task.get_health() print('exists: ' + str(health['exists'])) print('status: ' + str(health['status']))
def test_create(self, tmpdir): """ It is possible to create a new process running a given command. """ task = Task( image_url=ROOTFS_URI, args=['sleep', '5'], download_path=pathlib.Path(tmpdir.strpath), ) assert task.get_health() == {'exists': True, 'status': 'sleeping'}
def test_send_signal(self, tmpdir): """ Sending a ``SIGINT`` signal to ``task.send_signal`` kills the child process. """ task = Task( image_url=ROOTFS_URI, args=['sleep', '5'], download_path=pathlib.Path(tmpdir.strpath), ) task.send_signal(signal.SIGINT) assert task.get_health() == {'exists': False, 'status': None}
def test_existing_task(self, tmpdir): """ It is possible to get an existing task by its id. """ task = Task( image_url=ROOTFS_URI, args=['sleep', '5'], download_path=pathlib.Path(tmpdir.strpath), ) other_task = Task(existing_task=task.id) # Interrupting one task interrupts the other, so they are the same task task.send_signal(signal.SIGINT) assert other_task.get_health() == {'exists': False, 'status': None}
def test_create_task(self, tmpdir): """ It is possible to start a Task from the CLI. """ # Change directory to temporary directory so as not to pollute current # working directory with downloaded filesystem. os.chdir(tmpdir.strpath) runner = CliRunner() commands = 'sleep 5' result = runner.invoke(cli, ['create', ROOTFS_URI, commands]) assert result.exit_code == 0 task = Task(existing_task=int(result.output)) assert task._process.cmdline() == commands.split()
def create(image_url, args): """ Create a ``Task``. :param str image_url: The URL (or URI) pointing to an image to download, extract and use as the root of a new process. :param str args: Commands to run as a new process. """ task = Task( image_url=image_url, args=shlex.split(args), download_path=pathlib.Path(os.getcwd()), ) print(task.id)