From f45f78f4aa8c533f78c1ec0cb3dc7c521bd4ee18 Mon Sep 17 00:00:00 2001 From: Vladimir Kozhukalov Date: Wed, 30 Dec 2015 08:33:04 +0300 Subject: [PATCH] Add DockerCommand driver Introduces a new snapshot driver that is the same as Command driver but prepends 'docker exec ' to each command in the object. Change-Id: I8bf939bdb5a489982bc38257a245b4233103a07a Related-Bug: #1515517 --- shotgun/driver.py | 11 +++++++++++ shotgun/test/test_driver.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/shotgun/driver.py b/shotgun/driver.py index 2545168..fc89ab3 100644 --- a/shotgun/driver.py +++ b/shotgun/driver.py @@ -57,6 +57,7 @@ class Driver(object): "postgres": Postgres, "xmlrpc": XmlRpc, "command": Command, + "docker_command": DockerCommand, "offline": Offline, }.get(driver_type, cls)(data, conf) @@ -293,6 +294,16 @@ class Command(Driver): f.write(out.stderr) +class DockerCommand(Command): + def __init__(self, data, conf): + super(DockerCommand, self).__init__(data, conf) + self.cmds = [ + "docker exec " + "$(docker ps -q --filter 'name={0}' --format '{{.Names}}') " + "{1}".format(cnt, cmd) + for cnt in data["containers"] for cmd in self.cmds] + + class Offline(Driver): def __init__(self, data, conf): diff --git a/shotgun/test/test_driver.py b/shotgun/test/test_driver.py index e96f866..97218a9 100644 --- a/shotgun/test/test_driver.py +++ b/shotgun/test/test_driver.py @@ -332,6 +332,29 @@ class TestCommand(base.BaseTestCase): file_handle_mock.write.call_args_list) +class TestDockerCommand(base.BaseTestCase): + def setUp(self): + self.conf = mock.Mock() + self.conf.target = '/some/dir' + + def test_init(self): + data = { + "command": ["cmd1", "cmd2"], + "containers": ["cont1", "cont2"], + } + driver_inst = shotgun.driver.DockerCommand(data, self.conf) + template_str = ( + "docker exec " + "$(docker ps -q --filter 'name={0}' --format '{{.Names}}') {1}") + expected = [ + template_str.format("cont1", "cmd1"), + template_str.format("cont1", "cmd2"), + template_str.format("cont2", "cmd1"), + template_str.format("cont2", "cmd2"), + ] + self.assertListEqual(expected, driver_inst.cmds) + + class TestOffline(base.BaseTestCase): @mock.patch('shotgun.driver.open', create=True,