Add integration tests for aws lightsail (#63770)
* Add integration tests for aws lightsail * lightsail - use module_defaults instead of aws_connection_info * lightsail tests - assert instance state on create * Fix yaml syntax error Co-Authored-By: Jill R <4121322+jillr@users.noreply.github.com> * [lightsail] create keypair as part of the testsuite * Fix lightsail actions in compute-policy * Add ability to delete keypair in lightsail_keypair
This commit is contained in:
parent
6e652ae6df
commit
95bd92da04
6 changed files with 235 additions and 0 deletions
hacking/aws_config/testing_policies
lib/ansible/config
test/integration/targets/lightsail
|
@ -245,6 +245,23 @@
|
|||
"Resource": [
|
||||
"arn:aws:states:*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Sid": "AllowLightsail",
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"lightsail:CreateInstances",
|
||||
"lightsail:CreateKeyPair",
|
||||
"lightsail:DeleteInstance",
|
||||
"lightsail:DeleteKeyPair",
|
||||
"lightsail:GetInstance",
|
||||
"lightsail:GetInstances",
|
||||
"lightsail:GetKeyPairs",
|
||||
"lightsail:RebootInstance",
|
||||
"lightsail:StartInstance",
|
||||
"lightsail:StopInstance"
|
||||
],
|
||||
"Resource": "arn:aws:lightsail:*:*:*"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -489,6 +489,8 @@ groupings:
|
|||
- aws
|
||||
lightsail:
|
||||
- aws
|
||||
lightsail_keypair:
|
||||
- aws
|
||||
rds:
|
||||
- aws
|
||||
rds_instance:
|
||||
|
|
2
test/integration/targets/lightsail/aliases
Normal file
2
test/integration/targets/lightsail/aliases
Normal file
|
@ -0,0 +1,2 @@
|
|||
cloud/aws
|
||||
shippable/aws/group2
|
3
test/integration/targets/lightsail/defaults/main.yml
Normal file
3
test/integration/targets/lightsail/defaults/main.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
instance_name: "{{ resource_prefix }}_instance"
|
||||
keypair_name: "{{ resource_prefix }}_keypair"
|
||||
zone: "{{ aws_region }}a"
|
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
try:
|
||||
from botocore.exceptions import ClientError, BotoCoreError
|
||||
import boto3
|
||||
except ImportError:
|
||||
pass # caught by AnsibleAWSModule
|
||||
|
||||
from ansible.module_utils.aws.core import AnsibleAWSModule
|
||||
from ansible.module_utils.ec2 import (get_aws_connection_info, boto3_conn)
|
||||
|
||||
|
||||
def create_keypair(module, client, keypair_name):
|
||||
"""
|
||||
Create a keypair to use for your lightsail instance
|
||||
"""
|
||||
|
||||
try:
|
||||
client.create_key_pair(keyPairName=keypair_name)
|
||||
except ClientError as e:
|
||||
if "Some names are already in use" in e.response['Error']['Message']:
|
||||
module.exit_json(changed=False)
|
||||
module.fail_json_aws(e)
|
||||
|
||||
module.exit_json(changed=True)
|
||||
|
||||
|
||||
def delete_keypair(module, client, keypair_name):
|
||||
"""
|
||||
Delete a keypair in lightsail
|
||||
"""
|
||||
|
||||
try:
|
||||
client.delete_key_pair(keyPairName=keypair_name)
|
||||
except ClientError as e:
|
||||
if e.response['Error']['Code'] == "NotFoundException":
|
||||
module.exit_json(changed=False)
|
||||
module.fail_json_aws(e)
|
||||
|
||||
module.exit_json(changed=True)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = dict(
|
||||
name=dict(type='str', required=True),
|
||||
state=dict(type='str', default='present', choices=['present', 'absent']),
|
||||
)
|
||||
|
||||
module = AnsibleAWSModule(argument_spec=argument_spec)
|
||||
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
|
||||
try:
|
||||
client = boto3_conn(module, conn_type='client', resource='lightsail', region=region, endpoint=ec2_url,
|
||||
**aws_connect_params)
|
||||
except ClientError as e:
|
||||
module.fail_json_aws(e)
|
||||
|
||||
keypair_name = module.params.get('name')
|
||||
state = module.params.get('state')
|
||||
|
||||
if state == 'present':
|
||||
create_keypair(module, client, keypair_name)
|
||||
else:
|
||||
delete_keypair(module, client, keypair_name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
132
test/integration/targets/lightsail/tasks/main.yml
Normal file
132
test/integration/targets/lightsail/tasks/main.yml
Normal file
|
@ -0,0 +1,132 @@
|
|||
---
|
||||
|
||||
- module_defaults:
|
||||
group/aws:
|
||||
aws_access_key: '{{ aws_access_key | default(omit) }}'
|
||||
aws_secret_key: '{{ aws_secret_key | default(omit) }}'
|
||||
security_token: '{{ security_token | default(omit) }}'
|
||||
region: '{{ aws_region | default(omit) }}'
|
||||
|
||||
block:
|
||||
|
||||
# ==== Tests ===================================================
|
||||
|
||||
- name: Create a new keypair in lightsail
|
||||
lightsail_keypair:
|
||||
name: "{{ keypair_name }}"
|
||||
|
||||
- name: Create a new instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
zone: "{{ zone }}"
|
||||
blueprint_id: amazon_linux
|
||||
bundle_id: nano_2_0
|
||||
key_pair_name: "{{ keypair_name }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == True
|
||||
- "'instance' in result and result.instance.name == instance_name"
|
||||
- "result.instance.state.name in ['pending', 'running']"
|
||||
|
||||
- name: Make sure create is idempotent
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
zone: "{{ zone }}"
|
||||
blueprint_id: amazon_linux
|
||||
bundle_id: nano_2_0
|
||||
key_pair_name: "{{ keypair_name }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == False
|
||||
|
||||
- name: Start the running instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: running
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == False
|
||||
|
||||
- name: Stop the instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: stopped
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == True
|
||||
- "result.instance.state.name in ['stopping', 'stopped']"
|
||||
|
||||
- name: Stop the stopped instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: stopped
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == False
|
||||
|
||||
- name: Start the instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: running
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == True
|
||||
- "result.instance.state.name in ['running', 'pending']"
|
||||
|
||||
- name: Restart the instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: restarted
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == True
|
||||
|
||||
- name: Delete the instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == True
|
||||
|
||||
- name: Make sure instance deletion is idempotent
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: absent
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.changed == False
|
||||
|
||||
# ==== Cleanup ====================================================
|
||||
|
||||
always:
|
||||
|
||||
- name: Cleanup - delete instance
|
||||
lightsail:
|
||||
name: "{{ instance_name }}"
|
||||
state: absent
|
||||
ignore_errors: yes
|
||||
|
||||
- name: Cleanup - delete keypair
|
||||
lightsail_keypair:
|
||||
name: "{{ keypair_name }}"
|
||||
state: absent
|
||||
ignore_errors: yes
|
Loading…
Add table
Reference in a new issue