Skip to content

Commit

Permalink
CRAYSAT-1896: Add Jinja template support to additional_inventory
Browse files Browse the repository at this point in the history
Add support for Jinja template rendering to the `name`, `url`, `branch`,
and `commit` properties of the `additional_inventory` of a CFS
configuration specified in a bootprep input file.

This is useful for writing automated tests that obtain this information
from a system and pass it in through variables with `--vars-file`.

Test Description:
The added unit tests pass, and it will be tested on rocket as well.
  • Loading branch information
haasken-hpe committed Nov 26, 2024
1 parent 8a1f31b commit c751745
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sat/cli/bootprep/input/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,25 @@ class AdditionalInventory(InputConfigurationLayerBase):
"""Additional inventory data for a CFS configuration"""

@property
@jinja_rendered
def clone_url(self):
"""str: the clone URL for the additional inventory"""
return self.layer_data['url']

@property
@jinja_rendered
def commit(self):
"""str or None: the commit hash or None if branch was specified instead"""
return self.layer_data.get('commit')

@property
@jinja_rendered
def branch(self):
"""str or None: the branch or None if commit was specified instead"""
return self.layer_data.get('branch')

@property
@jinja_rendered
def name(self):
"""str or None: the optional name of the additional inventory"""
return self.layer_data.get('name')
Expand Down
49 changes: 49 additions & 0 deletions tests/cli/bootprep/input/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,19 @@ def test_clone_url_property(self):
self.mock_cfs_client)
self.assertEqual(self.repo_url, additional_inventory.clone_url)

def test_clone_url_jinja_rendered(self):
"""Test the clone_url property when it uses Jinja2 templating"""
repo_name = 'foo-inventory'
self.jinja_env.globals['test'] = {
'repo_name': repo_name
}

self.data_with_commit['url'] = 'https://api-gw-service.nmn.local/vcs/cray/{{test.repo_name}}.git'
additional_inventory = AdditionalInventory(self.data_with_commit, self.jinja_env,
self.mock_cfs_client)
self.assertEqual(f'https://api-gw-service.nmn.local/vcs/cray/{repo_name}.git',
additional_inventory.clone_url)

def test_commit_property_specified(self):
"""Test the commit property when specified"""
additional_inventory = AdditionalInventory(self.data_with_commit, self.jinja_env,
Expand All @@ -572,6 +585,18 @@ def test_commit_property_unspecified(self):
self.mock_cfs_client)
self.assertIsNone(additional_inventory.commit)

def test_commit_property_jinja_rendered(self):
"""Test the commit property when it uses Jinja2 templating"""
commit_hash = 'abc1234'
self.jinja_env.globals['test'] = {
'commit_hash': commit_hash
}

self.data_with_commit['commit'] = '{{test.commit_hash}}'
additional_inventory = AdditionalInventory(self.data_with_commit, self.jinja_env,
self.mock_cfs_client)
self.assertEqual(commit_hash, additional_inventory.commit)

def test_branch_property_specified(self):
""""Test the branch property when specified"""
additional_inventory = AdditionalInventory(self.data_with_branch, self.jinja_env,
Expand All @@ -584,6 +609,18 @@ def test_branch_property_not_specified(self):
self.mock_cfs_client)
self.assertIsNone(additional_inventory.branch)

def test_branch_property_jinja_rendered(self):
"""Test the branch property when it uses Jinja2 templating"""
branch_name = 'integration'
self.jinja_env.globals['test'] = {
'branch_name': branch_name
}

self.data_with_branch['branch'] = '{{test.branch_name}}'
additional_inventory = AdditionalInventory(self.data_with_branch, self.jinja_env,
self.mock_cfs_client)
self.assertEqual(branch_name, additional_inventory.branch)

def test_name_property_specified(self):
"""Test the name property when specified"""
additional_inventory = AdditionalInventory(self.data_with_name, self.jinja_env,
Expand All @@ -596,6 +633,18 @@ def test_name_property_not_specified(self):
self.mock_cfs_client)
self.assertIsNone(additional_inventory.name)

def test_name_property_jinja_rendered(self):
"""Test the name property when it uses Jinja2 templating"""
name = 'inventory'
self.jinja_env.globals['test'] = {
'name': name
}

self.data_with_name['name'] = '{{test.name}}'
additional_inventory = AdditionalInventory(self.data_with_name, self.jinja_env,
self.mock_cfs_client)
self.assertEqual(name, additional_inventory.name)

def test_get_cfs_api_data_no_resolve_branches(self):
"""Test get_cfs_api_data method without branch resolution"""
additional_inventory = AdditionalInventory(self.data_with_name, self.jinja_env,
Expand Down

0 comments on commit c751745

Please sign in to comment.