Ejemplo n.º 1
0
    def test__saves_power_parameters(self):
        self.prepare_rack_rpc()

        mac_addresses = [
            factory.make_mac_address() for _ in range(3)]
        architecture = make_usable_architecture(self)
        power_parameters = {
            'power_address': factory.make_ip_address(),  # XXX: URLs break.
            'power_pass': factory.make_name('power_pass'),
            'power_id': factory.make_name('power_id'),
        }

        node = create_node(
            architecture, 'virsh', power_parameters,
            mac_addresses)

        # Reload the object from the DB so that we're sure its power
        # parameters are being persisted.
        node = reload_object(node)
        self.assertEqual(power_parameters, node.power_parameters)
Ejemplo n.º 2
0
 def test__changes_boot_interface_vlan_not_relayed_through_rack(self):
     rack_controller = factory.make_RackController()
     rack_fabric = factory.make_Fabric()
     rack_vlan = rack_fabric.get_default_vlan()
     rack_interface = factory.make_Interface(
         INTERFACE_TYPE.PHYSICAL, node=rack_controller, vlan=rack_vlan)
     rack_subnet = factory.make_Subnet(vlan=rack_vlan)
     rack_ip = factory.make_StaticIPAddress(
         alloc_type=IPADDRESS_TYPE.STICKY, subnet=rack_subnet,
         interface=rack_interface)
     other_vlan = factory.make_VLAN()
     relay_vlan = factory.make_VLAN(relay_vlan=other_vlan)
     remote_ip = factory.make_ip_address()
     node = self.make_node(vlan=relay_vlan)
     mac = node.get_boot_interface().mac_address
     get_config(
         rack_controller.system_id, rack_ip.ip, remote_ip, mac=mac,
         query_count=28)
     self.assertEqual(
         rack_vlan, reload_object(node).get_boot_interface().vlan)
Ejemplo n.º 3
0
 def test_create_interface_creates_external(self):
     user = factory.make_admin()
     node = factory.make_Node(interface=False, node_type=NODE_TYPE.DEVICE)
     handler = DeviceHandler(user, {})
     name = factory.make_name("eth")
     mac_address = factory.make_mac_address()
     ip_address = factory.make_ip_address()
     handler.create_interface({
         "system_id": node.system_id,
         "name": name,
         "mac_address": mac_address,
         "ip_assignment": DEVICE_IP_ASSIGNMENT_TYPE.EXTERNAL,
         "ip_address": ip_address,
         })
     new_interface = node.interface_set.first()
     self.assertIsNotNone(new_interface)
     auto_ip = new_interface.ip_addresses.filter(
         alloc_type=IPADDRESS_TYPE.USER_RESERVED)
     self.assertIsNotNone(auto_ip)
     self.assertEqual(1, len(auto_ip))
Ejemplo n.º 4
0
 def test_can_create_forward_dns_server(self):
     name = factory.make_name("domain")
     forward_dns_servers = [factory.make_ip_address() for _ in range(0, 2)]
     form = DomainForm({
         "name":
         name,
         "authoritative":
         False,
         "forward_dns_servers":
         "  ".join(forward_dns_servers),
     })
     self.assertTrue(form.is_valid(), form.errors)
     domain = form.save()
     self.assertEqual(
         forward_dns_servers,
         [
             fwd_dns_srvr.ip_address
             for fwd_dns_srvr in domain.forward_dns_servers
         ],
     )
Ejemplo n.º 5
0
 def test_POST_create_returns_machine_with_matching_power_parameters(self):
     mock_create_machine = self.patch(machines_module, "create_machine")
     hostname = factory.make_name("hostname")
     architecture = make_usable_architecture(self)
     power_type = "ipmi"
     power_parameters = {
         "power_address": factory.make_ip_address(),
         "power_user": factory.make_name("power-user"),
         "power_pass": factory.make_name("power-pass"),
     }
     machine = factory.make_Machine(
         hostname=hostname,
         status=NODE_STATUS.NEW,
         architecture="",
         power_type=power_type,
         power_parameters=power_parameters,
     )
     # Simulate creating the MAAS IPMI user
     power_parameters["power_user"] = "******"
     power_parameters["power_pass"] = factory.make_name("power-pass")
     response = self.client.post(
         reverse("machines_handler"),
         {
             "hostname": "maas-enlistment",
             "architecture": architecture,
             "power_type": power_type,
             "mac_addresses": factory.make_mac_address(),
             "power_parameters": json.dumps(power_parameters),
         },
     )
     self.assertEqual(http.client.OK, response.status_code)
     machine = reload_object(machine)
     self.assertEqual(hostname, machine.hostname)
     self.assertEqual(architecture, machine.architecture)
     # The power form add default values, matching is based on the IP
     # address.
     self.assertDictContainsSubset(power_parameters,
                                   machine.bmc.power_parameters)
     self.assertThat(mock_create_machine, MockNotCalled())
     self.assertEqual(machine.system_id,
                      json_load_bytes(response.content)["system_id"])
