Пример #1
0
 def test_stats_all(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)
     self.assertEqual(
         stats.get_subnets_utilisation_stats(), {
             '1.2.0.0/16': {
                 'available': 2**16 - 36,
                 'dynamic_available': 9,
                 'dynamic_used': 1,
                 'reserved_available': 18,
                 'reserved_used': 2,
                 'static': 3,
                 'unavailable': 34
             }
         })
Пример #2
0
 def test_stats_reserved(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.RESERVED,
     )
     factory.make_IPRange(
         subnet=subnet,
         start_ip="1.2.0.51",
         end_ip="1.2.0.60",
         alloc_type=IPRANGE_TYPE.RESERVED,
     )
     factory.make_StaticIPAddress(
         ip="1.2.0.15",
         alloc_type=IPADDRESS_TYPE.USER_RESERVED,
         subnet=subnet,
     )
     self.assertEqual(
         stats.get_subnets_utilisation_stats(),
         {
             "1.2.0.0/16": {
                 "available": 2 ** 16 - 23,
                 "dynamic_available": 0,
                 "dynamic_used": 0,
                 "reserved_available": 19,
                 "reserved_used": 1,
                 "static": 0,
                 "unavailable": 21,
             }
         },
     )
Пример #3
0
 def test_stats_reserved(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.RESERVED)
     factory.make_IPRange(subnet=subnet,
                          start_ip='1.2.0.51',
                          end_ip='1.2.0.60',
                          alloc_type=IPRANGE_TYPE.RESERVED)
     factory.make_StaticIPAddress(ip='1.2.0.15',
                                  alloc_type=IPADDRESS_TYPE.USER_RESERVED,
                                  subnet=subnet)
     self.assertEqual(
         stats.get_subnets_utilisation_stats(), {
             '1.2.0.0/16': {
                 'available': 2**16 - 23,
                 'dynamic_available': 0,
                 'dynamic_used': 0,
                 'reserved_available': 19,
                 'reserved_used': 1,
                 'static': 0,
                 'unavailable': 21
             }
         })
Пример #4
0
 def test_read(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(subnet, "10.0.0.2", "10.0.0.10")
     factory.make_IPRange(subnet, "10.0.0.11", "10.0.0.20")
     factory.make_IPRange(subnet, "10.0.0.21", "10.0.0.30")
     uri = get_iprange_uri(iprange)
     response = self.client.get(uri)
     self.assertEqual(
         http.client.OK, response.status_code, response.content
     )
     parsed_ipranges = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET)
     )
     self.assertThat(
         parsed_ipranges,
         ContainsDict(
             {
                 "id": Equals(iprange.id),
                 "start_ip": Equals(iprange.start_ip),
                 "end_ip": Equals(iprange.end_ip),
                 "comment": Equals(iprange.comment),
                 "type": Equals(iprange.type),
                 "user": Equals(iprange.user),
             }
         ),
     )
Пример #5
0
 def test_DELETE_user_with_iprange_fails(self):
     self.become_admin()
     user = factory.make_User()
     factory.make_IPRange(user=user)
     response = self.client.delete(
         reverse('user_handler', args=[user.username]))
     self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                      response.status_code)
     self.assertIn(b'1 IP range(s) are still allocated', response.content)
Пример #6
0
 def _unreserved_ip_ranges_empty(self, subnet, first_address, last_address):
     """ Used by the succeeding three tests to prevent duplicating the
     boilerplate that creates the requested range, then makes sure the
     unreserved_ip_ranges API call successfully returns an empty list.
     """
     factory.make_IPRange(subnet, first_address, last_address)
     response = self.client.get(get_subnet_uri(subnet),
                                {'op': 'unreserved_ip_ranges'})
     result = json.loads(response.content.decode(settings.DEFAULT_CHARSET))
     self.assertEqual(http.client.OK, response.status_code,
                      explain_unexpected_response(http.client.OK, response))
     self.assertThat(result, Equals([]), str(subnet.get_ipranges_in_use()))
