18
18
from fixtures import TempDir
19
19
from maastest import utils
20
from maastest.utils import binary_content
22
from six import text_type
24
from testtools.matchers import MatchesStructure
24
27
class TestRunCommand(testtools.TestCase):
54
[u'one', u'two'], stdout=PIPE, stderr=PIPE,
57
['one', 'two'], stdout=PIPE, stderr=PIPE,
55
58
stdin=PIPE, shell=False),
56
59
mock.call().communicate(None),
63
66
def test_run_command_runs_command(self):
64
67
retcode, stdout, stderr = utils.run_command(['ls', '/'])
66
self.assertEqual((0, ''), (retcode, stderr))
67
self.assertIn("boot", stdout)
69
self.assertEqual((0, b''), (retcode, stderr))
70
self.assertIn(b"boot", stdout)
69
72
def test_run_command_checks_return_value(self):
70
73
mock_Popen = mock.MagicMock()
80
83
error = self.assertRaises(
81
84
Exception, utils.run_command, args, check_call=True)
82
self.assertIn(expected_stdout, error.message)
83
self.assertIn(expected_stderr, error.message)
85
self.assertIn(expected_stdout, text_type(error))
86
self.assertIn(expected_stderr, text_type(error))
85
88
def test_run_command_uses_input(self):
86
input_string = self.getUniqueString()
89
input_string = self.getUniqueString().encode("ascii")
87
90
retcode, stdout, stderr = utils.run_command(
88
91
['cat', '-'], input=input_string)
91
(0, '', input_string), (retcode, stderr, stdout))
94
(0, b'', input_string), (retcode, stderr, stdout))
97
class TestBinaryContent(testtools.TestCase):
98
"""Tests for `binary_content`."""
100
def test_returns_same_content(self):
101
content = binary_content(b'abc123')
103
b''.join(content.iter_bytes()),
106
def test_has_appropriate_content_type(self):
107
content = binary_content(b'abc123')
109
content.content_type,
110
MatchesStructure.byEquality(
112
subtype="octet-stream",