~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/provisioningserver/tests/test_api.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-04-12 16:46:22 UTC
  • mto: (20.1.1 quantal) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20120412164622-laz1qoxycfrddka0
Tags: upstream-0.1+bzr462+dfsg
ImportĀ upstreamĀ versionĀ 0.1+bzr462+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
from provisioningserver.cobblerclient import CobblerSystem
35
35
from provisioningserver.enum import POWER_TYPE
36
36
from provisioningserver.interfaces import IProvisioningAPI
37
 
from provisioningserver.testing.factory import (
38
 
    fake_node_metadata,
39
 
    ProvisioningFakeFactory,
40
 
    )
 
37
from provisioningserver.testing.factory import ProvisioningFakeFactory
41
38
from provisioningserver.testing.fakeapi import FakeAsynchronousProvisioningAPI
42
39
from provisioningserver.testing.fakecobbler import make_fake_cobbler_session
43
40
from provisioningserver.testing.realcobbler import RealCobbler
69
66
            "hostname": "dystopia",
70
67
            "interfaces": {
71
68
                "eth0": {"mac_address": "12:34:56:78:9a:bc"},
 
69
                "eth1": {"mac_address": "  "},
 
70
                "eth2": {"mac_address": ""},
 
71
                "eth3": {"mac_address": None},
72
72
                },
73
73
            "power_type": "virsh",
 
74
            "netboot_enabled": False,
74
75
            "ju": "nk",
75
76
            }
76
77
        expected = {
78
79
            "profile": "earth",
79
80
            "hostname": "dystopia",
80
81
            "mac_addresses": ["12:34:56:78:9a:bc"],
 
82
            "netboot_enabled": False,
81
83
            "power_type": "virsh",
82
84
            }
83
85
        observed = cobbler_to_papi_node(data)
89
91
            "profile": "earth",
90
92
            "hostname": "darksaga",
91
93
            "power_type": "ether_wake",
 
94
            "netboot_enabled": True,
92
95
            "ju": "nk",
93
96
            }
94
97
        expected = {
96
99
            "profile": "earth",
97
100
            "hostname": "darksaga",
98
101
            "mac_addresses": [],
 
102
            "netboot_enabled": True,
99
103
            "power_type": "ether_wake",
100
104
            }
101
105
        observed = cobbler_to_papi_node(data)
257
261
            current_interfaces, hostname, mac_addresses)
258
262
        self.assertItemsEqual(expected, observed)
259
263
 
 
264
    def test_gen_cobbler_interface_deltas_remove_all_macs(self):
 
265
        # Removing all MAC addresses results in a delta to remove all but the
 
266
        # first interface. The first interface is instead deconfigured; this
 
267
        # is necessary to satisfy the Cobbler data model.
 
268
        current_interfaces = {
 
269
            "eth0": {
 
270
                "mac_address": "11:11:11:11:11:11",
 
271
                },
 
272
            "eth1": {
 
273
                "mac_address": "22:22:22:22:22:22",
 
274
                },
 
275
            }
 
276
        hostname = "empiricism"
 
277
        mac_addresses = []
 
278
        expected = [
 
279
            {"interface": "eth0",
 
280
             "mac_address": "",
 
281
             "dns_name": ""},
 
282
            {"interface": "eth1",
 
283
             "delete_interface": True},
 
284
            ]
 
285
        observed = gen_cobbler_interface_deltas(
 
286
            current_interfaces, hostname, mac_addresses)
 
287
        self.assertItemsEqual(expected, observed)
 
288
 
260
289
 
261
290
class ProvisioningAPITests(ProvisioningFakeFactory):
262
291
    """Tests for `provisioningserver.api.ProvisioningAPI`.
394
423
            [mac_address2], values[node_name]["mac_addresses"])
395
424
 
396
425
    @inlineCallbacks
 
426
    def test_modify_nodes_set_netboot_enabled(self):
 
427
        papi = self.get_provisioning_api()
 
428
        node_name = yield self.add_node(papi)
 
429
        yield papi.modify_nodes({node_name: {"netboot_enabled": False}})
 
430
        values = yield papi.get_nodes_by_name([node_name])
 
431
        self.assertFalse(values[node_name]["netboot_enabled"])
 
432
        yield papi.modify_nodes({node_name: {"netboot_enabled": True}})
 
433
        values = yield papi.get_nodes_by_name([node_name])
 
434
        self.assertTrue(values[node_name]["netboot_enabled"])
 
435
 
 
436
    @inlineCallbacks
 
437
    def test_modify_nodes_remove_all_mac_addresses(self):
 
438
        papi = self.get_provisioning_api()
 
439
        node_name = yield self.add_node(papi)
 
440
        mac_address = factory.getRandomMACAddress()
 
441
        yield papi.modify_nodes(
 
442
            {node_name: {"mac_addresses": [mac_address]}})
 
443
        yield papi.modify_nodes(
 
444
            {node_name: {"mac_addresses": []}})
 
445
        values = yield papi.get_nodes_by_name([node_name])
 
446
        self.assertEqual(
 
447
            [], values[node_name]["mac_addresses"])
 
448
 
 
449
    @inlineCallbacks
397
450
    def test_delete_distros_by_name(self):
398
451
        # Create a distro via the Provisioning API.
399
452
        papi = self.get_provisioning_api()
545
598
        self.assertItemsEqual(
546
599
            dict(zip(power_types, power_types)), cobbler_power_types)
547
600
 
 
601
    @inlineCallbacks
 
602
    def test_add_node_provides_preseed(self):
 
603
        papi = self.get_provisioning_api()
 
604
        preseed_data = factory.getRandomString()
 
605
        node_name = yield self.add_node(papi, preseed_data=preseed_data)
 
606
        attrs = yield CobblerSystem(papi.session, node_name).get_values()
 
607
        self.assertEqual(
 
608
            preseed_data, b64decode(attrs['ks_meta']['MAAS_PRESEED']))
 
609
 
548
610
 
549
611
class TestFakeProvisioningAPI(ProvisioningAPITests, TestCase):
550
612
    """Test :class:`FakeAsynchronousProvisioningAPI`.
569
631
        """Return a real ProvisioningAPI, but using a fake Cobbler session."""
570
632
        return ProvisioningAPI(make_fake_cobbler_session())
571
633
 
572
 
    @inlineCallbacks
573
 
    def test_add_node_preseeds_metadata(self):
574
 
        papi = self.get_provisioning_api()
575
 
        metadata = fake_node_metadata()
576
 
        node_name = yield self.add_node(papi, metadata=metadata)
577
 
        attrs = yield CobblerSystem(papi.session, node_name).get_values()
578
 
        preseed = attrs['ks_meta']['MAAS_PRESEED']
579
 
        preseed = b64decode(preseed)
580
 
        self.assertIn(metadata['maas-metadata-url'], preseed)
581
 
        self.assertIn(metadata['maas-metadata-credentials'], preseed)
582
 
 
583
634
 
584
635
class TestProvisioningAPIWithRealCobbler(ProvisioningAPITests,
585
636
                                         ProvisioningAPITestsWithCobbler,