~ubuntu-branches/ubuntu/trusty/maas/trusty-security

« back to all changes in this revision

Viewing changes to src/provisioningserver/utils/tests/test_utils.py

  • Committer: Package Import Robot
  • Author(s): Greg Lutostanski
  • Date: 2014-08-29 13:27:34 UTC
  • mto: (61.1.4 trusty-proposed)
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: package-import@ubuntu.com-20140829132734-d47evihju2etdwcy
Tags: upstream-1.5.4+bzr2294
ImportĀ upstreamĀ versionĀ 1.5.4+bzr2294

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
    ExternalProcessError,
76
76
    filter_dict,
77
77
    find_ip_via_arp,
 
78
    find_mac_via_arp,
78
79
    get_all_interface_addresses,
79
80
    get_mtime,
80
81
    incremental_write,
1322
1323
        self.assertEqual("192.168.0.1", ip_address_observed)
1323
1324
 
1324
1325
 
 
1326
class TestFindMACViaARP(MAASTestCase):
 
1327
 
 
1328
    def patch_call(self, output):
 
1329
        """Replace `call_capture_and_check` with one that returns `output`."""
 
1330
        fake = self.patch(provisioningserver.utils, 'call_capture_and_check')
 
1331
        fake.return_value = output
 
1332
        return fake
 
1333
 
 
1334
    def test__resolves_IP_address_to_MAC(self):
 
1335
        sample = """\
 
1336
        Address HWtype  HWaddress Flags Mask            Iface
 
1337
        192.168.100.20 (incomplete)                              virbr1
 
1338
        192.168.0.104 (incomplete)                              eth0
 
1339
        192.168.0.5 (incomplete)                              eth0
 
1340
        192.168.0.2 (incomplete)                              eth0
 
1341
        192.168.0.100 (incomplete)                              eth0
 
1342
        192.168.122.20 ether   52:54:00:02:86:4b   C                     virbr0
 
1343
        192.168.0.4 (incomplete)                              eth0
 
1344
        192.168.0.1 ether   90:f6:52:f6:17:92   C                     eth0
 
1345
        """
 
1346
 
 
1347
        call_capture_and_check = self.patch_call(sample)
 
1348
        mac_address_observed = find_mac_via_arp("192.168.122.20")
 
1349
        self.assertThat(
 
1350
            call_capture_and_check,
 
1351
            MockCalledOnceWith(['arp', '-n']))
 
1352
        self.assertEqual("52:54:00:02:86:4b", mac_address_observed)
 
1353
 
 
1354
    def test__returns_consistent_output(self):
 
1355
        ip = factory.getRandomIPAddress()
 
1356
        macs = [
 
1357
            '52:54:00:02:86:4b',
 
1358
            '90:f6:52:f6:17:92',
 
1359
            ]
 
1360
        lines = ['%s ether %s C eth0' % (ip, mac) for mac in macs]
 
1361
        self.patch_call('\n'.join(lines))
 
1362
        one_result = find_mac_via_arp(ip)
 
1363
        self.patch_call('\n'.join(reversed(lines)))
 
1364
        other_result = find_mac_via_arp(ip)
 
1365
 
 
1366
        self.assertIn(one_result, macs)
 
1367
        self.assertEqual(one_result, other_result)
 
1368
 
 
1369
 
1325
1370
class TestAsynchronousDecorator(MAASTestCase):
1326
1371
 
1327
1372
    run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=5)