def test_13_patch_ld(self, mock_putdata, mock_getdata, mock_copyto, mock_size, mock_path, mock_gcl): """Test13 ElfPatcher().patch_ld().""" mock_size.return_value = -1 mock_putdata.return_value = False mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertFalse(elfp.patch_ld()) mock_size.return_value = 20 mock_copyto.return_value = False mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertFalse(elfp.patch_ld()) mock_size.return_value = 20 mock_copyto.return_value = True mock_getdata.return_value = [] mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertFalse(elfp.patch_ld()) mock_size.return_value = 20 mock_copyto.return_value = True mock_getdata.return_value = [] mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertFalse(elfp.patch_ld("OUTPUT_ELF"))
def test_08_get_patch_last_path(self, mock_getdata, mock_path): """Test08 ElfPatcher().get_patch_last_path().""" mock_getdata.return_value = "" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.get_patch_last_path(), "") mock_getdata.return_value = "/tmp" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.get_patch_last_path(), "/tmp")
def test_09_check_container_path(self, mock_lpath, mock_path): """Test09 ElfPatcher().check_container_path().""" mock_lpath.return_value = "" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp.check_container_path() self.assertTrue(status) mock_lpath.return_value = "xxx" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp.check_container_path() self.assertFalse(status)
def test_05_guess_elf_loader(self, mock_spelf, mock_walk, mock_path): """Test05 ElfPatcher().guess_elf_loader().""" mock_spelf.return_value = "ld.so" mock_walk.return_value = "" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.guess_elf_loader(), "") mock_walk.return_value = "ld.so" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.guess_elf_loader(), "ld.so")
def test_07_get_container_loader(self, mock_gol, mock_exists, mock_path): """Test07 ElfPatcher().get_container_loader(). Get an absolute pathname to the container ld.so""" mock_gol.return_value = "" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.get_container_loader(), "") mock_exists.return_value = True mock_gol.return_value = "ld.so" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.get_container_loader(), elfp._container_root + "/" + "ld.so")
def test_14_restore_ld(self, mock_copyto, mock_size, mock_path, mock_gcl, mock_msg): """Test14 ElfPatcher().restore_ld().""" mock_msg.level = 0 mock_size.return_value = -1 mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertFalse(elfp.restore_ld()) mock_size.return_value = 20 mock_copyto.return_value = True mock_path.return_value = "/some_contdir" mock_gcl.return_value = "" elfp = ElfPatcher(self.local, self.contid) self.assertTrue(elfp.restore_ld())
def test_15__get_ld_config(self, mock_upout, mock_dir, mock_path): """Test15 ElfPatcher().get_ld_config().""" mock_upout.return_value = [] mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp._get_ld_config() self.assertEqual(status, []) mock_upout.return_value = \ "ld.so.cache => /tmp/ROOT/etc/ld.so.cache\n" \ "ld.so.cache => /tmp/ROOT/etc/ld.so" mock_dir.side_effect = ['/ld.so.cache', '/ld.so'] mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp._get_ld_config() self.assertIsInstance(status, list)
def test_16__find_ld_libdirs(self, mock_walk, mock_access, mock_path, mock_isfile): """Test16 ElfPatcher()._find_ld_libdirs(). search for library directories in container""" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp._find_ld_libdirs() self.assertEqual(status, []) mock_path.return_value = "/some_contdir" mock_walk.return_value = [("libsome.so.0", ["dir"], ["libsome.so.0"]), ] mock_access.return_value = True mock_isfile.return_value = True elfp = ElfPatcher(self.local, self.contid) status = elfp._find_ld_libdirs() self.assertEqual(status, ["libsome.so.0"])
def test_01__init(self, mock_path, mock_hinfo): """Test01 ElfPatcher() constructor.""" mock_path.return_value = "/some_contdir" mock_hinfo.uid = "1000" elfp = ElfPatcher(self.local, self.contid) self.assertTrue(mock_path.callled) self.assertEqual(elfp._uid, "1000")
def test_03__replace(self, mock_path): """Test03 ElfPatcher()._replace.""" mock_path.return_value = "/some_contdir" cmd = ("#f", "ls") path = "/bin/" elfp = ElfPatcher(self.local, self.contid) output = elfp._replace(cmd, path) self.assertEqual(output, ["/bin/", "ls"])
def test_17_get_ld_libdirs(self, mock_exists, mock_getdata, mock_pudata, mock_findlib, mock_path): """Test17 ElfPatcher().get_ld_libdirs().""" mock_exists.return_value = True mock_getdata.return_value = '/lib:/usr/lib' mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp.get_ld_libdirs(False) self.assertEqual(status, ['/lib', '/usr/lib']) mock_exists.return_value = False mock_pudata.return_value = '/lib:/usr/lib' mock_findlib.return_value = ['/lib', '/usr/lib'] mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp.get_ld_libdirs(True) self.assertEqual(status, ['/lib', '/usr/lib'])
def test_18_get_ld_library_path(self, mock_ldconf, mock_ldlib, mock_path): """Test18 ElfPatcher().get_ld_library_path().""" Config().conf['lib_dirs_list_essential'] = "" mock_ldconf.return_value = ["/lib"] mock_ldlib.return_value = ["/usr/lib"] mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) status = elfp.get_ld_library_path() self.assertEqual(status, "/lib:/usr/lib:.")
def test_06_get_original_loader(self, mock_guess, mock_getdata, mock_putdata, mock_exists, mock_path): """Test06 ElfPatcher().get_original_loader().""" mock_path.return_value = "/some_contdir" mock_exists.return_value = False mock_guess.return_value = "ld.so" mock_getdata.return_value.strip.return_value = "ld.so" mock_putdata.return_value = "ld.so" elfp = ElfPatcher(self.local, self.contid) status = elfp.get_original_loader() self.assertEqual(status, "ld.so")
def test_02_select_patchelf(self, mock_path, mock_find, mock_arch, mock_exists, mock_msg): """Test02 ElfPatcher().select_patchelf().""" mock_msg.level = 0 mock_path.return_value = "/some_contdir" mock_arch.return_value = "arm" mock_find.return_value = "runc-arm" mock_exists.return_value = True elfp = ElfPatcher(self.local, self.contid) output = elfp.select_patchelf() self.assertEqual(output, "runc-arm") mock_path.return_value = "/some_contdir" mock_arch.return_value = "arm" mock_find.return_value = "" mock_exists.return_value = False with self.assertRaises(SystemExit) as epexpt: elfp = ElfPatcher(self.local, self.contid) elfp.select_patchelf() self.assertEqual(epexpt.exception.code, 1)
def run(self, container_id): """Execute a Docker container using Fakechroot. This is the main method invoked to run the a container with Fakechroot. * argument: container_id or name * options: many via self.opt see the help """ # warning root execution not supported self._uid_check() # setup execution exec_path = self._run_init(container_id) if not exec_path: return 2 self._run_invalid_options() # execution mode and get patcher xmode = self.exec_mode.get_mode() self._elfpatcher = ElfPatcher(self.localrepo, self.container_id) # verify if container pathnames are correct for this mode if not self._elfpatcher.check_container_path(): Msg().out("Warning: container path mismatch, use setup to convert", l=Msg.WAR) # set basic environment variables self._run_env_set() self._fakechroot_env_set() # if not --hostenv clean the environment self._run_env_cleanup_list() # build the actual command cmd_l = self._set_cpu_affinity() cmd_l.extend([ "env", "-i", ]) cmd_l.extend(self.opt["env"].list()) if xmode in ("F1", "F2"): container_loader = self._elfpatcher.get_container_loader() if container_loader: cmd_l.append(container_loader) cmd_l.extend(self._run_add_script_support(exec_path)) cmd_l.extend(self.opt["cmd"]) Msg().out("CMD =", cmd_l, l=Msg.VER) # execute self._run_banner(self.opt["cmd"][0], '#') cwd = FileUtil(self.container_root).cont2host(self.opt["cwd"], self.opt["vol"]) status = subprocess.call(cmd_l, shell=False, close_fds=True, cwd=cwd) return status
def test_11_patch_binaries(self, mock_chkcont, mock_gcl, mock_select, mock_guess, mock_walk, mock_putdata, mock_exists, mock_path): """Test11 ElfPatcher().patch_binaries().""" mock_exists.return_value = True mock_chkcont.return_value = True mock_walk.return_value = True mock_gcl.return_value = "/usr/bin/ld" mock_select.return_value = "runc-arm" mock_putdata.side_effect = ["10", "/tmp"] mock_guess.return_value = "/usr/bin/ld" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) elfp._container_root = "/tmp/ROOT" self.assertTrue(elfp.patch_binaries())
def test_12_restore_binaries(self, mock_select, mock_gol, mock_lpath, mock_walk, mock_guess, mock_rm, mock_path): """Test12 ElfPatcher().restore_binaries().""" mock_select.return_value = "runc-arm" mock_gol.return_value = "ld.so" mock_lpath.return_value = "xxx" mock_walk.return_value = "" mock_guess.return_value = "ld.so" mock_rm.return_value = True mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) elfp._container_root = "/tmp/ROOT" self.assertTrue(elfp.restore_binaries()) self.assertTrue(mock_rm.called)
def test_04__walk_fs(self, mock_path, mock_access, mock_walk, mock_stat, mock_islink, mock_uprocout): """Test04 ElfPatcher()._walk_fs().""" mock_path.return_value = "/some_contdir" mock_walk.return_value = [("/tmp", ["dir"], ["file"]), ] mock_islink.return_value = False mock_access.return_value = False mock_stat.return_value.st_uid = 1000 elfp = ElfPatcher(self.local, self.contid) elfp._uid = 0 status = elfp._walk_fs("cmd", "/tmp", elfp.ABORT_ON_ERROR) self.assertEqual(status, "") mock_path.return_value = "/some_contdir" mock_walk.return_value = [("/tmp", ["dir"], ["file"]), ] mock_islink.return_value = False mock_access.return_value = True mock_stat.return_value.st_uid = 0 mock_uprocout.return_value = "some output" elfp = ElfPatcher(self.local, self.contid) elfp._uid = 0 status = elfp._walk_fs("cmd", "/tmp", elfp.BIN) self.assertTrue(mock_uprocout.called) self.assertEqual(status, "some output")
def set_mode(self, xmode, force=False): """Set execution mode""" status = False prev_xmode = self.get_mode() elfpatcher = ElfPatcher(self.localrepo, self.container_id) filebind = FileBind(self.localrepo, self.container_id) futil_croot = FileUtil(self.container_orig_root) orig_path = futil_croot.getdata('r').strip() if xmode not in self.valid_modes: Msg().err("Error: invalid execmode:", xmode) return status if not (force or xmode != prev_xmode): return True if prev_xmode[0] in ('R', 'S') and xmode[0] != 'R': filebind.restore() if xmode[0] == 'F': if force or prev_xmode[0] in ("P", "R", "S"): futil_cont = FileUtil(self.container_root) status = (futil_cont.links_conv(force, True, orig_path) and elfpatcher.get_ld_libdirs(force)) if xmode in ('P1', 'P2', 'F1', 'R1', 'R2', 'R3', 'S1'): if prev_xmode in ('P1', 'P2', 'F1', 'R1', 'R2', 'R3', 'S1'): status = True elif force or prev_xmode in ("F2", "F3", "F4"): status = ((elfpatcher.restore_ld() or force) and elfpatcher.restore_binaries()) if xmode[0] == 'R': filebind.setup() elif xmode in ("F2", ): if force or prev_xmode in ("F3", "F4"): status = elfpatcher.restore_binaries() if force or prev_xmode in ('P1', 'P2', 'F1', 'R1', 'R2', 'R3', 'S1'): status = elfpatcher.patch_ld() elif xmode in ("F3", "F4"): if force or prev_xmode in ('P1', 'P2', 'F1', 'F2', 'R1', 'R2', 'R3', 'S1'): status = (elfpatcher.patch_ld() and elfpatcher.patch_binaries()) elif prev_xmode in ("F3", "F4"): status = True if xmode[0] in ("P", "R", "S"): if force or (status and prev_xmode.startswith("F")): futil = FileUtil(self.container_root) status = futil.links_conv(force, False, orig_path) if status or force: futil = FileUtil(self.container_execmode) status = futil.putdata(xmode, "w") if status or force: futil = FileUtil(self.container_orig_root) status = futil.putdata(os.path.realpath(self.container_root), "w") if (not status) and (not force): Msg().err("Error: container setup failed") return bool(status)
def test_10_get_patch_last_time(self, mock_getdata, mock_path): """Test10 ElfPatcher().patch_last_time().""" mock_getdata.return_value = "30" mock_path.return_value = "/some_contdir" elfp = ElfPatcher(self.local, self.contid) self.assertEqual(elfp.get_patch_last_time(), "30")