Пример #1
0
    def test_getConfiguration_returns_configuration_object(self):
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        servers, peers = self.make_servers_and_peers()
        rpc_service, protocol = yield prepareRegion(
            self,
            is_region=is_region,
            is_rack=is_rack,
            servers=servers,
            peers=peers,
        )
        service, ntp = self.make_RackNTP_ExternalService(rpc_service, reactor)
        config = yield service._getConfiguration()
        observed = ntp._getConfiguration(
            config.controller_type, config.time_configuration
        )

        self.assertThat(observed, IsInstance(external._NTPConfiguration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(
                references=servers,
                peers=peers,
                is_region=is_region,
                is_rack=is_rack,
            ),
        )
Пример #2
0
    def test_getConfiguration_returns_configuration_object(self):
        port = factory.pick_port()
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        rpc_service, protocol = yield prepareRegion(
            self, is_region=is_region, is_rack=is_rack, syslog_port=port
        )
        forwarders = self.extract_regions(rpc_service)
        service, syslog = self.make_RackSyslog_ExternalService(
            rpc_service, reactor
        )
        yield service.startService()
        self.addCleanup((yield service.stopService))

        config = yield service._getConfiguration()
        observed = syslog._getConfiguration(
            config.controller_type,
            config.syslog_configuration,
            config.connections,
        )

        self.assertThat(observed, IsInstance(external._SyslogConfiguration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(
                port=port,
                forwarders=forwarders,
                is_region=is_region,
                is_rack=is_rack,
            ),
        )
Пример #3
0
    def test_getConfiguration_returns_configuration_object(self):
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        trusted_networks = self.make_trusted_networks()
        rpc_service, protocol = yield prepareRegion(
            self,
            is_region=is_region,
            is_rack=is_rack,
            trusted_networks=trusted_networks,
        )
        region_ips = self.extract_regions(rpc_service)
        service, dns = self.make_RackDNS_ExternalService(rpc_service, reactor)
        yield service.startService()
        self.addCleanup((yield service.stopService))

        config = yield service._getConfiguration()
        observed = dns._getConfiguration(
            config.controller_type,
            config.dns_configuration,
            config.connections,
        )

        self.assertThat(observed, IsInstance(external._DNSConfiguration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(
                upstream_dns=region_ips,
                trusted_networks=trusted_networks,
                is_region=is_region,
                is_rack=is_rack,
            ),
        )
Пример #4
0
    def test__getConfiguration_returns_configuration_object(self):
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        allowed_cidrs = self.make_cidrs()
        proxy_enabled = factory.pick_bool()
        proxy_prefer_v4_proxy = factory.pick_bool()
        proxy_port = random.randint(1000, 8000)
        rpc_service, protocol = yield prepareRegion(
            self,
            is_region=is_region,
            is_rack=is_rack,
            proxy_enabled=proxy_enabled,
            proxy_allowed_cidrs=allowed_cidrs,
            proxy_port=proxy_port,
            proxy_prefer_v4_proxy=proxy_prefer_v4_proxy)
        region_ips = self.extract_regions(rpc_service)
        service, proxy = self.make_RackProxy_ExternalService(
            rpc_service, reactor)
        yield service.startService()
        self.addCleanup((yield service.stopService))

        config = yield service._getConfiguration()
        observed = proxy._getConfiguration(config.controller_type,
                                           config.proxy_configuration,
                                           config.connections)

        self.assertThat(observed, IsInstance(external._ProxyConfiguration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(enabled=proxy_enabled,
                                        port=proxy_port,
                                        allowed_cidrs=allowed_cidrs,
                                        prefer_v4_proxy=proxy_prefer_v4_proxy,
                                        upstream_proxies=region_ips,
                                        is_region=is_region,
                                        is_rack=is_rack))
Пример #5
0
 def test_allows_trailing_and_starting_double_colon(self):
     # we get random network addresses above, but lets play with a few that
     # we know need to work.
     addrs = ['::1', '::f', 'fe80::', 'fe80::1', 'fe80:37::3:1']
     for addr in addrs:
         # lower case
         url = factory.make_simple_http_url(netloc="[%s]" % addr,
                                            port=factory.pick_bool())
         self.assertEqual(url, self.validator.to_python(url),
                          "url: %s" % url)
         # upper case
         url = factory.make_simple_http_url(netloc="[%s]" % addr.upper(),
                                            port=factory.pick_bool())
         self.assertEqual(url, self.validator.to_python(url),
                          "url: %s" % url)
Пример #6
0
 def test_get_schema(self):
     fake_name = factory.make_name("name")
     fake_description = factory.make_name("description")
     fake_setting = factory.make_name("setting")
     fake_chassis = factory.pick_bool()
     fake_settings = [
         make_setting_field(fake_setting, fake_setting.title())
     ]
     fake_driver = make_power_driver_base(
         name=fake_name,
         description=fake_description,
         chassis=fake_chassis,
         settings=fake_settings,
     )
     self.assertEqual(
         {
             "driver_type": "power",
             "name": fake_name,
             "description": fake_description,
             "chassis": fake_chassis,
             "can_probe": False,
             "fields": fake_settings,
             "queryable": fake_driver.queryable,
             "missing_packages": fake_driver.detect_missing_packages(),
         },
         fake_driver.get_schema(),
     )
Пример #7
0
 def test_debug_enabled_cached(self):
     debug = factory.pick_bool()
     self.useFixture(ClusterConfigurationFixture(debug=debug))
     # Prime cache
     debug_enabled()
     mock_open = self.patch(config_module.ClusterConfiguration, "open")
     self.assertEqual(debug, debug_enabled())
     mock_open.assert_not_called()
Пример #8
0
    def test__getConfiguration_returns_configuration_object(self):
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        servers, peers = self.make_servers_and_peers()
        rpc_service, protocol = yield prepareRegion(self,
                                                    is_region=is_region,
                                                    is_rack=is_rack,
                                                    servers=servers,
                                                    peers=peers)
        service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor)
        observed = yield service._getConfiguration()

        self.assertThat(observed, IsInstance(ntp._Configuration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(references=servers,
                                        peers=peers,
                                        is_region=is_region,
                                        is_rack=is_rack))
Пример #9
0
 def test__creates_omshell_with_correct_arguments(self):
     omshell = self.patch(dhcp, "Omshell")
     server = Mock()
     server.ipv6 = factory.pick_bool()
     dhcp._update_hosts(server, [], [], [])
     self.assertThat(omshell, MockCallsMatch(
         call(
             ipv6=server.ipv6, server_address="127.0.0.1",
             shared_key=server.omapi_key),
     ))
Пример #10
0
    def test_compose_template_namespace_include_debug(self):
        debug = factory.pick_bool()
        boot.debug_enabled.cache_clear()
        self.addClassCleanup(boot.debug_enabled.cache_clear)
        self.useFixture(ClusterConfigurationFixture(debug=debug))
        kernel_params = make_kernel_parameters()
        method = FakeBootMethod()

        template_namespace = method.compose_template_namespace(kernel_params)

        self.assertEqual(debug, template_namespace["debug"])
Пример #11
0
def make_context():
    """Make and return a context for IPMI Power Driver."""
    ret = {
        "power_address": factory.make_name("power_address"),
        "power_user": factory.make_name("power_user"),
        "power_pass": factory.make_name("power_pass"),
        "power_driver": factory.make_name("power_driver"),
        "power_off_mode": factory.make_name("power_off_mode"),
        "ipmipower": "ipmipower",
        "ipmi_chassis_config": "ipmi-chassis-config",
    }
    # These options were added in MAAS 2.9. Add them optionally
    # to test users coming from older versions.
    if factory.pick_bool():
        ret["k_g"] = factory.make_name("k_g")
    if factory.pick_bool():
        ret["cipher_suite_id"] = random.choice(IPMI_CIPHER_SUITE_ID_CHOICES)[0]
    if factory.pick_bool():
        ret["privilege_level"] = random.choice(IPMI_PRIVILEGE_LEVEL_CHOICES)[0]
    return ret
Пример #12
0
    def test_tryUpdate_updates_proxy_server(self):
        self.useFixture(MAASRootFixture())
        allowed_cidrs = self.make_cidrs()
        proxy_prefer_v4_proxy = factory.pick_bool()
        proxy_port = random.randint(1000, 8000)
        rpc_service, _ = yield prepareRegion(
            self,
            proxy_allowed_cidrs=allowed_cidrs,
            proxy_port=proxy_port,
            proxy_prefer_v4_proxy=proxy_prefer_v4_proxy,
        )
        region_ips = self.extract_regions(rpc_service)
        service, _ = self.make_RackProxy_ExternalService(rpc_service, reactor)

        write_config = self.patch_autospec(
            external.proxy_config, "write_config"
        )
        service_monitor = self.patch_autospec(external, "service_monitor")

        yield service.startService()
        self.addCleanup((yield service.stopService))

        yield service._orig_tryUpdate()

        expected_peers = sorted(
            ["http://%s:%s" % (ip, proxy_port) for ip in region_ips]
        )
        self.assertThat(
            write_config,
            MockCalledOnceWith(
                allowed_cidrs,
                peer_proxies=expected_peers,
                prefer_v4_proxy=proxy_prefer_v4_proxy,
                maas_proxy_port=proxy_port,
            ),
        )
        self.assertThat(
            service_monitor.reloadService, MockCalledOnceWith("proxy_rack")
        )
        # If the configuration has not changed then a second call to
        # `_tryUpdate` does not result in another call to `_configure`.
        yield service._orig_tryUpdate()
        self.assertThat(
            write_config,
            MockCalledOnceWith(
                allowed_cidrs,
                peer_proxies=expected_peers,
                prefer_v4_proxy=proxy_prefer_v4_proxy,
                maas_proxy_port=proxy_port,
            ),
        )
        self.assertThat(
            service_monitor.reloadService, MockCalledOnceWith("proxy_rack")
        )
Пример #13
0
    def test__getConfiguration_returns_configuration_object(self):
        is_region, is_rack = factory.pick_bool(), factory.pick_bool()
        trusted_networks = self.make_trusted_networks()
        rpc_service, protocol = yield prepareRegion(
            self,
            is_region=is_region,
            is_rack=is_rack,
            trusted_networks=trusted_networks)
        region_ips = self.extract_regions(rpc_service)
        service = self.make_startable_RackDNSService(rpc_service, reactor)
        yield service.startService()
        self.addCleanup((yield service.stopService))
        observed = yield service._getConfiguration()

        self.assertThat(observed, IsInstance(dns._Configuration))
        self.assertThat(
            observed,
            MatchesStructure.byEquality(upstream_dns=region_ips,
                                        trusted_networks=trusted_networks,
                                        is_region=is_region,
                                        is_rack=is_rack))
Пример #14
0
    def test_requires_brackets_on_ipv6_address(self):
        name = "[%s]" % factory.make_ipv6_address()
        url = factory.make_simple_http_url(netloc=name,
                                           port=factory.pick_bool())
        self.assertEqual(url, self.validator.to_python(url), "url: %s" % url)

        # rejects bare ipv6 address
        name = "%s" % factory.make_ipv6_address()
        url = factory.make_simple_http_url(netloc=name)
        with ExpectedException(formencode.Invalid, 'That is not a valid URL'):
            self.assertEqual(url, self.validator.to_python(url),
                             "url: %s" % url)
Пример #15
0
 def test_machine_fromdict(self):
     hostname = factory.make_name("hostname")
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [dict() for _ in range(3)]
     block_devices = [
         dict(
             size=random.randint(512, 1024),
             tags=[factory.make_name("tag") for _ in range(3)],
         ) for _ in range(3)
     ]
     pinned_cores = random.sample(range(10), 3)
     hugepages_backed = factory.pick_bool()
     machine_data = dict(
         hostname=hostname,
         architecture="amd64/generic",
         cores=cores,
         cpu_speed=cpu_speed,
         pinned_cores=pinned_cores,
         hugepages_backed=hugepages_backed,
         memory=memory,
         interfaces=interfaces,
         block_devices=block_devices,
     )
     machine = RequestedMachine.fromdict(machine_data)
     self.assertThat(machine, IsInstance(RequestedMachine))
     self.assertThat(
         machine,
         MatchesStructure(
             hostname=Equals(hostname),
             architecture=Equals("amd64/generic"),
             cores=Equals(cores),
             cpu_speed=Equals(cpu_speed),
             memory=Equals(memory),
             pinned_cores=Equals(pinned_cores),
             hugepages_backed=Equals(hugepages_backed),
             interfaces=MatchesListwise([
                 IsInstance(RequestedMachineInterface)
                 for interface in interfaces
             ]),
             block_devices=MatchesListwise([
                 MatchesAll(
                     IsInstance(RequestedMachineBlockDevice),
                     MatchesStructure(
                         size=Equals(block_device["size"]),
                         tags=Equals(block_device["tags"]),
                     ),
                 ) for block_device in block_devices
             ]),
         ),
     )
Пример #16
0
 def test__performs_operations(self):
     omshell = Mock()
     self.patch(dhcp, "Omshell").return_value = omshell
     remove_host = make_host()
     add_host = make_host()
     modify_host = make_host()
     server = Mock()
     server.ipv6 = factory.pick_bool()
     dhcp._update_hosts(server, [remove_host], [add_host], [modify_host])
     self.assertThat(omshell.remove,
                     MockCallsMatch(call(remove_host["mac"]), ))
     self.assertThat(
         omshell.create,
         MockCallsMatch(call(add_host["ip"], add_host["mac"]), ))
     self.assertThat(
         omshell.modify,
         MockCallsMatch(call(modify_host["ip"], modify_host["mac"]), ))
Пример #17
0
def make_fake_service(expected_state=None, status_info=None):
    fake_name = factory.make_name("name")
    fake_service_name = factory.make_name("service")
    if expected_state is None:
        if factory.pick_bool():
            expected_state = SERVICE_STATE.ON
        else:
            expected_state = SERVICE_STATE.OFF

    class FakeService(Service):
        name = fake_name
        service_name = fake_service_name
        snap_service_name = fake_service_name

        def getExpectedState(self):
            return succeed((expected_state, status_info))

    return FakeService()
Пример #18
0
 def test_disabled_boot_architecture(self):
     if factory.pick_bool():
         ipv6 = True
         ip = factory.make_ipv6_address()
     else:
         ipv6 = False
         ip = factory.make_ipv4_address()
     disabled_arches = random.sample(
         [
             boot_method for _, boot_method in BootMethodRegistry
             if boot_method.arch_octet or boot_method.user_class
         ],
         3,
     )
     output = config.compose_conditional_bootloader(
         ipv6, ip, [bm.name for bm in disabled_arches])
     for disabled_arch in disabled_arches:
         if disabled_arch.arch_octet:
             self.assertNotIn(disabled_arch.arch_octet, output)
         else:
             self.assertNotIn(disabled_arch.user_class, output)
Пример #19
0
    def test_accepts_all_args(self):
        all_test_arguments = cluster_config_command.all_arguments

        default_arg_values = {
            '--region-url': None,
            '--uuid': None,
            '--init': False,
            '--tftp-port': None,
            '--tftp-root': None,
            '--debug': None,
        }

        failures = {}

        # Try all cardinalities of combinations of arguments
        for r in range(len(all_test_arguments) + 1):
            for test_arg_names in combinations(all_test_arguments, r):
                test_values = {
                    '--region-url': factory.make_simple_http_url(),
                    '--uuid': str(uuid.uuid4()),
                    '--init': '',
                    '--tftp-port': str(factory.pick_port()),
                    '--tftp-root': factory.make_string(),
                    '--debug': str(factory.pick_bool()),
                }

                # Build a query dictionary for the given combination of args
                args_under_test = []
                for param_name in test_arg_names:
                    args_under_test.append(param_name)
                    if param_name != '--init':
                        args_under_test.append(test_values[param_name])

                parser = ArgumentParser()
                cluster_config_command.add_arguments(parser)

                # If both init and uuid are passed, argparse will generate
                # a nice ArgumentError exception, which unfortunately,
                # gets caught and sent to exit.
                if '--init' in test_arg_names and '--uuid' in test_arg_names:
                    expected_exception = ExpectedException(SystemExit, '2')
                    with expected_exception, patch('sys.stderr'):
                        parser.parse_known_args(args_under_test)

                else:
                    # Otherwise, parsed args with defaults as usual
                    observed_args = vars(parser.parse_args(args_under_test))

                    expected_args = {}
                    for param_name in all_test_arguments:
                        parsed_param_name = param_name[2:].replace('-', '_')

                        if param_name not in test_arg_names:
                            expected_args[parsed_param_name] = \
                                default_arg_values[param_name]
                        else:
                            expected_args[parsed_param_name] = \
                                observed_args[parsed_param_name]

                    if expected_args != observed_args:
                        failures[str(test_arg_names)] = {
                            'expected_args': expected_args,
                            'observed_args': observed_args,
                        }

        error_message = io.StringIO()
        error_message.write("One or more key / value argument list(s)"
                            "passed in the query string (expected_args)"
                            "to the API do not match the values in "
                            "the returned query string. This "
                            "means that some arguments were "
                            "dropped / added / changed by the "
                            "the function, which is incorrect "
                            "behavior. The list of incorrect "
                            "arguments is as follows: \n")
        pp = pprint.PrettyPrinter(depth=3, stream=error_message)
        pp.pprint(failures)
        self.assertDictEqual({}, failures, error_message.getvalue())
Пример #20
0
def make_script(
        scripts_dir=None, with_added_attribs=True, name=None,
        script_version_id=None, timeout_seconds=None, parallel=None,
        hardware_type=None, with_output=True):
    if name is None:
        name = factory.make_name('name')
    if script_version_id is None:
        script_version_id = random.randint(1, 1000)
    if timeout_seconds is None:
        timeout_seconds = random.randint(1, 1000)
    if parallel is None:
        parallel = random.randint(0, 2)
    if hardware_type is None:
        hardware_type = random.randint(0, 4)
    global SCRIPT_RESULT_ID
    script_result_id = SCRIPT_RESULT_ID
    SCRIPT_RESULT_ID += 1
    ret = {
        'name': name,
        'path': '%s/%s' % (random.choice(['commissioning', 'testing']), name),
        'script_result_id': script_result_id,
        'script_version_id': script_version_id,
        'timeout_seconds': timeout_seconds,
        'parallel': parallel,
        'hardware_type': hardware_type,
        'args': {},
        'has_started': factory.pick_bool(),
    }
    ret['msg_name'] = '%s (id: %s, script_version_id: %s)' % (
        name, script_result_id, script_version_id)
    if with_added_attribs:
        if scripts_dir is None:
            scripts_dir = factory.make_name('scripts_dir')
        out_dir = os.path.join(
            scripts_dir, 'out', '%s.%s' % (name, script_result_id))

        ret['combined_name'] = name
        ret['combined_path'] = os.path.join(out_dir, ret['combined_name'])
        ret['combined'] = factory.make_string()
        ret['stdout_name'] = '%s.out' % name
        ret['stdout_path'] = os.path.join(out_dir, ret['stdout_name'])
        ret['stdout'] = factory.make_string()
        ret['stderr_name'] = '%s.err' % name
        ret['stderr_path'] = os.path.join(out_dir, ret['stderr_name'])
        ret['stderr'] = factory.make_string()
        ret['result_name'] = '%s.yaml' % name
        ret['result_path'] = os.path.join(out_dir, ret['result_name'])
        ret['result'] = factory.make_string()
        ret['download_path'] = os.path.join(scripts_dir, 'downloads', name)

        if os.path.exists(scripts_dir):
            os.makedirs(out_dir, exist_ok=True)
            os.makedirs(ret['download_path'], exist_ok=True)
            script_path = os.path.join(scripts_dir, ret['path'])
            os.makedirs(os.path.dirname(script_path), exist_ok=True)
            with open(os.path.join(scripts_dir, ret['path']), 'w') as f:
                f.write('#!/bin/bash')
            st = os.stat(script_path)
            os.chmod(script_path, st.st_mode | stat.S_IEXEC)

            if with_output:
                open(ret['combined_path'], 'w').write(ret['combined'])
                open(ret['stdout_path'], 'w').write(ret['stdout'])
                open(ret['stderr_path'], 'w').write(ret['stderr'])
                open(ret['result_path'], 'w').write(ret['result'])

    return ret
Пример #21
0
 def test_config_set_debug_sets_debug(self):
     expected = factory.pick_bool()
     cluster_config_command.run(self.make_args(debug=expected))
     with ClusterConfiguration.open() as config:
         observed = config.debug
     self.assertEqual(str(expected).lower(), observed)
Пример #22
0
 def test_debug_enabled(self):
     debug = factory.pick_bool()
     self.useFixture(ClusterConfigurationFixture(debug=debug))
     self.assertEqual(debug, debug_enabled())
Пример #23
0
 def test_machine_asdict(self):
     hostname = factory.make_name("hostname")
     cores = random.randint(1, 8)
     cpu_speed = random.randint(1000, 2000)
     memory = random.randint(4096, 8192)
     interfaces = [RequestedMachineInterface() for _ in range(3)]
     known_host_interfaces = [KnownHostInterface() for _ in range(3)]
     block_devices = [
         RequestedMachineBlockDevice(
             size=random.randint(512, 1024),
             tags=[factory.make_name("tag") for _ in range(3)],
         ) for _ in range(3)
     ]
     pinned_cores = random.sample(range(10), 3)
     hugepages_backed = factory.pick_bool()
     machine = RequestedMachine(
         hostname=hostname,
         architecture="amd64/generic",
         cores=cores,
         cpu_speed=cpu_speed,
         pinned_cores=pinned_cores,
         hugepages_backed=hugepages_backed,
         memory=memory,
         interfaces=interfaces,
         known_host_interfaces=known_host_interfaces,
         block_devices=block_devices,
     )
     self.assertThat(
         machine.asdict(),
         MatchesDict({
             "hostname":
             Equals(hostname),
             "architecture":
             Equals("amd64/generic"),
             "cores":
             Equals(cores),
             "cpu_speed":
             Equals(cpu_speed),
             "memory":
             Equals(memory),
             "pinned_cores":
             Equals(pinned_cores),
             "hugepages_backed":
             Equals(hugepages_backed),
             "interfaces":
             MatchesListwise([
                 MatchesDict({
                     "ifname":
                     Is(None),
                     "requested_ips":
                     Equals([]),
                     "ip_mode":
                     Is(None),
                     "attach_name":
                     Equals(interface.attach_name),
                     "attach_type":
                     Equals(interface.attach_type),
                     "attach_vlan":
                     Is(None),
                     "attach_options":
                     Equals(interface.attach_options),
                 }) for interface in interfaces
             ]),
             "known_host_interfaces":
             MatchesListwise([
                 MatchesDict({
                     "ifname":
                     Equals(known_host_interface.ifname),
                     "attach_name":
                     Equals(known_host_interface.attach_name),
                     "attach_type":
                     Equals(known_host_interface.attach_type),
                     "attach_vlan":
                     Is(None),
                     "dhcp_enabled":
                     Equals(False),
                 }) for known_host_interface in known_host_interfaces
             ]),
             "block_devices":
             MatchesListwise([
                 MatchesDict({
                     "size": Equals(block_device.size),
                     "tags": Equals(block_device.tags),
                 }) for block_device in block_devices
             ]),
         }),
     )
Пример #24
0
 def test_creates_client_with_correct_arguments(self):
     omapi_cli = self.patch(dhcp, "OmapiClient")
     server = Mock()
     server.ipv6 = factory.pick_bool()
     dhcp._update_hosts(server, [], [], [])
     omapi_cli.assert_called_once_with(server.omapi_key, server.ipv6)
Пример #25
0
 def test_allows_ipv4_addresses_in_ipv6_format(self):
     name = "[::ffff:%s]" % factory.make_ipv4_address()
     url = factory.make_simple_http_url(netloc=name,
                                        port=factory.pick_bool())
     self.assertEqual(url, self.validator.to_python(url), "url: %s" % url)
Пример #26
0
 def test_pick_bool_returns_bool(self):
     self.assertIsInstance(factory.pick_bool(), bool)