def test__calls_racks_synchronously(self): scan_all_rack_networks() self.assertThat( self.call_racks_sync_mock, MockCalledOnceWith(cluster.ScanNetworks, controllers=None, kwargs={}))
def test__calls_racks_synchronously_with_slow(self): scan_all_rack_networks(slow=True) self.assertThat( self.call_racks_sync_mock, MockCalledOnceWith(cluster.ScanNetworks, controllers=None, kwargs={'slow': True}))
def test__calls_racks_synchronously_with_threads(self): threads = random.randint(1, 99) scan_all_rack_networks(threads=threads) self.assertThat( self.call_racks_sync_mock, MockCalledOnceWith( cluster.ScanNetworks, controllers=None, kwargs={'threads': threads}))
def test_calls_racks_synchronously_with_force_ping(self): scan_all_rack_networks(ping=True) self.assertThat( self.call_racks_sync_mock, MockCalledOnceWith( cluster.ScanNetworks, controllers=None, kwargs={"force_ping": True}, ), )
def test__calls_racks_synchronously_with_cidrs(self): subnet_query = Subnet.objects.filter( staticipaddress__interface__node__in=self.started) cidrs = [ IPNetwork(cidr) for cidr in subnet_query.values_list('cidr', flat=True) ] scan_all_rack_networks(cidrs=cidrs) self.assertThat( self.call_racks_sync_mock, MockCalledOnceWith( cluster.ScanNetworks, controllers=ANY, kwargs={'cidrs': cidrs})) # Check `controllers` separately because its order may vary. controllers = self.call_racks_sync_mock.call_args[1]["controllers"] self.assertItemsEqual(self.started, controllers)
def scan(self, parameters): """Scan the subnet for connected neighbours. :return: user-friendly scan results (as defined by the `user_friendly_scan_results()` function). """ # Circular imports. from maasserver.api.discoveries import ( scan_all_rack_networks, user_friendly_scan_results, ) subnet = self.get_object(parameters) assert self.user.has_perm(NodePermission.admin, Discovery), "Permission denied." cidr = subnet.get_ipnetwork() if cidr.version != 4: raise ValueError( "Cannot scan subnet: only IPv4 subnets can be scanned.") cidrs = [cidr] if RackController.objects.filter_by_subnet_cidrs(cidrs).count() == 0: raise ValueError("Subnet must be configured on a rack controller.") rpc_results = scan_all_rack_networks(cidrs=[cidr]) maaslog.info( "User '%s' initiated a neighbour discovery scan against subnet: " "%s" % (self.user.username, cidr)) return user_friendly_scan_results(rpc_results)
def scan_all_subnets(self, cidrs): """Scans the specified list of subnet CIDRs. Updates the last_scan time if a scan was initiated. :return: human readable string indicating scan results. """ assert len(cidrs) != 0 scan_results = scan_all_rack_networks(cidrs=cidrs) # Don't increment the last_scan value if we couldn't contact any # rack controllers to initiate the scan. if len(scan_results.available) > 0: self._update_last_scan_time() return get_scan_result_string_for_humans(scan_results)
def test__populates_results_correctly(self): result = user_friendly_scan_results(scan_all_rack_networks()) self.assertThat(result, Equals( { "result": self.result, "scan_started_on": get_controller_summary(self.started), "scan_failed_on": get_controller_summary(self.failed), "scan_attempted_on": get_controller_summary(self.available), "failed_to_connect_to": get_controller_summary(self.unavailable), "rpc_call_timed_out_on": get_controller_summary(self.timed_out), "failures": get_failure_summary(self.failures), } ))
def test__results_can_be_converted_to_json_and_back(self): result = user_friendly_scan_results(scan_all_rack_networks()) json_result = json.dumps(result) self.assertThat(json.loads(json_result), Equals(result))