def test_jinja_template_advanced():
    """
    Ensure multi-Jinja sanity, as well as nested files.
    """
    with tmpdir() as tmp:
        jt1 = JinjaTemplate("tests/resources/templates/jinja1")
        jt2 = JinjaTemplate("tests/resources/templates/jinja2")

        context = {"foo": "foo1", "kruft": "kruft1", "plop": "plop1", "no": "no", "go": "go"}

        jt1.set_context(context)
        jt2.set_context(context)
        jt1.render(tmp)
        jt2.render(tmp)

        files = {
            "literal": "{{literal}}",
            "really/nested/directory/with/a/file/foo": context["foo"],
            "kruft": context["kruft"],
            "foo": context["foo"],
            "nogo": "%s %s" % (context["no"], context["go"]),
            "plop": context["plop"],
        }

        with cd(tmp):
            for f in files:
                assert_content(f, files[f])
Esempio n. 2
0
def test_more_walk():
    """
    Ensure s'more basics in walking a directory, with an extention
    filter.
    """
    with tmpdir() as t:
        with cd(t):
            mkdir("foo")
            mkdir("bar")
            mkdir("baz")

            valid_targets = ["./foo/bar.target", "./bar/foo.target", "./kruft.target"]

            invalid_targets = ["./foo/bar", "./baz/foo", "./kruft"]
            invalid_cmp = list(invalid_targets)

            for target in valid_targets + invalid_targets:
                touch(target)

            for entry in dir_walk("./", xtn="target"):
                if entry in valid_targets:
                    valid_targets.remove(entry)
                if entry in invalid_targets:
                    invalid_targets.remove(entry)

        assert valid_targets == []
        assert invalid_targets == invalid_cmp
def test_debian_shim_blank():
    """
    Ensure the DebianShim won't error when the directory doesn't have a
    debdir present.
    """
    with tmpdir() as tmp:
        ds = DebianShim()
        ds.render(tmp)
def test_jinja2_context():
    tpl = JinjaTemplate("tests/resources/templates/jinja1")
    with tmpdir() as tmp:
        try:
            tpl.render(tmp)
            assert True is False
        except ValueError:
            pass
Esempio n. 5
0
def test_hooks():
    """
    Test all the thingers.
    """
    ws = TestSuite(workspace)
    test = ws.get_test("hook-basics")
    with tmpdir() as tmp:
        test._run_hook("pre-build", tmp)
        assert os.path.exists("%s/pre-build" % (tmp))
Esempio n. 6
0
def test_file_diff_output():
    with tmpdir() as tmp:
        f = "%s/%s" % (tmp, "diff")
        fd = open(f, "w")
        assert not diff("tests/resources/util-diff/newf", "tests/resources/util-diff/orig", output_fd=fd)
        fd.close()

        cp1 = open(f, "r").read()
        cp2 = open("tests/resources/util-diff/diff", "r").read()
        assert cp1 == cp2
def test_plain_template_basic():
    """
    Ensure basic sanity with the PlainTemplate system.
    """
    with tmpdir() as tmp:
        pt1 = PlainTemplate("tests/resources/templates/plain1")
        pt1.render(tmp)
        with cd(tmp):
            for x in ["foo", "bar", "baz"]:
                assert_content(x, x)
Esempio n. 8
0
def test_string_diff_output():
    cmp1 = open("tests/resources/util-diff/orig", "r").read()
    with tmpdir() as tmp:
        f = "%s/%s" % (tmp, "diff.str")
        fd = open(f, "w")
        assert not diff_against_string("tests/resources/util-diff/newf", cmp1, output_fd=fd)
        fd.close()

        cp1 = open(f, "r").read()
        cp2 = open("tests/resources/util-diff/diff.str", "r").read()
        assert cp1 == cp2
Esempio n. 9
0
def test_render_nested():
    ws = TestSuite(workspace)
    test = ws.get_test("nested-thing")
    source, version = test.get_source_and_version()
    version = version['upstream']
    tm = test.get_template_stack()
    with tmpdir() as tmp:
        path = "%s/%s-%s" % (tmp, source, version)
        mkdir(path)
        tm.render(path)
        assert os.path.exists("%s/kruft" % (path))
Esempio n. 10
0
def test_todo():
    ws = TestSuite(workspace)
    test = ws.get_test("todo-test")
    source, version = test.get_source_and_version()
    version = version['upstream']
    tm = test.get_template_stack()
    with tmpdir() as tmp:
        path = "%s/%s-%s" % (tmp, source, version)
        mkdir(path)
        tm.render(path)
        ret = test.run()
        assert ret == {}
Esempio n. 11
0
def test_insane_compression_fails():
    pkgname = "pkgfoo"
    version = "2.0"
    debdir = "%s-%s" % (pkgname, version)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(debdir)
            try:
                make_orig_tarball(".", pkgname, version, compression="asfasf")
                assert True is False
            except ValueError:
                assert True is True
