Skip to content

Commit

Permalink
Update ec2 provider unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
smarlowucf committed Mar 8, 2018
1 parent 6ece989 commit 7a56c18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ipa/ipa_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(self,
self.ssh_private_key = (
ssh_private_key or
self._get_from_ec2_config('ssh_private_key') or
self._get_value('ssh_private_key', 'ec2')
self._get_value(ssh_private_key, config_key='ssh_private_key')
)
self.ssh_user = (
ssh_user or
Expand Down
31 changes: 15 additions & 16 deletions tests/test_ipa_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,23 @@ def test_ec2_get_subnet_exception(self, mock_get_driver):

assert msg == str(error.value)

@patch('ipa.ipa_ec2.ipa_utils.generate_public_ssh_key')
def test_ec2_get_user_data(self, mock_generate_ssh_key):
mock_generate_ssh_key.return_value = b'testkey12345'

provider = EC2Provider(**self.kwargs)

result = provider._get_user_data()

assert result == '#cloud-config\nusers:\n - name: ec2-user\n ' \
'ssh-authorized-keys:\n - testkey12345\n'

@patch.object(EC2Provider, '_get_user_data')
@patch.object(EC2Provider, '_get_subnet')
@patch.object(EC2Provider, '_get_driver')
def test_ec2_launch_instance(self, mock_get_driver, mock_get_subnet):
def test_ec2_launch_instance(
self, mock_get_driver, mock_get_subnet, mock_get_user_data
):
"""Test ec2 provider launch instance method."""
driver = MagicMock()

Expand Down Expand Up @@ -164,21 +178,6 @@ def test_ec2_launch_instance(self, mock_get_driver, mock_get_subnet):
assert driver.list_images.call_count == 1
assert driver.create_node.call_count == 1

@patch.object(EC2Provider, '_get_driver')
def test_ec2_launch_no_key_name(self, mock_get_driver):
"""Test ec2 provider raises exception if no ssh key name method."""
driver = MagicMock()
mock_get_driver.return_value = driver

provider = EC2Provider(**self.kwargs)
provider.ssh_key_name = None
msg = 'SSH Key Name is required to launch an EC2 instance.'

with pytest.raises(EC2ProviderException) as error:
provider._launch_instance()

assert str(error.value) == msg

@patch.object(EC2Provider, '_get_driver')
def test_ec2_launch_instance_no_size(self, mock_get_driver):
"""Test exception raised if instance type not found."""
Expand Down

0 comments on commit 7a56c18

Please sign in to comment.