21
27
self.assertEqual(args, Namespace(config='foo', host='bar'))
24
class FakeRunner(Runner):
25
""""Class that pretends to run commands."""
30
class FakePrimitives(Primitives):
32
def __init__(self, host, tempdir):
33
super(FakePrimitives, self).__init__(host, tempdir)
30
def factory(cls, host):
32
cls.last_instance = instance
35
def __init__(self, host):
36
super(FakeRunner, self).__init__(host)
37
def mktemp(cls, host):
39
40
def run(self, args, out_file):
40
41
self.run_calls.append(args)
41
42
out_file.write(''.join(args))
44
class TestRunnerClass:
47
runner = self.runner_class('asdf')
48
self.assertEqual(runner.host, 'asdf')
48
def primitives_context(self):
49
with Runner('asdf', self.primitives_class) as primitives:
50
52
def test_run(self):
51
runner = self.runner_class('asdf')
52
53
out_file = StringIO()
53
self.expect_run(runner, ['foo', 'bar'], out_file)
54
with self.primitives_context() as primitives:
55
self.expect_run(primitives, ['foo', 'bar'], out_file)
54
56
self.assertEqual(out_file.getvalue(), 'foobar')
57
class TestSSHRunner(TestRunnerClass, TestCase):
59
runner_class = SSHRunner
64
def factory(self, host, primitives_class):
65
instance = Runner(host, primitives_class)
66
self.last_instance = instance
70
class TestSSHPrimitives(TestPrimitives, TestCase):
72
primitives_class = SSHPrimitives
75
def primitives_context(self):
76
with patch('subprocess.check_output') as co_mock:
77
with Runner('asdf', self.primitives_class) as primitives:
79
co_mock.assert_called_once_with(
80
['ssh', 'asdf', 'mktemp', '--directory'])
61
82
def expect_run(self, runner, args, out_file):
62
83
def effect(args, stdout):
65
86
with patch('subprocess.check_call', side_effect=effect,
66
87
autospec=True) as cc_mock:
67
runner.run(args, out_file)
88
with patch('subprocess.check_output', autospec=True) as co_mock:
89
runner.run(args, out_file)
68
90
cc_mock.assert_called_once_with(['ssh', 'asdf'] + args,
72
class TestFakeRunner(TestRunnerClass, TestCase):
94
class TestFakePrimitives(TestPrimitives, TestCase):
74
runner_class = FakeRunner
96
primitives_class = FakePrimitives
76
98
def expect_run(self, runner, args, out_file):
77
99
runner.run(args, out_file)
78
100
self.assertEqual(runner.run_calls, [['foo', 'bar']])
103
class TestRunnerClass:
106
runner = Runner('asdf', self.primitives_class)
107
self.assertEqual(runner.host, 'asdf')
110
class TestRunner(TestRunnerClass, TestCase):
112
primitives_class = SSHPrimitives
114
def expect_run(self, runner, args, out_file):
115
def effect(args, stdout):
116
stdout.write(''.join(args[2:]))
118
with patch('subprocess.check_call', side_effect=effect,
119
autospec=True) as cc_mock:
120
with patch('subprocess.check_output', autospec=True) as co_mock:
121
runner.run(args, out_file)
122
cc_mock.assert_called_once_with(['ssh', 'asdf'] + args,
81
126
class TestWorkspaceRun(TestCase):
83
128
def test_minimal(self):
129
runner_factory = RunnerFactory()
84
130
with NamedTemporaryFile() as config_file:
85
131
safe_dump({'command': ['run', 'this']}, config_file)
86
workspace_run([config_file.name, 'bar'], FakeRunner.factory)
87
runner = FakeRunner.last_instance
132
workspace_run([config_file.name, 'bar'], runner_factory.factory,
134
runner = runner_factory.last_instance
88
135
self.assertIsNot(runner, None)
89
self.assertEqual(runner.run_calls, [['run', 'this']])
136
self.assertEqual(runner.primitives.run_calls, [['run', 'this']])