Ejemplo n.º 6
0
 def test_preseed_url_for_known_node_internal_domain(self):
     rack_url = 'http://%s' % factory.make_name('host')
     rack_controller = factory.make_RackController(url=rack_url)
     vlan = factory.make_VLAN(dhcp_on=True, primary_rack=rack_controller)
     subnet = factory.make_Subnet(vlan=vlan)
     subnet.dns_servers = []
     subnet.save()
     local_ip = factory.pick_ip_in_Subnet(subnet)
     remote_ip = factory.make_ip_address()
     self.patch(
         server_address, 'resolve_hostname').return_value = {local_ip}
     node = self.make_node(primary_rack=rack_controller)
     mac = node.get_boot_interface().mac_address
     observed_config = get_config(
         rack_controller.system_id, local_ip, remote_ip, mac=mac)
     self.assertThat(
         observed_config["preseed_url"],
         StartsWith(
             'http://%s.%s:5248' % (
                 get_resource_name_for_subnet(subnet),
                 Config.objects.get_config('maas_internal_domain'))))
Ejemplo n.º 7
0
 def test_POST_create_returns_machine_with_matching_power_parameters(self):
     mock_create_machine = self.patch(machines_module, "create_machine")
     hostname = factory.make_name("hostname")
     architecture = make_usable_architecture(self)
     power_type = 'ipmi'
     power_parameters = {
         "power_address": factory.make_ip_address(),
         "power_user": factory.make_name("power-user"),
         "power_pass": factory.make_name("power-pass"),
         "power_driver": 'LAN_2_0',
         "mac_address": '',
         "power_boot_type": 'auto',
         }
     machine = factory.make_Machine(
         hostname=hostname, status=NODE_STATUS.NEW,
         architecture='', power_type=power_type,
         power_parameters=power_parameters)
     # Simulate creating the MAAS IPMI user
     power_parameters["power_user"] = "******"
     power_parameters["power_pass"] = factory.make_name("power-pass")
     response = self.client.post(
         reverse('machines_handler'),
         {
             'hostname': 'maas-enlistment',
             'architecture': architecture,
             'power_type': power_type,
             'mac_addresses': factory.make_mac_address(),
             'power_parameters': json.dumps(power_parameters),
         })
     self.assertEqual(http.client.OK, response.status_code)
     machine = reload_object(machine)
     self.assertEqual(hostname, machine.hostname)
     self.assertEqual(architecture, machine.architecture)
     self.assertDictContainsSubset(
         machine.bmc.power_parameters, power_parameters)
     node_metadata = NodeMetadata.objects.get(node=machine, key='enlisting')
     self.assertEqual(node_metadata.value, 'True')
     self.assertThat(mock_create_machine, MockNotCalled())
     self.assertEqual(
         machine.system_id, json_load_bytes(response.content)['system_id'])
Ejemplo n.º 8
0
 def test__creates_staticipaddresses(self):
     name = factory.make_name("dnsresource")
     domain = factory.make_Domain()
     ips = [factory.make_ip_address() for _ in range(3)]
     request = Mock()
     request.user = factory.make_User()
     form = DNSResourceForm(
         {
             "name": name,
             "domain": domain.id,
             "ip_addresses": " ".join(ips),
         },
         request=request)
     self.assertTrue(form.is_valid(), form.errors)
     dnsresource = form.save()
     self.assertEqual(name, dnsresource.name)
     self.assertEqual(domain.id, dnsresource.domain.id)
     actual_ips = dnsresource.ip_addresses.all()
     actual = {str(ip.ip) for ip in actual_ips}
     self.assertItemsEqual(set(ips), actual)
     actual_users = {ip.user_id for ip in actual_ips}
     self.assertItemsEqual({request.user.id}, actual_users)
Ejemplo n.º 9
0
 def test_compose_preseed_for_commissioning_includes_metadata_status_url(
         self):
     rack_controller = factory.make_RackController(url='')
     node = factory.make_Node(interface=True,
                              status=NODE_STATUS.COMMISSIONING)
     nic = node.get_boot_interface()
     nic.vlan.dhcp_on = True
     nic.vlan.primary_rack = rack_controller
     nic.vlan.save()
     region_ip = factory.make_ip_address()
     preseed = yaml.safe_load(
         compose_preseed(PRESEED_TYPE.COMMISSIONING,
                         node,
                         default_region_ip=region_ip))
     self.assertEqual(
         absolute_reverse('metadata', default_region_ip=region_ip),
         preseed['datasource']['MAAS']['metadata_url'])
     self.assertEqual(
         absolute_reverse('metadata-status',
                          default_region_ip=region_ip,
                          args=[node.system_id]),
         preseed['reporting']['maas']['endpoint'])