Esempio n. 12
0
def test_localdir_generation():
    pkgname = "pkgfoo"
    version = "2.0"
    debdir = "%s-%s" % (pkgname, version)
    compression = "bzip2"
    tarball = "%s_%s.orig.tar.bz2" % (pkgname, version)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(debdir)
            assert not os.path.exists(tarball)
            make_orig_tarball(".", pkgname, version, compression=compression)
            assert os.path.exists(tarball)
Esempio n. 13
0
def test_debian_shim_there():
    """
    Ensure the shim removes the Debian directory, if it exists, and it can be
    run over the same thing more then once.
    """
    with tmpdir() as tmp:
        debdir = "%s/debian" % (tmp)
        mkdir(debdir)
        assert os.path.exists(debdir)
        ds = DebianShim()
        ds.render(tmp)
        assert not os.path.exists(debdir)
        ds.render(tmp)
Esempio n. 14
0
def test_run_all_the_things():
    """
    Test all the thingers.
    """
    ws = TestSuite(workspace)
    test = ws.get_test("cruft-empty-diff")
    source, version = test.get_source_and_version()
    version = version['upstream']

    tm = test.get_template_stack()
    with tmpdir() as tmp:
        path = "%s/%s-%s" % (tmp, source, version)
        mkdir(path)
        tm.render(path)
Esempio n. 15
0
def test_templater():
    """
    Make sure we can render out templates correctly
    """
    ws = TestSuite(workspace)
    test = ws.get_test("cruft-empty-diff")
    source, version = test.get_source_and_version()
    version = version['upstream']

    tm = test.get_template_stack()
    with tmpdir() as tmp:
        path = "%s/%s-%s" % (tmp, source, version)
        mkdir(path)
        tm.render(path)
Esempio n. 16
0
def test_upstream_shim():
    """
    Ensure we create a debian tarball thinger.
    """
    pkgname = "testpkg-name"
    version = "1.0"
    with tmpdir() as tmp:
        with cd(tmp):
            mkdir("%s-%s" % (pkgname, version))
        tmp += "/%s-%s" % (pkgname, version)
        uss = UpstreamShim(pkgname, version)
        uss.set_compression("gzip")
        uss.render(tmp)
        assert "%s/%s_%s.orig.tar.gz" % (tmp, pkgname, version)
        assert "%s/%s-%s" % (tmp, pkgname, version)
Esempio n. 17
0
def test_plain_template_advanced():
    """
    Ensure sanity when two PlainTemplates are rendered together,
    along with a directory.
    """
    with tmpdir() as tmp:
        pt1 = PlainTemplate("tests/resources/templates/plain1")
        pt2 = PlainTemplate("tests/resources/templates/plain2")
        pt1.render(tmp)
        pt2.render(tmp)

        values = {"foo": "foo", "bar": "not bar", "baz": "baz", "kruft/flip": "flip"}

        with cd(tmp):
            for entry in values:
                content = values[entry]
                assert_content(entry, content)
Esempio n. 18
0
def test_debian_shim_there_two():
    """
    Ensure the Debian shim won't remove files other then the Debian/
    directory.
    """
    with tmpdir() as tmp:
        debdir = "%s/debian" % (tmp)
        testfd = "%s/foo" % (tmp)
        mkdir(debdir)
        open(testfd, "w").close()
        assert os.path.exists(debdir)
        assert os.path.exists(testfd)
        ds = DebianShim()
        ds.render(tmp)
        assert not os.path.exists(debdir)
        assert os.path.exists(testfd)
        ds.render(tmp)
Esempio n. 19
0
def test_template_manager():
    """
    Make sure the template manager basics work right.
    """
    template_dir = abspath("./tests/resources/templates/")
    templs = {"plain1": "PlainTemplate", "jinja1": "JinjaTemplate", "jinja2": "JinjaTemplate"}
    context = {"foo": "foo"}
    with tmpdir() as tmp:
        tplm = TemplateManager()
        for template in templs:
            kwargs = {}
            klasse = templs[template]
            if klasse == "JinjaTemplate":
                kwargs["context"] = context

            tplm.add_template(klasse, os.path.join(template_dir, template), **kwargs)

        tplm.render(tmp)
Esempio n. 20
0
def test_jinja_template_basic():
    """
    Ensure basic Jinja sanity.
    """
    with tmpdir() as tmp:
        jt1 = JinjaTemplate("tests/resources/templates/jinja1")

        context = {"foo": "foo1", "kruft": "kruft1", "plop": "plop1"}

        jt1.set_context(context)
        jt1.render(tmp)

        with cd(tmp):
            for x in context:
                if not os.path.exists(x):
                    assert os.path.exists(x)

            for entry in context:
                assert_content(entry, context[entry])
