Exemple #1
0
 def test__delete_as_non_admin_asserts(self):
     user = factory.make_User()
     handler = SpaceHandler(user, {})
     space = factory.make_Space()
     with ExpectedException(AssertionError, "Permission denied."):
         handler.delete({
             "id": space.id,
         })
Exemple #2
0
 def test_delete_deletes_space(self):
     self.become_admin()
     space = factory.make_Space()
     uri = get_space_uri(space)
     response = self.client.delete(uri)
     self.assertEqual(http.client.NO_CONTENT, response.status_code,
                      response.content)
     self.assertIsNone(reload_object(space))
Exemple #3
0
 def test_update_sets_space_by_specifier(self):
     vlan = factory.make_VLAN()
     space = factory.make_Space()
     form = VLANForm(instance=vlan, data={"space": "name:" + space.name})
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     vlan = reload_object(vlan)
     self.assertEquals(space.id, vlan.space.id)
Exemple #4
0
 def test_update_clears_space_when_None(self):
     space = factory.make_Space()
     vlan = factory.make_VLAN(space=space)
     form = VLANForm(instance=vlan, data={"space": None})
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     vlan = reload_object(vlan)
     self.assertIsNone(vlan.space)
Exemple #5
0
 def test__reloads_user(self):
     user = factory.make_admin()
     handler = SpaceHandler(user, {}, None)
     space = factory.make_Space()
     user.is_superuser = False
     user.save()
     with ExpectedException(AssertionError, "Permission denied."):
         handler.delete({"id": space.id})
Exemple #6
0
 def test_update_admin_only(self):
     space = factory.make_Space()
     new_name = factory.make_name("space")
     uri = get_space_uri(space)
     response = self.client.put(uri, {
         "name": new_name,
     })
     self.assertEqual(http.client.FORBIDDEN, response.status_code,
                      response.content)
Exemple #7
0
 def test__space_specifier_matches_space_by_id(self):
     factory.make_VLAN()
     space = factory.make_Space()
     vlan = factory.make_VLAN(space=space)
     factory.make_VLAN()
     self.assertItemsEqual(
         VLAN.objects.filter_by_specifiers('space:%s' % space.id),
         [vlan]
     )
Exemple #8
0
 def create_device_with_parent(self, params=None):
     if params is None:
         params = {}
     vlan = factory.make_VLAN(space=factory.make_Space())
     parent = factory.make_Node(with_boot_disk=False)
     params["node_type"] = NODE_TYPE.DEVICE
     params["parent"] = parent
     device = factory.make_Node(vlan=vlan, **params)
     return device, parent
Exemple #9
0
 def test__user_view_returns_space(self):
     user = factory.make_User()
     space = factory.make_Space()
     self.assertEqual(
         space,
         Space.objects.get_space_or_404(
             space.id, user, NodePermission.view
         ),
     )
Exemple #10
0
 def test__delete_as_admin_success(self):
     user = factory.make_admin()
     handler = SpaceHandler(user, {}, None)
     space = factory.make_Space()
     handler.delete({
         "id": space.id,
     })
     space = reload_object(space)
     self.assertThat(space, Equals(None))
Exemple #11
0
 def test__admin_admin_returns_space(self):
     admin = factory.make_admin()
     space = factory.make_Space()
     self.assertEqual(
         space,
         Space.objects.get_space_or_404(
             space.id, admin, NodePermission.admin
         ),
     )
Exemple #12
0
 def test__user_admin_raises_PermissionError(self):
     user = factory.make_User()
     space = factory.make_Space()
     self.assertRaises(
         PermissionDenied,
         Space.objects.get_space_or_404,
         space.id,
         user,
         NodePermission.admin,
     )
Exemple #13
0
 def test_create_as_admin_succeeds_even_with_a_specified_space(self):
     user = factory.make_admin()
     handler = SubnetHandler(user, {}, None)
     vlan = factory.make_VLAN()
     space = factory.make_Space()
     result = handler.create(
         {"vlan": vlan.id, "cidr": "192.168.0.0/24", "space": space.id}
     )
     subnet = Subnet.objects.get(id=result["id"])
     self.assertThat(subnet.cidr, Equals("192.168.0.0/24"))
Exemple #14
0
 def test_get(self):
     user = factory.make_User()
     handler = SpaceHandler(user, {}, None)
     space = factory.make_Space()
     for _ in range(3):
         node = factory.make_Node(interface=True)
         interface = node.get_boot_interface()
         subnet = factory.make_Subnet(space=space, vlan=interface.vlan)
         factory.make_StaticIPAddress(subnet=subnet, interface=interface)
     self.assertEqual(self.dehydrate_space(space),
                      handler.get({"id": space.id}))
