Skip to content

Commit 8c77acb

Browse files
authored
Merge pull request #6030 from rebrowning/fix/4676_inherit_env
Fix #4676
2 parents e9430eb + 4853721 commit 8c77acb

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Fixed
2525

2626
* Fix codecov failures for stackstorm/st2 tests. #6035, #6046, #6048
2727

28+
* Fix #4676, edge case where --inherit-env is skipped if the action has no parameters
29+
2830
* Update cryptography 3.4.7 -> 39.0.1, pyOpenSSL 21.0.0 -> 23.1.0, paramiko 2.10.5 -> 2.11.0 (security). #6055
2931

3032
* Bumped `eventlet` to `0.33.3` and `gunicorn` to `21.2.0` to fix `RecursionError` bug in setting `SSLContext` `minimum_version` property. #6061

st2client/st2client/commands/action.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,9 @@ def normalize(name, value, action_params=None, auto_dict=False):
945945

946946
result = {}
947947

948+
if args.inherit_env:
949+
result["env"] = self._get_inherited_env_vars()
950+
948951
if not args.parameters:
949952
return result
950953

@@ -1008,9 +1011,6 @@ def normalize(name, value, action_params=None, auto_dict=False):
10081011

10091012
del result["_file_name"]
10101013

1011-
if args.inherit_env:
1012-
result["env"] = self._get_inherited_env_vars()
1013-
10141014
return result
10151015

10161016
@add_auth_token_to_kwargs_from_cli

st2client/tests/unit/test_command_actionrun.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,135 @@ def test_get_params_from_args_with_multiple_declarations(self):
261261

262262
# set auto_dict back to default
263263
mockarg.auto_dict = False
264+
265+
def test_correctly_process_inherit_env_when_no_parameters_set(self):
266+
"""test_correctly_process_inherit_env_when_no_parameters_set
267+
268+
This tests that we still correctly pass through the environment variables
269+
when --inherit-env is set and we run an action that does not have parameters
270+
"""
271+
272+
runner = RunnerType()
273+
runner.runner_parameters = {}
274+
275+
action = Action()
276+
action.ref = "test.action"
277+
278+
subparser = mock.Mock()
279+
command = ActionRunCommand(action, self, subparser, name="test")
280+
281+
mockarg = mock.Mock()
282+
mockarg.inherit_env = True
283+
mockarg.auto_dict = True
284+
mockarg.parameters = []
285+
286+
k1 = "key1"
287+
v1 = "value1"
288+
k2 = "key2"
289+
v2 = "value2"
290+
291+
with mock.patch("os.environ.copy") as mockCopy:
292+
mockCopy.return_value = {k1: v1, k2: v2}
293+
param = command._get_action_parameters_from_args(
294+
action=action, runner=runner, args=mockarg
295+
)
296+
297+
self.assertIn("env", param)
298+
299+
env_params = param["env"]
300+
self.assertIn(k1, env_params)
301+
self.assertIn(k2, env_params)
302+
self.assertEqual(v1, env_params[k1])
303+
self.assertEqual(v2, env_params[k2])
304+
305+
def test_correctly_process_inherit_env_when_parameters_set(self):
306+
"""test_correctly_process_inherit_env_when_parameters_set
307+
308+
This tests that we still correctly pass through the environment variables
309+
when --inherit-env is set and we run an action that has action parameters set
310+
"""
311+
312+
runner = RunnerType()
313+
runner.runner_parameters = {}
314+
315+
action = Action()
316+
action.ref = "test.action"
317+
action.parameters = {
318+
"param_string": {"type": "string"},
319+
"param_array": {"type": "array"},
320+
"param_array_of_dicts": {"type": "array"},
321+
}
322+
323+
subparser = mock.Mock()
324+
command = ActionRunCommand(action, self, subparser, name="test")
325+
326+
p_string = "param_string"
327+
p_array = "param_array"
328+
p_ra_dicts = "param_array_of_dicts"
329+
mockarg = mock.Mock()
330+
mockarg.inherit_env = True
331+
mockarg.auto_dict = True
332+
mockarg.parameters = [
333+
f"{p_string}=hoge",
334+
f"{p_array}=foo,bar",
335+
f"{p_ra_dicts}=foo:1,bar:2",
336+
]
337+
338+
k1 = "key1"
339+
v1 = "value1"
340+
k2 = "key2"
341+
v2 = "value2"
342+
343+
with mock.patch("os.environ.copy") as mockCopy:
344+
mockCopy.return_value = {k1: v1, k2: v2}
345+
param = command._get_action_parameters_from_args(
346+
action=action, runner=runner, args=mockarg
347+
)
348+
349+
self.assertIn("env", param)
350+
351+
env_params = param["env"]
352+
self.assertIn(k1, env_params)
353+
self.assertIn(k2, env_params)
354+
self.assertEqual(v1, env_params[k1])
355+
self.assertEqual(v2, env_params[k2])
356+
self.assertIn(p_string, param)
357+
self.assertEqual("hoge", param[p_string])
358+
self.assertIn(p_array, param)
359+
self.assertIn("foo", param[p_array])
360+
self.assertIn("bar", param[p_array])
361+
self.assertIn(p_ra_dicts, param)
362+
self.assertDictEqual({"foo": "1", "bar": "2"}, param[p_ra_dicts][0])
363+
364+
def test_correctly_generate_empty_params_no_inherit_empty_parameters(self):
365+
"""test_correctly_generate_empty_params_no_inherit_empty_parameters
366+
367+
Verifies that we return an empty dict when we do not provide inherit env and parameters
368+
"""
369+
370+
runner = RunnerType()
371+
runner.runner_parameters = {}
372+
373+
action = Action()
374+
action.ref = "test.action"
375+
376+
subparser = mock.Mock()
377+
command = ActionRunCommand(action, self, subparser, name="test")
378+
379+
mockarg = mock.Mock()
380+
mockarg.inherit_env = False
381+
mockarg.auto_dict = True
382+
mockarg.parameters = []
383+
384+
k1 = "key1"
385+
v1 = "value1"
386+
k2 = "key2"
387+
v2 = "value2"
388+
389+
with mock.patch("os.environ.copy") as mockCopy:
390+
mockCopy.return_value = {k1: v1, k2: v2}
391+
param = command._get_action_parameters_from_args(
392+
action=action, runner=runner, args=mockarg
393+
)
394+
395+
self.assertDictEqual({}, param)

0 commit comments

Comments
 (0)