Ejemplo n.º 1
0
 def __init__(self, name, has_instance_uuid=None, has_uuid=None):
     self.name = name
     self.guestId = random.choice(["otherLinux64Guest", "otherLinuxGuest"])
     if has_instance_uuid is None:
         has_instance_uuid = random.choice([True, False])
     if has_instance_uuid:
         self.instanceUuid = factory.make_UUID()
     if has_uuid is None:
         has_uuid = random.choice([True, False])
     if has_uuid:
         self.uuid = factory.make_UUID()
    def test_query_nodes_calls_query_all_nodes(self):
        service = self.make_monitor_service()
        service.max_nodes_at_once = sentinel.max_nodes_at_once

        example_power_parameters = {
            "system_id": factory.make_UUID(),
            "hostname": factory.make_hostname(),
            "power_state": factory.make_name("power_state"),
            "power_type": factory.make_name("power_type"),
            "context": {},
        }

        rpc_fixture = self.useFixture(MockClusterToRegionRPCFixture())
        proto_region, io = rpc_fixture.makeEventLoop(
            region.ListNodePowerParameters)
        proto_region.ListNodePowerParameters.side_effect = [
            succeed({"nodes": [example_power_parameters]}),
            succeed({"nodes": []}),
        ]

        query_all_nodes = self.patch(npms, "query_all_nodes")

        d = service.query_nodes(getRegionClient())
        io.flush()

        self.assertEqual(None, extract_result(d))
        self.assertThat(
            query_all_nodes,
            MockCalledOnceWith(
                [example_power_parameters],
                max_concurrency=sentinel.max_nodes_at_once,
                clock=service.clock,
            ),
        )
Ejemplo n.º 3
0
 def test_returns_result(self):
     uuid = factory.make_UUID()
     api = make_api()
     fake_result = make_fake_result("configResolveClass", "found")
     self.patch(api, "config_resolve_class").return_value = fake_result
     result = get_servers(api, uuid)
     self.assertEqual("found", result[0].tag)
Ejemplo n.º 4
0
    def test_calls_create_node_rpc(self):
        protocol, connecting = self.prepare_region_rpc()
        self.addCleanup((yield connecting))
        protocol.CreateNode.return_value = defer.succeed(
            {"system_id": factory.make_name("system-id")})

        uuid = 'node-' + factory.make_UUID()
        macs = sorted(factory.make_mac_address() for _ in range(3))
        arch = factory.make_name('architecture')
        hostname = factory.make_hostname()
        domain = factory.make_name('domain')

        power_type = factory.make_name('power_type')
        power_parameters = {
            'power_address': factory.make_ipv4_address(),
            'power_user': factory.make_name('power_user'),
            'power_pass': factory.make_name('power_pass'),
            'power_control': None,
            'system_id': uuid
        }

        yield create_node(
            macs, arch, power_type, power_parameters, domain=domain,
            hostname=hostname)
        self.assertThat(
            protocol.CreateNode, MockCalledOnceWith(
                protocol, architecture=arch, power_type=power_type,
                power_parameters=json.dumps(power_parameters),
                mac_addresses=macs, domain=domain,
                hostname=hostname))
Ejemplo n.º 5
0
    def test_logs_error_on_duplicate_macs(self):
        protocol, connecting = self.prepare_region_rpc()
        self.addCleanup((yield connecting))
        system_id = factory.make_name("system-id")
        maaslog = self.patch(provisioningserver.rpc.utils, 'maaslog')

        uuid = 'node-' + factory.make_UUID()
        macs = sorted(factory.make_mac_address() for _ in range(3))
        arch = factory.make_name('architecture')
        power_type = factory.make_name('power_type')
        power_parameters = {
            'power_address': factory.make_ipv4_address(),
            'power_user': factory.make_name('power_user'),
            'power_pass': factory.make_name('power_pass'),
            'power_control': None,
            'system_id': uuid
        }

        protocol.CreateNode.side_effect = [
            defer.succeed({"system_id": system_id}),
            defer.fail(NodeAlreadyExists("Node already exists.")),
        ]

        yield create_node(
            macs, arch, power_type, power_parameters)
        yield create_node(
            macs, arch, power_type, power_parameters)
        self.assertThat(
            maaslog.error, MockCalledOnceWith(
                "A node with one of the mac addresses in %s already "
                "exists.", macs))
