Пример #1
0
 def test_returns_interface_ips_but_no_nulls(self):
     default_domain = Domain.objects.get_default_domain().name
     domain = factory.make_Domain(name="henry")
     subnet = factory.make_Subnet(cidr=str(IPNetwork("10/29").cidr))
     subnet.gateway_ip = str(IPAddress(IPNetwork(subnet.cidr).ip + 1))
     subnet.save()
     # Create a node with two interfaces, with NULL ips
     node = factory.make_Node_with_Interface_on_Subnet(
         subnet=subnet,
         vlan=subnet.vlan,
         fabric=subnet.vlan.fabric,
         domain=domain,
         interface_count=3,
     )
     dnsdata = factory.make_DNSData(domain=domain)
     boot_iface = node.boot_interface
     interfaces = list(node.interface_set.all().exclude(id=boot_iface.id))
     # Now go add IP addresses to the boot interface, and one other
     boot_ip = factory.make_StaticIPAddress(interface=boot_iface,
                                            subnet=subnet)
     sip = factory.make_StaticIPAddress(interface=interfaces[0],
                                        subnet=subnet)
     default_ttl = random.randint(10, 300)
     Config.objects.set_config("default_dns_ttl", default_ttl)
     zones = ZoneGenerator(
         domain,
         subnet,
         default_ttl=default_ttl,
         serial=random.randint(0, 65535),
     ).as_list()
     self.assertThat(
         zones,
         MatchesSetwise(
             forward_zone("henry"),
             reverse_zone(default_domain, "10/29"),
             reverse_zone(default_domain, "10/24"),
         ),
     )
     self.assertEqual(
         {
             node.hostname:
             HostnameIPMapping(
                 node.system_id,
                 default_ttl,
                 {"%s" % boot_ip.ip},
                 node.node_type,
             ),
             "%s.%s" % (interfaces[0].name, node.hostname):
             HostnameIPMapping(
                 node.system_id,
                 default_ttl,
                 {"%s" % sip.ip},
                 node.node_type,
             ),
         },
         zones[0]._mapping,
     )
     self.assertEqual(
         {
             dnsdata.dnsresource.name:
             HostnameRRsetMapping(
                 None, {(default_ttl, dnsdata.rrtype, dnsdata.rrdata)})
         }.items(),
         zones[0]._other_mapping.items(),
     )
     self.assertEqual(
         {
             node.fqdn:
             HostnameIPMapping(
                 node.system_id,
                 default_ttl,
                 {"%s" % boot_ip.ip},
                 node.node_type,
             ),
             "%s.%s" % (interfaces[0].name, node.fqdn):
             HostnameIPMapping(
                 node.system_id,
                 default_ttl,
                 {"%s" % sip.ip},
                 node.node_type,
             ),
         },
         zones[1]._mapping,
     )
     self.assertEqual({}, zones[2]._mapping)
Пример #2
0
 def test_scan_ipv6_fails(self):
     user = factory.make_admin()
     handler = SubnetHandler(user, {}, None)
     subnet = factory.make_Subnet(version=6)
     with ExpectedException(ValueError, ".*only IPv4.*"):
         handler.scan({"id": subnet.id})
Пример #3
0
 def test_scan_as_non_admin_asserts(self):
     user = factory.make_User()
     handler = SubnetHandler(user, {}, None)
     subnet = factory.make_Subnet()
     with ExpectedException(AssertionError, "Permission denied."):
         handler.scan({"id": subnet.id})
Пример #4
0
 def test_sets_null_if_contains_vlan(self):
     space = factory.make_Space()
     subnet = factory.make_Subnet(space=space)
     space.delete()
     subnet = reload_object(subnet)
     self.assertThat(subnet.vlan.space, Equals(None))
