Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint tests #159

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
gather_facts: false
tasks:
- name: Install python for Ansible
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream)
ansible.builtin.raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream)
become: true
changed_when: false
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@
gather_facts: false
become: true
tasks: []

- name: Converge
hosts: bar
gather_facts: false
become: true
tasks: []

- name: Converge
hosts: foo
gather_facts: false
become: true
tasks: []

- name: Converge
hosts: baz
gather_facts: false
Expand Down
6 changes: 3 additions & 3 deletions test/gce/scenarios/linux/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
- name: Converge
hosts: all
tasks:
- name: "Include testrole"
include_role:
name: "testrole"
- name: Include testrole
ansible.builtin.include_role:
name: testrole
27 changes: 13 additions & 14 deletions test/gce/scenarios/linux/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@

tasks:
- name: Make sure if linux or windows either specified
assert:
ansible.builtin.assert:
that:
- molecule_yml.driver.instance_os_type | lower == "linux" or
molecule_yml.driver.instance_os_type | lower == "windows"
fail_msg: "instance_os_type is possible only to specify linux or windows either"
- molecule_yml.driver.instance_os_type | lower == "linux" or molecule_yml.driver.instance_os_type | lower == "windows"
fail_msg: instance_os_type is possible only to specify linux or windows either

- name: Get network info
google.cloud.gcp_compute_network_info:
filters:
- name = "{{ molecule_yml.driver.network_name | default('default') }}"
project: "{{ molecule_yml.driver.vpc_host_project | default(gcp_project_id) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default (omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default (omit, true) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
register: my_network

Expand All @@ -32,29 +31,29 @@
- name = "{{ molecule_yml.driver.subnetwork_name | default('default') }}"
project: "{{ molecule_yml.driver.vpc_host_project | default(gcp_project_id) }}"
region: "{{ molecule_yml.driver.region }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default (omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default (omit, true) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
register: my_subnetwork

- name: Set external access config
set_fact:
ansible.builtin.set_fact:
external_access_config:
- access_configs:
- name: "External NAT"
type: "ONE_TO_NAT"
- name: External NAT
type: ONE_TO_NAT
when: molecule_yml.driver.external_access

- name: Include create_linux_instance tasks
include_tasks: tasks/create_linux_instance.yml
ansible.builtin.include_tasks: tasks/create_linux_instance.yml
when:
- molecule_yml.driver.instance_os_type | lower == "linux"

- name: Include create_windows_instance tasks
include_tasks: tasks/create_windows_instance.yml
ansible.builtin.include_tasks: tasks/create_windows_instance.yml
when:
- molecule_yml.driver.instance_os_type | lower == "windows"

handlers:
- name: Import main handler tasks
import_tasks: handlers/main.yml
ansible.builtin.import_tasks: handlers/main.yml
12 changes: 6 additions & 6 deletions test/gce/scenarios/linux/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
zone: "{{ item.zone | default(molecule_yml.driver.region + '-b') }}"
project: "{{ molecule_yml.driver.project_id | default(lookup('env', 'GCE_PROJECT_ID')) }}"
scopes: "{{ molecule_yml.driver.scopes | default(['https://www.googleapis.com/auth/compute'], True) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default (omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default (omit, true) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
register: async_results
loop: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0
notify:
- "Wipe out instance config"
- "Dump instance config"
- Wipe out instance config
- Dump instance config

- name: Wait for instance(s) deletion to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: server
until: server.finished
Expand All @@ -35,4 +35,4 @@

handlers:
- name: Import main handler tasks
import_tasks: handlers/main.yml
ansible.builtin.import_tasks: handlers/main.yml
47 changes: 23 additions & 24 deletions test/gce/scenarios/linux/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
---
- name: Populate instance config dict Linux
set_fact:
ansible.builtin.set_fact:
instance_conf_dict:
{
"instance": "{{ instance_info.name }}",
"address": "{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP }}",
"user": "{{ lookup('env','USER') }}",
"port": "22",
"identity_file": "{{ ssh_identity_file }}",
"instance_os_type": "{{ molecule_yml.driver.instance_os_type }}",
}
instance: "{{ instance_info.name }}"
address:
"{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP
}}"
user: "{{ lookup('env', 'USER') }}"
port: "22"
identity_file: "{{ ssh_identity_file }}"
instance_os_type: "{{ molecule_yml.driver.instance_os_type }}"
loop: "{{ server.results }}"
loop_control:
loop_var: instance_info
no_log: true
register: instance_conf_dict

