5

I have an interesting set of requirements that I am trying to conduct using the Python subprocess module and docker-compose. This whole setup is possible in one docker-compose but due to requirement this is what I would like to setup:

  1. call the docker-compose using python subprocess to activate the test-servers
  2. print all the std-out of above docker-compose running.
  3. as soon as the test-server up and running via docker-compose; call the testing scripts for that server.

This is my docker-compose.py looks like:

import subprocess
from subprocess import PIPE
import os
from datetime import datetime

class MyLog:
    def my_log(self, message):
        date_now = datetime.today().strftime('%d-%m-%Y %H:%M:%S')
        print("{0} ||  {1}".format(date_now, message))


class DockercomposeRun:
    log = MyLog()

    def __init__(self):
        dir_name, _ = os.path.split(os.path.abspath(__file__))
        self.dirname = dir_name

    def run_docker_compose(self, filename):
        command_name = ["docker-compose", "-f", self.dirname + filename, "up"]
        popen = subprocess.Popen(command_name, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        return popen

now in my test.py as soon as my stdout is blank I would like to break the loop of printing and run the rest of the test in test.py.

docker_compose_run = DockercomposeRun()
rc = docker_compose_run.run_docker_compose('/docker-compose.yml.sas-viya-1')

for line in iter(rc.stdout.readline, ''):
            print(line, end='')

            if line == '':
                break

        popen.stdout.close()

# start here actual test cases 
.......

But for me the loop is never broken even though the stdout of docker-compose goes blank after the server is up and running. And, the test cases are never executed. Is it the right approach or how I can achieve this?

change198
  • 1,647
  • 3
  • 21
  • 60

1 Answers1

0

I think the issue here is because you are not running docker-compose in detached mode and its blocking the application run. Can you try adding "-d" to command_name?

Ranjini M
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '22 at 04:04