예제 #1
0
    def test_02_list__multi_generated_file(self):
        listfile = os.path.join(self.workdir, "file.list")
        listfile2 = os.path.join(self.workdir, "file2.list")
        config = init_config(listfile)

        line = "%s/file*.list\n" % self.workdir
        open(listfile, "w").write(line)
        open(listfile2, "w").write(line)

        collector = FilelistCollector(listfile, config)
        fos = collector.list(listfile)
        fos_ref = [Factory.create(listfile), Factory.create(listfile2)]

        self.assertEquals(sorted(fos), sorted(fos_ref))
예제 #2
0
    def test_copy__exists(self):
        fo = Factory.create(self.testfile1)
        dest = self.testfile1 + ".2"

        FileOps.copy(fo, dest)

        self.assertFalse(FileOps.copy(fo, dest))
예제 #3
0
    def test_update(self):
        modifier = RpmAttributeModifier()

        f = F.create("/bin/bash")
        new_f = modifier.update(f)

        self.assertTrue(getattr(new_f, "rpm_attr", False))
예제 #4
0
    def test_pred__not_exist(self):
        filter = NotExistFilter()
        path = "/bin/sh"

        assert os.path.exists(path)

        fo = Factory.create(path)
        self.assertFalse(filter.pred(fo))
예제 #5
0
    def test_copy_impl_and_remove(self):
        fo = Factory.create(self.testfile1)
        dest = self.testfile1 + ".2"

        FileOps.copy_impl(fo, dest)
        self.assertTrue(os.path.exists(dest))

        FileOps.remove(dest)
        self.assertFalse(os.path.exists(dest))
예제 #6
0
    def test__create_wo_attrs(self):
        fo = Factory.create(self.path)

        self.assertTrue(isinstance(fo, FO.DirObject))
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
        self.assertEquals(fo.path, self.path)
예제 #7
0
    def test_pred__unsupported__fileobjects(self):
        username = pwd.getpwuid(os.getuid()).pw_name
        paths = ["/dev/null", "/dev/zero"] + glob.glob("/tmp/orbit-%s/linc-*" % username)[:3]  # device files  # socket

        paths = [p for p in paths if os.path.exists(p)]

        for p in paths:
            fo = Factory.create(p)
            self.assertTrue(self.filter.pred(fo))
예제 #8
0
    def test_pred__permitted_to_read(self):
        filter = ReadAccessFilter()
        pred = lambda f: os.path.exists(f) and os.access(f, os.R_OK)

        path = random.choice([p for p in ("/etc/hosts", "/etc/resolv.conf",
                                          "/etc/sysctl.conf")
                              if pred(p)])

        fo = Factory.create(path)
        self.assertFalse(filter.pred(fo))
예제 #9
0
    def test__create_w_filetype_symlink(self):
        filetype = FO.typestr_to_type("symlink")
        fo = Factory.create(self.path, filetype=filetype)

        self.assertTrue(isinstance(fo, FO.SymlinkObject))
        self.assertEquals(fo.type(), G.TYPE_SYMLINK)
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
예제 #10
0
    def test__create_w_content(self):
        content = "Hello, world\n"
        fo = Factory.create(self.path, content=content)

        self.assertTrue(isinstance(fo, FO.FileObject))
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
        self.assertEquals(fo.content, content)
예제 #11
0
    def test__create_w_src(self):
        src = "/etc/hosts"
        fo = Factory.create(self.path, src=src)

        self.assertTrue(isinstance(fo, FO.FileObject))
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
        self.assertEquals(fo.src, src)
예제 #12
0
    def test__create_w_linkto(self):
        linkto = "/etc/hosts"
        fo = Factory.create(self.path, linkto=linkto)

        self.assertTrue(isinstance(fo, FO.SymlinkObject))
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
        self.assertEquals(fo.linkto, linkto)
예제 #13
0
    def test__create_w_filetype_dir(self):
        filetype = FO.typestr_to_type("dir")
        fo = Factory.create(self.path, filetype=filetype)

        self.assertTrue(isinstance(fo, FO.DirObject))
        self.assertEquals(fo.type(), G.TYPE_DIR)
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
예제 #14
0
    def test_pred__supported__fileobjects(self):
        paths = [
            "/etc/resolv.conf", "/etc/hosts",  # normal files
            "/etc",  # dirs
            "/etc/rc",  "/etc/init.d", "/etc/grub.conf"  # symlinks
        ]
        paths = [p for p in paths if os.path.exists(p)]

        for p in paths:
            fo = Factory.create(p)
            self.assertFalse(self.filter.pred(fo))
