Skip to content

Commit 91306b7

Browse files
committed
Allow downstreams to enforce that the ansible_core ref must have a version specifier
1 parent a1e99e8 commit 91306b7

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/ansible_builder/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@
4848

4949
DEFAULT_EE_BASENAME = "execution-environment"
5050
YAML_FILENAME_EXTENSIONS = ('yml', 'yaml')
51+
52+
REQUIRE_ANSIBLE_CORE_PIN = False

src/ansible_builder/user_definition.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Callable
1010

1111
import yaml
12+
from packaging.requirements import InvalidRequirement, Requirement
1213

1314
from . import constants
1415
from .exceptions import DefinitionError
@@ -158,7 +159,20 @@ def python_path(self):
158159

159160
@property
160161
def ansible_core_ref(self):
161-
return self.raw.get('dependencies', {}).get('ansible_core', {}).get('package_pip', None)
162+
ref = self.raw.get('dependencies', {}).get('ansible_core', {}).get('package_pip', None)
163+
164+
if ref and constants.REQUIRE_ANSIBLE_CORE_PIN:
165+
try:
166+
req = Requirement(ref)
167+
except InvalidRequirement as e:
168+
raise DefinitionError("Invalid package requirement specified for 'ansible_core'") from e
169+
170+
if not req.specifier:
171+
raise DefinitionError(
172+
"Value for 'ansible_core' must contain a version constraint, such as 'ansible-core==2.16.*'"
173+
)
174+
175+
return ref
162176

163177
@property
164178
def ansible_runner_ref(self):

test/unit/test_user_definition.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,36 @@ def test_v3_ansible_install_refs(self, exec_env_definition_file):
173173
{'version': 3,
174174
'images': { 'base_image': {'name': 'base_image:latest'}},
175175
'dependencies': {
176-
'ansible_core': {'package_pip': 'ansible-core==2.13'},
176+
'ansible_core': {'package_pip': 'ansible-core'},
177177
'ansible_runner': { 'package_pip': 'ansible-runner==2.3.1'}
178178
}
179179
}
180180
"""
181181
)
182182
definition = UserDefinition(path)
183183
definition.validate()
184-
assert definition.ansible_core_ref == "ansible-core==2.13"
184+
assert definition.ansible_core_ref == "ansible-core"
185185
assert definition.ansible_runner_ref == "ansible-runner==2.3.1"
186-
assert definition.ansible_ref_install_list == "ansible-core==2.13 ansible-runner==2.3.1"
186+
assert definition.ansible_ref_install_list == "ansible-core ansible-runner==2.3.1"
187+
188+
def test_v3_ansible_install_ref_pin_required(self, monkeypatch, exec_env_definition_file):
189+
path = exec_env_definition_file(
190+
"""
191+
{'version': 3,
192+
'images': { 'base_image': {'name': 'base_image:latest'}},
193+
'dependencies': {
194+
'ansible_core': {'package_pip': 'ansible-core'},
195+
'ansible_runner': { 'package_pip': 'ansible-runner==2.3.1'}
196+
}
197+
}
198+
"""
199+
)
200+
monkeypatch.setattr(constants, 'REQUIRE_ANSIBLE_CORE_PIN', True)
201+
definition = UserDefinition(path)
202+
definition.validate()
203+
with pytest.raises(DefinitionError) as error:
204+
_ = definition.ansible_core_ref
205+
assert "Value for 'ansible_core' must contain a version constraint" in str(error.value.args[0])
187206

188207
def test_v3_inline_python(self, exec_env_definition_file):
189208
"""

0 commit comments

Comments
 (0)