def main(): """Test all the mdl modules with unittests. Args: None Returns: None """ # Determine unittest directory root_dir = general.root_directory() test_dir = ('%s/mdl/test') % (root_dir) # Get list of test files test_files = os.listdir(test_dir) for filename in sorted(test_files): full_path = ('%s/%s') % (test_dir, filename) # Run the test if filename.startswith('test_'): run_script(full_path) # Print message = ('\nHooray - All Done OK!\n') print(message)
def _create_directory_entries(self, key, config): """Update the configuration with good defaults for directories. Args: key: Configuration key related to a directory. config: Configuration dictionary Returns: updated: True if we have to update a value """ # Initialize key variables updated = False dir_dict = {'log_directory': 'log'} directory = general.root_directory() # Setup the key value to a known good default if key in config['main']: # Verify whether key value is empty if config['main'][key] is not None: # Create if os.path.isdir(config['main'][key]) is False: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True else: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True else: config['main'][key] = ('%s/%s') % (directory, dir_dict[key]) updated = True # Return return (updated, config)
def test_root_directory(self): """Test function root_directory.""" # Determine root directory for mdl mdl_dir = mdl.__path__[0] components = mdl_dir.split(os.sep) # Determine root directory 2 levels above root_dir = os.sep.join(components[0:-2]) result = general.root_directory() self.assertEqual(result, root_dir)
def __init__(self): """Method for intializing the class. Args: None Returns: None """ # Initialize key variables self.root = ('%s/.status') % (general.root_directory())
def __init__(self): """Function for intializing the class. Args: None Returns: None """ # Initialize key variables username = getpass.getuser() self.root_directory = general.root_directory() self.mdl_user_exists = True self.mdl_user = None self.running_as_root = False # If running as the root user, then the mdl user needs to exist if username == 'root': self.running_as_root = True try: self.mdl_user = input( 'Please enter the username under which ' 'mdl will run: ') # Get GID and UID for user self.gid = getpwnam(self.mdl_user).pw_gid self.uid = getpwnam(self.mdl_user).pw_uid except KeyError: self.mdl_user_exists = False return # Die if user doesn't exist if self.mdl_user_exists is False: log_message = ( 'User {} not found. Please try again.' ''.format(self.mdl_user)) log.log2die_safe(1049, log_message)
def _install_pip3_packages(self): """Install PIP3 packages. Args: None Returns: None """ # Initialize key variables username = self.username # Don't attempt to install packages if running in the Travis CI # environment if 'TRAVIS' in os.environ and 'CI' in os.environ: return # Determine whether PIP3 exists print('Installing required pip3 packages') pip3 = general.search_file('pip3') if pip3 is None: log_message = ('Cannot find python "pip3". Please install.') log.log2die_safe(1052, log_message) # Install required PIP packages requirements_file = ( '%s/requirements.txt') % (general.root_directory()) if username == 'root': script_name = ( 'pip3 install --upgrade --requirement %s' '') % (requirements_file) else: script_name = ( 'pip3 install --user --upgrade --requirement %s' '') % (requirements_file) general.run_script(script_name)
def test_config_directories(self): """Test function config_directories.""" # Initialize key variables save_directory = None if 'MDL_CONFIGDIR' in os.environ: save_directory = os.environ['MDL_CONFIGDIR'] # Try with no MDL_CONFIGDIR os.environ.pop('MDL_CONFIGDIR', None) directory = '{}/etc'.format(general.root_directory()) result = general.config_directories() self.assertEqual(result, [directory]) # Test with MDL_CONFIGDIR set directory = tempfile.mkdtemp() os.environ['MDL_CONFIGDIR'] = directory result = general.config_directories() self.assertEqual(result, [directory]) # Restore state if save_directory is not None: os.environ['MDL_CONFIGDIR'] = save_directory