10587: Added tests to Python commands to check for the --version argument.
[arvados.git] / services / nodemanager / tests / test_arguments.py
1 #!/usr/bin/env python
2
3 import multiprocessing
4 import os
5 import sys
6 import tempfile
7 import unittest
8
9 import arvnodeman.launcher as nodeman
10
11 class ArvNodemArgumentsTestCase(unittest.TestCase):
12     def run_nodeman(self, args):
13         return nodeman.main(args)
14
15     def run_nodeman_process(self, args=[]):
16         _, stdout_path = tempfile.mkstemp()
17         _, stderr_path = tempfile.mkstemp()
18         def wrap():
19             def wrapper(*args, **kwargs):
20                 sys.stdout = open(stdout_path, 'w')
21                 sys.stderr = open(stderr_path, 'w')
22                 nodeman.main(*args, **kwargs)
23             return wrapper
24         p = multiprocessing.Process(target=wrap(), args=(args,))
25         p.start()
26         p.join()
27         out = open(stdout_path, 'r').read()
28         err = open(stderr_path, 'r').read()
29         os.unlink(stdout_path)
30         os.unlink(stderr_path)
31         return p.exitcode, out, err
32
33     def test_unsupported_arg(self):
34         with self.assertRaises(SystemExit):
35             self.run_nodeman(['-x=unknown'])
36
37     def test_version_argument(self):
38         exitcode, out, err = self.run_nodeman_process(['--version'])
39         self.assertEqual(0, exitcode)
40         self.assertEqual('', out)
41         self.assertNotEqual('', err)
42         self.assertRegexpMatches(err, "[0-9]+\.[0-9]+\.[0-9]+")