~gz/juju-ci-tools/test_win_more_robustness

« back to all changes in this revision

Viewing changes to test_gotesttarfile.py

  • Committer: Martin Packman
  • Date: 2015-04-21 15:54:25 UTC
  • Revision ID: martin.packman@canonical.com-20150421155425-gpobx0oyn2j8ltwa
Futher changes to gotesttarball script to clean up after mongo and re-add windows env hack

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from mock import patch
2
2
import os
3
3
import shutil
4
 
import subprocess
5
4
import tarfile
6
5
from unittest import TestCase
7
6
 
15
14
from utility import temp_dir
16
15
 
17
16
 
 
17
class FakePopen(object):
 
18
 
 
19
    def __init__(self, code):
 
20
        self._code = code
 
21
 
 
22
    def communicate(self):
 
23
        self.returncode = self._code
 
24
        return None, None
 
25
 
 
26
 
18
27
class gotesttarfileTestCase(TestCase):
19
28
 
20
29
    def test_run_success(self):
21
30
        env = {'a': 'b'}
22
 
        with patch('subprocess.check_output', return_value='pass') as mock:
23
 
            returncode, output = run('go', 'test', './...', env=env)
 
31
        with patch('subprocess.Popen', autospec=True,
 
32
                   return_value=FakePopen(0)) as mock:
 
33
            returncode = run(['go', 'test', './...'], env=env)
24
34
        self.assertEqual(0, returncode)
25
 
        self.assertEqual('pass', output)
26
 
        args, kwargs = mock.call_args
27
 
        self.assertEqual((('go', 'test', './...'), ), args)
28
 
        self.assertIs(env, kwargs['env'])
29
 
        self.assertIs(subprocess.STDOUT, kwargs['stderr'])
 
35
        mock.assert_called_once_with(['go', 'test', './...'], env=env)
30
36
 
31
37
    def test_run_fail(self):
32
38
        env = {'a': 'b'}
33
 
        e = subprocess.CalledProcessError(1, ['foo'], output='fail')
34
 
        with patch('subprocess.check_output', side_effect=e):
35
 
            returncode, output = run('go', 'test', './...', env=env)
 
39
        with patch('subprocess.Popen', autospec=True,
 
40
                   return_value=FakePopen(1)) as mock:
 
41
            returncode = run(['go', 'test', './...'], env=env)
36
42
        self.assertEqual(1, returncode)
37
 
        self.assertEqual('fail', output)
 
43
        mock.assert_called_once_with(['go', 'test', './...'], env=env)
38
44
 
39
45
    def test_untar_gopath(self):
40
46
        with temp_dir() as base_dir:
56
62
            package_path = os.path.join(
57
63
                gopath, 'src', 'github.com', 'juju', 'juju')
58
64
            os.makedirs(package_path)
59
 
            with patch('gotesttarfile.run', return_value=[0, 'success'],
 
65
            with patch('gotesttarfile.run', return_value=0,
60
66
                       autospec=True) as run_mock:
61
67
                devnull = open(os.devnull, 'w')
62
68
                with patch('sys.stdout', devnull):
63
69
                    returncode = go_test_package(
64
70
                        'github.com/juju/juju', 'go', gopath)
65
71
        self.assertEqual(0, returncode)
66
 
        args, kwargs = run_mock.call_args
67
 
        self.assertEqual(('go', 'test', './...'), args)
 
72
        self.assertEqual(run_mock.call_count, 2)
 
73
        args, kwargs = run_mock.call_args_list[0]
 
74
        self.assertEqual((['go', 'test', './...'],), args)
68
75
        self.assertEqual('amd64', kwargs['env'].get('GOARCH'))
69
76
        self.assertEqual(gopath, kwargs['env'].get('GOPATH'))
 
77
        run_mock.assert_called_with(['sudo', 'killall', '-SIGABRT', 'mongod'])
70
78
 
71
79
    def test_go_test_package_win32(self):
72
80
        with temp_dir() as gopath:
73
81
            package_path = os.path.join(
74
82
                gopath, 'src', 'github.com', 'juju', 'juju')
75
83
            os.makedirs(package_path)
76
 
            with patch('gotesttarfile.run', return_value=[0, 'success'],
 
84
            with patch('gotesttarfile.run', return_value=0,
77
85
                       autospec=True) as run_mock:
78
86
                devnull = open(os.devnull, 'w')
79
87
                with patch('sys.stdout', devnull):
83
91
                            returncode = go_test_package(
84
92
                                'github.com/juju/juju', 'go', gopath)
85
93
        self.assertEqual(0, returncode)
86
 
        args, kwargs = run_mock.call_args
 
94
        args, kwargs = run_mock.call_args_list[0]
 
95
        self.assertEqual(run_mock.call_count, 2)
87
96
        self.assertEqual(
88
 
            ('powershell.exe', '-Command', 'go', 'test', './...'),
 
97
            (['powershell.exe', '-Command', 'go', 'test', './...'], ),
89
98
            args)
90
99
        self.assertEqual(r'C:\foo;C:\baz', kwargs['env'].get('PATH'))
 
100
        self.assertEqual(kwargs['env'].get('Path'), kwargs['env'].get('PATH'))
91
101
        self.assertEqual(gopath, os.path.dirname(kwargs['env'].get('TMP')))
92
102
        self.assertIn("tmp-juju-", os.path.basename(kwargs['env'].get('TMP')))
93
103
        self.assertEqual(kwargs['env'].get('TEMP'), kwargs['env'].get('TMP'))
 
104
        run_mock.assert_called_with(
 
105
            ['taskkill.exe', '/F', '/FI', 'imagename eq mongod.exe'])
94
106
 
95
107
    def test_parse_args(self):
96
108
        args = parse_args(