def create_misc_archive(self): """ class method to create the /mnt/misc file system archive """ os.chdir(self.pkg_img_path) self.logger.info("Generating /mnt/misc file system archive") os.mkdir("miscdirs") shutil.move("opt", "miscdirs") shutil.move("etc", "miscdirs") shutil.move("var", "miscdirs") # add Software node to install items from /mnt/misc src_path = Dir("/mnt/misc") src = Source() src.insert_children(src_path) dst_path = Dir(INSTALL_TARGET_VAR) dst = Destination() dst.insert_children(dst_path) tr_install_misc = CPIOSpec() tr_install_misc.action = CPIOSpec.INSTALL tr_install_misc.contents = ["."] tr_install_misc.size = str(dir_size(os.path.join(self.pkg_img_path, "miscdirs"))) misc_software_node = Software(TRANSFER_MISC, type="CPIO") misc_software_node.insert_children([src, dst, tr_install_misc]) self.doc.persistent.insert_children(misc_software_node) cmd = [cli.MKISOFS, "-o", "solarismisc.zlib", "-N", "-l", "-R", "-U", "-allow-multidot", "-no-iso-translate", "-quiet", "-cache-inodes", "-d", "-D", "-V", "\"compress\"", "miscdirs"] run(cmd) self.logger.info("Compressing /mnt/misc file system archive " + "using: " + self.compression_type) cmd = [cli.LOFIADM, "-C", self.compression_type, os.path.join(self.pkg_img_path, "solarismisc.zlib")] p = run(cmd, check_result=Popen.ANY) if p.returncode != 0: if "invalid algorithm name" in p.stderr: raise RuntimeError("Invalid compression algorithm " + "specified for /mnt/misc archive: " + self.compression_type) else: raise RuntimeError("Compression of /mnt/misc file system " + "failed: " + os.strerror(p.returncode)) # the removal of /usr must be deferred to until solarismisc.zlib has # been created because the contents of solarismisc.zlib actually come # from /usr shutil.rmtree(os.path.join(self.pkg_img_path, "miscdirs"), ignore_errors=True) shutil.rmtree(os.path.join(self.pkg_img_path, "usr"), ignore_errors=True)
def add_content_list_to_doc(self, content_list): src_path = Dir(MEDIA_DIR_VAR) src = Source() src.insert_children(src_path) dst_path = Dir(INSTALL_TARGET_VAR) dst = Destination() dst.insert_children(dst_path) media_install = CPIOSpec() media_install.action = CPIOSpec.INSTALL media_install.contents = content_list total_size_byte = 0 for content in content_list: content_path = os.path.join(self.pkg_img_path, content) # only want to calculate the size of files, since directories # are traversed and it's files are included in the list. if not os.path.isdir(content_path): total_size_byte += file_size(content_path) media_install.size = str(total_size_byte) media_soft_node = Software(TRANSFER_MEDIA, type="CPIO") media_soft_node.insert_children([src, dst, media_install]) # Add that into the software transfer list. self.doc.persistent.insert_children(media_soft_node) # call manifest writer to write out the content of # the transfer manifest manifest_out = os.path.join(self.pkg_img_path, TRANSFER_MANIFEST_NAME) xslt_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), "xslt", "doc2_media_transfer.xslt") manifest_writer = ManifestWriter("manifest-writer", manifest_out, xslt_file=xslt_name) manifest_writer.write(self.doc)
def setUp(self): InstallEngine._instance = None InstallEngine() self.engine = InstallEngine.get_instance() self.doc = self.engine.data_object_cache.volatile self.soft_node = Software("CPIO_Transfer", "CPIO") self.tr_node = CPIOSpec() self.soft_node.insert_children([self.tr_node]) self.doc.insert_children([self.soft_node]) self.tr_cpio = TransferCPIO("CPIO_Transfer")
def test_live_cd_cleanup_dry(self): '''Test the dry run functionality for cleanup_livecd''' # Create an IPS node for packages to be removed self.soft_node = Software("cleanup_cpio_install") self.pkg_rm_node = IPSSpec() self.pkg_rm_node.action = "uninstall" self.pkg_rm_node.contents = self.TEXT_PKG_REMOVE_LIST self.soft_node.insert_children([self.pkg_rm_node]) self.simple.doc.persistent.insert_children([self.soft_node]) # Create a CPIO node for files to be removed self.add_cleanup_node = CPIOSpec() self.add_cleanup_node.action = "uninstall" self.add_cleanup_node.contents = self.add_files self.soft_node.insert_children([self.add_cleanup_node]) cleanup_list = ['.livecd', '.volsetid', '.textinstall', 'etc/sysconfig/language', '.liveusb', 'a', 'bootcd_microroot', 'var/user/jack', 'var/cache/gdm/jack/dmrc', 'var/cache/gdm/jack/', 'file1', 'file2', 'file3'] # Instantiate the checkpoint self.clean_lcd = CleanupCPIOInstall("cleanup_cpio_install") # Call the execute command for the checkpoint # with dry_run set to true. try: self.clean_lcd.execute(dry_run=True) except Exception as e: self.fail(str(e)) self.assertEquals(cleanup_list, self.clean_lcd.cleanup_list)
def test_dir_excl_list_file(self): '''Test the success of using directory exclusion''' # Copy all files/dirs from /etc/xdg except for anything under # /etc/xdg/menus to /rpool/cpio_test_dir using a file to # specify the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE # Populate the dir list file with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg" + "\n") # Create and insert a node for the excluded files/dirs self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" self.tr_node2.contents = self.TEST_DIR_EXCL_LIST_FILE self.soft_node.insert_children([self.tr_node2]) # Populate the excl file with open(self.TEST_DIR_EXCL_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg/menus" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err))
def test_skip_file_list_list(self): '''Test the success of using skip_file_list''' # Copy all the files/dirs from /etc except for /etc/name_to_major # to /rpool/cpio_test_dir using a file for the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" #self.tr_node.contents = self.TEST_DIR_LIST_FILE self.tr_node.contents = ["etc/dhcp"] # Create and insert a node for the files to be uninstalled self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" #self.tr_node2.contents = self.TEST_SKIP_FILE_LIST_FILE self.tr_node2.contents = ["etc/dhcp/duid", "etc/dhcp/iaid"] self.soft_node.insert_children([self.tr_node2]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err))
def create_misc_archive(self): """ class method to create the /mnt/misc file system archive """ os.chdir(self.pkg_img_path) self.logger.info("Generating /mnt/misc file system archive") os.mkdir("miscdirs") shutil.move("opt", "miscdirs") shutil.move("etc", "miscdirs") shutil.move("var", "miscdirs") # add Software node to install items from /mnt/misc src_path = Dir("/mnt/misc") src = Source() src.insert_children(src_path) dst_path = Dir(INSTALL_TARGET_VAR) dst = Destination() dst.insert_children(dst_path) tr_install_misc = CPIOSpec() tr_install_misc.action = CPIOSpec.INSTALL tr_install_misc.contents = ["."] tr_install_misc.size = str( dir_size(os.path.join(self.pkg_img_path, "miscdirs"))) misc_software_node = Software(TRANSFER_MISC, type="CPIO") misc_software_node.insert_children([src, dst, tr_install_misc]) self.doc.persistent.insert_children(misc_software_node) cmd = [ cli.MKISOFS, "-o", "solarismisc.zlib", "-N", "-l", "-R", "-U", "-allow-multidot", "-no-iso-translate", "-quiet", "-cache-inodes", "-d", "-D", "-V", "\"compress\"", "miscdirs" ] run(cmd) self.logger.info("Compressing /mnt/misc file system archive " + "using: " + self.compression_type) cmd = [ cli.LOFIADM, "-C", self.compression_type, os.path.join(self.pkg_img_path, "solarismisc.zlib") ] p = run(cmd, check_result=Popen.ANY) if p.returncode != 0: if "invalid algorithm name" in p.stderr: raise RuntimeError("Invalid compression algorithm " + "specified for /mnt/misc archive: " + self.compression_type) else: raise RuntimeError("Compression of /mnt/misc file system " + "failed: " + os.strerror(p.returncode)) # the removal of /usr must be deferred to until solarismisc.zlib has # been created because the contents of solarismisc.zlib actually come # from /usr shutil.rmtree(os.path.join(self.pkg_img_path, "miscdirs"), ignore_errors=True) shutil.rmtree(os.path.join(self.pkg_img_path, "usr"), ignore_errors=True)
class TestCPIOFunctions(unittest.TestCase): TEST_DST_DIR = "/tmp/cpio_test_dir" TEST_SRC_DIR = "/" TEST_FILE_LIST_FILE = "/tmp/test_file_list" TEST_CONTENTS_LIST = [] TEST_DIR_LIST_FILE = "/tmp/test_dir_list" TEST_SKIP_FILE_LIST_FILE = "/tmp/test_skip_file_list" TEST_DIR_EXCL_LIST_FILE = "/tmp/test_dir_excl_list" TEST_MEDIA_TRANSFORM = "/tmp/media_transform" def setUp(self): InstallEngine._instance = None InstallEngine() self.engine = InstallEngine.get_instance() self.doc = self.engine.data_object_cache.volatile self.soft_node = Software("CPIO_Transfer", "CPIO") self.tr_node = CPIOSpec() self.soft_node.insert_children([self.tr_node]) self.doc.insert_children([self.soft_node]) self.tr_cpio = TransferCPIO("CPIO_Transfer") def tearDown(self): if os.access(self.TEST_DST_DIR, os.F_OK): shutil.rmtree(self.TEST_DST_DIR) if os.access(self.TEST_FILE_LIST_FILE, os.F_OK): os.unlink(self.TEST_FILE_LIST_FILE) if os.access(self.TEST_DIR_LIST_FILE, os.F_OK): os.unlink(self.TEST_DIR_LIST_FILE) if os.access(self.TEST_SKIP_FILE_LIST_FILE, os.F_OK): os.unlink(self.TEST_SKIP_FILE_LIST_FILE) if os.access(self.TEST_DST_DIR, os.F_OK): os.unlink(self.TEST_DST_DIR) if os.access(self.TEST_MEDIA_TRANSFORM, os.F_OK): os.unlink(self.TEST_MEDIA_TRANSFORM) if os.access("/tmp/media_not_exec", os.F_OK): os.unlink("/tmp/media_not_exec") self.engine.data_object_cache.clear() self.doc = None self.engine = None self.soft_node = None self.tr_node = None self.tr_cpio = None InstallEngine._instance = None TEST_CONTENTS_LIST = [] def test_software_type(self): self.assertTrue(self.soft_node.tran_type == "CPIO") def test_entire(self): '''Test transfer all of /etc/X11 to /rpool/cpio_test_dir succeeds''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cleanup_temp_files(self): '''Test the cleanup of the temporary files''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] self.tr_cpio._parse_input() try: self.tr_cpio._cleanup_tmp_files() except Exception as err: self.fail(str(err)) def test_cleanup_temp_files_nonexistent_file(self): '''Test cleanup temp files should check nonexistent''' self.tr_cpio._transfer_list = [{ 'action': 'install', 'cpio_args': '-pdum', 'contents': '/noexist' }] try: self.tr_cpio._cleanup_tmp_files() except Exception as err: self.fail(str(err)) def test_cpio_w_file_list_file(self): '''Test copy of a file list file succeeds''' # Copy /bin/xclock and /bin/pv.sh to /rpool/cpio_test_dir # using a file list file as the contents souce # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_FILE_LIST_FILE with open(self.TEST_FILE_LIST_FILE, 'w') as filehandle: filehandle.write("bin/xclock" + "\n") filehandle.write("bin/pv.sh" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_file_list(self): '''Test copy from a list succeeds''' # Copy /bin/xclock and /bin/pv.sh to /rpool/cpio_test_dir # using a file list as the contents source # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_dir_list_file(self): '''Test directory cpio copy succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a file for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/X11" + "\n") filehandle.write("etc/zones" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_dir_list(self): '''Test copying a list of directories and files succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a list for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("etc/X11") self.TEST_CONTENTS_LIST.append("etc/zones") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_non_default_args_set(self): '''Test copying a list of directories and files succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a list for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) args = Args({"cpio_args": "-n -d -pdum"}) self.tr_node.insert_children([args]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("etc/X11") self.TEST_CONTENTS_LIST.append("etc/zones") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_empty_list(self): ''' Test that "empty contents list" scenario is handled''' # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = [] try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_no_contents(self): ''' Test that "no contents" scenario is handled''' # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified - no contents self.tr_node.action = "install" try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_skip_file_list_file(self): '''Test the success of using skip_file_list''' # Copy all the files/dirs from /etc except for /etc/name_to_major # to /rpool/cpio_test_dir using a file for the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE # Create and insert a node for the files to be uninstalled self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" self.tr_node2.contents = self.TEST_SKIP_FILE_LIST_FILE self.soft_node.insert_children([self.tr_node2]) # Populate the files with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/dhcp" + "\n") with open(self.TEST_SKIP_FILE_LIST_FILE, 'w') as filehandle: filehandle.write("etc/dhcp/duid" + "\n") filehandle.write("etc/dhcp/iaid" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_skip_file_list_list(self): '''Test the success of using skip_file_list''' # Copy all the files/dirs from /etc except for /etc/name_to_major # to /rpool/cpio_test_dir using a file for the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" #self.tr_node.contents = self.TEST_DIR_LIST_FILE self.tr_node.contents = ["etc/dhcp"] # Create and insert a node for the files to be uninstalled self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" #self.tr_node2.contents = self.TEST_SKIP_FILE_LIST_FILE self.tr_node2.contents = ["etc/dhcp/duid", "etc/dhcp/iaid"] self.soft_node.insert_children([self.tr_node2]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_dir_excl_list_file(self): '''Test the success of using directory exclusion''' # Copy all files/dirs from /etc/xdg except for anything under # /etc/xdg/menus to /rpool/cpio_test_dir using a file to # specify the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE # Populate the dir list file with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg" + "\n") # Create and insert a node for the excluded files/dirs self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" self.tr_node2.contents = self.TEST_DIR_EXCL_LIST_FILE self.soft_node.insert_children([self.tr_node2]) # Populate the excl file with open(self.TEST_DIR_EXCL_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg/menus" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_src_not_exist(self): ''' Test that an error is raised when src doesn't exist.''' #Set up the source src = Source() path = Dir("/hello") src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_src_not_specified(self): ''' Test that an error is raised when src is not specified''' # Test that an error is raised. dst = Destination() dst_path = self.TEST_DST_DIR path = Dir(dst_path) dst.insert_children([path]) self.soft_node.insert_children([dst]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dst_not_specified(self): ''' Test that an error is raised when dst is not specified''' # Test that an error is raised. src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) self.soft_node.insert_children([src]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_skip_file_list_file_not_valid(self): '''Test that an error is raised for invalid skip_file_list file''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "uninstall" self.tr_node.contents = "/tmp/invalid_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dir_list_not_valid(self): '''Test that an error is raised for invalid dir_list file''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = "/tmp/invalid_dir_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dir_excl_list_not_valid(self): '''Test that an error is raised for invalid dir_excl_list''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "uninstall" self.tr_node.contents = "/tmp/invalid_dir_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_media_transform_not_valid(self): '''Test that an error is raised for invalid media_transform executable ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = "/tmp/invalid_transform_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_media_transform_not_executable(self): '''Test media_transform doesn't have correct permissions error ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = "/tmp/media_not_exec" open(self.tr_node.contents, 'w') os.chmod(self.tr_node.contents, 01666) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=False) try: os.unlink(self.tr_node.contents) except (Exception): pass def test_progress_estimate_invalid_src(self): '''Test the progress estimate value when src is invalid. ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "/bogus") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] try: progress_estimate = self.tr_cpio.get_progress_estimate() except: self.assertTrue(False) self.assertTrue(progress_estimate == self.tr_cpio.DEFAULT_PROG_EST) def test_progress_estimate(self): '''Test that the progress estimate is the value expected.''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["xinit"] try: progress_estimate = self.tr_cpio.get_progress_estimate() except Exception: self.assertTrue(False) size = 0 size += file_size(os.path.join(src_path, "./")) for contents in self.tr_node.contents: size += file_size(os.path.join(src_path, contents)) for root, subdirs, files in os.walk( os.path.join(src_path, contents)): for subdir in subdirs: size += file_size(os.path.join(root, subdir)) for fname in files: size += file_size(os.path.join(root, fname)) # convert size to kilobytes size = size / 1024 self.assertTrue(progress_estimate == \ int(float(size) / self.tr_cpio.DEFAULT_SIZE * \ self.tr_cpio.DEFAULT_PROG_EST)) def test_get_size(self): '''Test that get_size returns an error when no source is set''' dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([dst]) self.assertRaises(IndexError, self.tr_cpio.get_size) def test_progress_estimate_with_size(self): '''Test progress estimate value when size is pre-calculated exists ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] size_to_transfer = dir_size(src_path) self.tr_node.size = str(size_to_transfer) progress_estimate = self.tr_cpio.get_progress_estimate() expect_estimate = \ int((float(size_to_transfer / 1024) / self.tr_cpio.DEFAULT_SIZE) \ * self.tr_cpio.DEFAULT_PROG_EST) self.assertEqual(progress_estimate, expect_estimate) def test_media_transform(self): '''Test media transform functionality''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = self.TEST_MEDIA_TRANSFORM with open(self.TEST_MEDIA_TRANSFORM, 'w') as filehandle: filehandle.write("#!/usr/bin/python\n") filehandle.write("import os\n") mkdir_cmd = "os.mkdir('" + self.TEST_DST_DIR + "')" filehandle.write(mkdir_cmd + "\n") mkdir_cmd = "os.mkdir('" + os.path.join(self.TEST_DST_DIR, "media") + "')" filehandle.write(mkdir_cmd) os.chmod(self.TEST_MEDIA_TRANSFORM, 0777) try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_checkpoint_soft_node_match(self): '''The checkpoint and software node match ''' try: tr_cpio = TransferCPIO("CPIO_Transfer") except Exception as err: self.fail(str(err)) def test_checkpoint_soft_node_mismatch(self): '''The checkpoint and software node match as expected ''' self.assertRaises(Exception, TransferCPIO, "CPIO Transfer")
def add_root_transfer_to_doc(self): """ Adds the list of files of directories to be transferred to the DOC """ if self.doc is None: self.doc = InstallEngine.get_instance().data_object_cache src_path = Dir("/") src = Source() src.insert_children(src_path) dst_path = Dir(INSTALL_TARGET_VAR) dst = Destination() dst.insert_children(dst_path) dot_node = CPIOSpec() dot_node.action = CPIOSpec.INSTALL dot_node.size = str(dir_size(os.path.join(self.ba_build, ""))) dot_node.contents = ["."] usr_node = CPIOSpec() usr_node.action = CPIOSpec.INSTALL usr_node.size = str(dir_size(os.path.join(self.pkg_img_path, "usr"))) usr_node.contents = ["usr"] dev_node = CPIOSpec() dev_node.action = CPIOSpec.INSTALL dev_node.size = str(dir_size(os.path.join(self.pkg_img_path, "dev"))) dev_node.contents = ["dev"] software_node = Software(TRANSFER_ROOT, type="CPIO") software_node.insert_children([src, dst, dot_node, usr_node, dev_node]) self.doc.persistent.insert_children(software_node) self.logger.debug(str(self.doc.persistent))
class TestCPIOFunctions(unittest.TestCase): TEST_DST_DIR = "/tmp/cpio_test_dir" TEST_SRC_DIR = "/" TEST_FILE_LIST_FILE = "/tmp/test_file_list" TEST_CONTENTS_LIST = [] TEST_DIR_LIST_FILE = "/tmp/test_dir_list" TEST_SKIP_FILE_LIST_FILE = "/tmp/test_skip_file_list" TEST_DIR_EXCL_LIST_FILE = "/tmp/test_dir_excl_list" TEST_MEDIA_TRANSFORM = "/tmp/media_transform" def setUp(self): InstallEngine._instance = None InstallEngine() self.engine = InstallEngine.get_instance() self.doc = self.engine.data_object_cache.volatile self.soft_node = Software("CPIO_Transfer", "CPIO") self.tr_node = CPIOSpec() self.soft_node.insert_children([self.tr_node]) self.doc.insert_children([self.soft_node]) self.tr_cpio = TransferCPIO("CPIO_Transfer") def tearDown(self): if os.access(self.TEST_DST_DIR, os.F_OK): shutil.rmtree(self.TEST_DST_DIR) if os.access(self.TEST_FILE_LIST_FILE, os.F_OK): os.unlink(self.TEST_FILE_LIST_FILE) if os.access(self.TEST_DIR_LIST_FILE, os.F_OK): os.unlink(self.TEST_DIR_LIST_FILE) if os.access(self.TEST_SKIP_FILE_LIST_FILE, os.F_OK): os.unlink(self.TEST_SKIP_FILE_LIST_FILE) if os.access(self.TEST_DST_DIR, os.F_OK): os.unlink(self.TEST_DST_DIR) if os.access(self.TEST_MEDIA_TRANSFORM, os.F_OK): os.unlink(self.TEST_MEDIA_TRANSFORM) if os.access("/tmp/media_not_exec", os.F_OK): os.unlink("/tmp/media_not_exec") self.engine.data_object_cache.clear() self.doc = None self.engine = None self.soft_node = None self.tr_node = None self.tr_cpio = None InstallEngine._instance = None TEST_CONTENTS_LIST = [] def test_software_type(self): self.assertTrue(self.soft_node.tran_type == "CPIO") def test_entire(self): '''Test transfer all of /etc/X11 to /rpool/cpio_test_dir succeeds''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cleanup_temp_files(self): '''Test the cleanup of the temporary files''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] self.tr_cpio._parse_input() try: self.tr_cpio._cleanup_tmp_files() except Exception as err: self.fail(str(err)) def test_cleanup_temp_files_nonexistent_file(self): '''Test cleanup temp files should check nonexistent''' self.tr_cpio._transfer_list = [{'action': 'install', 'cpio_args': '-pdum', 'contents': '/noexist'}] try: self.tr_cpio._cleanup_tmp_files() except Exception as err: self.fail(str(err)) def test_cpio_w_file_list_file(self): '''Test copy of a file list file succeeds''' # Copy /bin/xclock and /bin/pv.sh to /rpool/cpio_test_dir # using a file list file as the contents souce # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_FILE_LIST_FILE with open(self.TEST_FILE_LIST_FILE, 'w') as filehandle: filehandle.write("bin/xclock" + "\n") filehandle.write("bin/pv.sh" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_file_list(self): '''Test copy from a list succeeds''' # Copy /bin/xclock and /bin/pv.sh to /rpool/cpio_test_dir # using a file list as the contents source # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_dir_list_file(self): '''Test directory cpio copy succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a file for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/X11" + "\n") filehandle.write("etc/zones" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_dir_list(self): '''Test copying a list of directories and files succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a list for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("etc/X11") self.TEST_CONTENTS_LIST.append("etc/zones") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_non_default_args_set(self): '''Test copying a list of directories and files succeeds''' # Copy all the directories and files from /etc/X11 and /etc/zones # to /rpool/cpio_test_dir using a list for the contents # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) args = Args({"cpio_args": "-n -d -pdum"}) self.tr_node.insert_children([args]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) self.TEST_CONTENTS_LIST.append("bin/xclock") self.TEST_CONTENTS_LIST.append("bin/pv.sh") # The CPIO values that are specified self.TEST_CONTENTS_LIST.append("etc/X11") self.TEST_CONTENTS_LIST.append("etc/zones") self.tr_node.action = "install" self.tr_node.contents = self.TEST_CONTENTS_LIST try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_cpio_w_empty_list(self): ''' Test that an error is raised when contents list is empty''' # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = [] self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_cpio_no_contents(self): ''' Test that an error is raised when no contents exist''' # Set up the source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified - no contents self.tr_node.action = "install" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_skip_file_list_file(self): '''Test the success of using skip_file_list''' # Copy all the files/dirs from /etc except for /etc/name_to_major # to /rpool/cpio_test_dir using a file for the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE # Create and insert a node for the files to be uninstalled self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" self.tr_node2.contents = self.TEST_SKIP_FILE_LIST_FILE self.soft_node.insert_children([self.tr_node2]) # Populate the files with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/dhcp" + "\n") with open(self.TEST_SKIP_FILE_LIST_FILE, 'w') as filehandle: filehandle.write("etc/dhcp/duid" + "\n") filehandle.write("etc/dhcp/iaid" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_skip_file_list_list(self): '''Test the success of using skip_file_list''' # Copy all the files/dirs from /etc except for /etc/name_to_major # to /rpool/cpio_test_dir using a file for the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" #self.tr_node.contents = self.TEST_DIR_LIST_FILE self.tr_node.contents = ["etc/dhcp"] # Create and insert a node for the files to be uninstalled self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" #self.tr_node2.contents = self.TEST_SKIP_FILE_LIST_FILE self.tr_node2.contents = ["etc/dhcp/duid", "etc/dhcp/iaid"] self.soft_node.insert_children([self.tr_node2]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_dir_excl_list_file(self): '''Test the success of using directory exclusion''' # Copy all files/dirs from /etc/xdg except for anything under # /etc/xdg/menus to /rpool/cpio_test_dir using a file to # specify the contents # Set up the Source src = Source() path = Dir(self.TEST_SRC_DIR) src.insert_children([path]) # Set up the destination dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) # Insert the source and dest into the Software node self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = self.TEST_DIR_LIST_FILE # Populate the dir list file with open(self.TEST_DIR_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg" + "\n") # Create and insert a node for the excluded files/dirs self.tr_node2 = CPIOSpec() self.tr_node2.action = "uninstall" self.tr_node2.contents = self.TEST_DIR_EXCL_LIST_FILE self.soft_node.insert_children([self.tr_node2]) # Populate the excl file with open(self.TEST_DIR_EXCL_LIST_FILE, 'w') as filehandle: filehandle.write("etc/xdg/menus" + "\n") try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_src_not_exist(self): ''' Test that an error is raised when src doesn't exist.''' #Set up the source src = Source() path = Dir("/hello") src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_src_not_specified(self): ''' Test that an error is raised when src is not specified''' # Test that an error is raised. dst = Destination() dst_path = self.TEST_DST_DIR path = Dir(dst_path) dst.insert_children([path]) self.soft_node.insert_children([dst]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dst_not_specified(self): ''' Test that an error is raised when dst is not specified''' # Test that an error is raised. src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) self.soft_node.insert_children([src]) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_skip_file_list_file_not_valid(self): '''Test that an error is raised for invalid skip_file_list file''' # Set up the source src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "uninstall" self.tr_node.contents = "/tmp/invalid_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dir_list_not_valid(self): '''Test that an error is raised for invalid dir_list file''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = "/tmp/invalid_dir_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_dir_excl_list_not_valid(self): '''Test that an error is raised for invalid dir_excl_list''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "uninstall" self.tr_node.contents = "/tmp/invalid_dir_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_media_transform_not_valid(self): '''Test that an error is raised for invalid media_transform executable ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = "/tmp/invalid_transform_file" self.assertRaises(Exception, self.tr_cpio.execute, dry_run=True) def test_media_transform_not_executable(self): '''Test media_transform doesn't have correct permissions error ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = "/tmp/media_not_exec" open(self.tr_node.contents, 'w') os.chmod(self.tr_node.contents, 01666) self.assertRaises(Exception, self.tr_cpio.execute, dry_run=False) try: os.unlink(self.tr_node.contents) except (Exception): pass def test_progress_estimate_invalid_src(self): '''Test the progress estimate value when src is invalid. ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "/bogus") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] try: progress_estimate = self.tr_cpio.get_progress_estimate() except: self.assertTrue(False) self.assertTrue(progress_estimate == self.tr_cpio.DEFAULT_PROG_EST) def test_progress_estimate(self): '''Test that the progress estimate is the value expected.''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["xinit"] try: progress_estimate = self.tr_cpio.get_progress_estimate() except Exception: self.assertTrue(False) size = 0 size += file_size(os.path.join(src_path, "./")) for contents in self.tr_node.contents: size += file_size(os.path.join(src_path, contents)) for root, subdirs, files in os.walk(os.path.join(src_path, contents)): for subdir in subdirs: size += file_size(os.path.join(root, subdir)) for fname in files: size += file_size(os.path.join(root, fname)) # convert size to kilobytes size = size / 1024 self.assertTrue(progress_estimate == \ int(float(size) / self.tr_cpio.DEFAULT_SIZE * \ self.tr_cpio.DEFAULT_PROG_EST)) def test_get_size(self): '''Test that get_size returns an error when no source is set''' dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([dst]) self.assertRaises(IndexError, self.tr_cpio.get_size) def test_progress_estimate_with_size(self): '''Test progress estimate value when size is pre-calculated exists ''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "install" self.tr_node.contents = ["./"] size_to_transfer = dir_size(src_path) self.tr_node.size = str(size_to_transfer) progress_estimate = self.tr_cpio.get_progress_estimate() expect_estimate = \ int((float(size_to_transfer/1024) / self.tr_cpio.DEFAULT_SIZE) * \ self.tr_cpio.DEFAULT_PROG_EST) self.assertEqual(progress_estimate, expect_estimate) def test_media_transform(self): '''Test media transform functionality''' src = Source() src_path = os.path.join(self.TEST_SRC_DIR, "etc/X11") path = Dir(src_path) src.insert_children([path]) dst = Destination() path = Dir(self.TEST_DST_DIR) dst.insert_children([path]) self.soft_node.insert_children([src, dst]) # The CPIO values that are specified self.tr_node.action = "transform" self.tr_node.contents = self.TEST_MEDIA_TRANSFORM with open(self.TEST_MEDIA_TRANSFORM, 'w') as filehandle: filehandle.write("#!/usr/bin/python\n") filehandle.write("import os\n") mkdir_cmd = "os.mkdir('" + self.TEST_DST_DIR + "')" filehandle.write(mkdir_cmd + "\n") mkdir_cmd = "os.mkdir('" + os.path.join(self.TEST_DST_DIR, "media") + "')" filehandle.write(mkdir_cmd) os.chmod(self.TEST_MEDIA_TRANSFORM, 0777) try: self.tr_cpio.execute(dry_run=True) except Exception as err: self.fail(str(err)) def test_checkpoint_soft_node_match(self): '''The checkpoint and software node match ''' try: tr_cpio = TransferCPIO("CPIO_Transfer") except Exception as err: self.fail(str(err)) def test_checkpoint_soft_node_mismatch(self): '''The checkpoint and software node match as expected ''' self.assertRaises(Exception, TransferCPIO, "CPIO Transfer")
def test_info(self): '''Test that all the arguments get into the node correctly''' soft_node = Software("CPIO transfer test 1") cpio_node = CPIOSpec() dst = Destination() path = Dir("/a") dst.insert_children([path]) src = Source() path = Dir("/bin") src.insert_children([path]) # first check src and dst soft_node.insert_children([dst, src, cpio_node]) self.doc.insert_children([soft_node]) soft_list = self.doc.get_children("CPIO transfer test 1", Software) for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: try: args = tr.get_children("args", Args)[0] except: self.assertTrue(True) self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(tr.action, None) self.assertEqual(tr.contents, None) # set cpio args args = Args({"cpio_args": "-pdm"}) cpio_node.insert_children([args]) # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, None) self.assertEqual(tr.contents, None) # set file_list content cpio_node.action = "install" cpio_node.type = "FILE" cpio_node.contents = "/usr/share/tr_file_list" # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, "install") self.assertEqual(tr.contents, "/usr/share/tr_file_list") # set dir_list cpio_node.action = "install" cpio_node.type = "DIR" cpio_node.contents = "/usr/share/tr_dir_list" # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, "install") self.assertEqual(tr.contents, "/usr/share/tr_dir_list") # set skip_file_list cpio_node.action = "uninstall" cpio_node.type = "FILE" cpio_node.contents = "/usr/share/tr_skip_file_list" # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, "uninstall") self.assertEqual(tr.contents, "/usr/share/tr_skip_file_list") # set dir_excl_list cpio_node.action = "uninstall" cpio_node.type = "DIR" cpio_node.contents = "/usr/share/tr_dir_excl_list" # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, "uninstall") self.assertEqual(tr.contents, "/usr/share/tr_dir_excl_list") # set media transform cpio_node.action = "transform" cpio_node.type = "None" cpio_node.contents = "/usr/share/media_transform" # Check that we can read the attributes out correctly for soft in soft_list: src_list = soft.get_children("source", Source) self.assertEqual(len(src_list), 1) src_path = src_list[0].get_children("dir", Dir) self.assertEqual(len(src_path), 1) src = src_path[0].dir_path dst_list = soft.get_children("destination", Destination) self.assertEqual(len(dst_list), 1) dst_path = dst_list[0].get_children("dir", Dir) self.assertEqual(len(dst_path), 1) dst = dst_path[0].dir_path tr_list = soft.get_children("transfer", CPIOSpec) for tr in tr_list: args = tr.get_children("args", Args)[0] self.assertEqual(dst, "/a") self.assertEqual(src, "/bin") self.assertEqual(args.arg_dict["cpio_args"], "-pdm") self.assertEqual(tr.action, "transform") self.assertEqual(tr.contents, "/usr/share/media_transform")
def configure_system(self): """ class method for the execution of various, isolated shell commands needed to configure the boot archive. """ self.logger.info("preparing boot archive") # configure devices cmd = [cli.DEVFSADM, "-r", self.ba_build] run(cmd) # etc/dev/.devfsadm_dev.lock gets created every time # devfsadm is run. remove it since there's no point # in carrying it forward through to the image lockfile = os.path.join(self.ba_build, "etc/dev/.devfsadm_dev.lock") if os.path.exists(lockfile): self.logger.debug("removing devfsadm lock file") os.remove(lockfile) # Set a marker so that every boot is a reconfiguration boot cmd = [cli.TOUCH, os.path.join(self.ba_build, "reconfigure")] run(cmd) # Set up /etc/rtc_config cmd = [cli.CP, os.path.join(self.file_defaults, "rtc_config.default"), os.path.join(self.ba_build, "etc/rtc_config")] run(cmd) # go to the ba_build self.logger.debug("creating symlinks and mountpoints") os.chdir(self.ba_build) # create ./tmp. mkdir and chmod have to be done seperately self.logger.debug("creating tmp dir and setting it to 01777") os.mkdir("tmp") os.chmod("tmp", 01777) # create ./proc self.logger.debug("creating proc directory") os.mkdir("proc") # create ./mnt self.logger.debug("creating mnt directory") os.mkdir("mnt") # create bin symlink to /usr/bin if needed self.logger.debug("checking for symlink of bin -> usr/bin") if not os.path.islink("bin"): os.symlink("usr/bin", "bin") # create mountpoints for misc and pkg zlibs self.logger.debug("creating mnt/misc and mnt/pkg mountpoints") os.mkdir("mnt/misc", 0755) os.mkdir("mnt/pkg", 0755) # create volume set id file, use system name + date for uniqueness with open(".volsetid", "w") as v: volsetid = os.uname()[1] + '-' + \ datetime.datetime.now().isoformat() self.logger.debug("setting .volsetid to %s" % volsetid) v.write(volsetid) # chmod it to 444 and set the ownership to root:root (0:0) os.chmod(".volsetid", 0444) os.chown(".volsetid", 0, 0) # create the file marking the image type (e.g. .autoinstall or # .livecd) self.logger.debug("creating image_type file") with open(self.image_type, "w"): pass # create .cdrom directory self.logger.debug("creating .cdrom directory") os.mkdir(".cdrom", 0755) # create opt symlink to mnt/misc/opt if needed self.logger.debug("checking for symlink of opt -> mnt/misc/opt") if not os.path.islink("opt"): os.symlink("mnt/misc/opt", "opt") tr_uninstall = CPIOSpec() tr_uninstall.action = CPIOSpec.UNINSTALL tr_uninstall.contents = ["opt"] root_tr_software_node = self.doc.persistent.get_descendants( name=TRANSFER_ROOT, class_type=Software, not_found_is_err=True)[0] root_tr_software_node.insert_children(tr_uninstall) # copy the SMF repository from pkg_image_path to ba_build pkg_img_path_repo = os.path.join(self.pkg_img_path, "etc/svc/repository.db") ba_build_repo = os.path.join(self.ba_build, "etc/svc/repository.db") shutil.copy2(pkg_img_path_repo, ba_build_repo)
def configure_symlinks(self): """ class method for the configuration of symlinks needed in the boot archive. """ self.logger.debug("Creating additional symlinks in ramdisk") self.logger.debug("creating set of files in pkg_img_path: %s" % \ self.pkg_img_path) # change to the pkg_img_path directory os.chdir(self.pkg_img_path) # walk /etc and /var in pkg_img_path and create a list of # directories pkg_img_dirs = [] for rootdir in ["etc", "var"]: for root, dirs, files in os.walk(rootdir): for d in dirs: pkg_img_dirs.append(os.path.join(root, d)) # change to the boot_archive directory os.chdir(self.ba_build) # walk the pkg_img_dirs list and create each directory that doesn't # already exist. Also, copy the directory permissions and metadata # to the new directory for d in pkg_img_dirs: ba_path = os.path.join(self.ba_build, d) pkg_path = os.path.join(self.pkg_img_path, d) # split the directory on / to verify parent directories exist dir_list = d.split("/") # keep a 'path' string for verification path = "" for subdir in dir_list: # extend path path = os.path.join(path, subdir) full_path = os.path.join(self.ba_build, path) # check to see if it exists and is not already a symlink if not os.path.exists(full_path) and \ not os.path.islink(full_path): # create the directory os.mkdir(os.path.join(self.ba_build, path)) # copy the metadata from pkg_image to boot_archive shutil.copystat(os.path.join(self.pkg_img_path, path), os.path.join(self.ba_build, path)) # copy the uid/gid as well pkg_statinfo = os.stat( os.path.join(self.pkg_img_path, path)) os.chown(os.path.join(self.ba_build, path), pkg_statinfo.st_uid, pkg_statinfo.st_gid) # now that the directory structure is created, create symlinks for # all the missing files in the boot_archive # change to the pkg_img_path directory os.chdir(self.pkg_img_path) # keep track of all the symlinks created misc_symlinks = [] for rootdir in ["etc", "var"]: for root, dirs, files in os.walk(rootdir): for f in files: pkg_path = os.path.join(self.pkg_img_path, root, f) # skip symlinks if os.path.islink(pkg_path): continue ba_path = os.path.join(self.ba_build, root, f) if not os.path.exists(ba_path): # the file is missing from the boot_archive so # create a symlink to /mnt/misc/file/path misc_path = os.path.join("/mnt/misc", root, f) # save the cwd cwd = os.getcwd() # changedir to the dirname of the file os.chdir(os.path.dirname(ba_path)) # create the symlink os.symlink(misc_path, f) os.chdir(cwd) misc_symlinks.append(os.path.join(root, f)) tr_uninstall = CPIOSpec() tr_uninstall.action = CPIOSpec.UNINSTALL tr_uninstall.contents = misc_symlinks # Add that into the software transfer list. The list of files to # uninstall MUST go before the contents to be installed from /mnt/misc root_tr_software_node = self.doc.persistent.get_descendants( name=TRANSFER_ROOT, class_type=Software, not_found_is_err=True)[0] root_tr_software_node.insert_children(tr_uninstall) self.logger.debug(str(self.doc.persistent))
def configure_system(self): """ class method for the execution of various, isolated shell commands needed to configure the boot archive. """ self.logger.info("preparing boot archive") # configure devices cmd = [cli.DEVFSADM, "-r", self.ba_build] run(cmd) # etc/dev/.devfsadm_dev.lock gets created every time # devfsadm is run. remove it since there's no point # in carrying it forward through to the image lockfile = os.path.join(self.ba_build, "etc/dev/.devfsadm_dev.lock") if os.path.exists(lockfile): self.logger.debug("removing devfsadm lock file") os.remove(lockfile) # Set a marker so that every boot is a reconfiguration boot cmd = [cli.TOUCH, os.path.join(self.ba_build, "reconfigure")] run(cmd) # Set up /etc/rtc_config cmd = [ cli.CP, os.path.join(self.file_defaults, "rtc_config.default"), os.path.join(self.ba_build, "etc/rtc_config") ] run(cmd) # go to the ba_build self.logger.debug("creating symlinks and mountpoints") os.chdir(self.ba_build) # create ./tmp. mkdir and chmod have to be done seperately self.logger.debug("creating tmp dir and setting it to 01777") os.mkdir("tmp") os.chmod("tmp", 01777) # create ./proc self.logger.debug("creating proc directory") os.mkdir("proc") # create ./mnt self.logger.debug("creating mnt directory") os.mkdir("mnt") # create bin symlink to /usr/bin if needed self.logger.debug("checking for symlink of bin -> usr/bin") if not os.path.islink("bin"): os.symlink("usr/bin", "bin") # create mountpoints for misc and pkg zlibs self.logger.debug("creating mnt/misc and mnt/pkg mountpoints") os.mkdir("mnt/misc", 0755) os.mkdir("mnt/pkg", 0755) # create volume set id file, use system name + date for uniqueness with open(".volsetid", "w") as v: volsetid = os.uname()[1] + '-' + \ datetime.datetime.now().isoformat() self.logger.debug("setting .volsetid to %s" % volsetid) v.write(volsetid) # chmod it to 444 and set the ownership to root:root (0:0) os.chmod(".volsetid", 0444) os.chown(".volsetid", 0, 0) # create the file marking the image type (e.g. .autoinstall or # .livecd) self.logger.debug("creating image_type file") with open(self.image_type, "w"): pass # create .cdrom directory self.logger.debug("creating .cdrom directory") os.mkdir(".cdrom", 0755) # create opt symlink to mnt/misc/opt if needed self.logger.debug("checking for symlink of opt -> mnt/misc/opt") if not os.path.islink("opt"): os.symlink("mnt/misc/opt", "opt") tr_uninstall = CPIOSpec() tr_uninstall.action = CPIOSpec.UNINSTALL tr_uninstall.contents = ["opt"] root_tr_software_node = self.doc.persistent.get_descendants( name=TRANSFER_ROOT, class_type=Software, not_found_is_err=True)[0] root_tr_software_node.insert_children(tr_uninstall) # copy the SMF repository from pkg_image_path to ba_build pkg_img_path_repo = os.path.join(self.pkg_img_path, "etc/svc/repository.db") ba_build_repo = os.path.join(self.ba_build, "etc/svc/repository.db") shutil.copy2(pkg_img_path_repo, ba_build_repo)
def configure_symlinks(self): """ class method for the configuration of symlinks needed in the boot archive. """ self.logger.debug("Creating additional symlinks in ramdisk") self.logger.debug("creating set of files in pkg_img_path: %s" % \ self.pkg_img_path) # change to the pkg_img_path directory os.chdir(self.pkg_img_path) # walk /etc and /var in pkg_img_path and create a list of # directories pkg_img_dirs = [] for rootdir in ["etc", "var"]: for root, dirs, files in os.walk(rootdir): for d in dirs: pkg_img_dirs.append(os.path.join(root, d)) # change to the boot_archive directory os.chdir(self.ba_build) # walk the pkg_img_dirs list and create each directory that doesn't # already exist. Also, copy the directory permissions and metadata # to the new directory for d in pkg_img_dirs: ba_path = os.path.join(self.ba_build, d) pkg_path = os.path.join(self.pkg_img_path, d) # split the directory on / to verify parent directories exist dir_list = d.split("/") # keep a 'path' string for verification path = "" for subdir in dir_list: # extend path path = os.path.join(path, subdir) full_path = os.path.join(self.ba_build, path) # check to see if it exists and is not already a symlink if not os.path.exists(full_path) and \ not os.path.islink(full_path): # create the directory os.mkdir(os.path.join(self.ba_build, path)) # copy the metadata from pkg_image to boot_archive shutil.copystat(os.path.join(self.pkg_img_path, path), os.path.join(self.ba_build, path)) # copy the uid/gid as well pkg_statinfo = os.stat(os.path.join(self.pkg_img_path, path)) os.chown(os.path.join(self.ba_build, path), pkg_statinfo.st_uid, pkg_statinfo.st_gid) # now that the directory structure is created, create symlinks for # all the missing files in the boot_archive # change to the pkg_img_path directory os.chdir(self.pkg_img_path) # keep track of all the symlinks created misc_symlinks = [] for rootdir in ["etc", "var"]: for root, dirs, files in os.walk(rootdir): for f in files: pkg_path = os.path.join(self.pkg_img_path, root, f) # skip symlinks if os.path.islink(pkg_path): continue ba_path = os.path.join(self.ba_build, root, f) if not os.path.exists(ba_path): # the file is missing from the boot_archive so # create a symlink to /mnt/misc/file/path misc_path = os.path.join("/mnt/misc", root, f) # save the cwd cwd = os.getcwd() # changedir to the dirname of the file os.chdir(os.path.dirname(ba_path)) # create the symlink os.symlink(misc_path, f) os.chdir(cwd) misc_symlinks.append(os.path.join(root, f)) tr_uninstall = CPIOSpec() tr_uninstall.action = CPIOSpec.UNINSTALL tr_uninstall.contents = misc_symlinks # Add that into the software transfer list. The list of files to # uninstall MUST go before the contents to be installed from /mnt/misc root_tr_software_node = self.doc.persistent.get_descendants( name=TRANSFER_ROOT, class_type=Software, not_found_is_err=True)[0] root_tr_software_node.insert_children(tr_uninstall) self.logger.debug(str(self.doc.persistent))