-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1004 from lisongmin/substitution-with-service-env…
…ironment Substitution with service environment
- Loading branch information
Showing
4 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add ability to substitute variables with the environment of the service. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
version: '3' | ||
version: "3" | ||
|
||
services: | ||
env-test: | ||
image: busybox | ||
command: sh -c "export | grep ZZ" | ||
environment: | ||
- ZZVAR1=myval1 | ||
|
||
ZZVAR1: myval1 | ||
ZZVAR2: 2-$ZZVAR1 | ||
ZZVAR3: 3-$ZZVAR2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# SPDX-License-Identifier: GPL-2.0 | ||
# pylint: disable=protected-access | ||
|
||
import unittest | ||
|
||
from parameterized import parameterized | ||
|
||
from podman_compose import rec_subs | ||
|
||
|
||
class TestRecSubs(unittest.TestCase): | ||
substitutions = [ | ||
# dict with environment variables | ||
( | ||
"service's environment is low priority", | ||
{"environment": {"v1": "low priority", "actual-v1": "$v1"}}, | ||
{"environment": {"v1": "low priority", "actual-v1": "high priority"}}, | ||
), | ||
( | ||
"service's environment can be used in other values", | ||
{"environment": {"v100": "v1.0.0", "image": "abc:$v100"}}, | ||
{"environment": {"v100": "v1.0.0", "image": "abc:v1.0.0"}}, | ||
), | ||
( | ||
"Non-variable should not be substituted", | ||
{"environment": {"non_var": "$$v1", "vx": "$non_var"}, "image": "abc:$non_var"}, | ||
{"environment": {"non_var": "$v1", "vx": "$v1"}, "image": "abc:$v1"}, | ||
), | ||
# list | ||
( | ||
"Values in list are substituted", | ||
["$v1", "low priority"], | ||
["high priority", "low priority"], | ||
), | ||
# str | ||
( | ||
"Value with ${VARIABLE} format", | ||
"${v1}", | ||
"high priority", | ||
), | ||
( | ||
"Value with ${VARIABLE:-default} format", | ||
["${v1:-default}", "${empty:-default}", "${not_exits:-default}"], | ||
["high priority", "default", "default"], | ||
), | ||
( | ||
"Value with ${VARIABLE-default} format", | ||
["${v1-default}", "${empty-default}", "${not_exits-default}"], | ||
["high priority", "", "default"], | ||
), | ||
( | ||
"Value $$ means $", | ||
"$$v1", | ||
"$v1", | ||
), | ||
] | ||
|
||
@parameterized.expand(substitutions) | ||
def test_rec_subs(self, desc, input, expected): | ||
sub_dict = {"v1": "high priority", "empty": ""} | ||
result = rec_subs(input, sub_dict) | ||
self.assertEqual(result, expected, msg=desc) |