Skip to content

Commit

Permalink
Default to random docker-compose run project name.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsibilla committed Nov 29, 2020
1 parent 3ff5a57 commit 79b6a1e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
22 changes: 19 additions & 3 deletions runci/engine/runner/compose_run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import random
import string
import sys

from .base import RunnerBase
from runci.entities.context import Context

Expand All @@ -6,16 +10,28 @@ class ComposeRunRunner(RunnerBase):
_selector = 'compose-run'

async def run_internal(self, context: Context):
files = self.spec.get('file', context.parameters.dataconnection).split(' ')
files = self.spec.get('file', 'docker-compose.yml ' + context.parameters.dataconnection).split(' ')
service_list = self.spec.get('services', None)
project_name = self.spec.get('projectName', None)
build = self.spec.get('build', True)

if (project_name is None):
# Generate a random string
project_name = ''.join(random.choice(string.ascii_lowercase) for i in range(8))

dc_args = ['docker-compose']
for file in files:
dc_args.extend(['-f', file])

if project_name is not None:
dc_args.extend(['-p', project_name])
dc_args.extend(['-p', project_name])

if build:
self._log_runner_message(sys.stdout, "Ensuring images are built")

build_args = list(dc_args)
build_args.extend(['build', '-q'])

await self._run_process(build_args)

run_args = list(dc_args)
run_args.extend(['run', '--rm'])
Expand Down
8 changes: 4 additions & 4 deletions tests/test_runner_compose_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@


class test_runner_compose_build(unittest.TestCase):
step = Step("test", "compose-run", {"services": "app"})
step = Step("test", "compose-run", {"services": "app", "projectName": "test", "build": False})
project = Project(
services=[],
targets=[Target(
name="target",
dependencies=[],
steps=[step]
)])
parameters = Parameters(dataconnection="docker-compose.yml runci.yml", targets=["target"], verbosity=0)
parameters = Parameters(dataconnection="runci.yml", targets=["target"], verbosity=0)

@patch('runci.engine.runner.compose_run.ComposeRunRunner._run_process')
def test_command_line_args(self, mock):
Expand All @@ -26,8 +26,8 @@ async def run():
await runner.run(context)

asyncio.run(run())
mock.assert_has_calls([call('docker-compose -f docker-compose.yml -f runci.yml run --rm app'.split(' ')),
call('docker-compose -f docker-compose.yml -f runci.yml down'.split(' '))])
mock.assert_has_calls([call('docker-compose -f docker-compose.yml -f runci.yml -p test run --rm app'.split(' ')),
call('docker-compose -f docker-compose.yml -f runci.yml -p test down'.split(' '))])

@patch('runci.engine.runner.compose_run.ComposeRunRunner.run')
def test_integration(self, mock):
Expand Down

0 comments on commit 79b6a1e

Please sign in to comment.