- name: Populate instance config dict Windows
set_fact:
ansible.builtin.set_fact:
instance_conf_dict:
{
"instance": "{{ instance_info.name }}",
"address": "{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP }}",
"user": "molecule_usr",
"password": "{{ instance_info.password }}",
"port": "{{ instance_info.winrm_port | default(5986) }}",
"winrm_transport": "{{ molecule_yml.driver.winrm_transport | default('ntlm') }}",
"winrm_server_cert_validation": "{{ molecule_yml.driver.winrm_server_cert_validation | default('ignore') }}",
"instance_os_type": "{{ molecule_yml.driver.instance_os_type }}",
}
instance: "{{ instance_info.name }}"
address:
"{{ instance_info.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else instance_info.networkInterfaces.0.networkIP
}}"
user: molecule_usr
password: "{{ instance_info.password }}"
port: "{{ instance_info.winrm_port | default(5986) }}"
winrm_transport: "{{ molecule_yml.driver.winrm_transport | default('ntlm') }}"
winrm_server_cert_validation: "{{ molecule_yml.driver.winrm_server_cert_validation | default('ignore') }}"
instance_os_type: "{{ molecule_yml.driver.instance_os_type }}"
loop: "{{ win_instances }}"
loop_control:
loop_var: instance_info
no_log: true
register: instance_conf_dict

- name: Wipe out instance config
set_fact:
ansible.builtin.set_fact:
instance_conf: {}

- name: Convert instance config dict to a list
set_fact:
ansible.builtin.set_fact:
instance_conf: "{{ instance_conf_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"

- name: Dump instance config
copy:
ansible.builtin.copy:
content: "{{ instance_conf }}"
dest: "{{ molecule_instance_config }}"
mode: "0600"
8 changes: 4 additions & 4 deletions test/gce/scenarios/linux/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ dependency:
driver:
name: gce
project_id: change-to-id-of-the-gcp-project # if not set, will default to env GCE_PROJECT_ID
auth_kind: null # set to machineaccount or serviceaccount or application - if set to null will read env GCP_AUTH_KIND
service_account_email: null # set to an email associated with the project - if set to null, will default to GCP_SERVICE_ACCOUNT_EMAIL. Should not be set if using auth_kind serviceaccount.
service_account_file: null # set to the path to the JSON credentials file - if set to null, will default to env GCP_SERVICE_ACCOUNT_FILE
auth_kind: # set to machineaccount or serviceaccount or application - if set to null will read env GCP_AUTH_KIND
service_account_email: # set to an email associated with the project - if set to null, will default to GCP_SERVICE_ACCOUNT_EMAIL. Should not be set if using auth_kind serviceaccount.
service_account_file: # set to the path to the JSON credentials file - if set to null, will default to env GCP_SERVICE_ACCOUNT_FILE
region: us-west1 # REQUIRED. example: us-central1
external_access: false # chose whether to create a public IP for the VM or not - default is private IP only
instance_os_type: linux # will be considered linux by default, but can be explicitely set to windows
platforms:
- name: linuxgce-createdbymolecule # is an instance name
machine_type: n1-standard-1 # define your machine type
zone: null # example: us-west1-b, will default to zone b of driver.region
zone: # example: us-west1-b, will default to zone b of driver.region
provisioner:
name: ansible
verifier:
Expand Down
2 changes: 1 addition & 1 deletion test/gce/scenarios/linux/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
gather_facts: false
tasks:
- name: Wait 600 seconds for target connection to become reachable/usable
wait_for_connection:
ansible.builtin.wait_for_connection:
22 changes: 12 additions & 10 deletions test/gce/scenarios/linux/tasks/create_linux_instance.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: Create ssh keypair
community.crypto.openssh_keypair:
comment: "{{ lookup('env','USER') }} user for Molecule"
comment: "{{ lookup('env', 'USER') }} user for Molecule"
path: "{{ ssh_identity_file }}"
register: keypair

