Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize layer env #876

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyoutline/outline/backend/cue.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ def _serialize(launcher, use_pycuerun):
limit = Et.SubElement(limits, "limit")
limit.text = limit_name

layer_env = Et.SubElement(spec_layer, "env")
for env_k, env_v in layer.get_envs().items():
pair = Et.SubElement(layer_env, "key", {"name": "{}".format(env_k)})
pair.text = env_v

services = Et.SubElement(spec_layer, "services")
service = Et.SubElement(services, "service")
try:
Expand Down
6 changes: 5 additions & 1 deletion pyoutline/outline/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,11 @@ def set_env(self, key, value):

def get_env(self, key, default=None):
"""Get the value of the env var that will be set before execute."""
self.__env.get(key, default)
return self.__env.get(key, default)

def get_envs(self):
"""Return all env."""
return self.__env

def get_name(self):
"""Return the layer name."""
Expand Down
1 change: 1 addition & 0 deletions pyoutline/tests/json/shell.outline
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{
"name": "shell_layer",
"module": "outline.modules.shell.Shell",
"env": {"LAYER_KEY": "LAYER_VALUE"},
"command": ["/bin/ls"]
}
]
Expand Down
19 changes: 18 additions & 1 deletion pyoutline/tests/json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import mock
import os
import unittest
from xml.etree import ElementTree as Et

import outline
from outline import load_json
from . import test_utils

Expand All @@ -40,24 +42,39 @@ def testJson(self):
'"layers": [{'
'"name": "layer_1", '
'"module": "outline.modules.shell.Shell", '
'"env": {"LAYER_KEY1": "LAYER_VALUE1"}, '
'"command": ["/bin/ls"]'
'}]'
'}')

ol = load_json(s)
self.assertEqual('test_job', ol.get_name())
self.assertEqual('1-10', ol.get_frame_range())
self.assertEqual('LAYER_VALUE1', ol.get_layer('layer_1').get_env('LAYER_KEY1'))

ol.get_layer('layer_1').set_env('LAYER_KEY2', 'LAYER_VALUE2')

l = outline.cuerun.OutlineLauncher(ol)
root = Et.fromstring(l.serialize())
env1 = root.find('job/layers/layer/env/key[@name="LAYER_KEY1"]')
self.assertEqual('LAYER_VALUE1', env1.text)
env2 = root.find('job/layers/layer/env/key[@name="LAYER_KEY2"]')
self.assertEqual('LAYER_VALUE2', env2.text)

@mock.patch('outline.layer.Layer.system')
@mock.patch.dict(os.environ, {}, clear=True)
def testJsonFile(self, systemMock):
"""Load JSON from a file"""
with open(os.path.join(JSON_DIR, 'shell.outline')) as fp:
ol = load_json(fp.read())
with test_utils.TemporarySessionDirectory():
ol.setup()
ol.get_layer('shell_layer').execute('1000')
layer = ol.get_layer('shell_layer')
self.assertEqual('LAYER_VALUE', layer.get_env('LAYER_KEY'))
layer.execute('1000')

systemMock.assert_has_calls([mock.call(['/bin/ls'], frame=1000)])
self.assertEqual('LAYER_VALUE', os.environ['LAYER_KEY'])


if __name__ == '__main__':
Expand Down