예제 #15
0
    def _parse(self, bobj):
        """
        :param bobj: A Bunch object holds path and attrs (metadata) of files.
        """
        path = bobj.get("path", False)

        if not path or path.startswith("#"):
            return []
        else:
            paths = "*" in path and glob.glob(path) or [path]
            attrs = bobj.get("attrs", dict())

            return [Factory.create(p, self.use_rpmdb, **attrs) for p in paths]
예제 #16
0
    def test_02__parse__single_virtual_file(self):
        listfile = "/a/b/c/path.conf"
        config = init_config(listfile)

        line = " %s,create=1,content=\"generated file\" \n" % listfile

        collector = FilelistCollector(listfile, config)
        fos = collector._parse(line)
        fos_ref = [Factory.create(listfile, False,
                                  create=1, content="generated file")]

        self.assertNotEquals(fos, [])
        self.assertEquals(fos, fos_ref)
예제 #17
0
    def test_03_list__multi_real_files(self):
        listfile = os.path.join(self.workdir, "file.list")
        config = init_config(listfile)

        open(listfile, "w").write("\n".join(PATHS))

        collector = FilelistCollector(listfile, config)

        ur = not config.no_rpmdb
        fos = collector.list(listfile)
        fos_ref = sorted(
            Factory.create(p, use_rpmdb=ur) for p in PATHS_EXPANDED
        )

        self.assertEquals(sorted(fos), fos_ref)
예제 #18
0
    def test_08_collect__single_real_file__ignore_owner_mod(self):
        listfile = path = os.path.join(self.workdir, "file.list")

        config = init_config(listfile)
        config.ignore_owner = True

        open(listfile, "w").write(path + "\n")

        collector = FilelistCollector(listfile, config)

        fos = collector.collect()
        fo_ref = Factory.create(path, False)

        self.assertEquals(fos[0].uid, 0)
        self.assertEquals(fos[0].gid, 0)
예제 #19
0
    def test_04_collect__single_real_file__no_rpms_own(self):
        path = random.choice(SYSTEM_FILES_EXIST_AND_NO_RPMS_OWN)

        listfile = os.path.join(self.workdir, "file.list")
        config = init_config(listfile)

        open(listfile, "w").write(path + "\n")

        collector = FilelistCollector(listfile, config)

        fos = collector.collect()
        fo_ref = Factory.create(path, use_rpmdb=(not config.no_rpmdb))

        self.assertEquals(fos[0].path, path)
        self.assertEquals(fos, [fo_ref])
예제 #20
0
    def _parse(self, line):
        """Parse the line and returns FileObjects list generator.
        """
        # remove extra white spaces at the top and the end.
        line = line.rstrip().strip()

        if not line or line.startswith("#"):
            return []
        else:
            try:
                (paths, attrs) = P.parse_line_of_filelist(line)
            except ValueError:
                print "line=" + line
                raise

            return [Factory.create(p, self.use_rpmdb, **attrs) for p in paths]
예제 #21
0
    def test_07_collect__single_real_file__destdir_mod(self):
        listfile = path = os.path.join(self.workdir, "file.list")

        config = init_config(listfile)
        config.destdir = self.workdir

        open(listfile, "w").write(path + "\n")

        collector = FilelistCollector(listfile, config)

        fos = collector.collect()
        fo_ref = Factory.create(path, False)

        self.assertEquals(
            os.path.join(self.workdir, fos[0].path),
            fo_ref.path
        )
예제 #22
0
    def test_09_collect__single_real_file__rpmattr(self):
        path = random.choice(["/etc/hosts", "/etc/services"])

        listfile = os.path.join(self.workdir, "file.list")
        config = init_config(listfile)
        config.driver = "autotools.single.rpm"

        open(listfile, "w").write(path + "\n")

        collector = FilelistCollector(listfile, config)

        fos = collector.collect()
        fo_ref = Factory.create(path, False)

        ## should differ as RpmConflictsFilter works.
        #self.assertEquals(fos, [fo_ref])
        self.assertTrue("rpm_attr" in fos[0])
