2018-01-07 15:43:03 +00:00
|
|
|
"""
|
2018-01-09 12:24:58 +00:00
|
|
|
Test that environment variables may be defined
|
2018-01-07 15:43:03 +00:00
|
|
|
"""
|
2024-02-05 20:28:56 +00:00
|
|
|
|
2018-01-07 15:43:03 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2021-02-17 11:08:43 +00:00
|
|
|
import sys
|
2018-01-07 15:43:03 +00:00
|
|
|
import tempfile
|
|
|
|
import time
|
2019-09-08 09:54:22 +00:00
|
|
|
from getpass import getuser
|
2018-01-07 15:43:03 +00:00
|
|
|
|
2019-05-31 09:10:17 +00:00
|
|
|
|
2021-02-17 11:08:43 +00:00
|
|
|
def test_env(capfd):
|
2018-01-07 15:43:03 +00:00
|
|
|
"""
|
|
|
|
Validate that you can define environment variables
|
2020-04-19 22:58:16 +00:00
|
|
|
|
|
|
|
See https://gist.github.com/hwine/9f5b02c894427324fafcf12f772b27b7
|
|
|
|
for how docker handles its -e & --env argument values
|
2018-01-07 15:43:03 +00:00
|
|
|
"""
|
|
|
|
ts = str(time.time())
|
2020-04-23 04:33:13 +00:00
|
|
|
# There appear to be some odd combinations of default dir that do
|
|
|
|
# not work on macOS Catalina with Docker CE 2.2.0.5, so use
|
|
|
|
# the current dir -- it'll be deleted immediately
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory(dir=os.path.abspath(os.curdir)) as tmpdir:
|
2019-09-08 09:54:22 +00:00
|
|
|
username = getuser()
|
2020-04-18 14:45:35 +00:00
|
|
|
os.environ["SPAM"] = "eggs"
|
2020-04-19 22:58:16 +00:00
|
|
|
os.environ["SPAM_2"] = "ham"
|
2020-04-19 15:53:01 +00:00
|
|
|
result = subprocess.run(
|
2019-05-31 09:10:17 +00:00
|
|
|
[
|
|
|
|
"repo2docker",
|
2020-04-19 22:58:16 +00:00
|
|
|
# 'key=value' are exported as is in docker
|
2019-05-31 09:10:17 +00:00
|
|
|
"-e",
|
2022-10-23 16:09:11 +00:00
|
|
|
f"FOO={ts}",
|
2019-05-31 09:10:17 +00:00
|
|
|
"--env",
|
|
|
|
"BAR=baz",
|
2020-04-19 22:58:16 +00:00
|
|
|
# 'key' is exported with the currently exported value
|
2020-04-18 14:45:35 +00:00
|
|
|
"--env",
|
|
|
|
"SPAM",
|
2020-04-19 22:58:16 +00:00
|
|
|
# 'key' is not exported if it is not exported.
|
|
|
|
"-e",
|
|
|
|
"NO_SPAM",
|
|
|
|
# 'key=' is exported in docker with an empty string as
|
|
|
|
# value
|
|
|
|
"--env",
|
|
|
|
"SPAM_2=",
|
2019-05-31 09:10:17 +00:00
|
|
|
tmpdir,
|
2021-02-17 11:08:43 +00:00
|
|
|
"--",
|
2019-05-31 09:10:17 +00:00
|
|
|
"/bin/bash",
|
|
|
|
"-c",
|
2020-04-19 15:53:01 +00:00
|
|
|
# Docker exports all passed env variables, so we can
|
2020-04-19 22:58:16 +00:00
|
|
|
# just look at exported variables.
|
2021-02-17 11:08:43 +00:00
|
|
|
"export",
|
2020-04-19 15:53:01 +00:00
|
|
|
],
|
2019-05-31 09:10:17 +00:00
|
|
|
)
|
2021-02-17 11:08:43 +00:00
|
|
|
captured = capfd.readouterr()
|
|
|
|
print(captured.out, end="")
|
|
|
|
print(captured.err, file=sys.stderr, end="")
|
|
|
|
|
2020-04-19 15:53:01 +00:00
|
|
|
assert result.returncode == 0
|
2020-04-19 22:58:16 +00:00
|
|
|
|
2020-04-19 15:53:01 +00:00
|
|
|
# all docker output is returned by repo2docker on stderr
|
|
|
|
# extract just the declare for better failure message formatting
|
2020-11-26 13:53:31 +00:00
|
|
|
# stdout should be empty
|
|
|
|
assert not result.stdout
|
|
|
|
|
|
|
|
# stderr should contain lines of output
|
2021-02-17 11:08:43 +00:00
|
|
|
declares = [x for x in captured.err.splitlines() if x.startswith("declare")]
|
2022-10-23 16:09:11 +00:00
|
|
|
assert f'declare -x FOO="{ts}"' in declares
|
2020-04-19 15:53:01 +00:00
|
|
|
assert 'declare -x BAR="baz"' in declares
|
|
|
|
assert 'declare -x SPAM="eggs"' in declares
|
2020-04-19 22:58:16 +00:00
|
|
|
assert "declare -x NO_SPAM" not in declares
|
|
|
|
assert 'declare -x SPAM_2=""' in declares
|