示例#1
0
    def test_file_exists(self, tmpdir):
        test_file = tmpdir.join("testfile.txt")
        test_file.write("foobar...")
        o = tmpdir.join("Dockerfile")
        input1 = tmpdir.join("Dockerfile1.jinja")
        input1.write(
            "File exists: {{ file_exists('/tmp/foobar/barfoo/raboof') }}")

        c = Core({
            "--dockerfile": str(input1),
            "--outfile": str(o),
        })
        c.main()
        assert o.read().startswith("File exists: False"), "{0}".format(
            o.read())

        input2 = tmpdir.join("Dockerfile.jinja")
        input2.write("File exists: {{ file_exists('%s') }}" % str(test_file))

        c = Core({
            "--dockerfile": str(input2),
            "--outfile": str(o),
        })
        c.main()

        assert o.read().startswith("File exists: True"), "{0}".format(o.read())
示例#2
0
 def test_fail_load_non_existing_datasource(self, tmpdir):
     """
     Prove a path to a datasource that do not exists and try to load it
     and look for exception to be raised.
     """
     input = tmpdir.join("Dockerfile.jinja")
     output = tmpdir.join("Dockerfile")
     c = Core({
         "--dockerfile": str(input),
         "--outfile": str(output),
         "--datasource": ["/tmp/foobar/barfoo"]
     })
     with pytest.raises(Exception) as ex:
         c.main()
     assert str(ex.value).startswith("Unable to load datasource file : /tmp/foobar/barfoo")
 def test_fail_load_non_existing_datasource(self, tmpdir):
     """
     Prove a path to a datasource that do not exists and try to load it
     and look for exception to be raised.
     """
     input = tmpdir.join("Dockerfile.jinja")
     output = tmpdir.join("Dockerfile")
     c = Core({
         "--dockerfile": str(input),
         "--outfile": str(output),
         "--datasource": ["/tmp/foobar/barfoo"]
     })
     with pytest.raises(Exception) as ex:
         c.main()
     assert str(ex.value).startswith(
         "Unable to load datasource file : /tmp/foobar/barfoo")
    def test_main(self, tmpdir):
        """
        Test that the main methos is callable and use as many things as possible
        to verify as much as possible works in the main flow.
        """
        # TODO: Add datasource support
        # TODO: Add default config data files
        input = tmpdir.join("Dockerfile.jinja")
        input.write("{{ myvar }}")
        o = tmpdir.join("Dockerfile")
        c = tmpdir.join("conf.json")
        c.write('{"env": {"myvar": "foobar"}}')

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(o),
            "--config": str(c),
        })

        c.main()
        assert o.read() == "foobar"
示例#5
0
    def test_main(self, tmpdir):
        """
        Test that the main methos is callable and use as many things as possible
        to verify as much as possible works in the main flow.
        """
        # TODO: Add datasource support
        # TODO: Add default config data files
        input = tmpdir.join("Dockerfile.jinja")
        input.write("{{ myvar }}")
        o = tmpdir.join("Dockerfile")
        c = tmpdir.join("conf.json")
        c.write('{"env": {"myvar": "foobar"}}')

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(o),
            "--config": str(c),
        })

        c.main()
        assert o.read() == "foobar"
    def test_load_datasource_import_error(self, tmpdir):
        """
        Provide a datasource file that will raise ImportError. Ensure log msg
        and that exception was raised.

        We fake the ImportError exception by manually raising it from inside
        the datasource file to make it consistent.
        """
        input = tmpdir.join("Dockerfile.jinja")
        input.write("foobar")
        output = tmpdir.join("Dockerfile")
        dsfile = tmpdir.join("_datasource_.py")
        dsfile.write("""
raise ImportError("foobar")
        """)
        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(output),
            "--datasource": [str(dsfile)]
        })
        with pytest.raises(ImportError):
            c.main()
示例#7
0
    def test_env_var_is(self, tmpdir):
        input = tmpdir.join("Dockerfile.jinja")
        input.write("Key is set: {{ env_var_is('_foobar_', 'barfoo') }}")
        o = tmpdir.join("Dockerfile")

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(o),
        })
        c.main()
        assert o.read().startswith("Key is set: False")

        try:
            # Add key to environment and method should pick it up
            os.environ["_foobar_"] = "barfoo"
            c.main()
            assert o.read().startswith("Key is set: True")
        except Exception:
            raise
        finally:
            # Guaranteed cleanup of environment variable
            del os.environ["_foobar_"]
示例#8
0
    def test_env_var_is(self, tmpdir):
        input = tmpdir.join("Dockerfile.jinja")
        input.write("Key is set: {{ env_var_is('_foobar_', 'barfoo') }}")
        o = tmpdir.join("Dockerfile")

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(o),
        })
        c.main()
        assert o.read().startswith("Key is set: False")

        try:
            # Add key to environment and method should pick it up
            os.environ["_foobar_"] = "barfoo"
            c.main()
            assert o.read().startswith("Key is set: True")
        except Exception:
            raise
        finally:
            # Guaranteed cleanup of environment variable
            del os.environ["_foobar_"]
示例#9
0
    def test_load_datasource_import_error(self, tmpdir):
        """
        Provide a datasource file that will raise ImportError. Ensure log msg
        and that exception was raised.

        We fake the ImportError exception by manually raising it from inside
        the datasource file to make it consistent.
        """
        input = tmpdir.join("Dockerfile.jinja")
        input.write("foobar")
        output = tmpdir.join("Dockerfile")
        dsfile = tmpdir.join("_datasource_.py")
        dsfile.write("""
raise ImportError("foobar")
        """)
        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(output),
            "--datasource": [str(dsfile)]
        })
        with pytest.raises(ImportError):
            c.main()
示例#10
0
    def test_handle_datasources(self, tmpdir):
        """
        Test that loading of datasources work and that they are usable.
        """
        input = tmpdir.join("Dockerfile.jinja")
        input.write("{{ 'foo'|upper }} : {{ lower('BAR') }}")

        output = tmpdir.join("Dockerfile")
        dsfile = tmpdir.join("_datasource.py")
        dsfile.write("""
def _filter_upper(string):
    return string.upper()

def _global_lower(string):
    return string.lower()
        """)

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(output),
            "--datasource": [str(dsfile)]
        })
        c.main()
        assert output.read() == "FOO : bar"
示例#11
0
    def test_file_exists(self, tmpdir):
        test_file = tmpdir.join("testfile.txt")
        test_file.write("foobar...")
        o = tmpdir.join("Dockerfile")
        input1 = tmpdir.join("Dockerfile1.jinja")
        input1.write("File exists: {{ file_exists('/tmp/foobar/barfoo/raboof') }}")

        c = Core({
            "--dockerfile": str(input1),
            "--outfile": str(o),
        })
        c.main()
        assert o.read().startswith("File exists: False"), "{0}".format(o.read())

        input2 = tmpdir.join("Dockerfile.jinja")
        input2.write("File exists: {{ file_exists('%s') }}" % str(test_file))

        c = Core({
            "--dockerfile": str(input2),
            "--outfile": str(o),
        })
        c.main()

        assert o.read().startswith("File exists: True"), "{0}".format(o.read())
示例#12
0
    def test_handle_datasources(self, tmpdir):
        """
        Test that loading of datasources work and that they are usable.
        """
        input = tmpdir.join("Dockerfile.jinja")
        input.write("{{ 'foo'|upper }} : {{ lower('BAR') }}")

        output = tmpdir.join("Dockerfile")
        dsfile = tmpdir.join("_datasource.py")
        dsfile.write("""
def _filter_upper(string):
    return string.upper()

def _global_lower(string):
    return string.lower()
        """)

        c = Core({
            "--dockerfile": str(input),
            "--outfile": str(output),
            "--datasource": [str(dsfile)]
        })
        c.main()
        assert output.read() == "FOO : bar"