Browse Source

Refactor so tests don't shell out to docker_rerun.

pull/2/head
Chris Smith 7 years ago
parent
commit
9dd9a76d12
2 changed files with 11 additions and 8 deletions
  1. 6
    6
      docker_rerun.py
  2. 5
    2
      test.py

+ 6
- 6
docker_rerun.py View File

@@ -270,7 +270,7 @@ def modifiers():
270 270
     return [func for (name, func) in functions() if name.startswith('modify_')]
271 271
 
272 272
 
273
-def main():
273
+def main(argv, out):
274 274
     """Script entry point."""
275 275
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
276 276
                                                  'with different parameters.')
@@ -286,7 +286,7 @@ def main():
286 286
     for mod in mods:
287 287
         mod(parser=parser)
288 288
 
289
-    args = parser.parse_args()
289
+    args = parser.parse_args(argv)
290 290
     container_info = docker_inspect(args.container, 'container')
291 291
     image_info = docker_inspect(container_info['Config']['Image'], 'image')
292 292
     container = Container(container_info, image_info)
@@ -308,14 +308,14 @@ def main():
308 308
 
309 309
     if args.dry_run:
310 310
         print('Performing dry run for container %s. The following would be ' \
311
-              'executed:' % args.container)
311
+              'executed:' % args.container, file=out)
312 312
         for command in commands:
313
-            print(' '.join(command))
313
+            print(' '.join(command), file=out)
314 314
     else:
315
-        print('Re-running container %s...' % args.container)
315
+        print('Re-running container %s...' % args.container, file=out)
316 316
         for command in commands:
317 317
             subprocess.check_call(command)
318 318
 
319 319
 
320 320
 if __name__ == "__main__":
321
-    main()
321
+    main(sys.argv[1:], sys.stdout)

+ 5
- 2
test.py View File

@@ -1,6 +1,8 @@
1 1
 #!/usr/bin/python3
2 2
 
3
+import docker_rerun
3 4
 import subprocess
5
+from io import StringIO
4 6
 from nose.tools import with_setup
5 7
 
6 8
 
@@ -75,8 +77,9 @@ def test_modifiers():
75 77
 @with_setup(setup_each, teardown_each)
76 78
 def check(command, args=[], expected=None):
77 79
     _run(command)
78
-    output = subprocess.check_output(['./docker_rerun.py', '--dry-run', 'test123'] + args)
79
-    output = output.decode('utf-8').strip().splitlines()
80
+    output = StringIO()
81
+    docker_rerun.main(['--dry-run', 'test123'] + args, output) 
82
+    output = output.getvalue().splitlines()
80 83
     expected = ' '.join(expected or command)
81 84
     assert output[3] == expected, 'Expected "%s" but got "%s"' % (expected, output[3])
82 85
 

Loading…
Cancel
Save