Skip to content

Commit

Permalink
Merge pull request ansible-collections#1 from ansible-collections/loa…
Browse files Browse the repository at this point in the history
…d_balancer_role

load_balancer, virtual_machine, and network_interface roles and Molecule Tests
  • Loading branch information
jatorcasso authored Dec 2, 2021
2 parents de95dea + 34b7064 commit f9128b4
Show file tree
Hide file tree
Showing 19 changed files with 1,241 additions and 0 deletions.
30 changes: 30 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- resource_group
tags:
- always

- name: test networking_stack role
include_tasks:
file: tasks/networking_stack.yml
Expand All @@ -20,6 +21,7 @@
- networking_stack
tags:
- always

- name: test managed_postgresql role
include_tasks:
file: tasks/managed_postgresql.yml
Expand All @@ -28,6 +30,34 @@
- managed_postgresql
tags:
- always

- name: test virtual_machine role
include_tasks:
file: tasks/virtual_machine.yml
apply:
tags:
- virtual_machine
tags:
- always

- name: test network_interface role
include_tasks:
file: tasks/network_interface.yml
apply:
tags:
- network_interface
tags:
- always

- name: test load_balancer role
include_tasks:
file: tasks/load_balancer.yml
apply:
tags:
- load_balancer
tags:
- always

- name: test security_group role
include_tasks:
file: tasks/security_group.yml
Expand Down
180 changes: 180 additions & 0 deletions molecule/default/tasks/load_balancer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
---
- name: define resource group name
set_fact:
resource_group: "test-{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=6') }}"
azure_tags:
cloud.azure_roles: load_balancer

- name: define load balancer variables
set_fact:
azure_resource_group: "{{ resource_group }}"
azure_region: 'canadacentral'
azure_lb_name: "{{ resource_group }}-lb"

- name: Test load balancer role
block:
- name: Test valid creation of load balancer with no public ip specified
block:
- name: Create load balancer
include_role:
name: load_balancer
vars:
operation: "create"

- name: Get resource group info
azure_rm_resourcegroup_info:
name: '{{ azure_resource_group }}'
register: rg

- name: Assert that resource group was created
assert:
that:
- rg.resourcegroups | length == 1

- name: Get lb info
azure_rm_loadbalancer_info:
resource_group: '{{ azure_resource_group }}'
register: lb_info

- name: Assert that load balancer was successfully created & a public IP was assigned
assert:
that:
- lb_info.ansible_info.azure_loadbalancers | length == 1
- lb_info.ansible_info.azure_loadbalancers[0].properties.frontendIPConfigurations[0].properties.publicIPAddress is not none

- name: Delete load balancer
include_role:
name: load_balancer
vars:
operation: 'delete'

- name: Assert resource group still exists
azure_rm_resourcegroup_info:
name: '{{ azure_resource_group }}'
register: rg

- assert:
that:
- rg.resourcegroups | length == 1

- name: Assert that load balancer was deleted
azure_rm_loadbalancer_info:
resource_group: '{{ azure_resource_group }}'
register: lb_info

- assert:
that:
- lb_info.ansible_info.azure_loadbalancers | length == 0

- name: Test valid creation of load balancer with public ip specified
block:
- name: Set public IP name
set_fact:
azure_lb_pip_name: 'testinput'

- name: Create load balancer
include_role:
name: load_balancer
vars:
operation: "create"

- name: Get resource group info
azure_rm_resourcegroup_info:
name: '{{ azure_resource_group }}'
register: rg

- name: Assert that resource group was created
assert:
that:
- rg.resourcegroups | length == 1

- name: Get lb info
azure_rm_loadbalancer_info:
resource_group: '{{ azure_resource_group }}'
register: lb_info

- name: Assert that load balancer was successfully created & a public IP was assigned
assert:
that:
- lb_info.ansible_info.azure_loadbalancers | length == 1
- azure_lb_pip_name in lb_info.ansible_info.azure_loadbalancers[0].properties.frontendIPConfigurations[0].properties.publicIPAddress.id

- name: Delete load balancer
include_role:
name: load_balancer
vars:
operation: 'delete'

- name: Assert resource group still exists
azure_rm_resourcegroup_info:
name: '{{ azure_resource_group }}'
register: rg

- assert:
that:
- rg.resourcegroups | length == 1

- name: Assert that load balancer was deleted
azure_rm_loadbalancer_info:
resource_group: '{{ azure_resource_group }}'
register: lb_info

- assert:
that:
- lb_info.ansible_info.azure_loadbalancers | length == 0

- name: Test adding NICs to load balancer
block:
- name: Set networking variables to create NICs
set_fact:
azure_lb_network_interface_instances:
- "{{ resource_group }}-nic1"
- "{{ resource_group }}-nic2"
- "{{ resource_group }}-nic3"
azure_virtual_network: "{{ resource_group }}-vnet-00"
azure_subnet: "{{ resource_group }}-subnet-00"
azure_vnet_address_prefixes_cidr:
- 10.16.0.0/16
azure_subnet_address_prefixes_cidr: 10.16.0.0/24

- name: Create load balancer with supplied nics attached
include_role:
name: load_balancer
vars:
operation: "create"

- name: Get lb info
azure_rm_loadbalancer_info:
resource_group: '{{ azure_resource_group }}'
register: lb_info

