Remove dependency on local hostname
The json_plugin tests read the localhost name in a different way to how the hostname is fetched in the Monasca Agent check. This can cause these tests to fail since the hostnames may differ. For example, only one may contain .home as a suffix. In this change we avoid reading the hostname from the system running the tests. Story: 2007743 Task: 39921 Change-Id: Ifddba6aa9350f722a741a28a152ed9bc3e0b7da6
This commit is contained in:
parent
7c087801c9
commit
5af9c5a485
|
@ -15,6 +15,7 @@ import fcntl
|
||||||
import json
|
import json
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
from socket import gethostname
|
from socket import gethostname
|
||||||
|
from unittest import mock
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -25,9 +26,6 @@ from monasca_agent.collector.checks_d import json_plugin
|
||||||
import monasca_agent.common.config
|
import monasca_agent.common.config
|
||||||
|
|
||||||
|
|
||||||
HOSTNAME = gethostname()
|
|
||||||
|
|
||||||
|
|
||||||
def _create_agent_conf():
|
def _create_agent_conf():
|
||||||
# create a temp conf file
|
# create a temp conf file
|
||||||
tempdir = tempfile.mkdtemp()
|
tempdir = tempfile.mkdtemp()
|
||||||
|
@ -44,7 +42,7 @@ def _create_agent_conf():
|
||||||
check_freq: 60
|
check_freq: 60
|
||||||
dimensions: {{}}
|
dimensions: {{}}
|
||||||
hostname: {hostname}
|
hostname: {hostname}
|
||||||
""".format(hostname=HOSTNAME), incoming="utf-8")
|
""".format(hostname='testhost'), incoming="utf-8")
|
||||||
)
|
)
|
||||||
|
|
||||||
config_obj = monasca_agent.common.config.Config(conf_file)
|
config_obj = monasca_agent.common.config.Config(conf_file)
|
||||||
|
@ -72,7 +70,9 @@ class MockJsonPlugin(json_plugin.JsonPlugin):
|
||||||
)
|
)
|
||||||
self._metrics = []
|
self._metrics = []
|
||||||
|
|
||||||
def check(self, instance):
|
@mock.patch("monasca_agent.common.util.get_hostname")
|
||||||
|
def check(self, instance, mock_host):
|
||||||
|
mock_host.return_value = "testhost"
|
||||||
self._metrics = []
|
self._metrics = []
|
||||||
return super(MockJsonPlugin, self).check(instance)
|
return super(MockJsonPlugin, self).check(instance)
|
||||||
|
|
||||||
|
@ -134,10 +134,10 @@ def make_expected(metrics, file_name, now, ts_override=None):
|
||||||
for metric in list(metrics):
|
for metric in list(metrics):
|
||||||
if ts_override:
|
if ts_override:
|
||||||
metric['timestamp'] = ts_override
|
metric['timestamp'] = ts_override
|
||||||
metric['dimensions'].update({'hostname': HOSTNAME})
|
metric['dimensions'].update({'hostname': 'testhost'})
|
||||||
expected.append(metric)
|
expected.append(metric)
|
||||||
json_plugin_status = {'metric': 'monasca.json_plugin.status', 'value': 0,
|
json_plugin_status = {'metric': 'monasca.json_plugin.status', 'value': 0,
|
||||||
'dimensions': {'hostname': HOSTNAME},
|
'dimensions': {'hostname': 'testhost'},
|
||||||
'timestamp': now}
|
'timestamp': now}
|
||||||
expected.append(json_plugin_status)
|
expected.append(json_plugin_status)
|
||||||
return expected
|
return expected
|
||||||
|
@ -159,7 +159,7 @@ class JsonPluginCheckTest(unittest.TestCase):
|
||||||
self.assertEqual([], self.json_plugin.metrics_files)
|
self.assertEqual([], self.json_plugin.metrics_files)
|
||||||
expected = [
|
expected = [
|
||||||
{'metric': 'monasca.json_plugin.status', 'value': 0,
|
{'metric': 'monasca.json_plugin.status', 'value': 0,
|
||||||
'dimensions': {'hostname': HOSTNAME}}]
|
'dimensions': {'hostname': 'testhost'}}]
|
||||||
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
||||||
self.assertEqual('', differs, msg=differs)
|
self.assertEqual('', differs, msg=differs)
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ class JsonPluginCheckTest(unittest.TestCase):
|
||||||
|
|
||||||
expected = [
|
expected = [
|
||||||
{'metric': 'monasca.json_plugin.status', 'value': 0,
|
{'metric': 'monasca.json_plugin.status', 'value': 0,
|
||||||
'dimensions': {'hostname': HOSTNAME}}
|
'dimensions': {'hostname': 'testhost'}}
|
||||||
]
|
]
|
||||||
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
||||||
self.assertEqual('', differs, msg=differs)
|
self.assertEqual('', differs, msg=differs)
|
||||||
|
@ -199,7 +199,7 @@ class JsonPluginCheckTest(unittest.TestCase):
|
||||||
for now in [1000, 2000]:
|
for now in [1000, 2000]:
|
||||||
fake_now = now
|
fake_now = now
|
||||||
expected = [{'metric': 'monasca.json_plugin.status', 'value': 1,
|
expected = [{'metric': 'monasca.json_plugin.status', 'value': 1,
|
||||||
'dimensions': {'hostname': HOSTNAME},
|
'dimensions': {'hostname': 'testhost'},
|
||||||
'value_meta': {'msg': '%s: %s' % (file1, errmsg)}}]
|
'value_meta': {'msg': '%s: %s' % (file1, errmsg)}}]
|
||||||
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
differs = metricsDiffer(expected, self.json_plugin._metrics)
|
||||||
self.assertEqual('', differs, msg=differs)
|
self.assertEqual('', differs, msg=differs)
|
||||||
|
@ -280,7 +280,7 @@ class JsonPluginCheckTest(unittest.TestCase):
|
||||||
now = 2000
|
now = 2000
|
||||||
fake_now = now
|
fake_now = now
|
||||||
expected = [{'metric': 'monasca.json_plugin.status', 'value': 1,
|
expected = [{'metric': 'monasca.json_plugin.status', 'value': 1,
|
||||||
'dimensions': {'hostname': HOSTNAME},
|
'dimensions': {'hostname': 'testhost'},
|
||||||
'value_meta': {
|
'value_meta': {
|
||||||
'msg': '%s: Metrics are older than 500 seconds;'
|
'msg': '%s: Metrics are older than 500 seconds;'
|
||||||
' file not updating?' % file1}}]
|
' file not updating?' % file1}}]
|
||||||
|
@ -321,7 +321,7 @@ class JsonPluginCheckTest(unittest.TestCase):
|
||||||
# We don't get the metrics from the file again -- just the plugin
|
# We don't get the metrics from the file again -- just the plugin
|
||||||
# status metric
|
# status metric
|
||||||
expected = [{'metric': 'monasca.json_plugin.status', 'value': 0,
|
expected = [{'metric': 'monasca.json_plugin.status', 'value': 0,
|
||||||
'dimensions': {'hostname': HOSTNAME},
|
'dimensions': {'hostname': 'testhost'},
|
||||||
'timestamp': now}]
|
'timestamp': now}]
|
||||||
self.json_plugin.check({'dimensions': {},
|
self.json_plugin.check({'dimensions': {},
|
||||||
'metrics_file': file1})
|
'metrics_file': file1})
|
||||||
|
|
Loading…
Reference in New Issue