Ejemplo n.º 6
0
    def test_passes_on_no_duplicate_macs(self):
        protocol, connecting = self.prepare_region_rpc()
        self.addCleanup((yield connecting))
        system_id = factory.make_name("system-id")
        protocol.CreateNode.return_value = defer.succeed(
            {"system_id": system_id})

        uuid = 'node-' + factory.make_UUID()
        arch = factory.make_name('architecture')
        power_type = factory.make_name('power_type')
        power_parameters = {
            'power_address': factory.make_ipv4_address(),
            'power_user': factory.make_name('power_user'),
            'power_pass': factory.make_name('power_pass'),
            'power_control': None,
            'system_id': uuid
        }

        # Create a list of MACs with one random duplicate.
        macs = sorted(factory.make_mac_address() for _ in range(3))
        macs_with_duplicate = macs + [choice(macs)]

        yield create_node(
            macs_with_duplicate, arch, power_type, power_parameters)
        self.assertThat(
            protocol.CreateNode, MockCalledOnceWith(
                protocol, architecture=arch, power_type=power_type,
                power_parameters=json.dumps(power_parameters),
                mac_addresses=macs, domain=None, hostname=None))
Ejemplo n.º 7
0
    def test_set_boot_order_storage_volume(self):
        # zhmcclient_mock doesn't support storage groups.
        serial = factory.make_UUID()
        mock_storage_group = Mock()
        mock_storage_group.storage_volumes.find.return_value.uri = serial
        mock_partition = Mock()
        mock_partition.list_attached_storage_groups.return_value = [
            mock_storage_group,
        ]
        mock_get_partition = self.patch(self.hmcz, "_get_partition")
        mock_get_partition.return_value = mock_partition

        yield self.hmcz.set_boot_order(
            None,
            self.make_context(),
            [{
                "id": random.randint(0, 100),
                "name": factory.make_name("name"),
                "id_path": factory.make_name("id_path"),
                "model": factory.make_name("model"),
                "serial": serial,
            }] + [{
                factory.make_name("key"): factory.make_name("value")
                for _ in range(5)
            } for _ in range(5)],
        )

        mock_partition.update_properties.assert_called_once_with({
            "boot-device":
            "storage-volume",
            "boot-storage-volume":
            serial,
        })
Ejemplo n.º 8
0
 def test_no_results_with_no_server_macs(self):
     servers = [{'uuid': factory.make_UUID()}]
     api = make_api()
     self.patch(ucsm, 'get_servers').return_value = servers
     self.patch(ucsm, 'get_macs').return_value = []
     self.patch(ucsm, 'probe_lan_boot_options').return_value = ['option']
     server_list = probe_servers(api)
     self.assertEqual([], server_list)
Ejemplo n.º 9
0
 def test_uses_uuid(self):
     uuid = factory.make_UUID()
     api = make_api()
     mock = self.patch(api, "config_resolve_class")
     get_servers(api, uuid)
     filters = mock.call_args[0][1]
     attrib = {"class": "computeItem", "property": "uuid", "value": uuid}
     self.assertEqual(attrib, filters[0].attrib)
Ejemplo n.º 10
0
 def test_no_results_with_no_server_macs(self):
     servers = [{"uuid": factory.make_UUID()}]
     api = make_api()
     self.patch(ucsm, "get_servers").return_value = servers
     self.patch(ucsm, "get_macs").return_value = []
     self.patch(ucsm, "probe_lan_boot_options").return_value = ["option"]
     server_list = probe_servers(api)
     self.assertEqual([], server_list)