Пример #5
0
def populate_main():
    """Populate the main data all in one transaction."""
    admin = factory.make_admin(username="******",
                               password="******",
                               completed_intro=False)  # noqa
    user1, _ = factory.make_user_with_keys(username="******",
                                           password="******",
                                           completed_intro=False)
    user2, _ = factory.make_user_with_keys(username="******",
                                           password="******",
                                           completed_intro=False)

    # Physical zones.
    zones = [
        factory.make_Zone(name="zone-north"),
        factory.make_Zone(name="zone-south"),
    ]

    # DNS domains.
    domains = [
        Domain.objects.get_default_domain(),
        factory.make_Domain("sample"),
        factory.make_Domain("ubnt"),
    ]

    # Create the fabrics that will be used by the regions, racks,
    # machines, and devices.
    fabric0 = Fabric.objects.get_default_fabric()
    fabric0_untagged = fabric0.get_default_vlan()
    fabric0_vlan10 = factory.make_VLAN(fabric=fabric0, vid=10)
    fabric1 = factory.make_Fabric()
    fabric1_untagged = fabric1.get_default_vlan()
    fabric1_vlan42 = factory.make_VLAN(fabric=fabric1, vid=42)
    empty_fabric = factory.make_Fabric()  # noqa

    # Create some spaces.
    space_mgmt = factory.make_Space("management")
    space_storage = factory.make_Space("storage")
    space_internal = factory.make_Space("internal")
    space_ipv6_testbed = factory.make_Space("ipv6-testbed")

    # Subnets used by regions, racks, machines, and devices.
    subnet_1 = factory.make_Subnet(
        cidr="172.16.1.0/24",
        gateway_ip="172.16.1.1",
        vlan=fabric0_untagged,
        space=space_mgmt,
    )
    subnet_2 = factory.make_Subnet(
        cidr="172.16.2.0/24",
        gateway_ip="172.16.2.1",
        vlan=fabric1_untagged,
        space=space_mgmt,
    )
    subnet_3 = factory.make_Subnet(
        cidr="172.16.3.0/24",
        gateway_ip="172.16.3.1",
        vlan=fabric0_vlan10,
        space=space_storage,
    )
    subnet_4 = factory.make_Subnet(  # noqa
        cidr="172.16.4.0/24",
        gateway_ip="172.16.4.1",
        vlan=fabric0_vlan10,
        space=space_internal,
    )
    subnet_2001_db8_42 = factory.make_Subnet(  # noqa
        cidr="2001:db8:42::/64",
        gateway_ip="",
        vlan=fabric1_vlan42,
        space=space_ipv6_testbed,
    )
    ipv4_subnets = [subnet_1, subnet_2, subnet_3, subnet_4]

    # Static routes on subnets.
    factory.make_StaticRoute(source=subnet_1, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_1, destination=subnet_3)
    factory.make_StaticRoute(source=subnet_1, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_3)
    factory.make_StaticRoute(source=subnet_2, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_3, destination=subnet_4)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_1)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_2)
    factory.make_StaticRoute(source=subnet_4, destination=subnet_3)

    # Load builtin scripts in the database so we can generate fake results
    # below.
    load_builtin_scripts()

    hostname = gethostname()
    region_rack = get_one(
        Node.objects.filter(node_type=NODE_TYPE.REGION_AND_RACK_CONTROLLER,
                            hostname=hostname))
    # If "make run" executes before "make sampledata", the rack may have
    # already registered.
    if region_rack is None:
        region_rack = factory.make_Node(
            node_type=NODE_TYPE.REGION_AND_RACK_CONTROLLER,
            hostname=hostname,
            interface=False,
        )

        # Get list of mac addresses that should be used for the region
        # rack controller. This will make sure the RegionAdvertisingService
        # picks the correct region on first start-up and doesn't get multiple.
        mac_addresses = get_mac_addresses()

        def get_next_mac():
            try:
                return mac_addresses.pop()
            except IndexError:
                return factory.make_mac_address()

        # Region and rack controller (hostname of dev machine)
        #   eth0     - fabric 0 - untagged
        #   eth1     - fabric 0 - untagged
        #   eth2     - fabric 1 - untagged - 172.16.2.2/24 - static
        #   bond0    - fabric 0 - untagged - 172.16.1.2/24 - static
        #   bond0.10 - fabric 0 - 10       - 172.16.3.2/24 - static
        eth0 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth0",
            node=region_rack,
            vlan=fabric0_untagged,
            mac_address=get_next_mac(),
        )
        eth1 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth1",
            node=region_rack,
            vlan=fabric0_untagged,
            mac_address=get_next_mac(),
        )
        eth2 = factory.make_Interface(
            INTERFACE_TYPE.PHYSICAL,
            name="eth2",
            node=region_rack,
            vlan=fabric1_untagged,
            mac_address=get_next_mac(),
        )
        bond0 = factory.make_Interface(
            INTERFACE_TYPE.BOND,
            name="bond0",
            node=region_rack,
            vlan=fabric0_untagged,
            parents=[eth0, eth1],
            mac_address=eth0.mac_address,
        )
        bond0_10 = factory.make_Interface(
            INTERFACE_TYPE.VLAN,
            node=region_rack,
            vlan=fabric0_vlan10,
            parents=[bond0],
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.1.2",
            subnet=subnet_1,
            interface=bond0,
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.2.2",
            subnet=subnet_2,
            interface=eth2,
        )
        factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY,
            ip="172.16.3.2",
            subnet=subnet_3,
            interface=bond0_10,
        )
        fabric0_untagged.primary_rack = region_rack
        fabric0_untagged.save()
        fabric1_untagged.primary_rack = region_rack
        fabric1_untagged.save()
        fabric0_vlan10.primary_rack = region_rack
        fabric0_vlan10.save()

    # Rack controller (happy-rack)
    #   eth0     - fabric 0 - untagged
    #   eth1     - fabric 0 - untagged
    #   eth2     - fabric 1 - untagged - 172.16.2.3/24 - static
    #   bond0    - fabric 0 - untagged - 172.16.1.3/24 - static
    #   bond0.10 - fabric 0 - 10       - 172.16.3.3/24 - static
    rack = factory.make_Node(
        node_type=NODE_TYPE.RACK_CONTROLLER,
        hostname="happy-rack",
        interface=False,
    )
    eth0 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth0",
                                  node=rack,
                                  vlan=fabric0_untagged)
    eth1 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth1",
                                  node=rack,
                                  vlan=fabric0_untagged)
    eth2 = factory.make_Interface(INTERFACE_TYPE.PHYSICAL,
                                  name="eth2",
                                  node=rack,
                                  vlan=fabric1_untagged)
    bond0 = factory.make_Interface(
        INTERFACE_TYPE.BOND,
        name="bond0",
        node=rack,
        vlan=fabric0_untagged,
        parents=[eth0, eth1],
    )
    bond0_10 = factory.make_Interface(INTERFACE_TYPE.VLAN,
                                      node=rack,
                                      vlan=fabric0_vlan10,
                                      parents=[bond0])
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.1.3",
        subnet=subnet_1,
        interface=bond0,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.2.3",
        subnet=subnet_2,
        interface=eth2,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.3.3",
        subnet=subnet_3,
        interface=bond0_10,
    )
    fabric0_untagged.secondary_rack = rack
    fabric0_untagged.save()
    fabric1_untagged.secondary_rack = rack
    fabric1_untagged.save()
    fabric0_vlan10.secondary_rack = rack
    fabric0_vlan10.save()

    # Region controller (happy-region)
    #   eth0     - fabric 0 - untagged
    #   eth1     - fabric 0 - untagged
    #   eth2     - fabric 1 - untagged - 172.16.2.4/24 - static
    #   bond0    - fabric 0 - untagged - 172.16.1.4/24 - static
    #   bond0.10 - fabric 0 - 10       - 172.16.3.4/24 - static
    region = factory.make_Node(
        node_type=NODE_TYPE.REGION_CONTROLLER,
        hostname="happy-region",
        interface=False,
    )
    eth0 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth0",
        node=region,
        vlan=fabric0_untagged,
    )
    eth1 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth1",
        node=region,
        vlan=fabric0_untagged,
    )
    eth2 = factory.make_Interface(
        INTERFACE_TYPE.PHYSICAL,
        name="eth2",
        node=region,
        vlan=fabric1_untagged,
    )
    bond0 = factory.make_Interface(
        INTERFACE_TYPE.BOND,
        name="bond0",
        node=region,
        vlan=fabric0_untagged,
        parents=[eth0, eth1],
    )
    bond0_10 = factory.make_Interface(INTERFACE_TYPE.VLAN,
                                      node=region,
                                      vlan=fabric0_vlan10,
                                      parents=[bond0])
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.1.4",
        subnet=subnet_1,
        interface=bond0,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.2.4",
        subnet=subnet_2,
        interface=eth2,
    )
    factory.make_StaticIPAddress(
        alloc_type=IPADDRESS_TYPE.STICKY,
        ip="172.16.3.4",
        subnet=subnet_3,
        interface=bond0_10,
    )

    # Create one machine for every status. Each machine has a random interface
    # and storage configration.
    node_statuses = [
        status for status in map_enum(NODE_STATUS).items() if status not in
        [NODE_STATUS.MISSING, NODE_STATUS.RESERVED, NODE_STATUS.RETIRED]
    ]
    machines = []
    test_scripts = [
        script.name
        for script in Script.objects.filter(script_type=SCRIPT_TYPE.TESTING)
    ]
    for _, status in node_statuses:
        owner = None
        if status in ALLOCATED_NODE_STATUSES:
            owner = random.choice([admin, user1, user2])
        elif status in [
                NODE_STATUS.COMMISSIONING,
                NODE_STATUS.FAILED_RELEASING,
        ]:
            owner = admin

        machine = factory.make_Node(
            status=status,
            owner=owner,
            zone=random.choice(zones),
            interface=False,
            with_boot_disk=False,
            power_type="manual",
            domain=random.choice(domains),
            memory=random.choice([1024, 4096, 8192]),
            description=random.choice([
                "",
                "Scheduled for removeal",
                "Firmware old",
                "Earmarked for Project Fuse in April",
            ]),
            cpu_count=random.randint(2, 8),
        )
        machine.set_random_hostname()
        machines.append(machine)

        # Create random network configuration.
        RandomInterfaceFactory.create_random(machine)

        # Add random storage devices and set a random layout.
        for _ in range(random.randint(1, 5)):
            factory.make_PhysicalBlockDevice(
                node=machine,
                size=random.randint(LARGE_BLOCK_DEVICE,
                                    LARGE_BLOCK_DEVICE * 10),
            )
        if status in [
                NODE_STATUS.READY,
                NODE_STATUS.ALLOCATED,
                NODE_STATUS.DEPLOYING,
                NODE_STATUS.DEPLOYED,
                NODE_STATUS.FAILED_DEPLOYMENT,
                NODE_STATUS.RELEASING,
                NODE_STATUS.FAILED_RELEASING,
        ]:
            machine.set_storage_layout(
                random.choice([
                    layout for layout in STORAGE_LAYOUTS.keys()
                    if layout != "vmfs6"
                ]))
            if status != NODE_STATUS.READY:
                machine._create_acquired_filesystems()

        # Add a random amount of events.
        for _ in range(random.randint(25, 100)):
            factory.make_Event(node=machine)

        # Add in commissioning and testing results.
        if status != NODE_STATUS.NEW:
            for _ in range(0, random.randint(1, 10)):
                css = ScriptSet.objects.create_commissioning_script_set(
                    machine)
                scripts = set()
                for __ in range(1, len(test_scripts)):
                    scripts.add(random.choice(test_scripts))
                tss = ScriptSet.objects.create_testing_script_set(
                    machine, list(scripts))
            machine.current_commissioning_script_set = css
            machine.current_testing_script_set = tss
            machine.save()

        # Fill in historic results
        for script_set in machine.scriptset_set.all():
            if script_set in [css, tss]:
                continue
            for script_result in script_set:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.exit_status = random.randint(0, 255)
                if script_result.exit_status == 0:
                    script_result.status = SCRIPT_STATUS.PASSED
                else:
                    script_result.status = random.choice(
                        list(SCRIPT_STATUS_FAILED))
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()

        # Only add in results in states where commissiong should be completed.
        if status not in [NODE_STATUS.NEW, NODE_STATUS.COMMISSIONING]:
            if status == NODE_STATUS.FAILED_COMMISSIONING:
                exit_status = random.randint(1, 255)
                script_status = random.choice(list(SCRIPT_STATUS_FAILED))
            else:
                exit_status = 0
                script_status = SCRIPT_STATUS.PASSED
            for script_result in css:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.status = script_status
                script_result.exit_status = exit_status
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()
        elif status == NODE_STATUS.COMMISSIONING:
            for script_result in css:
                script_result.status = random.choice(
                    list(SCRIPT_STATUS_RUNNING_OR_PENDING))
                if script_result.status != SCRIPT_STATUS.PENDING:
                    script_result.started = factory.make_date()
                script_result.save()

        # Only add in results in states where testing should be completed.
        if status not in [NODE_STATUS.NEW, NODE_STATUS.TESTING]:
            if status == NODE_STATUS.FAILED_TESTING:
                exit_status = random.randint(1, 255)
                script_status = random.choice(list(SCRIPT_STATUS_FAILED))
            else:
                exit_status = 0
                script_status = SCRIPT_STATUS.PASSED
            for script_result in tss:
                # Can't use script_result.store_result as it will try to
                # process the result and fail on the fake data.
                script_result.status = script_status
                script_result.exit_status = exit_status
                script_result.started = factory.make_date()
                script_result.ended = script_result.started + timedelta(
                    seconds=random.randint(0, 10000))
                script_result.stdout = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.stderr = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.output = Bin(
                    factory.make_string().encode("utf-8"))
                script_result.save()
        elif status == NODE_STATUS.TESTING:
            for script_result in tss:
                script_result.status = random.choice(
                    list(SCRIPT_STATUS_RUNNING_OR_PENDING))
                if script_result.status != SCRIPT_STATUS.PENDING:
                    script_result.started = factory.make_date()
                script_result.save()

        # Add installation results.
        if status in [
                NODE_STATUS.DEPLOYING,
                NODE_STATUS.DEPLOYED,
                NODE_STATUS.FAILED_DEPLOYMENT,
        ]:
            script_set = ScriptSet.objects.create_installation_script_set(
                machine)
            machine.current_installation_script_set = script_set
            machine.save()

        if status == NODE_STATUS.DEPLOYED:
            for script_result in machine.current_installation_script_set:
                stdout = factory.make_string().encode("utf-8")
                script_result.store_result(0, stdout)
        elif status == NODE_STATUS.FAILED_DEPLOYMENT:
            for script_result in machine.current_installation_script_set:
                exit_status = random.randint(1, 255)
                stdout = factory.make_string().encode("utf-8")
                stderr = factory.make_string().encode("utf-8")
                script_result.store_result(exit_status, stdout, stderr)

        # Add children devices to the deployed machine.
        if status == NODE_STATUS.DEPLOYED:
            boot_interface = machine.get_boot_interface()
            for _ in range(5):
                device = factory.make_Device(
                    interface=True,
                    domain=machine.domain,
                    parent=machine,
                    vlan=boot_interface.vlan,
                )
                device.set_random_hostname()
                RandomInterfaceFactory.assign_ip(
                    device.get_boot_interface(),
                    alloc_type=IPADDRESS_TYPE.STICKY,
                )

    # Create a few pods to and assign a random set of the machines to the pods.
    pods = [None]
    pod_storage_pools = defaultdict(list)
    machines_in_pods = defaultdict(list)
    for _ in range(3):
        subnet = random.choice(ipv4_subnets)
        ip = factory.pick_ip_in_Subnet(subnet)
        ip_address = factory.make_StaticIPAddress(
            alloc_type=IPADDRESS_TYPE.STICKY, ip=ip, subnet=subnet)
        power_address = "qemu+ssh://ubuntu@%s/system" % ip
        pod = factory.make_Pod(
            pod_type="virsh",
            parameters={"power_address": power_address},
            ip_address=ip_address,
            capabilities=[
                Capabilities.DYNAMIC_LOCAL_STORAGE,
                Capabilities.COMPOSABLE,
            ],
        )
        for _ in range(3):
            pool = factory.make_PodStoragePool(pod)
            pod_storage_pools[pod].append(pool)
        pod.default_storage_pool = pool
        pod.save()
        pods.append(pod)
    for machine in machines:
        # Add the machine to the pod if its lucky day!
        pod = random.choice(pods)
        if pod is not None:
            machine.bmc = pod
            machine.instance_power_parameters = {"power_id": machine.hostname}
            machine.save()
            machines_in_pods[pod].append(machine)

            vm = factory.make_VirtualMachine(
                identifier=machine.hostname,
                bmc=pod,
                machine=machine,
                unpinned_cores=machine.cpu_count,
            )

            # Assign the block devices on the machine to a storage pool.
            for block_device in machine.physicalblockdevice_set.all():
                factory.make_VirtualMachineDisk(
                    vm=vm,
                    name=block_device.name,
                    size=block_device.size,
                    backing_pool=random.choice(pod_storage_pools[pod]),
                )

    # Update the pod attributes so that it has more available then used.
    for pod in pods[1:]:
        used_resources = get_vm_host_used_resources(pod)
        pod.cores = used_resources.cores + random.randint(4, 8)
        pod.memory = used_resources.total_memory + random.choice(
            [1024, 2048, 4096, 4096 * 4, 4096 * 8])
        pod.local_storage = sum(pool.storage
                                for pool in pod_storage_pools[pod])
        pod.save()

    # Create a few devices.
    for _ in range(10):
        device = factory.make_Device(interface=True)
        device.set_random_hostname()

    # Add some DHCP snippets.
    # - Global
    factory.make_DHCPSnippet(
        name="foo class",
        description="adds class for vender 'foo'",
        value=VersionedTextFile.objects.create(data=dedent("""\
            class "foo" {
                match if substring (
                    option vendor-class-identifier, 0, 3) = "foo";
            }
        """)),
    )
    factory.make_DHCPSnippet(
        name="bar class",
        description="adds class for vender 'bar'",
        value=VersionedTextFile.objects.create(data=dedent("""\
            class "bar" {
                match if substring (
                    option vendor-class-identifier, 0, 3) = "bar";
            }
        """)),
        enabled=False,
    )
    # - Subnet
    factory.make_DHCPSnippet(
        name="600 lease time",
        description="changes lease time to 600 secs.",
        value=VersionedTextFile.objects.create(data="default-lease-time 600;"),
        subnet=subnet_1,
    )
    factory.make_DHCPSnippet(
        name="7200 max lease time",
        description="changes max lease time to 7200 secs.",
        value=VersionedTextFile.objects.create(data="max-lease-time 7200;"),
        subnet=subnet_2,
        enabled=False,
    )
    # - Node
    factory.make_DHCPSnippet(
        name="boot from other server",
        description="instructs device to boot from other server",
        value=VersionedTextFile.objects.create(data=dedent("""\
            filename "test-boot";
            server-name "boot.from.me";
        """)),
        node=device,
    )

    # Add notifications for admins, users, and each individual user, and for
    # each notification category.
    factory.make_Notification(
        "Attention admins! Core critical! Meltdown imminent! Evacuate "
        "habitat immediately!",
        admins=True,
        category="error",
    )
    factory.make_Notification(
        "Dear users, rumours of a core meltdown are unfounded. Please "
        "return to your home-pods and places of business.",
        users=True,
        category="warning",
    )
    factory.make_Notification(
        "FREE! For the next 2 hours get FREE blueberry and iodine pellets "
        "at the nutri-dispensers.",
        users=True,
        category="success",
    )
    for user in User.objects.all():
        context = {"name": user.username.capitalize()}
        factory.make_Notification(
            "Greetings, {name}! Get away from the habitat for the weekend and "
            "visit the Mare Nubium with MAAS Tours. Use the code METAL to "
            "claim a special gift!",
            user=user,
            context=context,
            category="info",
        )
