def assertQueryResultMultiTool(self, query, mock_fetch_results, query_results_expected, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query returns a list with multiple tools and that resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. query_results_expected: The expected winning tools that the resolver returns. tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = sliver_tool_fetcher.SliverToolFetcher().fetch mock_fetch.return_value = mock_fetch_results query_results_actual = self.resolver.answer_query(query) self.assertSetEqual(set(query_results_expected), set(query_results_actual)) mock_fetch.assert_called_with(tool_properties_expected)
def assertQueryResultWithRandomShuffle(self, query, mock_fetch_results, query_results_expected, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query finds a list of tool candidates to return and then randomly selects a subset of those tools as the winners. Also asserts that the resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. query_results_expected: Expected results from calling resolver.answer_query(). tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = sliver_tool_fetcher.SliverToolFetcher().fetch mock_fetch.return_value = mock_fetch_results # Mock out random behavior to allow deterministic test results with mock.patch('random.shuffle') as mock_shuffle: # Change the random shuffle to a deterministic list reverse mock_shuffle.side_effect = lambda x: x.reverse() query_results_actual = self.resolver.answer_query(query) self.assertSetEqual(set(query_results_expected), set(query_results_actual)) mock_fetch.assert_called_with(tool_properties_expected)
def testAnswerQueryWhenNoToolsMatchToolId(self): tool_id = 'non_existent_tool' query = lookup_query.LookupQuery() query.tool_id = tool_id # Simulate no matching tools sliver_tool_fetcher.SliverToolFetcher().fetch.return_value = [] # Result should be None when there are no matches. self.assertIsNone(self.resolver.answer_query(query))
def setUp(self): sliver_tool_fetcher_datastore_patch = mock.patch.object( sliver_tool_fetcher, 'SliverToolFetcherDatastore', autospec=True) self.addCleanup(sliver_tool_fetcher_datastore_patch.stop) sliver_tool_fetcher_datastore_patch.start() sliver_tool_fetcher_memcache_patch = mock.patch.object( sliver_tool_fetcher, 'SliverToolFetcherMemcache', autospec=True) self.addCleanup(sliver_tool_fetcher_memcache_patch.stop) sliver_tool_fetcher_memcache_patch.start() self.fetcher = sliver_tool_fetcher.SliverToolFetcher()
def testAnswerReturnsNoneWhenCountryIsNotSpecified(self): query = lookup_query.LookupQuery() query.tool_id = _TOOL_ID # query omits country attribute candidate_tools = (_createSliverTool(_TOOL_ID), _createSliverTool(_TOOL_ID)) mock_fetch = sliver_tool_fetcher.SliverToolFetcher().fetch mock_fetch.return_value = candidate_tools query_results = self.resolver.answer_query(query) # Result should be None when there are no matches. self.assertIsNone(query_results)
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_all_stubs() ndb.get_context().clear_cache() sliver_tool_fetcher_datastore_patch = mock.patch.object( sliver_tool_fetcher, 'SliverToolFetcherDatastore', autospec=True) self.addCleanup(sliver_tool_fetcher_datastore_patch.stop) sliver_tool_fetcher_datastore_patch.start() sliver_tool_fetcher_memcache_patch = mock.patch.object( sliver_tool_fetcher, 'SliverToolFetcherMemcache', autospec=True) self.addCleanup(sliver_tool_fetcher_memcache_patch.stop) sliver_tool_fetcher_memcache_patch.start() self.fetcher = sliver_tool_fetcher.SliverToolFetcher()
def assertQueryResultMultiToolWithRandomSample(self, query, mock_fetch_results, filtered_tool_candidates, sample_size, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query finds a list of tool candidates to return and then randomly selects a single tool as the winner. Also asserts that the resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. filtered_tool_candidates: The expected candidate tools from which the resolver will randomly pick a winner. sample_size: The number of randomly selected elements expected in the final result. tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = sliver_tool_fetcher.SliverToolFetcher().fetch mock_fetch.return_value = mock_fetch_results # Mock out random behavior to allow deterministic test results with mock.patch('random.sample') as mock_random: # Make random.sample yield the k last elements of the set mock_random.side_effect = lambda x, k: x[-k:] query_results_expected = filtered_tool_candidates[-sample_size:] query_results_actual = self.resolver.answer_query(query) self.assertSequenceEqual(query_results_expected, query_results_actual) # Make sure that the random selection was between the expected # candidate tools, after any filtering self.assertSequenceEqual(filtered_tool_candidates, mock_random.call_args[0][0]) mock_fetch.assert_called_with(tool_properties_expected)
def __init__(self, client_signature=''): self.sliver_tool_fetcher = sliver_tool_fetcher.SliverToolFetcher() self.client_signature = client_signature
def update_sliver_tools_status(self, slice_status, tool_id, family): """Updates status of sliver tools in input slice. Args: slice_status: A dict that contains the status of the slivers in the slice {key=fqdn, status:online|offline} tool_id: A string representing the fqdn that resolves to an IP address. family: Address family to update. """ sliver_tools = sliver_tool_fetcher.SliverToolFetcher().fetch( sliver_tool_fetcher.ToolProperties(tool_id=tool_id, all_slivers=True)) updated_sliver_tools = [] for sliver_tool in sliver_tools: if sliver_tool.fqdn not in slice_status: logging.info('Nagios does not know sliver %s.', sliver_tool.fqdn) continue if family == '': if sliver_tool.sliver_ipv4 == message.NO_IP_ADDRESS: if sliver_tool.status_ipv4 == message.STATUS_ONLINE: logging.warning('Setting IPv4 status of %s to offline '\ 'due to missing IP.', sliver_tool.fqdn) sliver_tool.status_ipv4 = message.STATUS_OFFLINE else: if (sliver_tool.status_ipv4 != slice_status[sliver_tool.fqdn]['status'] or sliver_tool.tool_extra != slice_status[sliver_tool.fqdn]['tool_extra']): sliver_tool.status_ipv4 = \ slice_status[sliver_tool.fqdn]['status'] sliver_tool.tool_extra = \ slice_status[sliver_tool.fqdn]['tool_extra'] elif family == '_ipv6': if sliver_tool.sliver_ipv6 == message.NO_IP_ADDRESS: if sliver_tool.status_ipv6 == message.STATUS_ONLINE: logging.warning('Setting IPv6 status for %s to offline'\ ' due to missing IP.', sliver_tool.fqdn) sliver_tool.status_ipv6 = message.STATUS_OFFLINE else: if (sliver_tool.status_ipv6 != slice_status[sliver_tool.fqdn]['status'] or sliver_tool.tool_extra != slice_status[sliver_tool.fqdn]['tool_extra']): sliver_tool.status_ipv6 = \ slice_status[sliver_tool.fqdn]['status'] sliver_tool.tool_extra = \ slice_status[sliver_tool.fqdn]['tool_extra'] else: logging.error('Unexpected address family: %s.', family) continue sliver_tool.update_request_timestamp = long(time.time()) updated_sliver_tools.append(sliver_tool) if updated_sliver_tools: try: db.put(updated_sliver_tools) except db.TransactionFailedError as e: logging.error( 'Error updating sliver statuses in datastore. Some' \ 'statuses might be outdated. %s', e) if not memcache.set(tool_id, updated_sliver_tools, namespace=constants.MEMCACHE_NAMESPACE_TOOLS): logging.error('Failed to update sliver status in memcache.')
def __init__(self): self.sliver_tool_fetcher = sliver_tool_fetcher.SliverToolFetcher()