-
Notifications
You must be signed in to change notification settings - Fork 100
add the docker specific arg primitive. #334
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
19a501f
e84a259
8f6b483
910a56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=invalid-name, too-few-public-methods | ||
|
|
||
| """Environment primitive""" | ||
samcmill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import unicode_literals | ||
| from __future__ import print_function | ||
|
|
||
| import logging # pylint: disable=unused-import | ||
|
|
||
| import hpccm.config | ||
|
|
||
| from hpccm.common import container_type | ||
|
|
||
| class arg(object): | ||
| """The `arg` primitive sets the corresponding environment | ||
| variables during the build time of a docker container. | ||
| This primitive is docker specific and is ignored for singularity | ||
| # Parameters | ||
| variables: A dictionary of key / value pairs. The default is an | ||
| empty dictionary. | ||
| # Examples | ||
| ```python | ||
| arg(variables={'HTTP_PROXY': 'proxy.example.com', 'NO_PROXY':'example.com'}) | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__(self, **kwargs): | ||
| """Initialize primitive""" | ||
| self.__variables = kwargs.get('variables', {}) | ||
|
|
||
| def __str__(self): | ||
| """String representation of the primitive""" | ||
| if self.__variables: | ||
|
|
||
| if hpccm.config.g_ctype == container_type.SINGULARITY or \ | ||
|
||
| hpccm.config.g_ctype == container_type.BASH: | ||
samcmill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return "" | ||
| elif hpccm.config.g_ctype == container_type.DOCKER: | ||
| string = "" | ||
| num_vars = len(self.__variables) | ||
| for count, (key, val) in enumerate(sorted(self.__variables.items())): | ||
| eol = "" if count == num_vars - 1 else "\n" | ||
| if val == "": | ||
| string += 'ARG {0}'.format(key) + eol | ||
| else: | ||
| string += 'ARG {0}={1}'.format(key, val) + eol | ||
| return string | ||
| else: | ||
| raise RuntimeError('Unknown container type') | ||
| else: | ||
| return '' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=invalid-name, too-few-public-methods, bad-continuation | ||
|
|
||
| """Test cases for the arg module""" | ||
|
|
||
| from __future__ import unicode_literals | ||
| from __future__ import print_function | ||
|
|
||
| import logging # pylint: disable=unused-import | ||
| import unittest | ||
|
|
||
| from helpers import bash, docker, invalid_ctype, singularity | ||
|
|
||
| from hpccm.primitives.arg import arg | ||
|
|
||
| class Test_arg(unittest.TestCase): | ||
| def setUp(self): | ||
| """Disable logging output messages""" | ||
| logging.disable(logging.ERROR) | ||
|
|
||
| @docker | ||
| def test_empty(self): | ||
| """No arg specified""" | ||
| e = arg() | ||
| self.assertEqual(str(e), '') | ||
|
|
||
| @invalid_ctype | ||
| def test_invalid_ctype(self): | ||
| """Invalid container type specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| with self.assertRaises(RuntimeError): | ||
| str(e) | ||
|
|
||
| @docker | ||
| def test_single_docker(self): | ||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e), 'ARG A=B') | ||
|
|
||
| @docker | ||
| def test_single_docker_nodefault(self): | ||
| """Single arg variable specified (no default value)""" | ||
| e = arg(variables={'A': ''}) | ||
| self.assertEqual(str(e), 'ARG A') | ||
|
|
||
| @singularity | ||
| def test_single_singularity(self): | ||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e), '') | ||
|
|
||
| @bash | ||
| def test_single_bash(self): | ||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e), '') | ||
|
|
||
| @docker | ||
| def test_single_export_docker(self): | ||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e), 'ARG A=B') | ||
|
|
||
| @singularity | ||
| def test_single_export_singularity(self): | ||
samcmill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e),'') | ||
|
|
||
| @bash | ||
| def test_single_export_bash(self): | ||
| """Single arg variable specified""" | ||
| e = arg(variables={'A': 'B'}) | ||
| self.assertEqual(str(e), '') | ||
|
|
||
| @docker | ||
| def test_multiple_docker(self): | ||
| """Multiple arg variables specified""" | ||
| e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}) | ||
| self.assertEqual(str(e), | ||
| '''ARG ONE=1 | ||
| ARG THREE=3 | ||
| ARG TWO=2''') | ||
|
|
||
| @docker | ||
| def test_multiple_docker_nodefault(self): | ||
| """Multiple arg variables specified (no default value)""" | ||
| e = arg(variables={'ONE': '', 'TWO': '', 'THREE': ''}) | ||
| self.assertEqual(str(e), | ||
| '''ARG ONE | ||
| ARG THREE | ||
| ARG TWO''') | ||
|
|
||
| @singularity | ||
| def test_multiple_singularity(self): | ||
| """Multiple arg variables specified""" | ||
| e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}, | ||
| _export=False) | ||
samcmill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self.assertEqual(str(e),'') | ||
|
|
||
| @bash | ||
| def test_multiple_bash(self): | ||
| """Multiple arg variables specified""" | ||
| e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}, | ||
| _export=False) | ||
| self.assertEqual(str(e),'') | ||
samcmill marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.