Exemple #15
0
 def make_example_configuration(self):
     # Configure example time references.
     ntp_servers = {factory.make_name("ntp-server") for _ in range(5)}
     Config.objects.set_config("ntp_servers", " ".join(ntp_servers))
     # Populate the database with example peers.
     space = factory.make_Space()
     region, addr4, addr6 = make_region_with_address(space)
     self.useFixture(MAASIDFixture(region.system_id))
     peer1, addr1_4, addr1_6 = make_region_with_address(space)
     peer2, addr2_4, addr2_6 = make_region_with_address(space)
     # Return the servers and all possible peer IP addresses.
     return ntp_servers, {addr1_4.ip, addr1_6.ip, addr2_4.ip, addr2_6.ip}
Exemple #16
0
 def test__rejects_provided_space_on_update(self):
     space = factory.make_Space()
     subnet = factory.make_Subnet()
     form = SubnetForm(instance=subnet, data={"space": space.id})
     self.assertFalse(form.is_valid(), dict(form.errors))
     self.assertEqual(
         {
             "space": [
                 "Spaces may no longer be set on subnets. Set the space on the "
                 "underlying VLAN."
             ]
         }, dict(form.errors))
Exemple #17
0
    def test_does_not_contain_routes_between_nodes_on_differing_spaces(self):
        space1 = factory.make_Space()
        space2 = factory.make_Space()
        network1 = factory.make_ip4_or_6_network()
        network2 = factory.make_ip4_or_6_network(version=network1.version)
        node1, if1, sn1, sip1 = self.make_node_with_address(network1, space1)
        node2, if2, sn2, sip2 = self.make_node_with_address(network2, space2)

        # Only routes from left to left and right to right are found: right is
        # not routable from left, and left is not routable from right because
        # the spaces differ.
        left = node1.id, if1.id, sn1.id, sn1.vlan.id, sip1.ip
        right = node2.id, if2.id, sn2.id, sn2.vlan.id, sip2.ip
        expected = [
            (*left, *left, space1.id, 0),
            (*right, *right, space2.id, 0),
        ]

        with connection.cursor() as cursor:
            cursor.execute("SELECT * from maasserver_routable_pairs")
            self.assertItemsEqual(expected, cursor.fetchall())
Exemple #18
0
 def test__update_as_admin_succeeds_even_with_a_specified_space(self):
     user = factory.make_admin()
     handler = SubnetHandler(user, {})
     subnet = factory.make_Subnet(description="sad subnet")
     space = factory.make_Space()
     new_description = "happy subnet"
     handler.update({
         "id": subnet.id,
         "space": space.id,
         "description": new_description
     })
     subnet = reload_object(subnet)
     self.assertThat(subnet.description, Equals(new_description))
Exemple #19
0
 def test_get(self):
     user = factory.make_User()
     handler = VLANHandler(user, {}, None)
     vlan = factory.make_VLAN(space=factory.make_Space())
     for _ in range(3):
         factory.make_Subnet(vlan=vlan)
     for _ in range(3):
         node = factory.make_Node(interface=True)
         interface = node.get_boot_interface()
         interface.vlan = vlan
         interface.save()
     self.assertEqual(self.dehydrate_vlan(vlan),
                      handler.get({"id": vlan.id}))
Exemple #20
0
    def test_list_constant_queries(self):
        user = factory.make_User()
        handler = SpaceHandler(user, {}, None)
        for _ in range(10):
            space = factory.make_Space()
            node = factory.make_Node(interface=True)
            interface = node.get_boot_interface()
            subnet = factory.make_Subnet(space=space, vlan=interface.vlan)
            factory.make_StaticIPAddress(subnet=subnet, interface=interface)
        queries_one, _ = count_queries(handler.list, {"limit": 1})
        queries_multiple, _ = count_queries(handler.list, {})

        self.assertEqual(queries_one, queries_multiple)
Exemple #21
0
 def test__updates_space(self):
     new_name = factory.make_name("space")
     new_description = factory.make_name("description")
     space = factory.make_Space()
     form = SpaceForm(instance=space,
                      data={
                          "name": new_name,
                          "description": new_description,
                      })
     self.assertTrue(form.is_valid(), form.errors)
     form.save()
     self.assertEqual(new_name, reload_object(space).name)
     self.assertEqual(new_description, reload_object(space).description)
Exemple #22
0
    def test__does_not_contain_routes_between_addrs_of_diff_network_fams(self):
        space = factory.make_Space()  # One space.
        network1 = factory.make_ip4_or_6_network()
        network2 = factory.make_ip4_or_6_network(
            version=(4 if network1.version == 6 else 6))
        node1, ip1 = self.make_node_with_address(space, network1)
        node2, ip2 = self.make_node_with_address(space, network2)

        expected = []

        # No routable addresses are found.
        self.assertItemsEqual(expected,
                              find_addresses_between_nodes([node1], [node2]))
