Skip to content

Commit

Permalink
Merge pull request #130 from isaacm/validate-chroot-from-scratch-option
Browse files Browse the repository at this point in the history
builder/AmazonChroot: validate required options when from_scratch is true
  • Loading branch information
mayn authored Oct 4, 2018
2 parents f3793b8 + def0d8f commit 8e1441f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/packerlicious/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,28 @@ def validate(self):
]
validator.all_or_nothing(self.__class__.__name__, self.properties, conds)

conds = [
'source_ami',
'source_ami_filter',
from_scratch_conds = [
'ami_virtualization_type',
'pre_mount_commands',
'root_volume_size'
]
validator.exactly_one(self.__class__.__name__, self.properties, conds)

if (
'from_scratch' in self.properties and
self.properties['from_scratch'] == 'true' and
validator.count(self.properties, from_scratch_conds) != 3
):
raise ValueError(
"AmazonChroot: when from_config is True, source_ami "
"is ignored and {0} options are "
"required".format(', '.join(from_scratch_conds)))
else:
conds = [
'source_ami',
'source_ami_filter',
]
validator.exactly_one(
self.__class__.__name__, self.properties, conds)


class AmazonEbsSurrogate(PackerBuilder):
Expand Down
27 changes: 27 additions & 0 deletions tests/packerlicious/test_builder_amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ def test_exactly_one_source_ami(self):
b.to_dict()
assert 'AmazonChroot: one of the following must be specified: source_ami, source_ami_filter' == str(excinfo.value)

def test_exactly_one_source_ami_when_from_scratch_is_false(self):
b = builder.AmazonChroot(
ami_name ="some-ami",
access_key="dummy-access-key",
secret_key="dummy-secret-key",
from_scratch="false"
)

with pytest.raises(ValueError) as excinfo:
b.to_dict()
assert 'AmazonChroot: one of the following must be specified: source_ami, source_ami_filter' == str(excinfo.value)

def test_required_fields_when_from_scratch_is_true(self):
b = builder.AmazonChroot(
ami_name ="some-ami",
access_key="dummy-access-key",
secret_key="dummy-secret-key",
from_scratch="true"
)

with pytest.raises(ValueError) as excinfo:
b.to_dict()
assert 'AmazonChroot: when from_config is True, source_ami is ' \
'ignored and ami_virtualization_type, pre_mount_commands, ' \
'root_volume_size options are required' == str(excinfo.value)


class TestAmazonEbsSurrogate(object):

def test_required_fields_missing(self):
Expand Down

0 comments on commit 8e1441f

Please sign in to comment.