def test_discover_networks_represents_interface(self): eth = factory.make_name('eth') interface = make_interface() self.patch_netifaces({eth: interface}) self.assertEqual([{ 'interface': eth, 'ip': interface[AF_INET][0]['addr'], 'subnet_mask': interface[AF_INET][0]['netmask'], }], network.discover_networks())
def test_discover_networks_returns_suitable_interfaces(self): eth = factory.make_name('eth') self.patch_netifaces({ eth: make_interface(), 'lo': make_interface(make_loopback()), factory.make_name('dummy'): make_interface({}), }) self.assertEqual( [eth], [ interface['interface'] for interface in network.discover_networks()])
def register(server_url): """Request Rabbit connection details from the domain controller. Offers this machine to the region controller as a potential cluster controller. :param server_url: URL to the region controller's MAAS API. :return: A dict of connection details if this cluster controller has been accepted, or `None` if there is no definite response yet. If there is no definite response, retry this call later. :raise ClusterControllerRejected: if this system has been rejected as a cluster controller. """ known_responses = {httplib.OK, httplib.FORBIDDEN, httplib.ACCEPTED} interfaces = json.dumps(discover_networks()) client = make_anonymous_api_client(server_url) cluster_uuid = get_cluster_uuid() try: response = client.post('api/1.0/nodegroups/', 'register', interfaces=interfaces, uuid=cluster_uuid) except HTTPError as e: status_code = e.code if e.code not in known_responses: log_error(e) # Unknown error. Keep trying. return None except URLError as e: log_error(e) # Unknown error. Keep trying. return None else: status_code = response.getcode() if status_code == httplib.OK: # Our application has been approved. Proceed. return json.loads(response.read()) elif status_code == httplib.ACCEPTED: # Our application is still waiting for approval. Keep trying. return None elif status_code == httplib.FORBIDDEN: # Our application has been rejected. Give up. raise ClusterControllerRejected( "This system has been rejected as a cluster controller.") else: raise AssertionError("Unexpected return code: %r" % status_code)
def register(server_url): """Request Rabbit connection details from the domain controller. Offers this machine to the region controller as a potential cluster controller. :param server_url: URL to the region controller's MAAS API. :return: A dict of connection details if this cluster controller has been accepted, or `None` if there is no definite response yet. If there is no definite response, retry this call later. :raise ClusterControllerRejected: if this system has been rejected as a cluster controller. """ known_responses = {httplib.OK, httplib.FORBIDDEN, httplib.ACCEPTED} interfaces = json.dumps(discover_networks()) client = make_anonymous_api_client(server_url) cluster_uuid = get_cluster_uuid() try: response = client.post( 'api/1.0/nodegroups/', 'register', interfaces=interfaces, uuid=cluster_uuid) except HTTPError as e: status_code = e.code if e.code not in known_responses: log_error(e) # Unknown error. Keep trying. return None except URLError as e: log_error(e) # Unknown error. Keep trying. return None else: status_code = response.getcode() if status_code == httplib.OK: # Our application has been approved. Proceed. return json.loads(response.read()) elif status_code == httplib.ACCEPTED: # Our application is still waiting for approval. Keep trying. return None elif status_code == httplib.FORBIDDEN: # Our application has been rejected. Give up. raise ClusterControllerRejected( "This system has been rejected as a cluster controller.") else: raise AssertionError("Unexpected return code: %r" % status_code)
def test_discover_networks_runs_in_real_life(self): interfaces = network.discover_networks() self.assertIsInstance(interfaces, list)
def test_discover_networks_ignores_loopback(self): self.patch_netifaces({'lo': make_interface(make_loopback())}) self.assertEqual([], network.discover_networks())
def test_discover_networks_ignores_interface_without_IP_address(self): self.patch_netifaces({factory.make_name('eth'): {}}) self.assertEqual([], network.discover_networks())