Пример #7
0
 def test_update_403_when_not_user(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(
         subnet, '10.0.0.2', '10.0.0.10', user=factory.make_User())
     uri = get_iprange_uri(iprange)
     response = self.client.put(uri, {})
     self.assertEqual(
         http.client.FORBIDDEN, response.status_code, response.content)
Пример #8
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)
Пример #9
0
 def test_delete_403_when_not_user(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(
         subnet, '10.0.0.2', '10.0.0.10', user=factory.make_User())
     uri = get_iprange_uri(iprange)
     response = self.client.delete(uri)
     self.assertEqual(
         http.client.FORBIDDEN, response.status_code, response.content)
     self.assertIsNotNone(reload_object(iprange))
Пример #10
0
 def test_delete_deletes_iprange(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(
         subnet, '10.0.0.2', '10.0.0.10', user=self.user)
     uri = get_iprange_uri(iprange)
     response = self.client.delete(uri)
     self.assertEqual(
         http.client.NO_CONTENT, response.status_code, response.content)
     self.assertIsNone(reload_object(iprange))
Пример #11
0
 def test_stats_all(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,
         )
     self.assertEqual(
         stats.get_subnets_utilisation_stats(),
         {
             "1.2.0.0/16": {
                 "available": 2 ** 16 - 36,
                 "dynamic_available": 9,
                 "dynamic_used": 1,
                 "reserved_available": 18,
                 "reserved_used": 2,
                 "static": 3,
                 "unavailable": 34,
             }
         },
     )
Пример #12
0
 def test_transfer_resources(self):
     user = factory.make_User()
     node = factory.make_Node(owner=user)
     ipaddress = factory.make_StaticIPAddress(user=user)
     iprange = factory.make_IPRange(user=user)
     new_user = factory.make_User()
     user.userprofile.transfer_resources(new_user)
     self.assertEqual(reload_object(node).owner, new_user)
     self.assertEqual(reload_object(ipaddress).user, new_user)
     self.assertEqual(reload_object(iprange).user, new_user)
Пример #13
0
 def test_update(self):
     subnet = factory.make_Subnet(cidr="10.0.0.0/24")
     iprange = factory.make_IPRange(
         subnet, '10.0.0.2', '10.0.0.10', user=self.user)
     uri = get_iprange_uri(iprange)
     comment = factory.make_name("comment")
     response = self.client.put(uri, {"comment": comment})
     self.assertEqual(
         http.client.OK, response.status_code, response.content)
     self.assertEqual(
         comment,
         json.loads(
             response.content.decode(settings.DEFAULT_CHARSET))['comment'])
     self.assertEqual(comment, reload_object(iprange).comment)
Пример #14
0
    def test_read(self):
        subnet = factory.make_Subnet(cidr="10.0.0.0/24")
        factory.make_IPRange(subnet, "10.0.0.2", "10.0.0.10")
        factory.make_IPRange(subnet, "10.0.0.11", "10.0.0.20")
        factory.make_IPRange(subnet, "10.0.0.21", "10.0.0.30")
        uri = get_ipranges_uri()
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        expected_ids = [iprange.id for iprange in IPRange.objects.all()]
        result_ids = [
            iprange["id"] for iprange in json.loads(
                response.content.decode(settings.DEFAULT_CHARSET))
        ]
        self.assertItemsEqual(expected_ids, result_ids)
Пример #15
0
 def create_iprange(self, params=None):
     if params is None:
         params = {}
     return factory.make_IPRange(**params)
Пример #16
0
 def test_delete_attached_iprange(self):
     # Cannot delete a user with an IP range attached to it.
     profile = factory.make_User().userprofile
     factory.make_IPRange(user=profile.user)
     error = self.assertRaises(CannotDeleteUserException, profile.delete)
     self.assertIn("1 IP range(s)", str(error))
Пример #17
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))
Пример #18
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(
         '/api/2.0/ipranges/%s/' % iprange.id,
         get_iprange_uri(iprange))