Skip to content

Commit

Permalink
Slightly rework ec2_win_password unit tests to account for boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Oct 15, 2021
1 parent e59e034 commit 58e30ad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ netaddr
awscli
# Used for comparing SSH Public keys to the Amazon fingerprints
pycrypto
# Used by ec2_win_password
cryptography
34 changes: 24 additions & 10 deletions tests/unit/plugins/modules/test_ec2_win_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
string_cipher = base64.b64encode(base64_cipher)
'''

import datetime

from ansible.module_utils._text import to_bytes
from ansible.module_utils._text import to_text
from ansible_collections.community.aws.tests.unit.compat.mock import patch
Expand All @@ -31,25 +33,37 @@


class TestEc2WinPasswordModule(ModuleTestCase):
@patch('ansible_collections.community.aws.plugins.modules.ec2_win_password.ec2_connect')
def test_decryption(self, mock_connect):

path = fixture_prefix + '/ec2_win_password.pem'
# Future: It would be good to generate this data on the fly and use a
# temporary certificate and password.
PEM_PATH = fixture_prefix + '/ec2_win_password.pem'
UNENCRYPTED_DATA = 'Ansible_AWS_EC2_Win_Password'
ENCRYPTED_DATA = 'L2k1iFiu/TRrjGr6Rwco/T3C7xkWxUw4+YPYpGGOmP3KDdy3hT1' \
'8RvdDJ2i0e+y7wUcH43DwbRYSlkSyALY/nzjSV9R5NChUyVs3W5' \
'5oiVuyTKsk0lor8dFJ9z9unq14tScZHvyQ3Nx1ggOtS18S9Pk55q' \
'IaCXfx26ucH76VRho='
INSTANCE_ID = 'i-12345'

@patch('ansible_collections.community.aws.plugins.modules.s3_bucket_notification.AnsibleAWSModule.client')
def test_decryption(self, mock_client):

path = self.PEM_PATH
with open(path, 'r') as f:
pem = to_text(f.read())

with self.assertRaises(AnsibleExitJson) as exec_info:
set_module_args({'instance_id': 'i-12345',
'key_data': pem
set_module_args({'instance_id': self.INSTANCE_ID,
'key_data': pem,
})
module = setup_module_object()
mock_connect().get_password_data.return_value = 'L2k1iFiu/TRrjGr6Rwco/T3C7xkWxUw4+YPYpGGOmP3KDdy3hT1' \
'8RvdDJ2i0e+y7wUcH43DwbRYSlkSyALY/nzjSV9R5NChUyVs3W5' \
'5oiVuyTKsk0lor8dFJ9z9unq14tScZHvyQ3Nx1ggOtS18S9Pk55q' \
'IaCXfx26ucH76VRho='
mock_client().get_password_data.return_value = {
'InstanceId': self.INSTANCE_ID,
'PasswordData': self.ENCRYPTED_DATA,
'Timestamp': datetime.datetime.now(),
}
ec2_win_password(module)

self.assertEqual(
exec_info.exception.args[0]['win_password'],
to_bytes('Ansible_AWS_EC2_Win_Password'),
to_bytes(self.UNENCRYPTED_DATA),
)
1 change: 1 addition & 0 deletions tests/unit/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ boto3
boto

placebo
cryptography

0 comments on commit 58e30ad

Please sign in to comment.