Ejemplo n.º 10
0
 def make_kwargs(
     self,
     action=None,
     mac=None,
     ip=None,
     timestamp=None,
     lease_time=None,
     hostname=None,
     subnet=None,
 ):
     if action is None:
         action = random.choice(["commit", "expiry", "release"])
     if mac is None:
         mac = factory.make_mac_address()
     if ip is None:
         if subnet is not None:
             ip = factory.pick_ip_in_network(subnet.get_ipnetwork())
         else:
             ip = factory.make_ip_address()
     if timestamp is None:
         timestamp = int(time.time())
     if action == "commit":
         if lease_time is None:
             lease_time = random.randint(30, 1000)
         if hostname is None:
             hostname = factory.make_name("host")
     ip_family = "ipv4"
     if IPAddress(ip).version == IPADDRESS_FAMILY.IPv6:
         ip_family = "ipv6"
     return {
         "action": action,
         "mac": mac,
         "ip": ip,
         "ip_family": ip_family,
         "timestamp": timestamp,
         "lease_time": lease_time,
         "hostname": hostname,
     }
Ejemplo n.º 11
0
    def test_get_bmc_ip_enables_dynamic(self):
        ip = factory.make_ip_address()
        mock_bmc_set = self.patch(self.ipmi, "_bmc_set")
        self.patch(self.ipmi, "_get_bmc_ip").side_effect = (
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            ip,
        )

        self.assertEqual(ip, self.ipmi.get_bmc_ip())
        self.assertThat(
            mock_bmc_set,
            MockCallsMatch(
                call("Lan_Conf:IP_Address_Source", "Static"),
                call("Lan_Conf:IP_Address_Source", "Use_DHCP"),
            ),
        )
Ejemplo n.º 12
0
    def test_get_credentials_lan_old(self):
        self.ipmi.username = factory.make_name("username")
        self.ipmi.password = factory.make_name("password")
        self.patch(
            self.ipmi,
            "_get_ipmi_locate_output").return_value = "IPMI Version: 1.0"
        self.patch(bmc_config.platform, "machine").return_value = "x86_64"
        self.patch(bmc_config.os.path, "isdir").return_value = False
        ip = factory.make_ip_address()
        self.patch(self.ipmi, "get_bmc_ip").return_value = ip

        self.assertEqual(
            {
                "power_address": ip,
                "power_pass": self.ipmi.password,
                "power_user": self.ipmi.username,
                "power_driver": "LAN",
                "power_boot_type": "auto",
                "k_g": "",
                "cipher_suite_id": "",
                "privilege_level": "",
            },
            self.ipmi.get_credentials(),
        )
Ejemplo n.º 13
0
 def test_POST_simple_user_can_set_power_type_and_parameters(self):
     new_power_address = factory.make_ip_address()  # XXX: URLs don't work.
     new_power_id = factory.make_name('power_id')
     response = self.client.post(
         reverse('machines_handler'), {
             'architecture': make_usable_architecture(self),
             'power_type': 'virsh',
             'power_parameters': json.dumps(
                 {
                     "power_address": new_power_address,
                     "power_id": new_power_id,
                 }),
             'mac_addresses': ['AA:BB:CC:DD:EE:FF'],
             })
     machine = Machine.objects.get(
         system_id=json_load_bytes(response.content)['system_id'])
     self.assertEqual(http.client.OK, response.status_code)
     self.assertEqual('virsh', machine.power_type)
     self.assertEqual(
         {
             'power_pass': '',
             'power_id': new_power_id,
             'power_address': new_power_address,
         }, machine.power_parameters)
Ejemplo n.º 14
0
 def test_set_current_entry_asserts_for_empty_list(self):
     region = factory.make_RegionController()
     with ExpectedException(AssertionError):
         RDNS.objects.set_current_entry(factory.make_ip_address(), [],
                                        region)
Ejemplo n.º 15
0
 def test_returns_list_if_space_separated_ips(self):
     addresses = [factory.make_ip_address() for _ in range(3)]
     Config.objects.set_config("upstream_dns", " ".join(addresses))
     self.assertEqual(addresses, get_upstream_dns())
Ejemplo n.º 16
0
 def test_returns_list_of_one_address_if_set(self):
     address = factory.make_ip_address()
     Config.objects.set_config("upstream_dns", address)
     self.assertEqual([address], get_upstream_dns())
Ejemplo n.º 17
0
 def test__errors_when_no_event_type(self):
     name = factory.make_name('name')
     description = factory.make_name('description')
     self.assertRaises(NoSuchEventType, events.send_event_ip_address,
                       factory.make_ip_address(), name, description,
                       datetime.datetime.utcnow())
