Esempio n. 1
0
 def test__input_url_allows_list(self):
     script = factory.make_Script(
         parameters={'url': {
             'type': 'url',
             'allow_list': True,
         }})
     inputs = ','.join([
         factory.make_ipv4_address(),
         "%s://%s:%d/%s" % (self.pick_scheme(), factory.make_ipv4_address(),
                            random.randint(0, 65535), factory.make_name()),
         factory.make_ipv6_address(),
         "%s://[%s]:%d/%s" %
         (self.pick_scheme(), factory.make_ipv6_address(),
          random.randint(0, 65535), factory.make_name()),
         factory.make_hostname(),
         factory.make_url(scheme=self.pick_scheme()),
     ])
     form = ParametersForm(data={'url': inputs},
                           script=script,
                           node=factory.make_Node())
     self.assertTrue(form.is_valid(), form.errors)
     self.assertDictEqual(
         {'url': {
             'type': 'url',
             'allow_list': True,
             'value': inputs
         }}, form.cleaned_data['input'][0])
Esempio n. 2
0
 def test_input_url_list_requires_allow_list(self):
     script = factory.make_Script(parameters={"url": {"type": "url"}})
     inputs = ",".join([
         factory.make_ipv4_address(),
         "%s://%s:%d/%s" % (
             self.pick_scheme(),
             factory.make_ipv4_address(),
             random.randint(0, 65535),
             factory.make_name(),
         ),
         factory.make_ipv6_address(),
         "%s://[%s]:%d/%s" % (
             self.pick_scheme(),
             factory.make_ipv6_address(),
             random.randint(0, 65535),
             factory.make_name(),
         ),
         factory.make_hostname(),
         factory.make_url(scheme=self.pick_scheme()),
     ])
     form = ParametersForm(data={"url": inputs},
                           script=script,
                           node=factory.make_Node())
     self.assertFalse(form.is_valid())
     self.assertDictEqual({"url": ["Invalid URL"]}, form.errors)
Esempio n. 3
0
 def test__input_url_allows_list(self):
     script = factory.make_Script(
         parameters={"url": {"type": "url", "allow_list": True}}
     )
     inputs = ",".join(
         [
             factory.make_ipv4_address(),
             "%s://%s:%d/%s"
             % (
                 self.pick_scheme(),
                 factory.make_ipv4_address(),
                 random.randint(0, 65535),
                 factory.make_name(),
             ),
             factory.make_ipv6_address(),
             "%s://[%s]:%d/%s"
             % (
                 self.pick_scheme(),
                 factory.make_ipv6_address(),
                 random.randint(0, 65535),
                 factory.make_name(),
             ),
             factory.make_hostname(),
             factory.make_url(scheme=self.pick_scheme()),
         ]
     )
     form = ParametersForm(
         data={"url": inputs}, script=script, node=factory.make_Node()
     )
     self.assertTrue(form.is_valid(), form.errors)
     self.assertDictEqual(
         {"url": {"type": "url", "allow_list": True, "value": inputs}},
         form.cleaned_data["input"][0],
     )
Esempio n. 4
0
 def test__gets_client_ipv6_for_REMOTE_ADDR(self):
     ip_address = factory.make_ipv6_address()
     request = HttpRequest()
     request.META = {
         'REMOTE_ADDR': ip_address
         }
     self.assertEquals(ip_address, get_client_ip(request))
Esempio n. 5
0
 def test__ignores_IPv4_if_ipv4_not_set(self):
     v4_ip = factory.make_ipv4_address()
     v6_ip = factory.make_ipv6_address()
     self.patch_resolve_hostname([v4_ip, v6_ip])
     self.patch_get_maas_facing_server_host()
     self.assertEqual(str(v6_ip),
                      get_maas_facing_server_address(ipv4=False, ipv6=True))
Esempio n. 6
0
 def test__falls_back_on_IPv6_if_ipv4_set_but_no_IPv4_address_found(self):
     v6_ip = factory.make_ipv6_address()
     self.patch_resolve_hostname([v6_ip])
     self.patch_get_maas_facing_server_host()
     self.assertEqual([IPAddress(v6_ip)],
                      get_maas_facing_server_addresses(ipv4=True,
                                                       ipv6=True))
Esempio n. 7
0
 def test__gets_client_ipv6_for_HTTP_X_FORWARDED_FOR(self):
     ip_address = factory.make_ipv6_address()
     request = HttpRequest()
     request.META = {
         'HTTP_X_FORWARDED_FOR': ip_address
         }
     self.assertEquals(ip_address, get_client_ip(request))
