

Extracting values from environment variables in tox
source link: https://schinckel.net/2020/06/02/extracting-values-from-environment-variables-in-tox/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Extracting values from environment variables in tox
Tox is a great tool for automated testing. We use it, not only to run matrix testing, but to run different types of tests in different environments, enabling us to parallelise our test runs, and get better reporting about what types of tests failed.
Recently, we started using Robot Framework for some automated UI testing. This needs to run a django server, and almost certainly wants to run against a different database. This will require our tox -e robot
to drop the database if it exists, and then create it.
Because we use dj-database-url to provide our database settings, our Codeship configuration contains an environment variable set to DATABASE_URL
. This contains the host, port and database name, as well as the username/password if applicable. However, we don’t have the database name (or port) directly available in their own environment variables.
Instead, I wanted to extract these out of the postgres://user:password@host:port/dbname
string.
My tox environment also needed to ensure that a distinct database was used for robot:
[testenv:robot]
setenv=
CELERY_ALWAYS_EAGER=True
DATABASE_URL={env:DATABASE_URL}_robot
PORT=55002
BROWSER=headlesschrome
whitelist_externals=
/bin/sh
commands=
sh -c 'dropdb --if-exists $(echo {env:DATABASE_URL} | cut -d "/" -f 4)'
sh -c 'createdb $(echo {env:DATABASE_URL} | cut -d "/" -f 4)'
coverage run --parallel-mode --branch manage.py robot --runserver={env:PORT}
And this was working great. I’m also using the $PG_USER
environment variable, which is supplied by Codeship, but that just clutters things up.
However, when merged to our main repo, which has it’s own codeship environment, tests were failing. It would complain about the database not being present when attempting to run the robot tests.
It seems that we were using a different version of postgres, and thus were using a different port.
So, how can we extract the port from the $DATABASE_URL
?
commands=
sh -c 'dropdb --if-exists \
-p $(echo {env:DATABASE_URL} | cut -d "/" -f 3 | cut -d ":" -f 3) \
$(echo {env:DATABASE_URL} | cut -d "/" -f 4)'
Which is all well and good, until you have a $DATABASE_URL
that omits the port…
dropdb: error: missing required argument database name
Ah, that would mean the command being executed was:
$ dropdb --if-exists -p <database-name>
Eventually, I came up with the following:
sh -c 'export PG_PORT=$(echo {env:DATABASE_URL} | cut -d "/" -f 3 | cut -d ":" -f 3); \
dropdb --if-exists \
-p $\{PG_PORT:-5432} \
$(echo {env:DATABASE_URL} | cut -d "/" -f 4)'
Whew, that is a mouthful!
We store the extracted value in a variable PG_PORT
, and then use bash variable substitution (rather than tox variable substitution) to put it in, with a default value. But because of tox variable substitution, we need to escape the curly brace to allow it to be passed through to bash: $\{PG_PORT:-5432}
. Also note that you’ll need a space after this before a line continuation, because bash seems to strip leading spaces from the continued line.
Recommend
-
149
Use Environment Variables for php.ini Settings in Docker Use Environment Variables for php.ini Settings in Docker...
-
134
What you need to know about environment variables with PHP...
-
147
GoDotEnv A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file). From the original Library: Storing configuration...
-
91
PHP dotenv Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.
-
10
Getting the most out of tox Fri, Dec 17, 2010tox is a recent Python testing tool, by Holger Krekel. It’s stated purpose is to make testing Python projects against multiple versions of...
-
6
Tox 是一个开源的实时通信协议,不需要中央服务器,提供多种跨平台的客户端。 一个简单介绍: https://www.coldl.com/1545.html。 1. Tox 的基本用法 目前 To...
-
14
PHP passing values to variables in included files advertisements I have a php file (article.php) with variables holding con...
-
10
Extracting unique values from a data image using R advertisements I have a data frame with multiple columns and I want to...
-
8
Predefine values of custom pipeline variables February 08, 2022 < 1 min read Share Recently, we
-
8
Details (Whiteboard: [fidefe-reusable-components-ms1])...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK