示例#1
0
def test_backup(dbsession, ini_settings):
    """Execute backup script with having our settings content."""

    f = NamedTemporaryFile(delete=False)
    temp_fname = f.name
    f.close()

    ini_settings["websauna.backup_script"] = "websauna.tests:backup_script.bash"
    ini_settings["backup_test.filename"] = temp_fname

    # We have some scoping issues with the dbsession here, make sure we close transaction at the end of the test
    with transaction.manager:

        init = get_init(dict(__file__=ini_settings["_ini_file"]), ini_settings)
        init.run()

        testing.setUp(registry=init.config.registry)

        # Check we have faux AWS variable to export
        secrets = get_secrets(get_current_registry())
        assert "aws.access_key_id" in secrets

        try:

            # This will run the bash script above
            backup_site()

            # The result should be generated here
            assert os.path.exists(temp_fname)
            contents = io.open(temp_fname).read()

            # test-secrets.ini, AWS access key
            assert contents.strip() == "foo"
        finally:
            testing.tearDown()
示例#2
0
def create_settings_env(registry):
    """Create os.environ where have exported all settings and secrets.

    This is used for subprocess to create a child processes which are aware of our settings.  All dots are replaced with underscores. The exported environment varible is either prefixed with ``main``  or ``secret``. The exported environment variables are uppercased. Parent process environment variables are automatically inherited.

    The environment will look like::

        MAIN_websauna_SITE_ID=foo
        SECRETS_AWS_CONSUMER_KEY=baa

    You will also have::

        MAIN_SQL_HOST
        MAIN_SQL_DATABASE
        MAIN_SQL_USER
        MAIN_SQL_PASSWORD

    Integers, booleans and objects are exported as strings:

        MAIN_SOME_BOOLEAN=True
        MAIN_SOME_INTEGER=123

    """
    env = os.environ.copy()

    settings = registry.settings
    secrets = get_secrets(registry)

    # Export database credentials
    url = make_url(settings["sqlalchemy.url"])

    # Looks like both are acceptable options
    assert url.drivername in "postgresql", "postgres"

    env["MAIN_SQL_HOST"] =  url.host or ""
    env["MAIN_SQL_DATABASE"] = url.database or ""
    env["MAIN_SQL_USERNAME"] = url.username or ""
    env["MAIN_SQL_PASSWORD"] = url.password or ""

    for key, val in settings.items():
        key = "main.{}".format(key)
        key = key.replace(".", "_")
        env[key.upper()] = str(val)

    for key, val in secrets.items():
        key = "secret.{}".format(key)
        key = key.replace(".", "_")
        env[key.upper()] = str(val)

    return env
示例#3
0
def domain(mailgun, registry):
    """Outbound domain"""
    secrets = get_secrets(registry)
    return secrets["mailgun.domain"]