Ejemplo n.º 18
0
 def test_accepts_comma_separated_ips(self):
     ips = [factory.make_ip_address() for _ in range(5)]
     input = ",".join(ips)
     self.assertEqual(" ".join(ips), SubnetListFormField().clean(input))
Ejemplo n.º 19
0
 def test_accepts_single_ip(self):
     ip = factory.make_ip_address()
     self.assertEqual(ip, SubnetListFormField().clean(ip))
Ejemplo n.º 20
0
 def test_accepts_space_separated_ips(self):
     ips = [factory.make_ip_address() for _ in range(5)]
     input = " ".join(ips)
     self.assertEqual(input, HostListFormField().clean(input))
Ejemplo n.º 21
0
 def test_rejects_invalid_input(self):
     invalid = factory.make_name("invalid")
     input = " ".join([factory.make_ip_address(), invalid])
     error = self.assertRaises(ValidationError,
                               IPListFormField().clean, input)
     self.assertIn("Invalid IP address: %s" % invalid, error.message)
Ejemplo n.º 22
0
 def test_delete_current_entry_ignores_missing_entries(self):
     region = factory.make_RegionController()
     ip = factory.make_ip_address()
     with TwistedLoggerFixture() as logger:
         RDNS.objects.delete_current_entry(ip, region)
     self.assertThat(logger.output, Equals(""))
Ejemplo n.º 23
0
 def test_detected_ip(self):
     self.patch(self.wedge, "_detect_known_switch").return_value = "accton"
     self.patch(self.wedge,
                "get_bmc_ip").return_value = factory.make_ip_address()
     self.assertTrue(self.wedge.detected())
Ejemplo n.º 24
0
 def test_upstream_dns_accepts_ip_list(self):
     field = get_config_field('upstream_dns')
     ips1 = [factory.make_ip_address() for _ in range(3)]
     ips2 = [factory.make_ip_address() for _ in range(3)]
     input = ' '.join(ips1) + ' ' + ','.join(ips2)
     self.assertEqual(' '.join(ips1 + ips2), field.clean(input))
Ejemplo n.º 25
0
 def test__delete_current_entry_ignores_missing_entries(self):
     region = factory.make_RegionController()
     ip = factory.make_ip_address()
     RDNS.objects.delete_current_entry(ip, region)
     self.assertThat(self.maaslog.output, Equals(""))
Ejemplo n.º 26
0
 def test_returns_None_when_unknown_subnet(self):
     self.assertIsNone(
         find_rack_controller(make_request(factory.make_ip_address())))
Ejemplo n.º 27
0
    def test_get_credentials(self):
        self.hp_moonshot.username = "******"
        self.hp_moonshot.password = "******"

        mock_get_local_address = self.patch(self.hp_moonshot,
                                            "_get_local_address")
        local_address = factory.make_name("local_address")
        mock_get_local_address.return_value = local_address

        output = factory.make_string()
        self.mock_check_output.return_value = output.encode()

        mock_get_cartridge_address = self.patch(self.hp_moonshot,
                                                "_get_cartridge_address")
        node_address = factory.make_name("node_address")
        mock_get_cartridge_address.return_value = node_address

        mock_get_channel_number = self.patch(self.hp_moonshot,
                                             "_get_channel_number")
        local_chan = factory.make_name("local_chan")
        cartridge_chan = factory.make_name("cartridge_chan")
        mock_get_channel_number.side_effect = (local_chan, cartridge_chan)

        mock_get_bmc_ip = self.patch(self.hp_moonshot, "get_bmc_ip")
        ip = factory.make_ip_address()
        mock_get_bmc_ip.return_value = ip

        self.assertEqual(
            {
                "power_address":
                ip,
                "power_pass":
                "******",
                "power_user":
                "******",
                "power_hwaddress":
                "-B %s -T %s -b %s -t %s -m 0x20" % (
                    cartridge_chan,
                    node_address,
                    local_chan,
                    local_address,
                ),
            },
            self.hp_moonshot.get_credentials(),
        )
        self.assertThat(mock_get_local_address, MockCalledOnce())
        self.assertThat(mock_get_cartridge_address,
                        MockCalledOnceWith(local_address))
        self.assertThat(
            self.mock_check_output,
            MockCalledOnceWith(
                [
                    "ipmitool",
                    "-b",
                    "0",
                    "-t",
                    "0x20",
                    "-m",
                    local_address,
                    "sdr",
                    "list",
                    "mcloc",
                    "-v",
                ],
                timeout=60,
            ),
        )
        self.assertThat(
            mock_get_channel_number,
            MockCallsMatch(call(local_address, output),
                           call(node_address, output)),
        )