Ejemplo n.º 11
0
 def test_uses_uuid(self):
     uuid = factory.make_UUID()
     api = make_api()
     mock = self.patch(api, 'config_resolve_class')
     get_servers(api, uuid)
     filters = mock.call_args[0][1]
     attrib = {'class': 'computeItem', 'property': 'uuid', 'value': uuid}
     self.assertEqual(attrib, filters[0].attrib)
Ejemplo n.º 12
0
 def test_returns_results(self):
     servers = [{'uuid': factory.make_UUID()}]
     mac = 'mac'
     api = make_api()
     self.patch(ucsm, 'get_servers').return_value = servers
     self.patch(ucsm, 'get_macs').return_value = [mac]
     self.patch(ucsm, 'probe_lan_boot_options').return_value = ['option']
     server_list = probe_servers(api)
     self.assertEqual([(servers[0], [mac])], server_list)
Ejemplo n.º 13
0
 def test_returns_results(self):
     servers = [{"uuid": factory.make_UUID()}]
     mac = "mac"
     api = make_api()
     self.patch(ucsm, "get_servers").return_value = servers
     self.patch(ucsm, "get_macs").return_value = [mac]
     self.patch(ucsm, "probe_lan_boot_options").return_value = ["option"]
     server_list = probe_servers(api)
     self.assertEqual([(servers[0], [mac])], server_list)
Ejemplo n.º 14
0
 def test_re_config_file_matches_pxelinux_cfg_with_hardware_uuid(self):
     hardware_uuid = factory.make_UUID().encode()
     match = re_config_file.match(b'pxelinux.cfg/%s' % hardware_uuid)
     self.assertIsNotNone(match)
     self.assertDictEqual(
         {
             'mac': None,
             'hardware_uuid': hardware_uuid,
             'arch': None,
             'subarch': None,
         }, match.groupdict())
Ejemplo n.º 15
0
 def configure_hivex(self):
     mock_hivex = mock.MagicMock()
     self.patch(windows_module, 'load_hivex').return_value = mock_hivex
     mock_hivex.node_name.side_effect = ['Objects',
                                         Bcd.GUID_WINDOWS_BOOTMGR,
                                         Bcd.BOOT_MGR_DISPLAY_ORDER]
     mock_hivex.node_children.side_effect = [
         [factory.make_name('objects')], [factory.make_name('object')],
         ['value0', factory.make_UUID()],
         [factory.make_name('element')]]
     mock_hivex.node_values.return_value = [factory.make_name('val')]
Ejemplo n.º 16
0
    def test_power_state_error_on_unknown_state(self):
        url = factory.make_name('url')
        username = factory.make_name('username')
        password = factory.make_name('password')
        uuid = factory.make_UUID()
        api = Mock()
        self.patch(ucsm, 'UCSM_XML_API').return_value = api
        get_servers_mock = self.patch(ucsm, 'get_servers')
        get_servers_mock.return_value = [make_server()]

        self.assertRaises(UCSM_XML_API_Error, power_state_ucsm, url, username,
                          password, uuid)
Ejemplo n.º 17
0
    def test_power_state_get_on(self):
        url = factory.make_name("url")
        username = factory.make_name("username")
        password = factory.make_name("password")
        uuid = factory.make_UUID()
        api = Mock()
        self.patch(ucsm, "UCSM_XML_API").return_value = api
        get_servers_mock = self.patch(ucsm, "get_servers")
        get_servers_mock.return_value = [make_server("on")]

        power_state = power_state_ucsm(url, username, password, uuid)
        self.expectThat(get_servers_mock, MockCalledOnceWith(api, uuid))
        self.expectThat(power_state, Equals("on"))
Ejemplo n.º 18
0
 def test_re_config_file_matches_pxelinux_cfg_with_hardware_uuid(self):
     hardware_uuid = factory.make_UUID().encode()
     match = re_config_file.match(b"pxelinux.cfg/%s" % hardware_uuid)
     self.assertIsNotNone(match)
     self.assertDictEqual(
         {
             "mac": None,
             "hardware_uuid": hardware_uuid,
             "arch": None,
             "subarch": None,
         },
         match.groupdict(),
     )