예제 #23
0
    def test_01_list_and_collect__json(self):
        if E.json is None:
            return

        listfile = os.path.join(self.workdir, "filelist.json")
        data = dict(files=[dict(path=p) for p in PATHS])

        import json
        json.dump(data, open(listfile, "w"))

        config = init_config(listfile)

        fos_ref = sorted(
            f for f in (Factory.create(p, False) for p in PATHS_EXPANDED) \
        )

        ac = AnyFilelistCollector(listfile, config, Anycfg.CTYPE_JSON)
        fos = ac.list(listfile)
        paths_in_fos = sorted(f.path for f in fos)

        self.assertEquals(paths_in_fos, sorted(f.path for f in fos_ref))

        class UserDirFilter(Filters.BaseFilter):
            _reason = "file is under \"/usr\""

            def _pred(self, f):
                return f.path.startswith("/usr")

        filters = [
            Filters.UnsupportedTypesFilter(),
            Filters.NotExistFilter(),
            Filters.ReadAccessFilter(),
            UserDirFilter(),
        ]
        fos_ref_filtered = [
            f for f in fos_ref if not any(filter(f) for filter in filters)
        ]

        ac2 = AnyFilelistCollector(listfile, config, Anycfg.CTYPE_JSON)
        ac2.filters += filters
        fos_filtered = ac2.collect()

        self.assertEquals(
            sorted(f.path for f in fos_filtered),
            sorted(f.path for f in fos_ref_filtered)
        )
예제 #24
0
    def test_pred__not_permitted_to_read_or_to_be_created(self):
        if os.getuid() == 0:
            print >> sys.stderr, "You look root and cannot test this. Skipped"
            return

        pred = lambda f: os.path.exists(f) and not os.access(f, os.R_OK)

        filter = ReadAccessFilter()
        path = random.choice([p for p in ("/etc/at.deny", "/etc/securetty",
                                          "/etc/sudoer", "/etc/shadow",
                                          "/etc/grub.conf")
                              if pred(p)])

        fo = Factory.create(path)
        self.assertTrue(filter.pred(fo))

        fo.create = True
        self.assertFalse(filter.pred(fo))
예제 #25
0
    def test_04_collect__single_real_file_rpm_owns(self):
        """test_04_collect__single_real_file_rpm_owns: FIXME"""
        return

        path = random.choice(
            ["/etc/hosts", "/etc/services", "/etc/bashrc", "/etc/passwd"]
        )

        listfile = os.path.join(self.workdir, "file.list")
        config = init_config(listfile)

        open(listfile, "w").write(path + "\n")

        collector = FilelistCollector(listfile, config)

        fos = collector.collect()
        fo_ref = Factory.create(path, use_rpmdb=(not config.no_rpmdb))

        self.assertEquals(fos[0].path, path)
        self.assertEquals(fos, [fo_ref])
예제 #26
0
    def test_15_collect__multi_real_files(self):
        """test_15_collect__multi_real_files: FIXME"""
        return  # modifiers also needed to create reference data.

        listfile = os.path.join(self.workdir, "file.list")
        config = init_config(listfile)

        open(listfile, "w").write("\n".join(PATHS))

        collector = FilelistCollector(listfile, config)
        fos = collector.collect()
        filters = [
            Filters.UnsupportedTypesFilter(),
            Filters.NotExistFilter(),
            Filters.ReadAccessFilter(),
        ]
        fos_ref = sorted(
            Factory.create(p, False) for p in PATHS_EXPANDED
        )
        fos_ref = [
            f for f in fos_ref if not any(filter(f) for filter in filters)
        ]

        self.assertEquals(sorted(fos), fos_ref)
예제 #27
0
    def test_02_update(self):
        f = F.create("/bin/bash")
        new_f = self.modifier.update(f)

        self.assertNotEquals(new_f.original_path, f.install_path)
예제 #28
0
    def test_pred__not_exist(self):
        filter = NotExistFilter()
        path = "/a/b/c"

        fo = Factory.create(path)
        self.assertTrue(filter.pred(fo))
예제 #29
0
    def test_copy__exists__force(self):
        fo = Factory.create(self.testfile1)
        dest = self.testfile1 + ".2"

        self.assertTrue(FileOps.copy(fo, dest, force=True))
예제 #30
0
    def test_copy__mkdir(self):
        fo = Factory.create(self.testfile1)
        dest = os.path.join(self.workdir, "t", "test2.txt")

        self.assertTrue(FileOps.copy(fo, dest))