Esempio n. 8
0
 def test__prefers_global_IPv6_over_link_local_IPv6(self):
     global_ipv6 = factory.make_ipv6_address()
     local_ipv6 = [
         'fe80::%d:9876:5432:10' % randint(0, 9999) for _ in range(5)
     ]
     self.patch_resolve_hostname([global_ipv6] + local_ipv6)
     self.patch_get_maas_facing_server_host()
     self.assertEqual(str(global_ipv6), get_maas_facing_server_address())
Esempio n. 9
0
 def test__does_not_return_link_local_addresses(self):
     global_ipv6 = factory.make_ipv6_address()
     local_ipv6 = [
         'fe80::%d:9876:5432:10' % randint(0, 9999) for _ in range(5)
     ]
     self.patch_resolve_hostname([global_ipv6] + local_ipv6)
     self.patch_get_maas_facing_server_host()
     self.assertEqual([IPAddress(global_ipv6)],
                      get_maas_facing_server_addresses())
Esempio n. 10
0
 def test__returns_v4_and_v6_addresses(self):
     # If a server has mixed v4 and v6 addresses,
     # get_maas_facing_server_addresses() will return both.
     v4_ip = factory.make_ipv4_address()
     v6_ip = factory.make_ipv6_address()
     self.patch_resolve_hostname([v4_ip, v6_ip])
     self.patch_get_maas_facing_server_host()
     self.assertItemsEqual(
         [IPAddress(v4_ip), IPAddress(v6_ip)],
         get_maas_facing_server_addresses(ipv4=True, ipv6=True))
Esempio n. 11
0
 def test__returns_link_local_addresses_if_asked(self):
     global_ipv6 = factory.make_ipv6_address()
     local_ipv6 = [
         'fe80::%d:9876:5432:10' % randint(0, 9999) for _ in range(5)
     ]
     self.patch_resolve_hostname([global_ipv6] + local_ipv6)
     self.patch_get_maas_facing_server_host()
     self.assertItemsEqual(
         [IPAddress(ip) for ip in local_ipv6] + [IPAddress(global_ipv6)],
         get_maas_facing_server_addresses(link_local=True))
Esempio n. 12
0
 def test__prefers_IPv4_if_ipv4_set(self):
     # If a server has mixed v4 and v6 addresses,
     # get_maas_facing_server_address() will return a v4 address
     # rather than a v6 one.
     v4_ip = factory.make_ipv4_address()
     v6_ip = factory.make_ipv6_address()
     self.patch_resolve_hostname([v4_ip, v6_ip])
     self.patch_get_maas_facing_server_host()
     self.assertEqual(str(v4_ip),
                      get_maas_facing_server_address(ipv4=True, ipv6=True))
Esempio n. 13
0
 def test_calls_get_time_configuration(self):
     example_response = {
         "servers": [
             factory.make_ipv4_address(),
             factory.make_ipv6_address(),
             factory.make_hostname(),
         ],
         "peers": [
             factory.make_ipv4_address(),
             factory.make_ipv6_address(),
         ],
     }
     deferToDatabase = self.patch(regionservice, 'deferToDatabase')
     deferToDatabase.return_value = succeed(example_response)
     system_id = factory.make_name("id")
     response = yield call_responder(Region(), GetTimeConfiguration,
                                     {'system_id': system_id})
     self.assertThat(response, Equals(example_response))
     self.assertThat(deferToDatabase,
                     MockCalledOnceWith(get_time_configuration, system_id))
Esempio n. 14
0
 def test__input_url_allows_ipv6(self):
     script = factory.make_Script(parameters={"url": {"type": "url"}})
     input = factory.make_ipv6_address()
     form = ParametersForm(
         data={"url": input}, script=script, node=factory.make_Node()
     )
     self.assertTrue(form.is_valid(), form.errors)
     self.assertDictEqual(
         {"url": {"type": "url", "value": input}},
         form.cleaned_data["input"][0],
     )
Esempio n. 15
0
 def test__input_url_allows_ipv6(self):
     script = factory.make_Script(parameters={'url': {
         'type': 'url',
     }})
     input = factory.make_ipv6_address()
     form = ParametersForm(
         data={'url': input},
         script=script, node=factory.make_Node())
     self.assertTrue(form.is_valid(), form.errors)
     self.assertDictEqual(
         {'url': {'type': 'url', 'value': input}},
         form.cleaned_data['input'][0])
Esempio n. 16
0
 def test_external_only_yields_all_ntp_servers_when_defined(self):
     Config.objects.set_config("ntp_external_only", True)
     ntp_hosts = factory.make_hostname(), factory.make_hostname()
     ntp_addrs = factory.make_ipv4_address(), factory.make_ipv6_address()
     ntp_servers = ntp_hosts + ntp_addrs
     Config.objects.set_config("ntp_servers", " ".join(ntp_servers))
     configuration = generate_ntp_configuration(node=factory.make_Node())
     self.assertThat(dict(configuration), Equals({
         "ntp": {
             "servers": sorted(ntp_addrs, key=IPAddress),
             "pools": sorted(ntp_hosts),
         },
     }))
