def test_lock(self): tmpdir = tempfile.mkdtemp() obj = state.State(data=[], cfg_dir=tmpdir) obj.lock() self.assertTrue(os.path.exists(obj._lockname), obj._lockname) obj.unlock() shutil.rmtree(tmpdir)
def test_hardware_info(self, *args): obj = state.State(data=[('hw1', '*')]) data = obj.hardware_info('node1') self.assertEqual( data, { 'disks': [{ 'size': '20Gi' }, { 'size': '21Gi' }, { 'size': '20Gi' }, { 'size': '19Gi' }, { 'size': '20Gi' }], 'nics': [{ 'mac': 'dd:ee:ff' }, { 'mac': 'aa:bb:cc' }], 'memory': 8192, 'ncpus': 4 })
def collect(config_path): # check config directory path if not os.path.exists(config_path): print("Error: --config-dir='%s' does not exist." % config_path) sys.exit(1) # get state object state_obj = state.State() state_obj.load(os.path.join(config_path, 'edeploy') + '/') # get global conf global_conf = _get_yaml_content("%s/config-tools/global.yml" % config_path) # expand keys prefixed by "=" global_conf["hosts"] = generate.generate_dict(global_conf["hosts"], "=") # the virtual configuration of each host virt_platform = {"hosts": {}} for hostname in global_conf["hosts"]: # construct the host virtual configuration virt_platform["hosts"][hostname] = state_obj.hardware_info(hostname) # add the profile virt_platform["hosts"][hostname]["profile"] = \ global_conf["hosts"][hostname]["profile"] # release the lock obtained during the load call state_obj.unlock() # so far, the nodes are described excepted the install-server # the code below adds the install-server from the global conf. for hostname in global_conf["hosts"]: if global_conf["hosts"][hostname]["profile"] == "install-server": # add the admin_network config admin_network = global_conf["config"]["admin_network"] admin_network = netaddr.IPNetwork(admin_network) nics = [{ "name": "eth0", "ip": global_conf["hosts"][hostname]["ip"], "network": str(admin_network.network), "netmask": str(admin_network.netmask) }] virt_platform["hosts"][hostname]["nics"] = nics break return virt_platform
def encode(elt): 'Encode unicode strings as strings else return the object' try: return elt.encode('ascii', 'ignore') except AttributeError: return elt hw_items = [] for info in json_hw_items: hw_items.append(tuple(map(encode, info))) # avoid concurrent accesses lock_filename = config_get(section, 'LOCKFILE', '/var/run/httpd/edeploy.lock') state_obj = state.State(lockname=lock_filename) state_obj.load(cfg_dir) def cleanup(): 'Remove lock.' state_obj.unlock() atexit.register(cleanup) filename_and_macs = matcher.generate_filename_and_macs(hw_items) save_hw(hw_items, filename_and_macs['sysname'], hw_dir) use_pxemngr = (config_get(section, 'USEPXEMNGR', False) == 'True') pxemngr_url = config_get(section, 'PXEMNGRURL', None) metadata_url = config_get(section, 'METADATAURL', None)
def test_find_match(self): obj = state.State(data=[('hw', 2)], cfg_dir='/nowhere') items = [('a', 'b', 'c', 'd')] obj._load_specs = lambda x: items self.assertEqual(obj.find_match(items), ('hw', {}))
def test_find_match_empty(self): obj = state.State(data=[], cfg_dir='/tmp') self.assertRaises(state.StateError, obj.find_match, [])
def test_getitem_not_found(self): obj = state.State(data=[]) self.assertRaises(KeyError, obj.__getitem__, 'toto')
def test_failed_profile_times(self): obj = state.State(data=[('hw', '*')]) self.assertFalse(obj.failed_profile('hw'))
def test_failed_profile(self): obj = state.State(data=[('hw', 2)]) self.assertTrue(obj.failed_profile('hw')) self.assertEqual(obj['hw'], 3)
def test_hardware_info_empty(self, *args): obj = state.State(data=[('hw1', '*')]) data = obj.hardware_info('node1') self.assertEqual(data, {})