Esempio n. 1
0
  def test_filtered_cluster_node_stats(self, mock_get_current, mock_fetch,
                                       mock_ips_getter, mock_get_private_ip,
                                       mock_options):
    # Mock appscale_info functions for getting IPs
    mock_get_private_ip.return_value = '192.168.33.10'
    mock_ips_getter.return_value = ['192.168.33.10', '192.168.33.11']
    # Mock secret
    mock_options.secret = 'secret'
    # Read test data from json file
    raw_test_data, stats_test_data = get_stats_from_file(
      'node-stats.json', node_stats.NodeStatsSnapshot
    )
    # Mock local source
    mock_get_current.return_value = stats_test_data['192.168.33.10']
    # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data
    response = MagicMock(body=json.dumps(raw_test_data['192.168.33.11']),
                         code=200, reason='OK')
    future_response = gen.Future()
    future_response.set_result(response)
    mock_fetch.return_value = future_response
    #Prepare raw dict with include lists
    raw_include_lists = {
      'node': ['cpu', 'memory'],
      'node.cpu': ['percent', 'count'],
      'node.memory': ['available']
    }

    # Initialize cluster stats source with include lists
    cluster_stats_source = cluster_stats.ClusterNodesStatsSource()

    # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^
    # Call method under test to get stats with filtered set of fields
    include_lists = IncludeLists(raw_include_lists)
    stats, failures = yield cluster_stats_source.get_current_async(
      max_age=10, include_lists=include_lists
    )

    # ASSERTING EXPECTATIONS
    request_to_slave = mock_fetch.call_args[0][0]
    self.assertEqual(
      json.loads(request_to_slave.body),
      {
        'max_age': 10,
        'include_lists': raw_include_lists,
      })
    self.assertEqual(
      request_to_slave.url, 'http://192.168.33.11:4378/stats/local/node'
    )
    self.assertDictContainsSubset(
      request_to_slave.headers, {'Appscale-Secret': 'secret'}
    )
    self.assertEqual(failures, {})

    local_stats = stats['192.168.33.10']
    slave_stats = stats['192.168.33.11']
    self.assertIsInstance(local_stats, node_stats.NodeStatsSnapshot)
    self.assertEqual(local_stats.utc_timestamp, 1494248091.0)
    self.assertIsInstance(slave_stats, node_stats.NodeStatsSnapshot)
    self.assertEqual(slave_stats.utc_timestamp, 1494248082.0)
Esempio n. 2
0
  def test_local_failure(self, mock_get_current, mock_ips_getter,
                         mock_get_private_ip):
    # Mock appscale_info functions for getting IPs
    mock_get_private_ip.return_value = '192.168.33.10'
    mock_ips_getter.return_value = ['192.168.33.10']
    # Mock local source
    mock_get_current.side_effect = ValueError(u"Something strange \u2234")
    # Initialize cluster stats source
    cluster_stats_source = cluster_stats.ClusterNodesStatsSource()

    # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^
    # Call method under test
    stats, failures = yield cluster_stats_source.get_current_async()

    # ASSERTING EXPECTATIONS
    self.assertEqual(stats, {})
    self.assertEqual(failures, {'192.168.33.10': u"Something strange \u2234"})
Esempio n. 3
0
  def test_verbose_cluster_node_stats(self, mock_get_current, mock_fetch,
                                      mock_ips_getter, mock_get_private_ip,
                                      mock_options):
    # Mock appscale_info functions for getting IPs
    mock_get_private_ip.return_value = '192.168.33.10'
    mock_ips_getter.return_value = ['192.168.33.10', '192.168.33.11']
    # Mock secret
    mock_options.secret = 'secret'
    # Read test data from json file
    raw_test_data, stats_test_data = get_stats_from_file(
      'node-stats.json', node_stats.NodeStatsSnapshot
    )
    # Mock local source
    mock_get_current.return_value = stats_test_data['192.168.33.10']
    # Mock AsyncHTTPClient.fetch using raw stats dictionaries from test data
    response = MagicMock(body=json.dumps(raw_test_data['192.168.33.11']))
    future_response = gen.Future()
    future_response.set_result(response)
    mock_fetch.return_value = future_response

    # Initialize cluster stats source
    cluster_stats_source = cluster_stats.ClusterNodesStatsSource()

    # ^^^ ALL INPUTS ARE SPECIFIED (or mocked) ^^^
    # Call method under test
    stats = yield cluster_stats_source.get_current_async()

    # ASSERTING EXPECTATIONS
    request_to_slave = mock_fetch.call_args[0][0]
    self.assertEqual(json.loads(request_to_slave.body), {})
    self.assertEqual(
      request_to_slave.url, 'http://192.168.33.11:4378/stats/local/node'
    )
    self.assertDictContainsSubset(
      request_to_slave.headers, {'Appscale-Secret': 'secret'}
    )

    local_stats = stats['192.168.33.10']
    slave_stats = stats['192.168.33.11']
    self.assertIsInstance(local_stats, node_stats.NodeStatsSnapshot)
    self.assertEqual(local_stats.utc_timestamp, 1494248091.0)
    self.assertIsInstance(slave_stats, node_stats.NodeStatsSnapshot)
    self.assertEqual(slave_stats.utc_timestamp, 1494248082.0)