Ejemplo n.º 19
0
    def test_power_state_get_on(self):
        url = factory.make_name('url')
        username = factory.make_name('username')
        password = factory.make_name('password')
        uuid = factory.make_UUID()
        api = Mock()
        self.patch(ucsm, 'UCSM_XML_API').return_value = api
        get_servers_mock = self.patch(ucsm, 'get_servers')
        get_servers_mock.return_value = [make_server("on")]

        power_state = power_state_ucsm(url, username, password, uuid)
        self.expectThat(get_servers_mock, MockCalledOnceWith(api, uuid))
        self.expectThat(power_state, Equals('on'))
Ejemplo n.º 20
0
 def make_parameters(self):
     system_id = factory.make_name("system_id")
     url = factory.make_name("power_address")
     username = factory.make_name("power_user")
     password = factory.make_name("power_pass")
     uuid = factory.make_UUID()
     context = {
         "system_id": system_id,
         "power_address": url,
         "power_user": username,
         "power_pass": password,
         "uuid": uuid,
     }
     return system_id, url, username, password, uuid, context
Ejemplo n.º 21
0
 def make_parameters(self):
     system_id = factory.make_name('system_id')
     url = factory.make_name('power_address')
     username = factory.make_name('power_user')
     password = factory.make_name('power_pass')
     uuid = factory.make_UUID()
     context = {
         'system_id': system_id,
         'power_address': url,
         'power_user': username,
         'power_pass': password,
         'uuid': uuid,
     }
     return system_id, url, username, password, uuid, context
Ejemplo n.º 22
0
    def test__get_boot_sources_calls_get_boot_sources_v2_before_v1(self):
        clock = Clock()
        client_call = Mock()
        client_call.side_effect = [
            defer.succeed(dict(sources=sentinel.sources)),
        ]
        client_call.localIdent = factory.make_UUID()

        service = ImageDownloadService(sentinel.rpc, sentinel.tftp_root, clock)
        sources = yield service._get_boot_sources(client_call)
        self.assertEqual(sources.get('sources'), sentinel.sources)
        self.assertThat(
            client_call,
            MockCalledOnceWith(GetBootSourcesV2, uuid=client_call.localIdent))
Ejemplo n.º 23
0
    def test__get_boot_sources_calls_get_boot_sources_v1_on_v2_missing(self):
        clock = Clock()
        client_call = Mock()
        client_call.side_effect = [
            defer.fail(UnhandledCommand()),
            defer.succeed(dict(sources=[])),
        ]
        client_call.localIdent = factory.make_UUID()

        service = ImageDownloadService(sentinel.rpc, sentinel.tftp_root, clock)
        yield service._get_boot_sources(client_call)
        self.assertThat(
            client_call,
            MockCallsMatch(call(GetBootSourcesV2, uuid=client_call.localIdent),
                           call(GetBootSources, uuid=client_call.localIdent)))
Ejemplo n.º 24
0
    def test_returns_system_id_of_new_node(self):
        protocol, connecting = self.prepare_region_rpc()
        self.addCleanup((yield connecting))
        system_id = factory.make_name("system-id")
        protocol.CreateNode.return_value = defer.succeed(
            {"system_id": system_id})
        get_cluster_uuid = self.patch(
            provisioningserver.utils, 'get_cluster_uuid')
        get_cluster_uuid.return_value = 'cluster-' + factory.make_UUID()

        uuid = 'node-' + factory.make_UUID()
        macs = sorted(factory.make_mac_address() for _ in range(3))
        arch = factory.make_name('architecture')
        power_type = factory.make_name('power_type')
        power_parameters = {
            'power_address': factory.make_ipv4_address(),
            'power_user': factory.make_name('power_user'),
            'power_pass': factory.make_name('power_pass'),
            'power_control': None,
            'system_id': uuid
        }
        new_system_id = yield create_node(
            macs, arch, power_type, power_parameters)
        self.assertEqual(system_id, new_system_id)