Пример #6
0
 def test__returns_single_network(self):
     subnet = factory.make_Subnet()
     expected = [str(subnet.cidr)]
     self.assertEqual(expected, get_trusted_networks())
Пример #7
0
 def test__returns_many_networks(self):
     subnets = [factory.make_Subnet() for _ in range(random.randint(1, 5))]
     expected = [str(subnet.cidr) for subnet in subnets]
     # Note: This test was seen randomly failing because the networks were
     # in an unexpected order...
     self.assertItemsEqual(expected, get_trusted_networks())
Пример #8
0
 def make_node_with_address(self, space, cidr):
     node = factory.make_Node()
     iface = factory.make_Interface(node=node)
     subnet = factory.make_Subnet(space=space, cidr=cidr)
     sip = factory.make_StaticIPAddress(interface=iface, subnet=subnet)
     return node, sip.get_ipaddress()
Пример #9
0
def make_plain_subnet():
    return factory.make_Subnet(cidr="192.168.0.0/24",
                               gateway_ip="192.168.0.1",
                               dns_servers=[])
Пример #10
0
 def test_handler_path(self):
     subnet = factory.make_Subnet()
     self.assertEqual("/MAAS/api/2.0/subnets/%s/" % subnet.id,
                      get_subnet_uri(subnet))
