~rvb/maas/transaction-1.7-bug-1409852

« back to all changes in this revision

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

merged upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import celery
29
29
from celery import states
30
30
from celery.app import app_or_default
31
 
from celery.task import Task
32
31
from maastesting.celery import CeleryFixture
33
32
from maastesting.factory import factory
34
33
from maastesting.fakemethod import (
62
61
    DNSForwardZoneConfig,
63
62
    DNSReverseZoneConfig,
64
63
    )
65
 
from provisioningserver.import_images import boot_resources
66
 
from provisioningserver.power.poweraction import PowerActionFail
67
64
from provisioningserver.tags import MissingCredentials
68
65
from provisioningserver.tasks import (
69
66
    enlist_nodes_from_mscm,
70
67
    enlist_nodes_from_ucsm,
71
 
    import_boot_images,
72
68
    Omshell,
73
 
    power_off,
74
 
    power_on,
75
69
    refresh_secrets,
76
70
    remove_dhcp_host_map,
77
71
    report_boot_images,
88
82
    write_full_dns_config,
89
83
    )
90
84
from provisioningserver.testing.boot_images import make_boot_image_params
91
 
from provisioningserver.testing.config import (
92
 
    BootSourcesFixture,
93
 
    set_tftp_root,
94
 
    )
 
85
from provisioningserver.testing.config import set_tftp_root
95
86
from provisioningserver.testing.testcase import PservTestCase
96
 
from provisioningserver.utils import filter_dict
97
87
import provisioningserver.utils.fs as fs_module
98
88
from provisioningserver.utils.shell import ExternalProcessError
99
89
from testresources import FixtureResource
104
94
    MatchesListwise,
105
95
    )
106
96
 
107
 
# An arbitrary MAC address.  Not using a properly random one here since
108
 
# we might accidentally affect real machines on the network.
109
 
arbitrary_mac = "AA:BB:CC:DD:EE:FF"
110
 
 
111
97
 
112
98
celery_config = app_or_default().conf
113
99
 
142
128
        self.assertEqual(nodegroup_uuid, cache.cache.get('nodegroup_uuid'))
143
129
 
144
130
 
145
 
class TestPowerTasks(PservTestCase):
146
 
 
147
 
    resources = (
148
 
        ("celery", FixtureResource(CeleryFixture())),
149
 
        )
150
 
 
151
 
    def test_ether_wake_power_on_with_not_enough_template_args(self):
152
 
        # In eager test mode the assertion is raised immediately rather
153
 
        # than being stored in the AsyncResult, so we need to test for
154
 
        # that instead of using result.get().
155
 
        self.assertRaises(
156
 
            PowerActionFail, power_on.delay, "ether_wake")
157
 
 
158
 
    def test_ether_wake_power_on(self):
159
 
        result = power_on.delay(
160
 
            "ether_wake", mac_address=arbitrary_mac)
161
 
        self.assertTrue(result.successful())
162
 
 
163
 
    def test_ether_wake_does_not_support_power_off(self):
164
 
        self.assertRaises(
165
 
            PowerActionFail, power_off.delay,
166
 
            "ether_wake", mac=arbitrary_mac)
167
 
 
168
 
 
169
131
class TestDHCPTasks(PservTestCase):
170
132
 
171
133
    resources = (
537
499
            '//node', tag_nsmap=None, retry=True)
538
500
 
539
501
 
540
 
class TestImportBootImages(PservTestCase):
541
 
 
542
 
    def make_archive_url(self, name=None):
543
 
        if name is None:
544
 
            name = factory.make_name('archive')
545
 
        return 'http://%s.example.com/%s' % (name, factory.make_name('path'))
546
 
 
547
 
    def patch_boot_resources_function(self):
548
 
        """Patch out `boot_resources.import_images`.
549
 
 
550
 
        Returns the installed fake.  After the fake has been called, but not
551
 
        before, its `env` attribute will have a copy of the environment dict.
552
 
        """
553
 
 
554
 
        class CaptureEnv:
555
 
            """Fake function; records a copy of the environment."""
556
 
 
557
 
            def __call__(self, *args, **kwargs):
558
 
                self.args = args
559
 
                self.env = os.environ.copy()
560
 
 
561
 
        return self.patch(boot_resources, 'import_images', CaptureEnv())
562
 
 
563
 
    def test_import_boot_images_integrates_with_boot_resources_function(self):
564
 
        # If the config specifies no sources, nothing will be imported.  But
565
 
        # the task succeeds without errors.
566
 
        fixture = self.useFixture(BootSourcesFixture([]))
567
 
        self.patch(boot_resources, 'logger')
568
 
        self.patch(boot_resources, 'locate_config').return_value = (
569
 
            fixture.filename)
570
 
        import_boot_images(sources=[])
571
 
        self.assertIsInstance(import_boot_images, Task)
572
 
 
573
 
    def test_import_boot_images_sets_GPGHOME(self):
574
 
        home = self.make_dir()
575
 
        self.patch(tasks, 'get_maas_user_gpghome').return_value = home
576
 
        fake = self.patch_boot_resources_function()
577
 
        import_boot_images(sources=[])
578
 
        self.assertEqual(home, fake.env['GNUPGHOME'])
579
 
 
580
 
    def test_import_boot_images_sets_proxy_if_given(self):
581
 
        proxy = 'http://%s.example.com' % factory.make_name('proxy')
582
 
        proxy_vars = ['http_proxy', 'https_proxy']
583
 
        fake = self.patch_boot_resources_function()
584
 
        import_boot_images(sources=[], http_proxy=proxy)
585
 
        self.assertEqual(
586
 
            {
587
 
                var: proxy
588
 
                for var in proxy_vars
589
 
            },
590
 
            filter_dict(fake.env, proxy_vars))
591
 
 
592
 
    def test_import_boot_images_leaves_proxy_unchanged_if_not_given(self):
593
 
        proxy_vars = ['http_proxy', 'https_proxy']
594
 
        fake = self.patch_boot_resources_function()
595
 
        import_boot_images(sources=[])
596
 
        self.assertEqual({}, filter_dict(fake.env, proxy_vars))
597
 
 
598
 
    def test_import_boot_images_calls_callback(self):
599
 
        self.patch_boot_resources_function()
600
 
        mock_callback = Mock()
601
 
        import_boot_images(sources=[], callback=mock_callback)
602
 
        self.assertThat(mock_callback.delay, MockCalledOnceWith())
603
 
 
604
 
    def test_import_boot_images_accepts_sources_parameter(self):
605
 
        fake = self.patch(boot_resources, 'import_images')
606
 
        sources = [
607
 
            {
608
 
                'path': "http://example.com",
609
 
                'selections': [
610
 
                    {
611
 
                        'release': "trusty",
612
 
                        'arches': ["amd64"],
613
 
                        'subarches': ["generic"],
614
 
                        'labels': ["release"]
615
 
                    },
616
 
                ],
617
 
            },
618
 
        ]
619
 
        import_boot_images(sources=sources)
620
 
        self.assertThat(fake, MockCalledOnceWith(sources))
621
 
 
622
 
 
623
502
class TestAddUCSM(PservTestCase):
624
503
 
625
504
    def test_enlist_nodes_from_ucsm(self):