~rvb/maas/ephemeral-simplestreams

« back to all changes in this revision

Viewing changes to maastest/tests/test_kvmfixture.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:
22
22
import fixtures
23
23
from lxml import etree
24
24
from maastest import kvmfixture
25
 
from maastest.utils import read_file
 
25
from maastest.utils import (
 
26
    binary_content,
 
27
    read_file,
 
28
    )
26
29
import mock
27
30
import netaddr
 
31
from six import text_type
28
32
import testtools
29
33
from testtools.matchers import (
30
34
    FileContains,
49
53
        self.patch(
50
54
            os.path, 'expanduser', mock.MagicMock(return_value=home_dir))
51
55
 
52
 
    def patch_run_command(self, return_value=0, stdout='', stderr=''):
 
56
    def patch_run_command(self, return_value=0, stdout=b'', stderr=b''):
53
57
        """Patch out `kvmfixture` calls to `run_command`.
54
58
 
55
59
        Call this so your test doesn't accidentally make `KVMFixture` download
57
61
 
58
62
        Returns the `MagicMock` fake it has installed.
59
63
        """
60
 
        if not isinstance(stdout, bytes):
61
 
            stdout = stdout.encode('ascii')
62
 
        if not isinstance(stderr, bytes):
63
 
            stderr = stderr.encode('ascii')
 
64
        self.assertIsInstance(stdout, bytes)
 
65
        self.assertIsInstance(stderr, bytes)
64
66
        fake = mock.MagicMock(return_value=(return_value, stdout, stderr))
65
67
        self.patch(kvmfixture, 'run_command', fake)
66
68
        return fake
119
121
        series = "test-series"
120
122
        architecture = "test-arch"
121
123
        ip_address = '192.168.2.1'
122
 
        self.patch_run_command(0, stdout=ip_address)
 
124
        self.patch_run_command(0, stdout=ip_address.encode("ascii"))
123
125
        with self.make_KVMFixture(series, architecture) as fixture:
124
126
            self.assertEqual(ip_address, fixture.ip_address())
125
127
 
127
129
        series = "test-series"
128
130
        architecture = "test-arch"
129
131
        ip_address = '192.168.2.1'
130
 
        self.patch_run_command(stdout=ip_address)
 
132
        self.patch_run_command(stdout=ip_address.encode("ascii"))
131
133
        with self.make_KVMFixture(series, architecture) as fixture:
132
134
            self.assertEqual("ubuntu@%s" % ip_address, fixture.identity())
133
135
 
252
254
        earlier_fixture = self.make_KVMFixture()
253
255
        fixtures.Fixture.setUp(earlier_fixture)
254
256
        earlier_fixture.generate_ssh_key()
 
257
 
 
258
        # We decode the file contents here to work around bug #1250483
 
259
        # in FileContains later. This is for Python 3 compatibility.
255
260
        earlier_private_key_contents = read_file(
256
 
            earlier_fixture.ssh_private_key_file)
 
261
            earlier_fixture.ssh_private_key_file).decode("ascii")
257
262
        earlier_public_key_contents = read_file(
258
 
            earlier_fixture.get_ssh_public_key_file())
 
263
            earlier_fixture.get_ssh_public_key_file()).decode("ascii")
259
264
 
260
265
        later_fixture = self.make_KVMFixture()
261
266
        fixtures.Fixture.setUp(later_fixture)
311
316
 
312
317
        exception = self.assertRaises(RuntimeError, fixture.wait_for_cloudinit)
313
318
 
314
 
        message = unicode(exception)
 
319
        message = text_type(exception)
315
320
        self.assertIn(
316
321
            "Cloud-init initialization on virtual machine %s timed out"
317
322
            % fixture.name,
328
333
 
329
334
        exception = self.assertRaises(RuntimeError, fixture.wait_for_cloudinit)
330
335
 
331
 
        message = unicode(exception)
 
336
        message = text_type(exception)
332
337
        self.assertIn(
333
338
            "Error contacting virtual machine %s" % fixture.name,
334
339
            message)
453
458
        architecture = "test-arch"
454
459
        return_value = randint(0, 3)
455
460
        input = self.getUniqueString()
456
 
        stdout_content = self.getUniqueString()
457
 
        stderr_content = self.getUniqueString()
 
461
        stdout_content = self.getUniqueString().encode("ascii")
 
462
        stderr_content = self.getUniqueString().encode("ascii")
458
463
        self.patch_run_command(
459
464
            return_value, stdout=stdout_content, stderr=stderr_content)
460
465
        self.patch(
467
472
            details = fixture.getDetails()
468
473
 
469
474
        self.assertThat(
470
 
            details['cmd #1'].as_text(),
 
475
            details['cmd #0001'].as_text(),
471
476
            MatchesRegex('ssh .* echo Hello$'))
472
477
        self.assertEqual(
473
478
            (
474
 
                unicode(return_value),
 
479
                text_type(return_value),
475
480
                input,
476
 
                stdout_content,
477
 
                stderr_content,
 
481
                binary_content(stdout_content),
 
482
                binary_content(stderr_content),
478
483
            ),
479
484
            (
480
 
                details['cmd #1 retcode'].as_text(),
481
 
                details['cmd #1 input'].as_text(),
482
 
                details['cmd #1 stdout'].as_text(),
483
 
                details['cmd #1 stderr'].as_text(),
 
485
                details['cmd #0001 retcode'].as_text(),
 
486
                details['cmd #0001 input'].as_text(),
 
487
                details['cmd #0001 stdout'],
 
488
                details['cmd #0001 stderr'],
484
489
            ))
485
490
 
486
491
 
507
512
              <model type='virtio'/>
508
513
            </interface>"""]
509
514
        self.assertEqual(
510
 
            map(xml_normalize, expected_interfaces),
511
 
            map(etree.tostring, interface_tags))
 
515
            [xml_normalize(iface) for iface in expected_interfaces],
 
516
            [etree.tostring(iface) for iface in interface_tags])
512
517
 
513
518
    def test_kvm_template_with_direct_interface_has_two_interface(self):
514
519
        interface = self.getUniqueString()
528
533
            </interface>""" % interface,
529
534
        ]
530
535
        self.assertEqual(
531
 
            map(xml_normalize, expected_interfaces),
532
 
            map(etree.tostring, interface_tags))
 
536
            [xml_normalize(iface) for iface in expected_interfaces],
 
537
            [etree.tostring(iface) for iface in interface_tags])
533
538
 
534
539
 
535
540
class TestNetworkConfigGeneration(testtools.TestCase):