Пример #11
0
    def test__yields_routes_with_lowest_metrics_first(self):
        space = factory.make_Space()
        # Ensure networks are disjoint but of the same family.
        networks = self.gen_disjoint_networks()

        # Create the node for the "left" that has two IP addresses, one in the
        # null space, one in a non-null space.
        origin = factory.make_Node(hostname="origin")
        origin_iface = factory.make_Interface(node=origin,
                                              link_connected=False)
        origin_subnet = factory.make_Subnet(space=space, cidr=next(networks))
        origin_subnet_null_space = factory.make_Subnet(space=None,
                                                       cidr=next(networks))
        origin_sip = factory.make_StaticIPAddress(interface=origin_iface,
                                                  subnet=origin_subnet)
        origin_sip_null_space = factory.make_StaticIPAddress(
            interface=origin_iface, subnet=origin_subnet_null_space)

        # Same subnet, different node.
        node_same_subnet = factory.make_Node(hostname="same-subnet")
        sip_same_subnet = factory.make_StaticIPAddress(
            interface=factory.make_Interface(node=node_same_subnet,
                                             link_connected=False),
            subnet=origin_subnet)

        # Same VLAN, different subnet, different node.
        node_same_vlan = factory.make_Node(hostname="same-vlan")
        sip_same_vlan = factory.make_StaticIPAddress(
            interface=factory.make_Interface(node=node_same_vlan,
                                             link_connected=False),
            subnet=factory.make_Subnet(space=space,
                                       vlan=origin_subnet.vlan,
                                       cidr=next(networks)))

        # Same space, different VLAN, subnet, and node.
        node_same_space = factory.make_Node(hostname="same-space")
        sip_same_space = factory.make_StaticIPAddress(
            interface=factory.make_Interface(node=node_same_space,
                                             link_connected=False),
            subnet=factory.make_Subnet(space=space, cidr=next(networks)))

        # Null space, different VLAN, subnet, and node. (won't be included)
        node_null_space = factory.make_Node(hostname="null-space")
        factory.make_StaticIPAddress(
            interface=factory.make_Interface(node=node_null_space,
                                             link_connected=False),
            subnet=factory.make_Subnet(space=None, cidr=next(networks)))

        # We'll search for routes between `lefts` and `rights`.
        lefts = [
            origin,
        ]
        rights = [
            node_same_subnet,
            node_same_vlan,
            node_same_space,
            node_null_space,  # Should not be included.
        ]

        # This is in order, lowest "metric" first.
        expected = [
            (origin, origin_sip.get_ipaddress(), node_same_subnet,
             sip_same_subnet.get_ipaddress()),
            (origin, origin_sip.get_ipaddress(), node_same_vlan,
             sip_same_vlan.get_ipaddress()),
            (origin, origin_sip.get_ipaddress(), node_same_space,
             sip_same_space.get_ipaddress()),
        ]
        self.assertThat(find_addresses_between_nodes(lefts, rights),
                        AfterPreprocessing(list, Equals(expected)))

        # Same node, same space, different VLAN and subnet. We did not add
        # this earlier because its existence allows for a large number of
        # additional routes between the origin and the other nodes, which
        # would have obscured the test.
        origin_sip_2 = factory.make_StaticIPAddress(
            interface=factory.make_Interface(node=origin,
                                             link_connected=False),
            subnet=factory.make_Subnet(space=space, cidr=next(networks)))

        # Now the first addresses returned are between those addresses we
        # created on the same node, in no particular order.
        origin_ips = origin_sip.get_ipaddress(), origin_sip_2.get_ipaddress()
        expected_mutual = {(origin, ip1, origin, ip2)
                           for ip1, ip2 in product(origin_ips, origin_ips)}
        # There's a mutual route for the null-space IP address too.
        expected_mutual.add(
            (origin, origin_sip_null_space.get_ipaddress(), origin,
             origin_sip_null_space.get_ipaddress()))
        observed_mutual = takewhile(
            (lambda route: route[0] == route[2]),  # Route is mutual.
            find_addresses_between_nodes(lefts, [origin, *rights]),
        )
        self.assertItemsEqual(expected_mutual, observed_mutual)