Ejemplo n.º 25
0
 def configure_hivex(self):
     mock_hivex = mock.MagicMock()
     self.patch(windows_module, "load_hivex").return_value = mock_hivex
     mock_hivex.node_name.side_effect = [
         "Objects",
         Bcd.GUID_WINDOWS_BOOTMGR,
         Bcd.BOOT_MGR_DISPLAY_ORDER,
     ]
     mock_hivex.node_children.side_effect = [
         [factory.make_name("objects")],
         [factory.make_name("object")],
         ["value0", factory.make_UUID()],
         [factory.make_name("element")],
     ]
     mock_hivex.node_values.return_value = [factory.make_name("val")]
Ejemplo n.º 26
0
    def test_try_query_nodes_logs_other_errors(self):
        service = self.make_monitor_service()
        self.patch(npms, "getRegionClient").return_value = sentinel.client
        sentinel.client.localIdent = factory.make_UUID()

        query_nodes = self.patch(service, "query_nodes")
        query_nodes.return_value = fail(
            ZeroDivisionError("Such a shame I can't divide by zero"))

        with FakeLogger("maas") as maaslog, TwistedLoggerFixture():
            d = service.try_query_nodes()

        self.assertEqual(None, extract_result(d))
        self.assertDocTestMatches(
            "Failed to query nodes' power status: "
            "Such a shame I can't divide by zero",
            maaslog.output)
Ejemplo n.º 27
0
 def test_power_control_ucsm(self):
     uuid = factory.make_UUID()
     api = Mock()
     self.patch(ucsm, 'UCSM_XML_API').return_value = api
     get_servers_mock = self.patch(ucsm, 'get_servers')
     server = make_server()
     state = 'admin-down'
     power_control = Element('lsPower', {'state': state})
     get_servers_mock.return_value = [server]
     get_server_power_control_mock = self.patch(ucsm,
                                                'get_server_power_control')
     get_server_power_control_mock.return_value = power_control
     set_server_power_control_mock = self.patch(ucsm,
                                                'set_server_power_control')
     power_control_ucsm('url', 'username', 'password', uuid, 'off')
     self.assertThat(get_servers_mock, MockCalledOnceWith(api, uuid))
     self.assertThat(set_server_power_control_mock,
                     MockCalledOnceWith(api, power_control, state))
Ejemplo n.º 28
0
 def test_power_control_ucsm(self):
     uuid = factory.make_UUID()
     api = Mock()
     self.patch(ucsm, "UCSM_XML_API").return_value = api
     get_servers_mock = self.patch(ucsm, "get_servers")
     server = make_server()
     state = "admin-down"
     power_control = Element("lsPower", {"state": state})
     get_servers_mock.return_value = [server]
     get_server_power_control_mock = self.patch(ucsm,
                                                "get_server_power_control")
     get_server_power_control_mock.return_value = power_control
     set_server_power_control_mock = self.patch(ucsm,
                                                "set_server_power_control")
     power_control_ucsm("url", "username", "password", uuid, "off")
     self.assertThat(get_servers_mock, MockCalledOnceWith(api, uuid))
     self.assertThat(
         set_server_power_control_mock,
         MockCalledOnceWith(api, power_control, state),
     )
Ejemplo n.º 29
0
 def test_class_id(self):
     uuid = factory.make_UUID()
     api = make_api()
     mock = self.patch(api, "config_resolve_class")
     get_servers(api, uuid)
     self.assertThat(mock, MockCalledOnceWith("computeItem", ANY))
Ejemplo n.º 30
0
 def test_make_UUID(self):
     uuid = factory.make_UUID()
     self.assertIsInstance(uuid, str)
     self.assertEqual(36, len(uuid))