
Currently, the configurations are being loaded from a statically defined file (/etc/monasca/agent/agent.yaml). Adds CLI arg option for the configuration file (--config-file). Also added this argument to the forwarder's tornado options, in order to avoid exception due to unrecognised argument. Co-Authored-By: Craig Bryant <craig.bryant@hpe.com> Partially-Implements: blueprint add-windows-support Change-Id: I400fdb3db6562a7171ecadcb27ee63ae4eaf8006
118 lines
4.0 KiB
Python
118 lines
4.0 KiB
Python
# -*- coding: latin-1 -*-
|
|
import unittest
|
|
import mock
|
|
import os.path
|
|
import tempfile
|
|
|
|
from monasca_agent.common import config
|
|
from monasca_agent.common.config import Config
|
|
from monasca_agent.common.util import PidFile, is_valid_hostname
|
|
|
|
|
|
class TestConfig(unittest.TestCase):
|
|
|
|
def xtestWhiteSpaceConfig(self):
|
|
"""Leading whitespace confuse ConfigParser
|
|
"""
|
|
agent_config = Config.get_config(
|
|
cfg_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "badconfig.conf"))
|
|
self.assertEqual(agent_config["api_key"], "1234")
|
|
|
|
def testGoodPidFie(self):
|
|
"""Verify that the pid file succeeds and fails appropriately"""
|
|
|
|
pid_dir = tempfile.mkdtemp()
|
|
program = 'test'
|
|
|
|
expected_path = os.path.join(pid_dir, '%s.pid' % program)
|
|
pid = "666"
|
|
pid_f = open(expected_path, 'w')
|
|
pid_f.write(pid)
|
|
pid_f.close()
|
|
|
|
p = PidFile(program, pid_dir)
|
|
|
|
self.assertEqual(p.get_pid(), 666)
|
|
# clean up
|
|
self.assertEqual(p.clean(), True)
|
|
self.assertEqual(os.path.exists(expected_path), False)
|
|
|
|
def testHostname(self):
|
|
valid_hostnames = [
|
|
u'i-123445',
|
|
u'5dfsdfsdrrfsv',
|
|
u'432498234234A'
|
|
u'234234235235235235', # Couldn't find anything in the RFC saying it's not valid
|
|
u'A45fsdff045-dsflk4dfsdc.ret43tjssfd',
|
|
u'4354sfsdkfj4TEfdlv56gdgdfRET.dsf-dg',
|
|
u'r' * 255,
|
|
]
|
|
|
|
not_valid_hostnames = [
|
|
u'abc' * 150,
|
|
u'sdf4..sfsd',
|
|
u'$42sdf',
|
|
u'.sfdsfds'
|
|
u's™£™£¢ª•ªdfésdfs'
|
|
]
|
|
|
|
for hostname in valid_hostnames:
|
|
self.assertTrue(is_valid_hostname(hostname), hostname)
|
|
|
|
for hostname in not_valid_hostnames:
|
|
self.assertFalse(is_valid_hostname(hostname), hostname)
|
|
|
|
def testConfigIsSingleton(self):
|
|
# create a temp conf file
|
|
tempdir = tempfile.mkdtemp()
|
|
conf_file = os.path.join(tempdir, 'agent.yaml')
|
|
with open(conf_file, 'wb') as fd:
|
|
fd.write(
|
|
"""
|
|
Logging:
|
|
collector_log_file: /var/log/monasca/agent/collector.log
|
|
forwarder_log_file: /var/log/monasca/agent/forwarder.log
|
|
log_level: DEBUG
|
|
statsd_log_file: /var/log/monasca/agent/statsd.log
|
|
Main:
|
|
check_freq: 60
|
|
dimensions: {}
|
|
hostname: example.com
|
|
"""
|
|
)
|
|
conf_1 = Config(configFile=conf_file)
|
|
conf_2 = Config(configFile=conf_file)
|
|
conf_3 = Config()
|
|
self.assertTrue(conf_1 is conf_2)
|
|
self.assertTrue(conf_1 is conf_3)
|
|
|
|
@mock.patch.object(Config, '_read_config')
|
|
@mock.patch('monasca_agent.common.util.get_parsed_args')
|
|
def testConfigFromParsedArgs(self, mock_parsed_args, mock_read_config):
|
|
mock_options = mock.Mock()
|
|
mock_parsed_args.return_value = (mock_options, mock.sentinel.args)
|
|
conf = Config()
|
|
# object is singleton, for this case, it needs to be reloaded.
|
|
conf.__init__()
|
|
|
|
self.assertEqual(mock_options.config_file, conf._configFile)
|
|
|
|
@mock.patch.object(Config, '_read_config')
|
|
@mock.patch('monasca_agent.common.util.get_parsed_args')
|
|
@mock.patch('monasca_agent.common.config.os')
|
|
def testConfigFileFromDefault(self, mock_os, mock_parsed_args, mock_read_config):
|
|
mock_os.path.exists = mock.create_autospec(mock_os.path.exists, return_value=True)
|
|
mock_options = mock.Mock()
|
|
mock_options.config_file = None
|
|
mock_parsed_args.return_value = (mock_options, mock.sentinel.args)
|
|
conf = Config()
|
|
# object is singleton, for this case, it needs to be reloaded.
|
|
conf.__init__()
|
|
|
|
self.assertEqual(config.DEFAULT_CONFIG_FILE, conf._configFile)
|
|
mock_os.path.exists.assert_called_once_with(config.DEFAULT_CONFIG_FILE)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|