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

+ 5
- 2
test.py View File

1
 #!/usr/bin/python3
1
 #!/usr/bin/python3
2
 
2
 
3
+import docker_rerun
3
 import subprocess
4
 import subprocess
5
+from io import StringIO
4
 from nose.tools import with_setup
6
 from nose.tools import with_setup
5
 
7
 
6
 
8
 
75
 @with_setup(setup_each, teardown_each)
77
 @with_setup(setup_each, teardown_each)
76
 def check(command, args=[], expected=None):
78
 def check(command, args=[], expected=None):
77
     _run(command)
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
     expected = ' '.join(expected or command)
83
     expected = ' '.join(expected or command)
81
     assert output[3] == expected, 'Expected "%s" but got "%s"' % (expected, output[3])
84
     assert output[3] == expected, 'Expected "%s" but got "%s"' % (expected, output[3])
82
 
85
 

Loading…
Cancel
Save