Esempio n. 17
0
 def test_get_bmc_ipv6_static(self):
     ip = factory.make_ipv6_address()
     mock_bmc_get = self.patch(self.ipmi, "_bmc_get")
     mock_bmc_get.side_effect = (
         ("Section Lan_Conf\n"
          "\t## Give valid IP address\n"
          "\tIP_Address              0.0.0.0\n"
          "EndSection\n"),
         ("Section Lan6_Conf\n"
          "\t## Give valid IPv6 address\n"
          "\tIP_Address              %s\n"
          "EndSection\n" % ip),
     )
     self.assertEqual(f"[{ip}]", self.ipmi._get_bmc_ip())
Esempio n. 18
0
 def test_calls_get_trusted_networks(self):
     example_networks = [
         factory.make_ipv4_address(),
         factory.make_ipv6_address(),
     ]
     deferToDatabase = self.patch(regionservice, 'deferToDatabase')
     deferToDatabase.return_value = succeed(example_networks)
     system_id = factory.make_name("id")
     response = yield call_responder(Region(), GetDNSConfiguration,
                                     {'system_id': system_id})
     self.assertThat(response,
                     Equals({
                         'trusted_networks': example_networks,
                     }))
     self.assertThat(deferToDatabase,
                     MockCalledOnceWith(get_trusted_networks))
Esempio n. 19
0
 def test__STATIC_not_allowed_if_ip_address_not_in_subnet(self):
     interface = factory.make_Interface(INTERFACE_TYPE.PHYSICAL)
     network = factory.make_ipv4_network()
     subnet = factory.make_Subnet(vlan=interface.vlan,
                                  cidr=str(network.cidr))
     ip_not_in_subnet = factory.make_ipv6_address()
     form = InterfaceLinkForm(instance=interface,
                              data={
                                  "mode": INTERFACE_LINK_TYPE.STATIC,
                                  "subnet": subnet.id,
                                  "ip_address": ip_not_in_subnet,
                              })
     self.assertFalse(form.is_valid(), form.errors)
     self.assertEqual(
         {
             "ip_address":
             ["IP address is not in the given subnet '%s'." % subnet]
         }, form.errors)
Esempio n. 20
0
 def test_get_bmc_ipv6_dynamic(self):
     ip = factory.make_ipv6_address()
     mock_bmc_get = self.patch(self.ipmi, "_bmc_get")
     mock_bmc_get.side_effect = (
         ("Section Lan_Conf\n"
          "\t## Give valid IP address\n"
          "\tIPv6_Address              0.0.0.0\n"
          "EndSection\n"),
         ("Section Lan6_Conf\n"
          "\t## Give valid IPv6 address\n"
          "\tIP_Address              fe80::216:ffe3:f9eb:1f58\n"
          "EndSection\n"),
         ("Section Lan6_Conf\n"
          "\t## READ-ONLY: IPv6 dynamic address\n"
          "\t## IPv6_Dynamic_Addresses        %s\n"
          "EndSection\n" % ip),
     )
     self.assertEqual(f"[{ip}]", self.ipmi._get_bmc_ip())
Esempio n. 21
0
 def test_input_url_allows_ipv6_url(self):
     script = factory.make_Script(parameters={"url": {"type": "url"}})
     input = "%s://[%s]:%d/%s" % (
         self.pick_scheme(),
         factory.make_ipv6_address(),
         random.randint(0, 65535),
         factory.make_name(),
     )
     form = ParametersForm(data={"url": input},
                           script=script,
                           node=factory.make_Node())
     self.assertTrue(form.is_valid(), form.errors)
     self.assertDictEqual(
         {"url": {
             "type": "url",
             "value": input
         }},
         form.cleaned_data["input"][0],
     )
Esempio n. 22
0
 def make_addresses(self):
     """Return a set of IP addresses, mixing IPv4 and IPv6."""
     return {
         factory.make_ipv4_address(),
         factory.make_ipv6_address(),
     }
Esempio n. 23
0
 def test_get_maas_facing_server_host_parses_IPv6_address_in_URL(self):
     ip = factory.make_ipv6_address()
     self.set_maas_url('[%s]' % ip)
     self.assertEqual(str(ip), server_address.get_maas_facing_server_host())
Esempio n. 24
0
 def test_separators_dont_conflict_with_ipv6_address(self):
     self.assertIsNone(
         re.search(SubnetListFormField.separators,
                   factory.make_ipv6_address()))