Skip to content

Commit

Permalink
Includes a CLI parameter to include cpu-options in AWS instances
Browse files Browse the repository at this point in the history
  • Loading branch information
apozsuse committed Sep 8, 2023
1 parent 32829be commit 96619c1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
6 changes: 4 additions & 2 deletions img_proof/ipa_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def test_image(
image_version=None,
architecture=None,
beta=None,
exclude=None
exclude=None,
cpu_options=None
):
"""Creates a cloud framework instance and initiates testing."""
kwargs = {
Expand Down Expand Up @@ -150,7 +151,8 @@ def test_image(
'secret_access_key': secret_access_key,
'security_group_id': security_group_id,
'ssh_key_name': ssh_key_name,
'additional_info': additional_info
'additional_info': additional_info,
'cpu_options': cpu_options
}
cloud = EC2Cloud(**kwargs)
elif cloud_name == 'gce':
Expand Down
6 changes: 6 additions & 0 deletions img_proof/ipa_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ def _launch_instance(self):
if self.additional_info:
kwargs['AdditionalInfo'] = self.additional_info

cpu_options = self.custom_args.get('cpu_options', [])
if cpu_options:
kwargs['CpuOptions'] = {}
for cpu_op in cpu_options:
kwargs['CpuOption'][cpu_op.get('key')] = cpu_op.get('value')

try:
instances = resource.create_instances(**kwargs)
except Exception as error:
Expand Down
15 changes: 14 additions & 1 deletion img_proof/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from img_proof.ipa_controller import collect_tests, test_image
from img_proof.scripts.cli_utils import (
archive_history_item,
cli_process_cpu_options,
echo_log,
echo_results,
echo_results_file,
Expand Down Expand Up @@ -117,6 +118,16 @@ def main(context, no_color):
type=click.Path(exists=True),
help='img_proof config file location. Default: ~/.config/img_proof/config'
)
@click.option(
'--cpu-options',
callback=cli_process_cpu_options,
default='',
help=(
'CPU options to be activated when launching instances for tests.'
'Example: --cpu-option AmdSevSnp=enabled'
'Multiple values can be specified separated by comma.'
)
)
@click.option(
'-D',
'--description',
Expand Down Expand Up @@ -384,6 +395,7 @@ def test(context,
account,
cleanup,
config,
cpu_options,
description,
distro,
early_exit,
Expand Down Expand Up @@ -497,7 +509,8 @@ def test(context,
image_version,
architecture,
beta,
exclude
exclude,
cpu_options
)
echo_results(results, no_color)
sys.exit(status)
Expand Down
18 changes: 18 additions & 0 deletions img_proof/scripts/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def archive_history_item(item, destination, no_color):
)


def cli_process_cpu_options(cxt, param, value):
cpu_options = []
if value:
try:
for cpu_option in value.split(','):
key, val = cpu_option.split('=', 1)
option = {
'key': key,
'value': val
}
cpu_options.append(option)
except Exception as e:
raise click.BadParameter(
"Issue with cpu-option parameter: %s" % e
)
return cpu_options


def echo_log(log_file, no_color):
try:
with open(log_file, 'r') as f:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_ipa_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import pytest

from img_proof.scripts import cli_utils
from click import BadParameter

DATA = {
"info": {
Expand Down Expand Up @@ -52,3 +55,37 @@
def test_echo_results():
"""Test cli utils echo results."""
cli_utils.echo_results(DATA, False, verbose=True)


def test_cli_process_cpu_options():
"""Test process-cpu options"""
TEST_DATA = [
{
'input': 'AmdSevSnp=enabled',
'expected_output': {
'key': 'AmdSevSnp',
'value': 'enabled'
}
},
{
'input': 'ExceptionShouldBeRaised'
}
]

for data in TEST_DATA:
if 'expected_output' in data:
output = cli_utils.cli_process_cpu_options(
{},
'cpu-options',
data['input']
)

assert data['expected_output'] == output[0]
else:
with pytest.raises(BadParameter) as exc:
cli_utils.cli_process_cpu_options(
{},
'cpu-options',
data['input']
)
assert str(exc).contains(data['input'])
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ envlist =
py35
py36
py37
py38

[testenv]
passenv = HOME
commands =
pip install -e .[dev]
pytest --cov=img_proof
flake8 img-proof
flake8 img_proof
flake8 tests --exclude=data

0 comments on commit 96619c1

Please sign in to comment.