- name: Assert that load balancer was successfully created & nics were attached
assert:
that:
- lb_info.ansible_info.azure_loadbalancers | length == 1
- "{{ lb_info.ansible_info.azure_loadbalancers[0].properties.backendAddressPools[0].properties.backendIPConfigurations | length == ( azure_lb_network_interface_instances | length ) }}"

- name: Test deleting NIC when attached to LB
include_role:
name: network_interface
vars:
operation: 'delete'
azure_network_interface_name: "{{ resource_group }}-nic1"

- name: Assert that NIC was deleted
azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
name: "{{ resource_group }}-nic1"
register: nic_info

- assert:
that:
- nic_info.networkinterfaces | length == 0

always:
- name: Delete resource group
azure_rm_resourcegroup:
name: "{{ azure_resource_group }}"
state: absent
force_delete_nonempty: yes
ignore_errors: yes
164 changes: 164 additions & 0 deletions molecule/default/tasks/network_interface.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
---
- name: Define resource group name
set_fact:
resource_group: "test-{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=6') }}"
azure_tags:
cloud.azure_roles: network_interface

- name: Define network interface variables
set_fact:
azure_resource_group: "{{ resource_group }}"
azure_region: 'canadacentral'
nic_instances: 2
azure_virtual_network: "{{ resource_group }}-vnet-00"
azure_subnet: "{{ resource_group }}-subnet-00"
azure_vnet_address_prefixes_cidr:
- 10.16.0.0/16
azure_subnet_address_prefixes_cidr: 10.16.0.0/24

- name: Test network interface role
block:
- name: Test valid creation of network interface w/ no load balancer
block:
- name: Create NICs
include_role:
name: network_interface
vars:
operation: "create"
azure_network_interface_name: "{{ resource_group }}-nic{{ idx }}"
loop: "{{ range(0, (nic_instances | int)) | list }}"
loop_control:
index_var: idx

- name: Get resource group info
azure_rm_resourcegroup_info:
name: '{{ azure_resource_group }}'
register: rg

- name: Assert that resource group was created
assert:
that:
- rg.resourcegroups | length == 1

- azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
register: nics_info

- name: Assert that network interfaces were successfully created
assert:
that:
- "nics_info.networkinterfaces | length == {{ nic_instances | int }}"

- name: Assert that network interface is not attached to load balancer
assert:
that:
- nics_info.networkinterfaces[0].ip_configurations[0].load_balancer_backend_address_pools is none

- name: Test updating network interface w/ load balancer
block:
- name: Update NICs
include_role:
name: network_interface
vars:
operation: "create"
azure_lb_name: "{{ resource_group }}-lb"
azure_network_interface_name: "{{ resource_group }}-nic{{ idx }}"
loop: "{{ range(0, (nic_instances | int)) | list }}"
loop_control:
index_var: idx

- azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
register: nics_info

- name: Assert that network interfaces still exist
assert:
that:
- "nics_info.networkinterfaces | length == {{ nic_instances | int }}"

- name: Assert that network interface is attached to load balancer
assert:
that:
- nics_info.networkinterfaces[0].ip_configurations[0].load_balancer_backend_address_pools is not none

- name: Test deletion of network interfaces
block:
- name: Delete NICs
include_role:
name: network_interface
vars:
operation: "delete"
azure_network_interface_name: "{{ resource_group }}-nic{{ idx }}"
loop: "{{ range(0, (nic_instances | int)) | list }}"
loop_control:
index_var: idx

- azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
register: nics_info

- name: Assert that network interfaces were successfully deleted
assert:
that:
- nics_info.networkinterfaces | length == 0

- name: Test creating network interface w/ load balancer
block:
- name: Create NICs w/ load balancer
include_role:
name: network_interface
vars:
operation: "create"
azure_lb_name: "{{ resource_group }}-lb"
azure_network_interface_name: "{{ resource_group }}-nic{{ idx }}"
loop: "{{ range(0, (nic_instances | int)) | list }}"
loop_control:
index_var: idx

- azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
register: nics_info

- name: Assert that network interfaces were created
assert:
that:
- "nics_info.networkinterfaces | length == {{ nic_instances | int }}"

- name: Assert that network interface is attached to load balancer
assert:
that:
- nics_info.networkinterfaces[0].ip_configurations[0].load_balancer_backend_address_pools is not none

- name: Test updating network interface to remove load balancer
block:
- name: Update NICs
include_role:
name: network_interface
vars:
operation: "create"
azure_network_interface_name: "{{ resource_group }}-nic{{ idx }}"
loop: "{{ range(0, (nic_instances | int)) | list }}"
loop_control:
index_var: idx

- azure_rm_networkinterface_info:
resource_group: '{{ azure_resource_group }}'
register: nics_info

- name: Assert that network interfaces still exist
assert:
that:
- "nics_info.networkinterfaces | length == {{ nic_instances | int }}"

- name: Assert that network interface is not attached to load balancer
assert:
that:
- nics_info.networkinterfaces[0].ip_configurations[0].load_balancer_backend_address_pools is none

always:
- name: Delete resource group
azure_rm_resourcegroup:
name: "{{ azure_resource_group }}"
state: absent
force_delete_nonempty: yes
ignore_errors: yes
Loading

0 comments on commit f9128b4

Please sign in to comment.