def setUp(self): """setup logging and a doc object """ # Init engine, if not already done. if self.engine is not None: reset_engine(self.engine) self.engine = get_new_engine_instance() self.doc = self.engine.data_object_cache d = {"pkg_img_path": "/tmp", "ba_build": "/tmp"} dc_dict = DataObjectDict("DC specific", d, generate_xml=False) self.doc.volatile.insert_children(dc_dict) self.custom_script = CustomScript("Custom Script", "echo Hello") self.dict_parent = SimpleDataObject("dicts") dd = DataObjectDict("TestDict", dict()) dd.data_dict['key'] = 1 self.dict_parent.insert_children(dd) dd = DataObjectDict("TestDict2", dict()) dd.data_dict['key'] = 2 self.dict_parent.insert_children(dd) self.doc.volatile.insert_children(self.dict_parent)
def register_checkpoint(sc_profile=SC_FILE, xslt=XSLT_FILE): '''Registers the GENERATE_SC_PROFILE_CHKPOINT checkpoint with the engine. Also adds config_profile to InstallEngine.doc.persistent''' eng = InstallEngine.get_instance() sc_kwargs = {'xslt_file': xslt} sc_args = [sc_profile] eng.register_checkpoint(GENERATE_SC_PROFILE_CHKPOINT, "solaris_install/manifest/writer", "ManifestWriter", args=sc_args, kwargs=sc_kwargs) # Add profile location to the ApplySysconfig checkpoint's data dict. # Try to find the ApplySysconfig data dict from the DOC in case it # already exists. as_doc_dict = None as_doc_dict = eng.doc.volatile.get_first_child(name=APPLY_SYSCONFIG_DICT) if as_doc_dict is None: # Initialize new dictionary in DOC as_dict = {APPLY_SYSCONFIG_PROFILE_KEY: sc_profile} as_doc_dict = DataObjectDict(APPLY_SYSCONFIG_DICT, as_dict) eng.doc.volatile.insert_children(as_doc_dict) else: # Add to existing dictionary in DOC as_doc_dict.data_dict[APPLY_SYSCONFIG_PROFILE_KEY] = sc_profile eng.doc.persistent.insert_children([ConfigProfile()])
def test_data_object_dict_val_attr_fail_not_dict_prop(self): '''Validate failure if non-dict passed as data_dict on creation''' try: obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = list() self.fail("Unexpected success setting data_dict to a list") except ValueError: pass
def test_restore_data_tmp(self): '''Slightly more than a "Unit" test, this verifies that rollback interacts as expected with the DOC''' string_data = DataObjectDict("TESTKEY", {"TESTKEY": "TESTVALUE"}) doc = self.engine.data_object_cache.persistent doc.insert_children([string_data]) stored_data = doc.get_first_child(name="TESTKEY") self.assertNotEqual(stored_data, None) self.engine.execute_checkpoints(pause_before=self.name_list[1]) stored_data = doc.get_first_child(name="TESTKEY") self.assertNotEqual(stored_data, None) self.assertEquals("TESTVALUE", stored_data.data_dict["TESTKEY"]) stored_data.data_dict["TESTKEY"] = "new value" stored_data_2 = doc.get_first_child(name="TESTKEY") self.assertTrue(stored_data is stored_data_2) # Rollback to beginning and run all checkpoints self.engine.execute_checkpoints(start_from=self.name_list[0]) stored_data_3 = doc.get_first_child(name="TESTKEY") self.assertNotEqual("new value", stored_data_3.data_dict["TESTKEY"]) self.assertEqual("TESTVALUE", stored_data_3.data_dict["TESTKEY"])
def setup_croinfo(self): """ set up a DataObjectDict representing the output from /usr/sbin/croinfo """ cmd = [CROINFO, "-h", "-O", "cAR"] p = run(cmd) # for systems that do not support CRO, nothing will be returned in # stdout so simply return. if not p.stdout: return # keep a positional counter since we can't use OrderedDicts until 2.7 i = 1 for line in p.stdout.splitlines(): (ctd, alias, receptacle) = line.split(":") # skip any entries where the ctd is missing. if not ctd: continue self.cro_dict[ctd] = (i, alias or None, receptacle) i += 1 if self.cro_dict: # Only insert if there is something in it self.doc.persistent.insert_children( DataObjectDict(CRO_LABEL, self.cro_dict, generate_xml=True))
def test_data_object_dict_val_attr_fail_not_dict(self): '''Validate failure if non-dict type passed for dictionary''' try: DataObjectDict("not_dict", data_dict=["elem1"]) self.fail("Unexpected success creating obj with a list") except ValueError: pass
def test_transfer_files(self): '''Test transferring logs''' self.dod = DataObjectDict("TFS", self.trans_dict, generate_xml=True) self.doc.volatile.insert_children(self.dod) # Call the execute command for the checkpoint self.trans_files.execute()
def setUp(self): """ Initialises a runtime environment for execution of ISOImageBootMenu subclassed unit tests. Currently needs an X86 legacy GRUB environment. """ super(ISOBootMenuTestCase, self).setUp() temp_dir = tempfile.mkdtemp(dir=TMP_TEST_DIR, prefix="install_boot_test_") rel_file_dir = os.path.join(temp_dir, "etc") os.makedirs(rel_file_dir) rel_file = os.path.join(rel_file_dir, 'release') with open(rel_file, 'w') as file_handle: file_handle.write('Solaris Boot Test') doc_dict = {"pkg_img_path": temp_dir} self.temp_dir = temp_dir self.doc.volatile.insert_children( DataObjectDict("DC specific", doc_dict, generate_xml=True)) # Create a dummy boot and grub menu.ls environment # so that pybootmgmt can instantiate a legacyGrub # boot loader under the bootconfig object # UEFI - this needs updating for SPARC, GRUB2 & UEFI # when support gets added to pybootmgmt if platform.processor() == 'i386': os.makedirs(os.path.join(temp_dir, 'boot/grub')) copyfile('/boot/grub/menu.lst', os.path.join(temp_dir, 'boot/grub/menu.lst')) copyfile('/boot/grub/stage2_eltorito', os.path.join(temp_dir, 'boot/grub/stage2_eltorito')) os.makedirs(os.path.join(temp_dir, 'boot/solaris')) copyfile('/boot/solaris/bootenv.rc', os.path.join(temp_dir, 'boot/solaris/bootenv.rc'))
def test_transfer_empty_dict(self): '''Test no transfer files returns gracefully''' self.trans_dict.clear() self.dod = DataObjectDict("TFS", self.trans_dict, generate_xml=True) self.doc.volatile.insert_children(self.dod) try: self.trans_files.execute() except Exception as e: self.fail(str(e))
def test_transfer_no_source_exists(self): '''Test no source doesn't exist''' self.trans_dict["/bogus"] = "/bogus/" self.dod = DataObjectDict("TFS", self.trans_dict, generate_xml=True) self.doc.volatile.insert_children(self.dod) try: self.trans_files.execute() except Exception as e: self.fail(str(e))
def setUp(self): eng = engine_test_utils.get_new_engine_instance() self.ppim = AIPrePkgImgMod("Test PPIM") self.ppim.pkg_img_path = "/" (fd, path) = tempfile.mkstemp(dir="/var/tmp", prefix="test_pkg_ver_") self.outfile = path os.chmod(self.outfile, 0777) # create a dummy DOC entry for the DC_LABEL self.ppim.doc = eng.data_object_cache self.ppim.doc.volatile.insert_children(DataObjectDict(DC_LABEL, {}))
def test_data_object_dict_val_attr_xml_default_no_name(self): '''Validate that from_xml() imports XML(value attr) if no name for tag ''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary> <data name="name1" value="value1"/> <data name="name2" value="value2"/> <data name="name3" value="value3"/> <data name="name4" value="value4"/> <data name="name5" value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDict.can_handle(xml_tree): new_obj = DataObjectDict.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(new_obj.name, None, "new object's name is not None.") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!")
def setUp(self): """ Initialises a runtime environment for execution of ISOImageBootMenu subclassed unit tests. Currently needs an X86 legacy GRUB environment. """ super(ISOBootMenuTestCase, self).setUp() temp_dir = tempfile.mkdtemp(dir=TMP_TEST_DIR, prefix="install_boot_test_") rel_file_dir = os.path.join(temp_dir, "etc") os.makedirs(rel_file_dir) rel_file = os.path.join(rel_file_dir, 'release') with open(rel_file, 'w') as file_handle: file_handle.write('Solaris Boot Test') # Under normal DC circumstances pkg_img_path and ba_build are # different paths, but this is irrelevant to the boot checkpoint doc_dict = { "pkg_img_path": temp_dir, "ba_build": temp_dir, "tmp_dir": temp_dir } self.temp_dir = temp_dir with open(os.path.join(temp_dir, ".volsetid"), "w") as volsetid: volsetid.write("boot_checkpoint_ISO_unit_test") self.doc.volatile.insert_children( DataObjectDict("DC specific", doc_dict, generate_xml=True)) # Create a dummy boot and grub menu.lst environment so that pybootmgmt # can instantiate a Legacy GRUB or GRUB2 boot loader under the # bootconfig object. # XXX - this needs updating for SPARC if platform.processor() == 'i386': # Create a dummy legacy GRUB environment if that's what the build # host has if os.path.exists('/boot/grub/stage2_eltorito'): os.makedirs(os.path.join(temp_dir, 'boot/grub')) copyfile('/boot/grub/menu.lst', os.path.join(temp_dir, 'boot/grub/menu.lst')) copyfile('/boot/grub/stage2_eltorito', os.path.join(temp_dir, 'boot/grub/stage2_eltorito')) os.makedirs(os.path.join(temp_dir, 'boot/solaris')) copyfile('/boot/solaris/bootenv.rc', os.path.join(temp_dir, 'boot/solaris/bootenv.rc')) # If it has GRUB 2, copy over the entire GRUB2 tree if os.path.exists('/usr/lib/grub2'): copytree('/usr/lib/grub2', os.path.join(temp_dir, 'usr/lib/grub2'))
def test_data_object_dict_fail_can_handle_invalid_tag(self): '''Validate that can_handle fails using an invalid tag name''' indentation = '''\ ''' TEST_XML = '''\ <bad_data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </bad_data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) self.assertFalse( DataObjectDict.can_handle(xml_tree), "can_handle returned True when given a bad tag: %s" % (TEST_XML))
def test_data_object_dict_val_attr_fail_can_handle_invalid_tag(self): '''Validate that can_handle fails using an invalid value attr name''' indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1" bad_value="value1"/> <data name="name2" bad_value="value2"/> <data name="name3" bad_value="value3"/> <data name="name4" bad_value="value4"/> <data name="name5" bad_value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) self.assertFalse( DataObjectDict.can_handle(xml_tree), "can_handle returned True when given a bad value attr: %s" % (TEST_XML))
def add_transfer_files_to_doc(transfer_name, files): ''' Create dataobjectdict dictionary containing src/dest pairs for files to be transferred via transfer-files checkpoint to newly installed boot environment. ''' # Check for existence of data object dictionary, create if not found engine = InstallEngine.get_instance() doc = engine.data_object_cache tf_doc_dict = doc.volatile.get_first_child(name=transfer_name) if tf_doc_dict is None: # Iniitalize dictionary in DOC tf_dict = dict() tf_doc_dict = DataObjectDict(transfer_name, tf_dict) doc.volatile.insert_children(tf_doc_dict) else: tf_dict = tf_doc_dict.data_dict # Add list of files to be transferred tf_dict.update(files)
def execute(self, dry_run=False): """ Primary execution method used by the Checkpoint parent class to create the ISO for x86 dry_run is not used in DC """ self.logger.info("=== Executing Create ISO Checkpoint ==") self.parse_doc() if self.arch == "i386": self.setup() self.run_mkisofs() elif self.arch == "sparc": # prepare the bootblock inblock = os.path.join(self.ba_build, "platform/sun4u/lib/fs/hsfs/bootblk") outblock = os.path.join(self.pkg_img_path, "boot/hsfs.bootblock") self.prepare_bootblock(inblock, outblock) self.setup(outblock) self.run_mkisofs() if self.add_timestamp is not None and \ self.add_timestamp.capitalize() == "True": self.create_additional_timestamp() # add_timestamp_iso needs to live in the persistent # section of the DOC to ensure pause/resume works # correctly. # # update the DC_PERS_LABEL DOC object with an entry # for add_timestamp_iso if self.dc_pers_dict: self.doc.persistent.delete_children(name=DC_PERS_LABEL) self.dc_pers_dict["add_timestamp_iso"] = self.dist_iso self.doc.persistent.insert_children(DataObjectDict(DC_PERS_LABEL, self.dc_pers_dict, generate_xml=True))
def execute(self, dry_run=False): """ Primary execution method used by the Checkpoint parent class. """ self.logger.info("=== Executing Pre-Package Image Modification " + "Checkpoint ===") self.parse_doc() # Customize various configuration files. self.customize_config_files() # preload smf manifests self.configure_smf() # set up the pkg_img_path with auto-install information self.logger.debug("creating auto_install directory") # change source path to 'usr/share' of the package image os.chdir(os.path.join(self.pkg_img_path, "usr/share")) # set destination path pkg_ai_path = os.path.join(self.pkg_img_path, "auto_install") # Copy files from /usr/share/auto_install shutil.copytree("auto_install", pkg_ai_path, symlinks=True) # Copy files from /usr/share/install too old_wd = os.getcwd() os.chdir(os.path.join(self.pkg_img_path, "usr/share/install")) ai_dtd_found = False for dtd_file in [f for f in os.listdir(".") if path_matches_dtd(f)]: shutil.copy(dtd_file, pkg_ai_path) if not ai_dtd_found and dtd_file.startswith("ai.dtd"): ai_dtd_found = True os.symlink(dtd_file, os.path.join(pkg_ai_path, "ai.dtd")) os.chdir(old_wd) # Restore Working Directory # move in service_bundle(4) for AI server profile validation shutil.copy("lib/xml/dtd/service_bundle.dtd.1", pkg_ai_path) # clear way for get_pkg_version and add_default_svcname # to update the dc_pers_dict if self.dc_pers_dict: self.doc.persistent.delete_children(name=DC_PERS_LABEL) self.get_pkg_version("auto-install") self.get_license() # write out the .image_info file self.calculate_size() # write out the image type into the .image_info file self.add_image_type() self.add_versions("usr/share/auto_install/version") self.add_default_svcname("auto-install") # update the DC_PERS_LABEL DOC object with a new # dictionary that contains auto-install and service_name # as additional entries. self.logger.debug("updating persistent doc %s" % self.dc_pers_dict) self.doc.persistent.insert_children( DataObjectDict(DC_PERS_LABEL, self.dc_pers_dict, generate_xml=True))
if dry_run: self.logger.debug('Executing: %s', cmd) else: Popen.check_call(cmd, stdout=Popen.STORE, \ stderr=Popen.STORE, logger=self.logger) # Add the zone configuration directory into the dictionary in the DOC # that will be processed by the transfer-ai-files checkpoint which will # copy files over to the installed root. tf_doc_dict = None tf_doc_dict = self.doc.volatile.get_first_child( \ name=TRANSFER_FILES_CHECKPOINT) if tf_doc_dict is None: # Initialize new dictionary in DOC tf_dict = dict() tf_doc_dict = DataObjectDict(TRANSFER_FILES_CHECKPOINT, tf_dict) self.doc.volatile.insert_children(tf_doc_dict) else: tf_dict = tf_doc_dict.data_dict tf_dict[TMP_ZONES_INSTALL_DIR] = TARGET_ZONES_INSTALL_DIR def parse_doc(self): """ class method for parsing the data object cache (DOC) objects for use by this checkpoint """ self.engine = InstallEngine.get_instance() self.doc = self.engine.data_object_cache # Get a reference to ApplicationData object self.app_data = self.doc.persistent.get_first_child( \
def test_data_object_dict_can_insert_to_doc(self): '''Validate DataObjectDict can be inserted as child of DataObject''' data_obj = SimpleDataObject("test_obj") data_dict = {'key1': 'value1', 'key2': 'value2'} data_dict_obj = DataObjectDict("TestChild", data_dict) data_obj.insert_children(data_dict_obj)
def setUp(self): '''Create simple dictionary, and several objects using it''' tmp_dict = dict() tmp_dict["name1"] = "value1" tmp_dict["name2"] = "value2" tmp_dict["name3"] = "value3" tmp_dict["name4"] = "value4" tmp_dict["name5"] = "value5" self.data_dict_xml = DataObjectDict("test_dictionary_xml", tmp_dict, generate_xml=True) self.data_dict_xml_no_name = DataObjectDict(None, tmp_dict, generate_xml=True) self.data_dict_no_xml = DataObjectDict("test_dictionary_no_xml", tmp_dict) self.data_dict_diff_tag = DataObjectDictDiffTag( "test_dictionary_diff_tag", tmp_dict, generate_xml=True) self.data_dict_diff_sub_tag = DataObjectDictDiffSubTag( "test_dictionary_diff_sub_tag", tmp_dict, generate_xml=True) self.data_dict_diff_both_tags = DataObjectDictDiffBothTag( "test_dictionary_diff_both_tags", tmp_dict, generate_xml=True) self.data_dict_xml_value_attr = DataObjectDict("test_dictionary_xml", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_xml_value_attr_no_name = DataObjectDict( None, tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_tag = DataObjectDictDiffValueAndTag( "test_dictionary_diff_value_and_tag", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_sub_tag = \ DataObjectDictDiffValueAndSubTag( "test_dictionary_diff_value_and_sub_tag", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_both_tags = \ DataObjectDictDiffValueAndBothTag( "test_dictionary_diff_value_and_both_tags", tmp_dict, generate_xml=True, value_as_attr=True) # Slightly different dictionary, add a key of 'name'. tmp_dict = dict() tmp_dict["name"] = "value" tmp_dict["name1"] = "value1" tmp_dict["name2"] = "value2" tmp_dict["name3"] = "value3" tmp_dict["name4"] = "value4" tmp_dict["name5"] = "value5" self.data_dict_attr = DataObjectDict("test_dictionary_attr", tmp_dict)
class TestDataObjectDict(unittest.TestCase): '''Tests to validate DataObjectDict functionality''' def setUp(self): '''Create simple dictionary, and several objects using it''' tmp_dict = dict() tmp_dict["name1"] = "value1" tmp_dict["name2"] = "value2" tmp_dict["name3"] = "value3" tmp_dict["name4"] = "value4" tmp_dict["name5"] = "value5" self.data_dict_xml = DataObjectDict("test_dictionary_xml", tmp_dict, generate_xml=True) self.data_dict_xml_no_name = DataObjectDict(None, tmp_dict, generate_xml=True) self.data_dict_no_xml = DataObjectDict("test_dictionary_no_xml", tmp_dict) self.data_dict_diff_tag = DataObjectDictDiffTag( "test_dictionary_diff_tag", tmp_dict, generate_xml=True) self.data_dict_diff_sub_tag = DataObjectDictDiffSubTag( "test_dictionary_diff_sub_tag", tmp_dict, generate_xml=True) self.data_dict_diff_both_tags = DataObjectDictDiffBothTag( "test_dictionary_diff_both_tags", tmp_dict, generate_xml=True) self.data_dict_xml_value_attr = DataObjectDict("test_dictionary_xml", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_xml_value_attr_no_name = DataObjectDict( None, tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_tag = DataObjectDictDiffValueAndTag( "test_dictionary_diff_value_and_tag", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_sub_tag = \ DataObjectDictDiffValueAndSubTag( "test_dictionary_diff_value_and_sub_tag", tmp_dict, generate_xml=True, value_as_attr=True) self.data_dict_diff_value_and_both_tags = \ DataObjectDictDiffValueAndBothTag( "test_dictionary_diff_value_and_both_tags", tmp_dict, generate_xml=True, value_as_attr=True) # Slightly different dictionary, add a key of 'name'. tmp_dict = dict() tmp_dict["name"] = "value" tmp_dict["name1"] = "value1" tmp_dict["name2"] = "value2" tmp_dict["name3"] = "value3" tmp_dict["name4"] = "value4" tmp_dict["name5"] = "value5" self.data_dict_attr = DataObjectDict("test_dictionary_attr", tmp_dict) def tearDown(self): '''Free up references to objects''' self.data_dict_xml = None self.data_dict_no_xml = None self.data_dict_diff_tag = None self.data_dict_diff_sub_tag = None self.data_dict_diff_both_tags = None self.data_dict_xml_value_attr = None self.data_dict_value_attr_no_xml = None self.data_dict_diff_value_and_tag = None self.data_dict_diff_value_and_sub_tag = None self.data_dict_diff_value_and_both_tags = None # Tests for value as text def test_data_object_dict_no_xml(self): '''Validate that XML isn't generated if generate_xml=False''' self.assertTrue(self.data_dict_no_xml.get_xml_tree() == None, self.data_dict_no_xml.get_xml_tree_str()) def test_data_object_dict_xml_default(self): '''Validate that XML is generated with default settings''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_xml.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_xml_default_no_name(self): '''Validate that XML is generated with default settings but no name''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_xml_no_name.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_value_using_attr(self): '''Validate that its possible to refer to a value as an attribute''' self.assertEqual(self.data_dict_attr.name1, self.data_dict_attr.data_dict["name1"]) # Ensure that name returns _name, not value in dictionary. self.assertEqual(self.data_dict_attr.name, self.data_dict_attr._name) def test_data_object_dict_value_fail_using_attr(self): '''Validate that its not possible to refer to an invalid attribute''' try: self.data_dict_attr.name10 self.fail("Didn't raise exception referring to invalid attribute") except AttributeError: pass def test_data_object_dict_xml_diff_tag(self): '''Validate that XML is generated using a different TAG_NAME''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <different_tag name="test_dictionary_diff_tag"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </different_tag> '''.replace(indentation, "") xml_str = self.data_dict_diff_tag.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_xml_diff_sub_tag(self): '''Validate that XML is generated using a different SUB_TAG_NAME''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary name="test_dictionary_diff_sub_tag"> <different_sub_tag name="name1">value1</different_sub_tag> <different_sub_tag name="name2">value2</different_sub_tag> <different_sub_tag name="name3">value3</different_sub_tag> <different_sub_tag name="name4">value4</different_sub_tag> <different_sub_tag name="name5">value5</different_sub_tag> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_diff_sub_tag.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_xml_diff_both_tag(self): '''Validate that XML uses different TAG_NAME and SUB_TAG_NAME''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <different_both_tag name="test_dictionary_diff_both_tags"> <different_both_sub_tag name="name1">value1</different_both_sub_tag> <different_both_sub_tag name="name2">value2</different_both_sub_tag> <different_both_sub_tag name="name3">value3</different_both_sub_tag> <different_both_sub_tag name="name4">value4</different_both_sub_tag> <different_both_sub_tag name="name5">value5</different_both_sub_tag> </different_both_tag> '''.replace(indentation, "") xml_str = self.data_dict_diff_both_tags.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_fail_not_dict(self): '''Validate failure if non-dict type passed for dictionary''' try: DataObjectDict("not_dict", data_dict=["elem1"]) self.fail("Unexpected success creating obj with a list") except ValueError: pass def test_data_object_dict_set_dict_prop(self): '''Validate correct setting of data_dict property on creation''' obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = {'key1': 'value1', 'key2': 'value2'} self.assertEqual(obj.data_dict['key1'], 'value1') self.assertEqual(obj.data_dict['key2'], 'value2') def test_data_object_dict_fail_not_dict_prop(self): '''Validate failure if non-dict passed as data_dict on creation''' try: obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = list() self.fail("Unexpected success setting data_dict to a list") except ValueError: pass def test_data_object_dict_fail_invalid_tag(self): '''Validate that XML generation fails using an invalid TAG_NAME''' try: data_dict = {'key1': 'value1', 'key2': 'value2'} data_obj = DataObjectDictBadTag("invalid_tag", data_dict, generate_xml=True) data_obj.to_xml() self.fail("Unexpected success creating obj with a bad tag name") except ValueError: pass def test_data_object_dict_fail_invalid_sub_tag(self): '''Validate that XML generation fails using an invalid SUB_TAG_NAME''' try: data_dict = {'key1': 'value1', 'key2': 'value2'} data_obj = DataObjectDictBadSubTag("invalid_tag", data_dict, generate_xml=True) data_obj.to_xml() self.fail( "Unexpected success creating obj with a bad sub-tag name") except ValueError: pass def test_data_object_dict_fail_can_handle_invalid_tag(self): '''Validate that can_handle fails using an invalid tag name''' indentation = '''\ ''' TEST_XML = '''\ <bad_data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </bad_data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) self.assertFalse( DataObjectDict.can_handle(xml_tree), "can_handle returned True when given a bad tag: %s" % (TEST_XML)) def test_data_object_dict_fail_can_handle_invalid_sub_tag(self): '''Validate that can_handle fails using an invalid sub tag''' indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <baddata name="name4">value4</baddata> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) self.assertFalse( DataObjectDict.can_handle(xml_tree), "can_handle returned True when given a bad sub_tag: %s" % (TEST_XML)) def test_data_object_dict_fail_from_xml_invalid_tag(self): '''Validate that from_xml() fails using an invalid XML tag''' indentation = '''\ ''' TEST_XML = '''\ <bad_data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </bad_data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) # can_handle tested seperately, just ensure from_xml will fail too. self.assertRaises(ParsingError, DataObjectDict.from_xml, xml_tree) def test_data_object_dict_fail_from_xml_invalid_sub_tag(self): '''Validate that from_xml() fails using an invalid XML sub tag''' indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <baddata name="name4">value4</baddata> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) # can_handle tested seperately, just ensure from_xml will fail too. self.assertRaises(ParsingError, DataObjectDict.from_xml, xml_tree) def test_data_object_dict_set_generate_xml_prop(self): '''Validate that set/get of generate_xml flag works''' self.assertFalse(self.data_dict_no_xml.generate_xml) self.assertTrue(self.data_dict_no_xml.to_xml() == None) self.data_dict_no_xml.generate_xml = True self.assertTrue(self.data_dict_no_xml.generate_xml) self.assertTrue(self.data_dict_no_xml.to_xml() != None) self.data_dict_no_xml.generate_xml = False self.assertFalse(self.data_dict_no_xml.generate_xml) self.assertTrue(self.data_dict_no_xml.to_xml() == None) def test_data_object_dict_import_xml_default_no_name(self): '''Validate that from_xml() correctly imports XML if no name for tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDict.can_handle(xml_tree): new_obj = DataObjectDict.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(new_obj.name, None, "new object's name is not None.") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_import_xml_default(self): '''Validate that from_xml() correctly imports XML''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDict.can_handle(xml_tree): new_obj = DataObjectDict.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_import_xml_diff_tag(self): '''Validate from_xml() imports correctly with diff tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <different_tag name="test_dictionary_xml"> <data name="name1">value1</data> <data name="name2">value2</data> <data name="name3">value3</data> <data name="name4">value4</data> <data name="name5">value5</data> </different_tag> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffTag.can_handle(xml_tree): new_obj = DataObjectDictDiffTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_import_xml_diff_sub_tag(self): '''Validate from_xml() imports correctly with diff sub tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <different_sub_tag name="name1">value1</different_sub_tag> <different_sub_tag name="name2">value2</different_sub_tag> <different_sub_tag name="name3">value3</different_sub_tag> <different_sub_tag name="name4">value4</different_sub_tag> <different_sub_tag name="name5">value5</different_sub_tag> </data_dictionary> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffSubTag.can_handle(xml_tree): new_obj = DataObjectDictDiffSubTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_import_xml_diff_both_tag(self): '''Validate from_xml() imports correctly with diff tag and sub-tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <different_both_tag name="test_dictionary_diff_both_tags"> <different_both_sub_tag name="name1">value1</different_both_sub_tag> <different_both_sub_tag name="name2">value2</different_both_sub_tag> <different_both_sub_tag name="name3">value3</different_both_sub_tag> <different_both_sub_tag name="name4">value4</different_both_sub_tag> <different_both_sub_tag name="name5">value5</different_both_sub_tag> </different_both_tag> '''.replace(indentation, "") # Parse the XML into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffBothTag.can_handle(xml_tree): new_obj = DataObjectDictDiffBothTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_can_insert_to_doc(self): '''Validate DataObjectDict can be inserted as child of DataObject''' data_obj = SimpleDataObject("test_obj") data_dict = {'key1': 'value1', 'key2': 'value2'} data_dict_obj = DataObjectDict("TestChild", data_dict) data_obj.insert_children(data_dict_obj) # Tests for value as an attribute def test_data_object_dict_val_attr_xml_default(self): '''Validate that XML(value attr) is generated with default settings''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1" value="value1"/> <data name="name2" value="value2"/> <data name="name3" value="value3"/> <data name="name4" value="value4"/> <data name="name5" value="value5"/> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_xml_value_attr.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_val_attr_xml_default_no_name(self): '''Validate that XML(value attr) is generated with defaults w/no name ''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary> <data name="name1" value="value1"/> <data name="name2" value="value2"/> <data name="name3" value="value3"/> <data name="name4" value="value4"/> <data name="name5" value="value5"/> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_xml_value_attr_no_name.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_val_attr_xml_diff_value_and_tag(self): '''Validate that XML(value attr) is generated using a diff TAG_NAME''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <different_tag name="test_dictionary_diff_value_and_tag"> <data name="name1" different_value="value1"/> <data name="name2" different_value="value2"/> <data name="name3" different_value="value3"/> <data name="name4" different_value="value4"/> <data name="name5" different_value="value5"/> </different_tag> '''.replace(indentation, "") xml_str = self.data_dict_diff_value_and_tag.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_val_attr_xml_diff_value_and_sub_tag(self): '''Validate that XML(value attr) is generated with a diff SUB_TAG_NAME ''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <data_dictionary name="test_dictionary_diff_value_and_sub_tag"> <different_sub_tag name="name1" different_value="value1"/> <different_sub_tag name="name2" different_value="value2"/> <different_sub_tag name="name3" different_value="value3"/> <different_sub_tag name="name4" different_value="value4"/> <different_sub_tag name="name5" different_value="value5"/> </data_dictionary> '''.replace(indentation, "") xml_str = self.data_dict_diff_value_and_sub_tag.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_val_attr_xml_diff_value_and_both_tag(self): '''Validate that XML(value attr) uses diff TAG_NAME and SUB_TAG_NAME ''' # Set expected xml, and compensate for indent indentation = '''\ ''' expected_xml = '''\ <different_both_tag name="test_dictionary_diff_value_and_both_tags"> <different_both_sub_tag name="name1" different_value="value1"/> <different_both_sub_tag name="name2" different_value="value2"/> <different_both_sub_tag name="name3" different_value="value3"/> <different_both_sub_tag name="name4" different_value="value4"/> <different_both_sub_tag name="name5" different_value="value5"/> </different_both_tag> '''.replace(indentation, "") xml_str = self.data_dict_diff_value_and_both_tags.get_xml_tree_str() self.assertEqual(xml_str, expected_xml, "EXPECTED:\n%s\nGOT:\n%s" %\ (expected_xml, xml_str)) def test_data_object_dict_val_attr_fail_not_dict(self): '''Validate failure if non-dict type passed for dictionary''' try: DataObjectDict("not_dict", data_dict=["elem1"]) self.fail("Unexpected success creating obj with a list") except ValueError: pass def test_data_object_dict_val_attr_set_dict_prop(self): '''Validate correct setting of data_dict property on creation''' obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = {'key1': 'value1', 'key2': 'value2'} self.assertEqual(obj.data_dict['key1'], 'value1') self.assertEqual(obj.data_dict['key2'], 'value2') def test_data_object_dict_val_attr_fail_not_dict_prop(self): '''Validate failure if non-dict passed as data_dict on creation''' try: obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = list() self.fail("Unexpected success setting data_dict to a list") except ValueError: pass def test_data_object_dict_val_attr_fail_invalid_tag(self): '''Validate that XML(value attr) generation fails with invalid TAG_NAME ''' try: data_dict = {'key1': 'value1', 'key2': 'value2'} data_obj = DataObjectDictBadTag("invalid_tag", data_dict, generate_xml=True) data_obj.to_xml() self.fail("Unexpected success creating obj with a bad tag name") except ValueError: pass def test_data_object_dict_val_attr_fail_invalid_sub_tag(self): '''Validate that XML(value attr) generation fails with bad SUB_TAG_NAME ''' try: data_dict = {'key1': 'value1', 'key2': 'value2'} data_obj = DataObjectDictBadSubTag("invalid_tag", data_dict, generate_xml=True) data_obj.to_xml() self.fail( "Unexpected success creating obj with a bad sub-tag name") except ValueError: pass def test_data_object_dict_val_attr_fail_can_handle_invalid_tag(self): '''Validate that can_handle fails using an invalid value attr name''' indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1" bad_value="value1"/> <data name="name2" bad_value="value2"/> <data name="name3" bad_value="value3"/> <data name="name4" bad_value="value4"/> <data name="name5" bad_value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) self.assertFalse( DataObjectDict.can_handle(xml_tree), "can_handle returned True when given a bad value attr: %s" % (TEST_XML)) def test_data_object_dict_val_attr_fail_from_xml_invalid_tag(self): '''Validate that from_xml() fails using an invalid value attr name''' indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1" bad_value="value1"/> <data name="name2" bad_value="value2"/> <data name="name3" bad_value="value3"/> <data name="name4" bad_value="value4"/> <data name="name5" bad_value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) # can_handle tested seperately, just ensure from_xml will fail too. self.assertRaises(ParsingError, DataObjectDict.from_xml, xml_tree) def test_data_object_dict_val_attr_xml_default_no_name(self): '''Validate that from_xml() imports XML(value attr) if no name for tag ''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary> <data name="name1" value="value1"/> <data name="name2" value="value2"/> <data name="name3" value="value3"/> <data name="name4" value="value4"/> <data name="name5" value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDict.can_handle(xml_tree): new_obj = DataObjectDict.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(new_obj.name, None, "new object's name is not None.") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_val_attr_xml_default(self): '''Validate that from_xml() correctly imports XML''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <data name="name1" value="value1"/> <data name="name2" value="value2"/> <data name="name3" value="value3"/> <data name="name4" value="value4"/> <data name="name5" value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDict.can_handle(xml_tree): new_obj = DataObjectDict.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_val_attr_xml_diff_value_and_tag(self): '''Validate from_xml() imports correctly with diff tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <different_tag name="test_dictionary_xml"> <data name="name1" different_value="value1"/> <data name="name2" different_value="value2"/> <data name="name3" different_value="value3"/> <data name="name4" different_value="value4"/> <data name="name5" different_value="value5"/> </different_tag> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffValueAndTag.can_handle(xml_tree): new_obj = DataObjectDictDiffValueAndTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_val_attr_xml_diff_value_and_sub_tag(self): '''Validate from_xml() imports correctly with diff sub tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <data_dictionary name="test_dictionary_xml"> <different_sub_tag name="name1" different_value="value1"/> <different_sub_tag name="name2" different_value="value2"/> <different_sub_tag name="name3" different_value="value3"/> <different_sub_tag name="name4" different_value="value4"/> <different_sub_tag name="name5" different_value="value5"/> </data_dictionary> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffValueAndSubTag.can_handle(xml_tree): new_obj = DataObjectDictDiffValueAndSubTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_val_attr_xml_diff_value_and_both_tag(self): '''Validate from_xml() imports correctly with diff tag and sub-tag''' # Set expected xml, and compensate for indent indentation = '''\ ''' TEST_XML = '''\ <different_both_tag name="test_dictionary_diff_value_and_both_tags"> <different_both_sub_tag name="name1" different_value="value1"/> <different_both_sub_tag name="name2" different_value="value2"/> <different_both_sub_tag name="name3" different_value="value3"/> <different_both_sub_tag name="name4" different_value="value4"/> <different_both_sub_tag name="name5" different_value="value5"/> </different_both_tag> '''.replace(indentation, "") # Parse the XML(value attr) into an XML tree. xml_tree = etree.fromstring(TEST_XML) if DataObjectDictDiffValueAndBothTag.can_handle(xml_tree): new_obj = DataObjectDictDiffValueAndBothTag.from_xml(xml_tree) self.assertTrue(new_obj != None, "Failed to create DataObjectDict from XML") self.assertEquals(type(new_obj.data_dict), dict, "new object's data_dict is not a data_dict.") self.assertEquals(new_obj.data_dict["name1"], "value1", "new object's name1 doesn't have correct value") self.assertEquals(new_obj.data_dict["name2"], "value2", "new object's name2 doesn't have correct value") self.assertEquals(new_obj.data_dict["name3"], "value3", "new object's name3 doesn't have correct value") self.assertEquals(new_obj.data_dict["name4"], "value4", "new object's name4 doesn't have correct value") self.assertEquals(new_obj.data_dict["name5"], "value5", "new object's name5 doesn't have correct value") else: self.fail("can_handle returned False, expected True!") def test_data_object_dict_val_attr_can_insert_to_doc(self): '''Validate DataObjectDict can be inserted as child of DataObject''' data_obj = SimpleDataObject("test_obj") data_dict = {'key1': 'value1', 'key2': 'value2'} data_dict_obj = DataObjectDict("TestChild", data_dict) data_obj.insert_children(data_dict_obj)
def main(): """ primary execution function for distro_const """ # clear the error service to be sure that we start with a clean slate errsvc.clear_error_list() options, args = parse_args() manifest = args[-1] pause_checkpoint = None resume_checkpoint = None verbose = options.verbose list_cps = options.list_checkpoints try: # We initialize the Engine with stop_on_error set so that if there are # errors during manifest parsing, the processing stops eng = InstallEngine(debug=False, stop_on_error=True) global DC_LOGGER DC_LOGGER = logging.getLogger(INSTALL_LOGGER_NAME) # set the logfile name log_name = "log.%s" % time.strftime("%Y-%m-%d.%H:%M") detail_log_name = "detail-%s" % log_name simple_log_name = "simple-%s" % log_name # create an additional FileHandler for a simple log base, logfile = os.path.split(DEFAULTLOG) simple_logname = os.path.join(base, "simple-" + logfile) simple_fh = FileHandler(simple_logname) simple_fh.setLevel(logging.INFO) DC_LOGGER.addHandler(simple_fh) if options.resume_checkpoint: resume_checkpoint = options.resume_checkpoint DC_LOGGER.info("distro_const will resume from: " + \ resume_checkpoint) if options.pause_checkpoint: pause_checkpoint = options.pause_checkpoint DC_LOGGER.info("distro_const will pause at: " + pause_checkpoint) # create a simple StreamHandler to output messages to the screen set_stream_handler(DC_LOGGER, list_cps, verbose) base_dataset = None parse_manifest(manifest) # get a reference to the data object cache doc = eng.data_object_cache # validate the target section of the manifest zpool_name, base_dataset, base_action, base_dataset_mp = \ validate_target() if list_cps: # set the execute flag of setup_build_dataset to 'False' # to prevent any actions from occuring. The TI checkpoint # needs to be registered with the engine for list_checkpoints # to work correctly. setup_build_dataset(zpool_name, base_dataset, base_action, base_dataset_mp, resume_checkpoint, execute=False) # set the InstallEngine.dataset property to enable snapshots eng.dataset = os.path.join(zpool_name, base_dataset, "build_data") list_checkpoints(DC_LOGGER) else: (base_dataset_mp, build_data_mp, logs_mp, media_mp) = \ setup_build_dataset(zpool_name, base_dataset, base_action, base_dataset_mp, resume_checkpoint) # update the DOC with actual directory values update_doc_paths(build_data_mp) # lock the dataset with Lockfile(os.path.join(base_dataset_mp, DC_LOCKFILE)): # output the log file path to the screen and transfer the logs new_detaillog = os.path.join(logs_mp, detail_log_name) new_simplelog = os.path.join(logs_mp, simple_log_name) DC_LOGGER.info("Simple log: %s" % new_simplelog) DC_LOGGER.info("Detail Log: %s" % new_detaillog) DC_LOGGER.transfer_log(destination=new_detaillog) simple_fh.transfer_log(destination=new_simplelog) # set the http_proxy if one is specified in the manifest dc_set_http_proxy(DC_LOGGER) # reset the InstallEngine.dataset property to enable snapshots eng.dataset = os.path.join(zpool_name, base_dataset, "build_data") # register each checkpoint listed in the execution section registered_checkpoints = register_checkpoints(DC_LOGGER) # now populate the DOC with the common information needed # by the various checkpoints -- pkg_img_path, etc doc_dict = { "pkg_img_path": os.path.join(build_data_mp, "pkg_image"), "ba_build": os.path.join(build_data_mp, "boot_archive"), "tmp_dir": os.path.join(build_data_mp, "tmp"), "media_dir": media_mp } doc.volatile.insert_children( DataObjectDict(DC_LABEL, doc_dict, generate_xml=True)) # if we're trying to pause at the very first checkpoint, # there's nothing to execute, so return 0 if pause_checkpoint == registered_checkpoints[0][0]: return 0 execute_checkpoint(new_detaillog, resume_checkpoint, pause_checkpoint) # catch any errors and log them. except BaseException as msg: if DC_LOGGER is not None: DC_LOGGER.exception(msg) else: # DC_LOGGER hasn't even been setup and we ran into an error print msg return 1 finally: if DC_LOGGER is not None: DC_LOGGER.close() return 0
def test_data_object_dict_val_attr_set_dict_prop(self): '''Validate correct setting of data_dict property on creation''' obj = DataObjectDict("not_dict", data_dict=dict()) obj.data_dict = {'key1': 'value1', 'key2': 'value2'} self.assertEqual(obj.data_dict['key1'], 'value1') self.assertEqual(obj.data_dict['key2'], 'value2')