Esempio n. 21
0
def test_basic_walk():
    """
    Test to make sure we can walk a directory.
    """
    with tmpdir() as t:
        with cd(t):
            mkdir("foo")
            mkdir("bar")
            mkdir("baz")

            targets = ["./foo/bar.target", "./foo/bar", "./bar/foo.target", "./baz/foo"]

            for target in targets:
                touch(target)

            for entry in dir_walk("./"):
                assert entry in targets
                targets.remove(entry)

        assert targets == []
Esempio n. 22
0
def check_tarball(resdir, tfilter=None, rootmod=None):
    test_res = os.path.join(resources, resdir)
    man = parse_manifest(os.path.join(test_res, "manifest"))
    tarroot = os.path.join(test_res, "root")
    if tfilter is None:
        tfilter = _tar_filter

    with tmpdir() as staging:
        if rootmod is not None:
            newroot = os.path.join(staging, "root")
            rsync(tarroot, newroot)
            tarroot = newroot
            rootmod(tarroot)
        tname = os.path.join(staging, "test.tar.gz")
        tf = tarfile.open(tname, mode="w:gz")
        tf.add(tarroot, arcname=".", filter=tfilter)

        tf.close()
        with open_compressed_tarball(tname) as tar:
            man.check_tarball(tar)
Esempio n. 23
0
    def render(self, dest):
        """
        `dest' is a path (abs or rel) to the output directory, or where to
        write the model files to. Be extra-sure to call setContext on this
        particular class, it's important to render out the Jinja context
        correctly.
        """
        dest = abspath(dest)

        if self.context is None:
            raise ValueError("No context set for this JinjaTemplate")

        with tmpdir() as tmp:
            PlainTemplate.render(self, tmp)
            for template in dir_walk(tmp, xtn=".tpl"):
                with open(template, 'r') as fd:
                    tobj = Template(fd.read())
                    with open(template.rsplit(".", 1)[0], 'w') as obj:
                        obj.write(tobj.render(**self.context))
                rm(template)
            rsync(tmp, dest)
Esempio n. 24
0
def make_and_check_tarball(testname, rundir, upname, upversion, compression,
                           visitor, visit_open=True):
    """Create, check and clean up a tarball (test utility)

    Utility for setting up a dir, compile a tarball from a resource path,
    opening the tarball and passing it to vistor.

    testname is used to create a unique name for the test.  The
    compression is also used for this purpose, so multiple tests can
    share the same "testname" as long as the compression differs.

    rundir, upname, upversion and compression are passed (as is) to
    make_orig_tarball.

    The tarball passed to visitor may not be seekable and should be
    checked inorder.
    """

    testdir = "%s-%s" % (testname, compression)
    xtn = compression
    if xtn == "gzip":
        xtn = "gz"
    elif xtn == "bzip2":
        xtn = "bz2"

    rundir = abspath(rundir)

    with tmpdir() as tmp:
        with cd(tmp):
            mkdir(testdir)

            make_orig_tarball(rundir, upname, upversion,
                              compression=compression, outputdir=testdir)
            tarname = "%s_%s.orig.tar.%s" % (upname, upversion, xtn)
            path = os.path.join(testdir, tarname)
            if visit_open:
                with open_compressed_tarball(path, compression) as tar:
                    visitor(tar)
            else:
                visitor(path)
Esempio n. 25
0
    def run(self, verbose=False):
        templates = self._context['templates'][:]
        templates.reverse()

        for template in templates:
            template = "%s.json" % (template)

            try:
                context = self._workspace._look_up('contexts', template)
                context = load_config(context)
                self.set_global_context(context)
            except NoSuchCallableError as e:
                pass

        self._run_hook("init")

        if "todo" in self._context:
            return {}

        source, version = self.get_source_and_version()
        version = version['upstream']
        tm = self.get_template_stack()
        with tmpdir() as tmp:
            self.path = "%s/%s-%s" % (tmp, source, version)
            path = self.path
            mkdir(path)
            self._run_hook("tmpdir", path=tmp)
            tm.render(path)

            self._run_hook("pre-build", path=path)
            self._run_builds()
            self._run_hook("post-build", path=path)

            self._run_hook("pre-check", path=path)
            self._run_checks()
            self._run_hook("post-check", path=path)

            results = {}
            for checker in self._context['checkers']:
                pristine = "%s/%s" % (self._test_path, checker)
                output = "%s/%s" % (tmp, checker)
                if os.path.exists(pristine):
                    # OK, let's verify
                    if os.path.exists(output):
                        with open("/dev/null", "w") as null:
                            if diff(pristine, output,
                                    output_fd=null):
                                results[checker] = "passed"
                            else:
                                results[checker] = "failed"
                                if verbose:
                                    print "================================"
                                    print "Checker match failure:"
                                    print "  -> %s" % (self.name)
                                    print "  -> %s" % (checker)
                                    print "--------------------------------"
                                    diff(pristine, output)
                                    print "================================"
                    else:
                        results[checker] = "no-output"
                else:
                    results[checker] = "no-pristine"
            self._run_hook("finally", path=path)
        return results