def test_emptying_dictionaries(self): self.cinder_config['container_skel']['cinder_volumes_container'] = {} self.write_override_env() fs.load_environment(TARGET_DIR, self.base_env) test_vol = self.base_env['container_skel']['cinder_volumes_container'] self.assertNotIn('belongs_to', test_vol)
def test_cinder_metal_override(self): vol = self.cinder_config['container_skel']['cinder_volumes_container'] vol['properties']['is_metal'] = False self.write_override_env() fs.load_environment(TARGET_DIR, self.base_env) test_vol = self.base_env['container_skel']['cinder_volumes_container'] self.assertFalse(test_vol['properties']['is_metal'])
def test_emptying_lists(self): vol = self.cinder_config['container_skel']['cinder_volumes_container'] vol['belongs_to'] = [] self.write_override_env() fs.load_environment(TARGET_DIR, self.base_env) test_vol = self.base_env['container_skel']['cinder_volumes_container'] self.assertEqual(test_vol['belongs_to'], [])
def test_adding_new_keys(self): vol = self.cinder_config['container_skel']['cinder_volumes_container'] vol['a_new_key'] = 'Added' self.write_override_env() fs.load_environment(TARGET_DIR, self.base_env) test_vol = self.base_env['container_skel']['cinder_volumes_container'] self.assertIn('a_new_key', test_vol) self.assertEqual(test_vol['a_new_key'], 'Added')
def test_deleting_elements(self): # Leave only the 'properties' dictionary attached to simulate writing # a partial override file vol = self.cinder_config['container_skel']['cinder_volumes_container'] for key in vol.keys(): if not key == 'properties': del vol[key] self.write_override_env() fs.load_environment(TARGET_DIR, self.base_env) test_vol = self.base_env['container_skel']['cinder_volumes_container'] self.assertIn('belongs_to', test_vol)
def test_duplicates(self, mock_load_config, mock_load_env): """Test that no duplicate IPs are made on any network.""" # Grab our values read from the file system just once. mock_load_config.return_value = get_config() mock_load_env.return_value = fs.load_environment(BASE_ENV_DIR, {}) mock_open = mock.mock_open() for i in range(0, 99): # tearDown is ineffective for this loop, so clean the USED_IPs # on each run inventory = None di.ip.USED_IPS = set() # Mock out the context manager being used to write files. # We don't need to hit the file system for this test. with mock.patch('__main__.open', mock_open): inventory = get_inventory() ips = collections.defaultdict(int) hostvars = inventory['_meta']['hostvars'] for host, var_dict in hostvars.items(): nets = var_dict['container_networks'] for net, vals in nets.items(): if 'address' in vals.keys(): addr = vals['address'] ips[addr] += 1 self.assertEqual(1, ips[addr], msg="IP %s duplicated." % addr)
def skel_setup(self): self.environment = fs.load_environment(TARGET_DIR, self.base_env) di.skel_setup(self.environment, self.inv) di.skel_load( self.environment.get('physical_skel'), self.inv )
def setUp(self): self.base_env = fs.load_environment(BASE_ENV_DIR, {}) # Use the cinder configuration as our sample for override testing with open(path.join(BASE_ENV_DIR, 'env.d', 'cinder.yml'), 'r') as f: self.cinder_config = yaml.safe_load(f.read()) self.override_path = path.join(TARGET_DIR, 'env.d') os.mkdir(self.override_path)
def setUp(self): self.env = fs.load_environment(BASE_ENV_DIR, {}) # Copy because we manipulate the structure in each test; # not copying would modify the global var in the target code self.inv = copy.deepcopy(di.INVENTORY_SKEL) # Since we're not running skel_setup, add necessary keys self.host_vars = self.inv['_meta']['hostvars'] # The _ensure_inventory_uptodate function depends on values inserted # by the skel_setup function di.skel_setup(self.env, self.inv)
def setUp(self): # Allow custom assertion errors. self.longMessage = True self.env = fs.load_environment(BASE_ENV_DIR, {})
def setUp(self): self.env = fs.load_environment(BASE_ENV_DIR, {})
def setUp(self): self.longMessage = True self.loaded_environment = fs.load_environment(BASE_ENV_DIR, {})
def main(config=None, check=False, debug=False, environment=None, **kwargs): """Run the main application. :param config: ``str`` Directory from which to pull configs and overrides :param check: ``bool`` Flag to enable check mode :param debug: ``bool`` Flag to enable debug logging :param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for catching Ansible's required `--list` parameter without name shadowing the `list` built-in. :param environment: ``str`` Directory containing the base env.d """ if debug: _prepare_debug_logger() try: user_defined_config = filesys.load_user_configuration(config) except filesys.MissingDataSource as ex: raise SystemExit(ex) base_env_dir = environment base_env = filesys.load_environment(base_env_dir, {}) environment = filesys.load_environment(config, base_env) # Load existing inventory file if found inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL) # Save the users container cidr as a group variable cidr_networks = user_defined_config.get('cidr_networks') if not cidr_networks: raise SystemExit('No container CIDR specified in user config') if 'container' in cidr_networks: user_cidr = cidr_networks['container'] elif 'management' in cidr_networks: user_cidr = cidr_networks['management'] else: raise SystemExit('No container or management network ' 'specified in user config.') # make sure user_defined config is self contained _check_config_settings( cidr_networks, user_defined_config, environment.get('container_skel') ) # Add the container_cidr into the all global ansible group_vars _parse_global_variables(user_cidr, inventory, user_defined_config) # Load all of the IP addresses that we know are used and set the queue ip.set_used_ips(user_defined_config, inventory) user_defined_setup(user_defined_config, inventory) skel_setup(environment, inventory) _check_group_branches( user_defined_config, environment.get('physical_skel') ) logger.debug("Loading physical skel.") skel_load( environment.get('physical_skel'), inventory ) logger.debug("Loading component skel") skel_load( environment.get('component_skel'), inventory ) container_skel_load( environment.get('container_skel'), inventory, user_defined_config ) # Look at inventory and ensure all entries have all required values. _ensure_inventory_uptodate( inventory=inventory, container_skel=environment.get('container_skel'), ) # Load the inventory json inventory_json = json.dumps( inventory, indent=4, sort_keys=True ) if check: if _check_all_conf_groups_present(user_defined_config, environment): return 'Configuration ok!' # Save a list of all hosts and their given IP addresses hostnames_ips = _collect_hostnames(inventory) filesys.write_hostnames(config, hostnames_ips) if logger.isEnabledFor(logging.DEBUG): num_hosts = len(inventory['_meta']['hostvars']) logger.debug("%d hosts found.", num_hosts) # Save new dynamic inventory filesys.save_inventory(inventory_json, inv_path) return inventory_json
def main(config=None, check=False, debug=False, environment=None, **kwargs): """Run the main application. :param config: ``str`` Directory from which to pull configs and overrides :param check: ``bool`` Flag to enable check mode :param debug: ``bool`` Flag to enable debug logging :param kwargs: ``dict`` Dictionary of arbitrary arguments; mostly for catching Ansible's required `--list` parameter without name shadowing the `list` built-in. :param environment: ``str`` Directory containing the base env.d """ if debug: _prepare_debug_logger() try: user_defined_config = filesys.load_user_configuration(config) except filesys.MissingDataSource as ex: raise SystemExit(ex) base_env_dir = environment base_env = filesys.load_environment(base_env_dir, {}) environment = filesys.load_environment(config, base_env) # Load existing inventory file if found inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL) # Save the users container cidr as a group variable cidr_networks = user_defined_config.get('cidr_networks') if not cidr_networks: raise SystemExit('No container CIDR specified in user config') if 'container' in cidr_networks: user_cidr = cidr_networks['container'] elif 'management' in cidr_networks: user_cidr = cidr_networks['management'] else: raise SystemExit('No container or management network ' 'specified in user config.') # make sure user_defined config is self contained _check_config_settings(cidr_networks, user_defined_config, environment.get('container_skel')) # Add the container_cidr into the all global ansible group_vars _parse_global_variables(user_cidr, inventory, user_defined_config) # Load all of the IP addresses that we know are used and set the queue ip.set_used_ips(user_defined_config, inventory) user_defined_setup(user_defined_config, inventory) skel_setup(environment, inventory) _check_group_branches(user_defined_config, environment.get('physical_skel')) logger.debug("Loading physical skel.") skel_load(environment.get('physical_skel'), inventory) logger.debug("Loading component skel") skel_load(environment.get('component_skel'), inventory) container_skel_load(environment.get('container_skel'), inventory, user_defined_config) # Look at inventory and ensure all entries have all required values. _ensure_inventory_uptodate( inventory=inventory, container_skel=environment.get('container_skel'), ) # Load the inventory json inventory_json = json.dumps(inventory, indent=4, separators=(',', ': '), sort_keys=True) if check: if _check_all_conf_groups_present(user_defined_config, environment): return 'Configuration ok!' # Save a list of all hosts and their given IP addresses hostnames_ips = _collect_hostnames(inventory) filesys.write_hostnames(config, hostnames_ips) if logger.isEnabledFor(logging.DEBUG): num_hosts = len(inventory['_meta']['hostvars']) logger.debug("%d hosts found.", num_hosts) # Save new dynamic inventory filesys.save_inventory(inventory_json, inv_path) return inventory_json