[Solved] Different Default Settings in Tests, Stage and Production Systems

I use an external Database for some cases, e.g. collect values for Named Vocabularies. The connection values (host, user, etc... lives outside the addon in a secret.cfg as a part of buildout and differs between the enviroments (Test, Dev, Stage, Prod)

[instance]
environment-vars +=
    DB_HOST 127.0.0.1
    DB_NAME my_db_name
    DB_USER john_doe
    DB_PW 123xxx
# config.py
DB_SETTINGS = {
    "DB_HOST": os.environ.get("DB_HOST", None),
    "DB_USER": os.environ.get("DB_USER", None),
    "DB_PASSWORD": os.environ.get("DB_PW", None),
    "DB_NAME": os.environ.get("DB_NAME", None)
}

But in a Test the Enviroment Variable is not available:

bin/test -s my.addon -t DatabaseFunctionalTest
V1 = os.environ.get("DB_HOST", None)
print(V1) -> None

only this helps:

DB_HOST=127.0.0.1 bin/test -s my.addon -t DatabaseFunctionalTest
V1 = os.environ.get("DB_HOST", None)
print(V1) -> 127.0.0.1

How can i provide different configurations for different enviroments, e.g. Test, Dev, Stage, Prod ... without to write this pseudocode:

def get_config():
  if is_in_test:
     return config1

  if is_in_dev:
    return config2

  if is_in_stage:
    return config3

  if is_in_prod:
    return config4

You can have different buildout files for each environment (staging.cfg, production.cfg, testing.cfg) in which you can provide the different values for the environment variables.

Or you can provision your environment servers with different environment variable values.

the coredev buildout does this

[environment]
BUILDOUT_DIR = ${buildout:directory}
# This dir must exist.  Recent plone.recipe.zope2instance normally creates it:
CHAMELEON_CACHE = ${buildout:directory}/var/cache
ROBOTSUITE_LOGLEVEL = ERROR
ROBOTSUITE_APPEND_OUTPUT_XML = 1
zope_i18n_compile_mo_files = true

[test]
recipe = collective.xmltestreport
eggs = ${buildout:test-eggs}
defaults = ['--auto-color', '--auto-progress', '--ignore_dir=.git', '--ignore_dir=bower_components', '--ignore_dir=node_modules']
environment = environment

Maybe your buildout above set variables only on the instance?

1 Like

Yes i set this on instance. I will check the global definition in buildout.

my local.cfg:

[test]
initialization +=
    os.environ['DB_HOST'] = '127.0.0.1'
    os.environ['DB_USER'] =  'john_doe'
    os.environ['DB_PW'] = '123xxx'
    os.environ['DB_NAME'] = 'db_unittest'

Perfect, Thanks for help!

1 Like