def write(self): """Write the config to file. Args: None Returns: None """ # Initialize key variables directory = self.directories[0] # Update configuration file if required for next_directory in self.directories: # Delete all YAML files in the configuration directory general.delete_yaml_files(next_directory) # Write config back to directory filepath = ('%s/config.yaml') % (directory) with open(filepath, 'w') as outfile: yaml.dump(self.config_dict, outfile, default_flow_style=False) # Write status Update shared.print_ok('Created configuration file {}.'.format(filepath))
def run(self): """Update the configuration with good defaults. Args: None Returns: None """ # Initialize key variables valid = True updated_list = [] config = copy.deepcopy(self.config) directory = self.directories[0] # Update log_directory and ingest_cache_directory if isinstance(config, dict) is True: if 'main' in config: # Setup the log_directory to a known good default (updated, config) = self._create_directory_entries( 'log_directory', config) updated_list.append(updated) # Setup the ingest_cache_directory to a known good default (updated, config) = self._create_directory_entries( 'ingest_cache_directory', config) updated_list.append(updated) else: valid = False else: valid = False # Gracefully exit if things are not OK if valid is False: log_message = ('Configuration files found in {} is invalid' ''.format(self.directories)) log.log2die_safe(1101, log_message) # Update configuration file if required if len(updated_list) == updated_list.count(True): for next_directory in self.directories: # Delete all YAML files in the directory general.delete_yaml_files(next_directory) # Write config back to directory filepath = ('%s/config.yaml') % (directory) with open(filepath, 'w') as outfile: yaml.dump(config, outfile, default_flow_style=False) # Print status shared.print_ok('Configuration setup complete.')
def test_delete_yaml_files(self): """Test function delete_yaml_files.""" # Testing with a known invalid directory directory = self.random_string with self.assertRaises(SystemExit): general.delete_files(directory) # Creating temporary yaml and json files for testing directory = tempfile.mkdtemp() testfiles = ['test1.yaml', 'test2.yaml', 'test3.json'] for filename in testfiles: filepath = '{}/{}'.format(directory, filename) open(filepath, 'a').close() # Testing if all yaml files were created count = len([name for name in os.listdir( directory) if name.endswith('.yaml')]) self.assertEqual(count, 2) # Test if json file was created jcount = len([name for name in os.listdir( directory) if name.endswith('.json')]) self.assertEqual(jcount, 1) # Deleting all yaml files using function general.delete_yaml_files(directory) # Test if all yaml files were deleted result = len([name for name in os.listdir( directory) if name.endswith('.yaml')]) self.assertEqual(result, 0) # Test if json file was not deleted jcount = len([name for name in os.listdir( directory) if name.endswith('.json')]) self.assertEqual(jcount, 1) # Delete json file general.delete_files(directory, extension='.json') # Test if json file was deleted jcount = len([name for name in os.listdir( directory) if name.endswith('.json')]) self.assertEqual(jcount, 0) # Removing test directory os.removedirs(directory) # Test if directory has been deleted self.assertEqual(os.path.isdir(directory), False)