~rvb/maas/ephemeral-simplestreams

« back to all changes in this revision

Viewing changes to maastest/tests/test_utils.py

  • Committer: Graham Binns
  • Date: 2013-11-14 09:55:21 UTC
  • mfrom: (28 maas-test)
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: graham.binns@canonical.com-20131114095521-bs1uh8w0decac7ib
Made changes to bring things into line with Raphaël's fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
from fixtures import TempDir
19
19
from maastest import utils
 
20
from maastest.utils import binary_content
20
21
import mock
 
22
from six import text_type
21
23
import testtools
 
24
from testtools.matchers import MatchesStructure
22
25
 
23
26
 
24
27
class TestRunCommand(testtools.TestCase):
51
54
        self.assertEqual(
52
55
            [
53
56
                mock.call(
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),
57
60
            ],
63
66
    def test_run_command_runs_command(self):
64
67
        retcode, stdout, stderr = utils.run_command(['ls', '/'])
65
68
 
66
 
        self.assertEqual((0, ''), (retcode, stderr))
67
 
        self.assertIn("boot", stdout)
 
69
        self.assertEqual((0, b''), (retcode, stderr))
 
70
        self.assertIn(b"boot", stdout)
68
71
 
69
72
    def test_run_command_checks_return_value(self):
70
73
        mock_Popen = mock.MagicMock()
79
82
 
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))
84
87
 
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)
89
92
 
90
93
        self.assertEqual(
91
 
            (0, '', input_string), (retcode, stderr, stdout))
 
94
            (0, b'', input_string), (retcode, stderr, stdout))
 
95
 
 
96
 
 
97
class TestBinaryContent(testtools.TestCase):
 
98
    """Tests for `binary_content`."""
 
99
 
 
100
    def test_returns_same_content(self):
 
101
        content = binary_content(b'abc123')
 
102
        self.assertEqual(
 
103
            b''.join(content.iter_bytes()),
 
104
            b'abc123')
 
105
 
 
106
    def test_has_appropriate_content_type(self):
 
107
        content = binary_content(b'abc123')
 
108
        self.assertThat(
 
109
            content.content_type,
 
110
            MatchesStructure.byEquality(
 
111
                type="application",
 
112
                subtype="octet-stream",
 
113
                parameters={},
 
114
            ))