Browse Source

Add support for --user, improve tests.

Make the tests use nose instead of unittest, and use properly
parameterized arguments. This makes it a lot easier to see
what fails.
tags/v0.1.1
Chris Smith 7 years ago
parent
commit
9e7e6e555a
3 changed files with 58 additions and 38 deletions
  1. 29
    5
      docker-rerun
  2. 1
    0
      requirements.txt
  3. 28
    33
      test.py

+ 29
- 5
docker-rerun View File

@@ -60,6 +60,27 @@ class Container(object):
60 60
             self.args.extend([template % entry for entry in target])
61 61
 
62 62
 
63
+    def if_image_diff(self, selector, fallback):
64
+        """Gets a property if it's different from the image's config.
65
+
66
+        Compares the value of a property in the container's information to the
67
+        same property in the image information. If the value is different then
68
+        the container's version is returned, otherwise the specified fallback
69
+        is returned.
70
+
71
+        This is useful where the container inherits config from the image,
72
+        such as the command or the user to run as. We only want to include it
73
+        in the arguments if it has been explicitly changed.
74
+
75
+        Args:
76
+            selector (func): Function to extract the property.
77
+            fallback (object): Value to return if the properties are identical.
78
+        """
79
+        container = selector(self.info)
80
+        image = selector(self.image_info)
81
+        return fallback if container == image else container
82
+
83
+
63 84
 def docker_inspect(target, what):
64 85
     """Uses `docker inspect` to get details about the given container or image.
65 86
 
@@ -86,11 +107,7 @@ def handle_binds(container):
86 107
 
87 108
 def handle_command(container):
88 109
     """Copies the command (trailing arguments)."""
89
-    cont_cmd = container.info['Config']['Cmd']
90
-    image_cmd = container.image_info['Config']['Cmd']
91
-
92
-    if cont_cmd != image_cmd:
93
-        container.cmd = cont_cmd
110
+    container.cmd = container.if_image_diff(lambda c: c['Config']['Cmd'], [])
94 111
 
95 112
 
96 113
 def handle_image(container):
@@ -139,6 +156,13 @@ def handle_restart(container):
139 156
         container.args.append(arg)
140 157
 
141 158
 
159
+def handle_user(container):
160
+    """Copies the user (--user/-u) argument."""
161
+    user = container.if_image_diff(lambda c: c['Config']['User'], None)
162
+    if user:
163
+        container.args.append('--user=%s' % user)
164
+
165
+
142 166
 def handle_volumes_from(container):
143 167
     """Copies the volumes from (--volumes-from) argument."""
144 168
     container.add_args_from_list('--volumes-from=%s',

+ 1
- 0
requirements.txt View File

@@ -1,3 +1,4 @@
1 1
 nose
2
+nose_parameterized
2 3
 pylint
3 4
 

+ 28
- 33
test.py View File

@@ -1,37 +1,32 @@
1 1
 #!/usr/bin/python3
2 2
 
3 3
 import subprocess
4
-import unittest
5
-
6
-
7
-class RerunTest(unittest.TestCase):
8
-
9
-
10
-    def _run(self, cmd):
11
-        subprocess.call(cmd,
12
-                        stdout=subprocess.DEVNULL,
13
-                        stderr=subprocess.DEVNULL)
14
-
15
-
16
-    def test_command_matches(self):
17
-        commands = [
18
-            'docker run --name=test123 -d hello-world',
19
-            'docker run --name=test123 -d hello-world:latest',
20
-            'docker run --name=test123 -d hello-world /hello world...',
21
-            'docker run --name=test123 --restart=always -d hello-world',
22
-            'docker run --name=test123 --restart=on-failure:10 -d hello-world',
23
-            'docker run --name=test123 --net=host -d hello-world',
24
-            'docker run --name=test123 -d -p=127.0.0.1:443:443/tcp -p=127.0.0.1::1336/tcp hello-world',
25
-            'docker run --name=test123 -d -p=443/tcp hello-world',
26
-            'docker run --name=test123 --volume=/dev/null:/null --volume=/dev/urandom:/mnt/random -d hello-world',
27
-        ]
28
-
29
-        for command in commands:
30
-            with self.subTest(cmd=command):
31
-                self._run(['docker', 'rm', '-f', 'test123'])
32
-                self._run(command.split(' '))
33
-                output = subprocess.check_output(['./docker-rerun', '--dry-run', 'test123'])
34
-                output = output.decode('utf-8').strip().splitlines()
35
-                self.assertEqual(output[3], command)
36
-                self._run(['docker', 'rm', '-f', 'test123'])
4
+from nose_parameterized import parameterized
5
+
6
+
7
+def _run(cmd):
8
+    subprocess.call(cmd,
9
+                    stdout=subprocess.DEVNULL,
10
+                    stderr=subprocess.DEVNULL)
11
+
12
+
13
+@parameterized([
14
+    'docker run --name=test123 -d hello-world',
15
+    'docker run --name=test123 -d hello-world:latest',
16
+    'docker run --name=test123 -d hello-world /hello world...',
17
+    'docker run --name=test123 --restart=always -d hello-world',
18
+    'docker run --name=test123 --restart=on-failure:10 -d hello-world',
19
+    'docker run --name=test123 --net=host -d hello-world',
20
+    'docker run --name=test123 -d -p=127.0.0.1:443:443/tcp -p=127.0.0.1::1336/tcp hello-world',
21
+    'docker run --name=test123 -d -p=443/tcp hello-world',
22
+    'docker run --name=test123 --user=root -d hello-world /hello foobar',
23
+    'docker run --name=test123 --volume=/dev/null:/null --volume=/dev/urandom:/mnt/random -d hello-world',
24
+])
25
+def test_command_matches(command):
26
+    _run(['docker', 'rm', '-f', 'test123'])
27
+    _run(command.split(' '))
28
+    output = subprocess.check_output(['./docker-rerun', '--dry-run', 'test123'])
29
+    output = output.decode('utf-8').strip().splitlines()
30
+    assert output[3] == command
31
+    _run(['docker', 'rm', '-f', 'test123'])
37 32
 

Loading…
Cancel
Save