Exemple #23
0
 def test_update(self):
     self.become_admin()
     space = factory.make_Space()
     new_name = factory.make_name("space")
     uri = get_space_uri(space)
     response = self.client.put(uri, {"name": new_name})
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     self.assertEqual(
         new_name,
         json.loads(response.content.decode(
             settings.DEFAULT_CHARSET))["name"],
     )
     self.assertEqual(new_name, reload_object(space).name)
Exemple #24
0
 def test__rejects_space_on_create(self):
     space = factory.make_Space()
     form = SubnetForm({
         "space": space.id,
         "cidr": factory._make_random_network()
     })
     self.assertFalse(form.is_valid(), dict(form.errors))
     self.assertEqual(
         {
             "space": [
                 "Spaces may no longer be set on subnets. Set the space on the "
                 "underlying VLAN."
             ]
         }, dict(form.errors))
Exemple #25
0
    def test__yields_routes_between_nodes_on_same_space(self):
        space = factory.make_Space()
        network1 = factory.make_ip4_or_6_network()
        network2 = factory.make_ip4_or_6_network(version=network1.version)
        node1, ip1 = self.make_node_with_address(space, network1)
        node2, ip2 = self.make_node_with_address(space, network2)

        left = node1, ip1
        right = node2, ip2
        expected = [left + right]

        # A route from node1 to node2 is found.
        self.assertItemsEqual(expected,
                              find_addresses_between_nodes([node1], [node2]))
Exemple #26
0
    def test_read_with_no_undefined_space(self):
        factory.make_VLAN()
        space = factory.make_Space()
        uri = get_spaces_uri()
        fill_empty_spaces(space)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        # Does not include the undefined space.
        expected_ids = [space.id]
        result_ids = [
            space["id"] for space in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET))
        ]
        self.assertItemsEqual(expected_ids, result_ids)
Exemple #27
0
    def test__getConfiguration_returns_configuration_object(self):
        service = syslog.RegionSyslogService(reactor)

        # Put all addresses in the same space so they're mutually routable.
        space = factory.make_Space()
        # Populate the database with "this" region rack and an example peer.
        region_rack, _, _ = make_region_rack_with_address(space)
        self.useFixture(MAASIDFixture(region_rack.system_id))
        peer, addr4, addr6 = make_region_rack_with_address(space)

        observed = service._getConfiguration()
        self.assertThat(observed, IsInstance(syslog._Configuration))

        expected_peers = AllMatch(Equals((peer.hostname, IPAddress(addr4.ip))))

        self.assertThat(observed, MatchesStructure(peers=expected_peers))
Exemple #28
0
 def make_example_configuration(self):
     # Set the syslog port.
     port = factory.pick_port()
     Config.objects.set_config('maas_syslog_port', port)
     # Populate the database with example peers.
     space = factory.make_Space()
     region, addr4, addr6 = make_region_rack_with_address(space)
     self.useFixture(MAASIDFixture(region.system_id))
     peer1, addr1_4, addr1_6 = make_region_rack_with_address(space)
     peer2, addr2_4, addr2_6 = make_region_rack_with_address(space)
     # Return the servers and all possible peer IP addresses.
     return port, [
         (peer1, sorted([IPAddress(addr1_4.ip),
                         IPAddress(addr1_6.ip)])[0]),
         (peer2, sorted([IPAddress(addr2_4.ip),
                         IPAddress(addr2_6.ip)])[0]),
     ]
Exemple #29
0
 def test_includes_vlan_objects(self):
     space = factory.make_Space()
     vlan = factory.make_VLAN(space=space)
     uri = get_space_uri(space)
     response = self.client.get(uri)
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     parsed_space = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     parsed_vlan = parsed_space['vlans'][0]
     self.assertThat(
         parsed_vlan,
         ContainsDict({
             "id": Equals(vlan.id),
             "vid": Equals(vlan.vid),
             "fabric_id": Equals(vlan.fabric_id),
         }))
Exemple #30
0
    def test_read(self):
        space = factory.make_Space()
        subnet_ids = [factory.make_Subnet(space=space).id for _ in range(3)]
        uri = get_space_uri(space)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_space = json.loads(
            response.content.decode(settings.DEFAULT_CHARSET))
        self.assertThat(
            parsed_space,
            ContainsDict({
                "id": Equals(space.id),
                "name": Equals(space.get_name()),
            }))
        parsed_subnets = [subnet["id"] for subnet in parsed_space["subnets"]]
        self.assertItemsEqual(subnet_ids, parsed_subnets)