def test_init(self): """Testing method init.""" # Testing with non-existant directory directory = 'bogus' os.environ['MDL_CONFIGDIR'] = directory with self.assertRaises(SystemExit): configuration.Config() # Testing with an empty directory empty_directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = empty_directory with self.assertRaises(SystemExit): configuration.Config() # Write bad_config to file empty_config_file = ('%s/test_config.yaml') % (empty_directory) with open(empty_config_file, 'w') as f_handle: f_handle.write('') # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.log_file() # Cleanup files in temp directories _delete_files(directory)
def test_sqlalchemy_pool_size(self): """Testing method sqlalchemy_pool_size.""" # Testing sqlalchemy_pool_size with a good dictionary # good key and key_value result = self.config.sqlalchemy_pool_size() self.assertEqual(result, 10) self.assertEqual(result, self.good_dict['main']['sqlalchemy_pool_size']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing sqlalchemy_pool_size with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.sqlalchemy_pool_size() # Testing sqlalchemy_pool_size with good key and blank key_value key = 'sqlalchemy_pool_size:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.sqlalchemy_pool_size() self.assertEqual(result, 10) # Cleanup files in temp directories _delete_files(directory)
def test_db_password(self): """Testing method db_password.""" result = self.config.db_password() self.assertEqual(result, 'test_B3bFHgxQfsEy86TN') self.assertEqual(result, self.good_dict['main']['db_password']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing db_password with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_password() # Testing db_password with good key and blank key_value key = 'db_password:'******'' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_password() # Cleanup files in temp directories _delete_files(directory)
def test_ingest_cache_directory(self): """Testing method ingest_cache_directory.""" # Testing ingest_cache_directory with temp directory # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing ingest_cache_directory with blank key_value(filepath) key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.ingest_cache_directory() # Cleanup files in temp directories _delete_files(directory)
def __init__(self): """Function for intializing the class. Args: None Returns: None """ # Initialize key variables self.reserved = 'UNASSIGNED_SYSTEM_RESERVED' self.config = configuration.Config()
def __init__(self): """Method initializing the class.""" # Setup database variables self.url = URL self.engine = TEST_ENGINE # Get configuration self.config = configuration.Config() # Validate the configuration unittest_setup.ready() # Validate the database self.validate()
def main(): """Get Flask server running. Args: None Returns: None """ # Start config = configuration.Config() bind_port = config.bind_port() listen_address = config.listen_address() API.run(debug=True, host=listen_address, threaded=True, port=bind_port)
def __init__(self): """Method initializing the class.""" # Define key variables app_name = 'mdl' levels = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } # Get the logging directory config = configuration.Config() log_file = config.log_file() config_log_level = config.log_level() # Set logging level if config_log_level in levels: log_level = levels[config_log_level] else: log_level = levels['debug'] # create logger with app_name self.logger_file = logging.getLogger(('%s_file') % (app_name)) self.logger_stdout = logging.getLogger(('%s_console') % (app_name)) # Set logging levels to file and stdout self.logger_stdout.setLevel(log_level) self.logger_file.setLevel(log_level) # create file handler which logs even debug messages file_handler = logging.FileHandler(log_file) file_handler.setLevel(log_level) # create console handler with a higher log level stdout_handler = logging.StreamHandler() stdout_handler.setLevel(log_level) # create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) stdout_handler.setFormatter(formatter) # add the handlers to the logger self.logger_file.addHandler(file_handler) self.logger_stdout.addHandler(stdout_handler)
def main(): """Process agent data. Args: None Returns: None """ # Initialize key variables use_mysql = True global POOL global URL global TEST_ENGINE # Get configuration config = configuration.Config() # Define SQLAlchemy parameters from configuration pool_size = config.sqlalchemy_pool_size() max_overflow = config.sqlalchemy_max_overflow() # Create DB connection pool if use_mysql is True: URL = ('mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4') % ( config.db_username(), config.db_password(), config.db_hostname(), config.db_name()) # Add MySQL to the pool db_engine = create_engine(URL, echo=False, encoding='utf8', max_overflow=max_overflow, pool_size=pool_size, pool_recycle=600) # Fix for multiprocessing _add_engine_pidguard(db_engine) POOL = sessionmaker(autoflush=True, autocommit=False, bind=db_engine) else: POOL = None # Populate the test engine if this is a test database if config.db_name().startswith('test_') is True: TEST_ENGINE = db_engine
def normalized_timestamp(timestamp=None): """Normalize timestamp to a multiple of 'interval' seconds. Args: timestamp: epoch timestamp in seconds Returns: value: Normalized value """ # Initialize key variables config = configuration.Config() interval = config.interval() # Process data if timestamp is None: value = (int(datetime.utcnow().timestamp()) // interval) * interval else: value = (int(timestamp) // interval) * interval # Return return value
def validate_timestamp(timestamp): """Validate timestamp to be a multiple of 'interval' seconds. Args: timestamp: epoch timestamp in seconds Returns: valid: True if valid """ # Initialize key variables valid = False config = configuration.Config() interval = config.interval() # Process data test = (int(timestamp) // interval) * interval if test == timestamp: valid = True # Return return valid
"""Initialize the API module.""" # Import PIP3 libraries from flask import Flask # Import configuration. This has to be done before all other infoset imports. from mdl.utils import configuration CONFIG = configuration.Config() # Setup memcache. Required for all API imports from mdl.api import cache CACHE = cache.Cache(CONFIG) # Do remaining mdl importations from mdl.api.post import POST from mdl.api.version import VERSION from mdl.api.get.coordinates import COORDINATES # Define the global URL prefix API_PREFIX = '/{}/mobile'.format(CONFIG.mdl_server_uri()) API = Flask(__name__) API.register_blueprint(POST, url_prefix=API_PREFIX) API.register_blueprint(VERSION, url_prefix=API_PREFIX) API.register_blueprint(COORDINATES, url_prefix=API_PREFIX)
class TestConfiguration(unittest.TestCase): """Checks all functions and methods.""" ######################################################################### # General object setup ######################################################################### log_directory = tempfile.mkdtemp() cache_directory = tempfile.mkdtemp() good_config = ("""\ main: log_directory: %s log_level: debug ingest_cache_directory: %s ingest_pool_size: 20 bind_port: 3000 interval: 300 sqlalchemy_pool_size: 10 sqlalchemy_max_overflow: 10 memcached_hostname: localhost memcached_port: 22122 db_hostname: localhost db_username: test_mdl db_password: test_B3bFHgxQfsEy86TN db_name: test_mdl """) % (log_directory, cache_directory) # Convert good_config to dictionary good_dict = yaml.load(bytes(good_config, 'utf-8')) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Write good_config to file with open(config_file, 'w') as f_handle: yaml.dump(good_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() @classmethod def tearDownClass(cls): """Post test cleanup.""" os.rmdir(cls.log_directory) os.rmdir(cls.cache_directory) os.remove(cls.config_file) os.rmdir(cls.directory) def test_init(self): """Testing method init.""" # Testing with non-existant directory directory = 'bogus' os.environ['MDL_CONFIGDIR'] = directory with self.assertRaises(SystemExit): configuration.Config() # Testing with an empty directory empty_directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = empty_directory with self.assertRaises(SystemExit): configuration.Config() # Write bad_config to file empty_config_file = ('%s/test_config.yaml') % (empty_directory) with open(empty_config_file, 'w') as f_handle: f_handle.write('') # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.log_file() # Cleanup files in temp directories _delete_files(directory) def test_log_file(self): """Testing method log_file.""" # Test the log_file with a good_dict # good key and key_value result = self.config.log_file() self.assertEqual(result, ('%s/mdl.log') % (self.log_directory)) def test_web_log_file(self): """Testing method web_log_file .""" # Testing web_log_file with a good dictionary. result = self.config.web_log_file() self.assertEqual(result, ('%s/api-web.log') % (self.log_directory)) def test_log_level(self): """Testing method log_level.""" # Tesing with a good_dictionary # good key and good key_value result = self.config.log_level() self.assertEqual(result, 'debug') self.assertEqual(result, self.good_dict['main']['log_level']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing log_level with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.log_level() # Testing log_level with good key and blank key_value key = 'log_level:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.log_level() # Cleanup files in temp directories _delete_files(directory) def test_log_directory(self): """Testing method log_directory.""" # Testing log_directory with temp directory # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing log_directory with blank key_value(filepath) key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.log_directory() # Cleanup files in temp directories _delete_files(directory) def test_ingest_cache_directory(self): """Testing method ingest_cache_directory.""" # Testing ingest_cache_directory with temp directory # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing ingest_cache_directory with blank key_value(filepath) key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.ingest_cache_directory() # Cleanup files in temp directories _delete_files(directory) def test_ingest_pool_size(self): """Testing method ingest_pool_size.""" # Testing ingest_pool_size with good_dict # good key and key_value result = self.config.ingest_pool_size() self.assertEqual(result, 20) self.assertEqual(result, self.good_dict['main']['ingest_pool_size']) def test_bind_port(self): """Testing method bind_port.""" # Testing bind_port with good_dictionary # good key and key_value result = self.config.bind_port() self.assertEqual(result, 3000) self.assertEqual(result, self.good_dict['main']['bind_port']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing bind_port with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.bind_port() # Testing bind_port with good key and blank key_value key = 'bind_port:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.bind_port() self.assertEqual(result, 6000) # Cleanup files in temp directories _delete_files(directory) def test_interval(self): """Testing method interval.""" # Testing interval with good_dictionary # good key value and key_value result = self.config.interval() self.assertEqual(result, 300) self.assertEqual(result, self.good_dict['main']['interval']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing interval with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.interval() # Testing interval with blank key_value key = 'interval:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.interval() self.assertEqual(result, 300) # Cleanup files in temp directories _delete_files(directory) def test_sqlalchemy_pool_size(self): """Testing method sqlalchemy_pool_size.""" # Testing sqlalchemy_pool_size with a good dictionary # good key and key_value result = self.config.sqlalchemy_pool_size() self.assertEqual(result, 10) self.assertEqual(result, self.good_dict['main']['sqlalchemy_pool_size']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing sqlalchemy_pool_size with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.sqlalchemy_pool_size() # Testing sqlalchemy_pool_size with good key and blank key_value key = 'sqlalchemy_pool_size:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.sqlalchemy_pool_size() self.assertEqual(result, 10) # Cleanup files in temp directories _delete_files(directory) def test_sqlalchemy_max_overflow(self): """Testing method sqlalchemy_max_overflow.""" result = self.config.sqlalchemy_max_overflow() self.assertEqual(result, 10) self.assertEqual(result, self.good_dict['main']['sqlalchemy_max_overflow']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing sqlalchemy_max_overflow with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.sqlalchemy_max_overflow() # Testing sqlalchemy_max_overflow with good key and blank key_value key = 'sqlalchemy_max_overflow:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.sqlalchemy_max_overflow() self.assertEqual(result, 10) # Cleanup files in temp directories _delete_files(directory) def test_memcached_port(self): """Testing method memcached_port.""" # Testing memcached_port with good_dictionary # good key and key_value result = self.config.memcached_port() self.assertEqual(result, 22122) self.assertEqual(result, self.good_dict['main']['memcached_port']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing memcached_port with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.memcached_port() # Testing memcached_port with good key and blank key_value key = 'memcached_port:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() result = config.memcached_port() self.assertEqual(result, 11211) # Cleanup files in temp directories _delete_files(directory) def test_memcached_hostname(self): """Testing method memcached_hostname.""" result = self.config.memcached_hostname() self.assertEqual(result, 'localhost') self.assertEqual(result, self.good_dict['main']['memcached_hostname']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing memcached_hostname with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.memcached_hostname() # Testing memcached_hostname with good key and blank key_value key = 'memcached_hostname:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object defaults to 'localhost' config = configuration.Config() result = config.memcached_hostname() self.assertEqual(result, 'localhost') # Cleanup files in temp directories _delete_files(directory) def test_db_hostname(self): """Testing method db_hostname.""" result = self.config.db_hostname() self.assertEqual(result, 'localhost') self.assertEqual(result, self.good_dict['main']['db_hostname']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing db_hostname with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_hostname() # Testing db_hostname with good key and blank key_value key = 'db_hostname:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_hostname() # Cleanup files in temp directories _delete_files(directory) def test_db_username(self): """Testing method db_username.""" result = self.config.db_username() self.assertEqual(result, 'test_mdl') self.assertEqual(result, self.good_dict['main']['db_username']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing db_username with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_username() # Testing db_username with good key and blank key_value key = 'db_username:'******'' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_username() # Cleanup files in temp directories _delete_files(directory) def test_db_password(self): """Testing method db_password.""" result = self.config.db_password() self.assertEqual(result, 'test_B3bFHgxQfsEy86TN') self.assertEqual(result, self.good_dict['main']['db_password']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing db_password with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_password() # Testing db_password with good key and blank key_value key = 'db_password:'******'' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_password() # Cleanup files in temp directories _delete_files(directory) def test_db_name(self): """Testing method db_name.""" result = self.config.db_name() self.assertEqual(result, 'test_mdl') self.assertEqual(result, self.good_dict['main']['db_name']) # Set the environmental variable for the configuration directory directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory config_file = ('%s/test_config.yaml') % (directory) # Testing db_name with blank key and blank key_value key = '' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_name() # Testing db_name with good key and blank key_value key = 'db_name:' key_value = '' bad_config = ("""\ main: %s %s """) % (key, key_value) bad_dict = yaml.load(bytes(bad_config, 'utf-8')) # Write bad_config to file with open(config_file, 'w') as f_handle: yaml.dump(bad_dict, f_handle, default_flow_style=True) # Create configuration object config = configuration.Config() with self.assertRaises(SystemExit): config.db_name() # Cleanup files in temp directories _delete_files(directory)