Skip to content

Commit

Permalink
15323 FIX mk_postgres: Adapt environment file parsing
Browse files Browse the repository at this point in the history
SUP-17190

Change-Id: I95477bcbef9cb8dc2f54d40b3c2dfe13560a8bda
  • Loading branch information
BenediktSeidl committed Aug 27, 2024
1 parent 477222d commit b1dcc0c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .werks/15323.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[//]: # (werk v2)
# mk_postgres: Adapt environment file parsing

key | value
---------- | ---
date | 2024-01-31T10:45:29+00:00
version | 2.4.0b1
class | fix
edition | cre
component | checks
level | 1
compatible | yes

Reading variables from the environment file was adapted:
Lines starting with `#` will now be ignored.
4 changes: 4 additions & 0 deletions agents/plugins/mk_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,14 +1209,18 @@ def parse_env_file(env_file):
pg_port = None # mandatory in env_file
pg_database = "postgres" # default value
pg_version = None

for line in open_env_file(env_file):
line = line.strip()
if not line or "=" not in line or line.startswith("#"):
continue
if "PGDATABASE=" in line:
pg_database = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip()
elif "PGPORT=" in line:
pg_port = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip()
elif "PGVERSION=" in line:
pg_version = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip()

if pg_port is None:
raise ValueError("PGPORT is not specified in %s" % env_file)
return pg_database, pg_port, pg_version
Expand Down
24 changes: 22 additions & 2 deletions tests/agent-plugin-unit/test_mk_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,27 @@ def test_parse_env_file_comments(tmp_path):
).encode("utf-8")
)
assert mk_postgres.parse_env_file(str(path)) == (
"ut_some_other_database", # this is a bug, should be ut_pg_database
"345", # this is a bug, should be 123
"ut_pg_database",
"123",
None,
)


def test_parse_env_file_parser(tmp_path):
path = tmp_path / ".env"
with open(str(path), "wb") as fo:
fo.write(
(
"# this is a comment\n"
" # t = is a comment\n"
"\t#\tt =s a comment\n"
"\n" # empty line
"export PGDATABASE=ut_pg_database\n"
"PGPORT=123\n"
).encode("utf-8")
)
assert mk_postgres.parse_env_file(str(path)) == (
"ut_pg_database",
"123",
None,
)

0 comments on commit b1dcc0c

Please sign in to comment.