Expand All @@ -11,7 +11,7 @@
name: "{{ item.name }}"
machine_type: "{{ item.machine_type | default('n1-standard-1') }}"
metadata:
ssh-keys: "{{ lookup('env','USER') }}:{{ keypair.public_key }}}"
ssh-keys: "{{ lookup('env', 'USER') }}:{{ keypair.public_key }}"
disks:
- auto_delete: true
boot: true
Expand All @@ -20,12 +20,14 @@
source_image: "{{ item.image | default('projects/debian-cloud/global/images/family/debian-10') }}"
source_image_encryption_key:
raw_key: "{{ item.image_encryption_key | default(omit) }}"
network_interfaces: "{{ [ { 'network': my_network.resources.0 | default(omit), 'subnetwork': my_subnetwork.resources.0 | default(omit) } | combine(external_access_config | default([]) ) ] }}"
network_interfaces:
"{{ [ { 'network': my_network.resources.0 | default(omit), 'subnetwork': my_subnetwork.resources.0 | default(omit) } | combine(external_access_config
| default([]) ) ] }}"
zone: "{{ item.zone | default(molecule_yml.driver.region + '-b') }}"
project: "{{ gcp_project_id }}"
scopes: "{{ molecule_yml.driver.scopes | default(['https://www.googleapis.com/auth/compute'], True) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default (omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default (omit, true) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
register: async_results
loop: "{{ molecule_yml.platforms }}"
Expand All @@ -35,20 +37,20 @@
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
loop: "{{ async_results.results }}"
register: server
until: server.finished
retries: 300
delay: 10
notify:
- "Populate instance config dict Linux"
- "Convert instance config dict to a list"
- "Dump instance config"
- Populate instance config dict Linux
- Convert instance config dict to a list
- Dump instance config

- name: Wait for SSH
wait_for:
ansible.builtin.wait_for:
port: 22
host: "{{ item.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else item.networkInterfaces.0.networkIP }}"
search_regex: SSH
Expand Down
26 changes: 15 additions & 11 deletions test/gce/scenarios/linux/tasks/create_windows_instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,44 @@
source_image: "{{ item.image | default('projects/windows-cloud/global/images/family/windows-2019') }}"
source_image_encryption_key:
raw_key: "{{ item.image_encryption_key | default(omit) }}"
network_interfaces: "{{ [ { 'network': my_network.resources.0 | default(omit), 'subnetwork': my_subnetwork.resources.0 | default(omit) } | combine(external_access_config | default([])) ] }}"
network_interfaces:
"{{ [ { 'network': my_network.resources.0 | default(omit), 'subnetwork': my_subnetwork.resources.0 | default(omit) } | combine(external_access_config
| default([])) ] }}"
zone: "{{ item.zone | default(molecule_yml.driver.region + '-b') }}"
project: "{{ gcp_project_id }}"
scopes: "{{ molecule_yml.driver.scopes | default(['https://www.googleapis.com/auth/compute'], True) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default (omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default (omit, true) }}"
service_account_email: "{{ molecule_yml.driver.service_account_email | default(omit, true) }}"
service_account_file: "{{ molecule_yml.driver.service_account_file | default(omit, true) }}"
auth_kind: "{{ molecule_yml.driver.auth_kind | default(omit, true) }}"
register: async_results
loop: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
loop: "{{ async_results.results }}"
register: server
until: server.finished
retries: 300
delay: 10
notify:
- "Populate instance config dict Windows"
- "Convert instance config dict to a list"
- "Dump instance config"
- Populate instance config dict Windows
- Convert instance config dict to a list
- Dump instance config

- name: Wait for WinRM
wait_for:
ansible.builtin.wait_for:
port: 5986
host: "{{ item.networkInterfaces.0.accessConfigs.0.natIP if molecule_yml.driver.external_access else item.networkInterfaces.0.networkIP }}"
delay: 10
loop: "{{ server.results }}"

- name: Prepare Windows User
script: ./files/windows_auth.py --instance {{ item.name }} --zone {{ item.zone | default(molecule_yml.driver.region + '-b') }} --project {{ gcp_project_id }} --username molecule_usr
ansible.builtin.script:
./files/windows_auth.py --instance {{ item.name }} --zone {{ item.zone | default(molecule_yml.driver.region + '-b') }} --project {{ gcp_project_id
}} --username molecule_usr
args:
executable: python3
loop: "{{ molecule_yml.platforms }}"
Expand All @@ -57,7 +61,7 @@
delay: 10

- name: Add password for instances in server list
set_fact:
win_instances: "{{ win_instances|default([]) + [dict(item[0], password=item[1].stdout_lines |last)] }}"
ansible.builtin.set_fact:
win_instances: "{{ win_instances | default([]) + [dict(item[0], password=item[1].stdout_lines | last)] }}"
loop: "{{ server.results | zip(password.results) | list }}"
no_log: true
2 changes: 1 addition & 1 deletion test/gce/scenarios/linux/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
gather_facts: false
tasks:
- name: Example assertion
assert:
ansible.builtin.assert:
that: true
6 changes: 3 additions & 3 deletions test/gce/scenarios/windows/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
- name: Converge
hosts: all
tasks:
- name: "Include testrole"
include_role:
name: "testrole"
- name: Include testrole
ansible.builtin.include_role:
name: testrole
Loading