Пример #12
0
 def make_subnet(self, allow_proxy=True):
     return factory.make_Subnet(allow_proxy=allow_proxy)
Пример #13
0
 def test_handler_path(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(subnet, "10.0.0.2", "10.0.0.10")
     self.assertEqual(
         "/MAAS/api/2.0/ipranges/%s/" % iprange.id, get_iprange_uri(iprange)
     )
Пример #14
0
 def create_subnet(self, params=None):
     if params is None:
         params = {}
     return factory.make_Subnet(**params, space=RANDOM)
Пример #15
0
 def test_POST_is_prohibited(self):
     self.become_admin()
     subnet = factory.make_Subnet()
     response = self.client.post(self.get_url(subnet),
                                 {'description': "New description"})
     self.assertEqual(http.client.BAD_REQUEST, response.status_code)
Пример #16
0
def make_plain_ipv6_subnet():
    return factory.make_Subnet(cidr="2001::/64",
                               gateway_ip="2001::1",
                               dns_servers=[])
Пример #17
0
 def test_DELETE_returns_410(self):
     self.become_admin()
     subnet = factory.make_Subnet()
     response = self.client.delete(self.get_url(subnet))
     self.assertEqual(http.client.GONE, response.status_code)
Пример #18
0
 def test__sets_subnet_queryset_to_subnets_on_interface_vlan(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     subnets = [factory.make_Subnet(vlan=interface.vlan) for _ in range(3)]
     form = InterfaceLinkForm(instance=interface, data={})
     self.assertItemsEqual(subnets, form.fields["subnet"].queryset)
Пример #19
0
 def test__returns_no_networks_if_not_allow_dns(self):
     factory.make_Subnet(allow_dns=False)
     subnet_allowed = factory.make_Subnet(allow_dns=True)
     expected = [str(subnet_allowed.cidr)]
     self.assertEqual(expected, get_trusted_networks())
Пример #20
0
 def test_subnet_stats(self):
     subnet = factory.make_Subnet(cidr="1.2.0.0/16", gateway_ip="1.2.0.254")
     factory.make_IPRange(
         subnet=subnet,
         start_ip="1.2.0.11",
         end_ip="1.2.0.20",
         alloc_type=IPRANGE_TYPE.DYNAMIC,
     )
     factory.make_IPRange(
         subnet=subnet,
         start_ip="1.2.0.51",
         end_ip="1.2.0.70",
         alloc_type=IPRANGE_TYPE.RESERVED,
     )
     factory.make_StaticIPAddress(ip="1.2.0.12",
                                  alloc_type=IPADDRESS_TYPE.DHCP,
                                  subnet=subnet)
     for n in (60, 61):
         factory.make_StaticIPAddress(
             ip="1.2.0.{}".format(n),
             alloc_type=IPADDRESS_TYPE.USER_RESERVED,
             subnet=subnet,
         )
     for n in (80, 90, 100):
         factory.make_StaticIPAddress(
             ip="1.2.0.{}".format(n),
             alloc_type=IPADDRESS_TYPE.STICKY,
             subnet=subnet,
         )
     metrics = create_metrics(
         STATS_DEFINITIONS, registry=prometheus_client.CollectorRegistry())
     update_prometheus_stats(metrics)
     output = metrics.generate_latest().decode("ascii")
     self.assertIn(
         "maas_net_subnet_ip_count"
         '{cidr="1.2.0.0/16",status="available"} 65500.0',
         output,
     )
     self.assertIn(
         "maas_net_subnet_ip_count"
         '{cidr="1.2.0.0/16",status="unavailable"} 34.0',
         output,
     )
     self.assertIn(
         "maas_net_subnet_ip_dynamic"
         '{cidr="1.2.0.0/16",status="available"} 9.0',
         output,
     )
     self.assertIn(
         'maas_net_subnet_ip_dynamic{cidr="1.2.0.0/16",status="used"} 1.0',
         output,
     )
     self.assertIn(
         "maas_net_subnet_ip_reserved"
         '{cidr="1.2.0.0/16",status="available"} 18.0',
         output,
     )
     self.assertIn(
         'maas_net_subnet_ip_reserved{cidr="1.2.0.0/16",status="used"} 2.0',
         output,
     )
     self.assertIn('maas_net_subnet_ip_static{cidr="1.2.0.0/16"} 3.0',
                   output)
Пример #21
0
 def test__returns_valid(self):
     subnet = factory.make_Subnet(cidr=self.cidr)
     self.assertEqual(self.result, get_resource_name_for_subnet(subnet))
Пример #22
0
 def test_ignores_unconnected_macs(self):
     self.make_interface(subnets=[factory.make_Subnet()], owner=self.user)
     self.make_interface(subnets=[], owner=self.user)
     self.assertEqual([],
                      self.request_connected_macs(factory.make_Subnet()))
Пример #23
0
 def test_POST_reserve_errors_for_no_matching_subnet(self):
     network = factory.make_ipv4_network()
     factory.make_Subnet(cidr=str(network.cidr))
     other_net = factory.make_ipv4_network(but_not=[network])
     response = self.post_reservation_request(network=other_net)
     self.assertNoMatchingNetworkError(response, other_net)
Пример #24
0
 def test_includes_MACs_for_nodes_visible_to_user(self):
     subnet = factory.make_Subnet()
     interface = self.make_interface(subnets=[subnet], owner=self.user)
     self.assertEqual([interface.mac_address],
                      self.extract_macs(
                          self.request_connected_macs(subnet)))
Пример #25
0
 def test_subnets_are_reconnected_when_vlan_is_deleted(self):
     fabric = factory.make_Fabric()
     vlan = factory.make_VLAN(fabric=fabric)
     subnet = factory.make_Subnet(vlan=vlan)
     vlan.delete()
     self.assertEqual(reload_object(subnet).vlan, fabric.get_default_vlan())
Пример #26
0
 def test_excludes_MACs_for_nodes_not_visible_to_user(self):
     subnet = factory.make_Subnet()
     node = factory.make_Node(pool=factory.make_ResourcePool())
     self.make_interface(subnets=[subnet], node=node)
     self.assertEqual([], self.request_connected_macs(subnet))
Пример #27
0
 def test_scan_fails_if_no_rack_is_configured_with_subnet(self):
     user = factory.make_admin()
     handler = SubnetHandler(user, {}, None)
     subnet = factory.make_Subnet(version=4)
     with ExpectedException(ValueError, ".*must be configured on a rack*"):
         handler.scan({"id": subnet.id})
Пример #28
0
 def test_handler_path(self):
     subnet = factory.make_Subnet()
     self.assertEqual('/api/2.0/networks/subnet-%d/' % subnet.id,
                      self.get_url(subnet))
Пример #29
0
 def test_doest_require_vlan_or_cidr_on_update(self):
     subnet = factory.make_Subnet()
     form = SubnetForm(instance=subnet, data={})
     self.assertTrue(form.is_valid(), dict(form.errors))
Пример #30
0
 def test_returns_None_when_subnet_is_not_managed(self):
     subnet = factory.make_Subnet()
     self.assertIsNone(
         find_rack_controller(
             make_request(factory.pick_ip_in_Subnet(subnet))))