summaryrefslogtreecommitdiff
path: root/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestEmitter.py
blob: 56e6475df99dd71040f318d28d72ba7c9c36d405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python

'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''

import json
import urllib2

import logging
from unittest import TestCase
from only_for_platform import get_platform, PLATFORM_WINDOWS

from mock.mock import patch, MagicMock

if get_platform() != PLATFORM_WINDOWS:
  os_distro_value = ('Suse','11','Final')
else:
  os_distro_value = ('win2012serverr2','6.3','WindowsServer')

with patch("platform.linux_distribution", return_value = os_distro_value):
  from ambari_commons import OSCheck
  from application_metric_map import ApplicationMetricMap
  from config_reader import Configuration
  from emitter import Emitter
  from stop_handler import bind_signal_handlers

logger = logging.getLogger()

class TestEmitter(TestCase):

  @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
  @patch("urllib2.urlopen")
  def testJavaHomeAvailableCheck(self, url_open_mock):
    url_open_mock.return_value = MagicMock()
    url_open_mock.return_value.getcode.return_value = 200
    self.assertEqual(urllib2.urlopen(None, None).getcode(), 200)
    url_open_mock.reset_mock()

    stop_handler = bind_signal_handlers()

    config = Configuration()
    application_metric_map = ApplicationMetricMap("host","10.10.10.10")
    application_metric_map.clear()
    application_metric_map.put_metric("APP1", {"metric1":1}, 1)
    emitter = Emitter(config, application_metric_map, stop_handler)
    emitter.submit_metrics()
    
    self.assertEqual(url_open_mock.call_count, 1)
    self.assertUrlData(url_open_mock)


  @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
  @patch("urllib2.urlopen")
  def testRetryFetch(self, url_open_mock):
    stop_handler = bind_signal_handlers()

    config = Configuration()
    application_metric_map = ApplicationMetricMap("host","10.10.10.10")
    application_metric_map.clear()
    application_metric_map.put_metric("APP1", {"metric1":1}, 1)
    emitter = Emitter(config, application_metric_map, stop_handler)
    emitter.RETRY_SLEEP_INTERVAL = .001
    emitter.submit_metrics()
    
    self.assertEqual(url_open_mock.call_count, 3)
    self.assertUrlData(url_open_mock)
    

  def assertUrlData(self, url_open_mock):
    self.assertEqual(len(url_open_mock.call_args), 2)
    data = url_open_mock.call_args[0][0].data
    self.assertTrue(data is not None)
    
    metrics = json.loads(data)
    self.assertEqual(len(metrics['metrics']), 1)
    self.assertEqual(metrics['metrics'][0]['metricname'],'metric1')
    self.assertEqual(metrics['metrics'][0]['starttime'],1)
    pass