Skip to content

Instantly share code, notes, and snippets.

@wabmca
Created August 10, 2021 09:23
Show Gist options
  • Save wabmca/2868b1a804e73e74af0985a79c6e3ba3 to your computer and use it in GitHub Desktop.
Save wabmca/2868b1a804e73e74af0985a79c6e3ba3 to your computer and use it in GitHub Desktop.
my_gist_2;why test cases are executed locally
test_default.py
*******************
"""Role testing files using testinfra."""
import os
import pytest
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
@pytest.mark.parametrize('pkg', [
'sensu-go-agent-5.21.5-1.el7.x86_64.rpm'])
def test_pkg(host, pkg):
package = host.package(pkg)
assert package.is_installed
@pytest.mark.parametrize('svc', [
'sensu-agent'])
def test_svc(host, svc):
service = host.service(svc)
assert service.is_running
assert service.is_enabled
***************************************
ansible_inventory.yml
# Molecule managed
***************************************
---
all:
hosts:
instance: &id001
ansible_host: 172.27.75.166
ansible_port: 22
ansible_private_key_file: /home/gaian/.cache/molecule/AnsibleRoles.opensource-sensugo/install_sensu/ssh_key
ansible_ssh_common_args: -o UserKnownHostsFile=/dev/null -o ControlMaster=auto
-o ControlPersist=60s -o ForwardX11=no -o LogLevel=ERROR -o IdentitiesOnly=yes
-o StrictHostKeyChecking=no
ansible_user: centos
connection: ssh
vars:
molecule_ephemeral_directory: '{{ lookup(''env'', ''MOLECULE_EPHEMERAL_DIRECTORY'')
}}'
molecule_file: '{{ lookup(''env'', ''MOLECULE_FILE'') }}'
molecule_instance_config: '{{ lookup(''env'', ''MOLECULE_INSTANCE_CONFIG'') }}'
molecule_no_log: '{{ lookup(''env'', ''MOLECULE_NO_LOG'') or not molecule_yml.provisioner.log|default(False)
| bool }}'
molecule_scenario_directory: '{{ lookup(''env'', ''MOLECULE_SCENARIO_DIRECTORY'')
}}'
molecule_yml: '{{ lookup(''file'', molecule_file) | from_yaml }}'
ungrouped:
hosts:
instance: *id001
vars: {}
*******************************************************************************************************************
***************************************
Execution console:
***************************************
molecule test -s install_sensu
INFO install_sensu scenario test matrix: dependency, lint, cleanup, destroy, syntax, create, prepare, converge, idempotence, side_effect, verify, cleanup, destroy
INFO Performing prerun...
INFO Added ANSIBLE_ROLES_PATH=~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:roles
INFO Running install_sensu > dependency
WARNING Skipping, missing the requirements file.
WARNING Skipping, missing the requirements file.
INFO Running install_sensu > lint
INFO Lint is disabled.
INFO Running install_sensu > cleanup
WARNING Skipping, cleanup playbook not configured.
INFO Running install_sensu > destroy
PLAY [Destroy] ************************************************************************************************************
TASK [Populate instance config] *******************************************************************************************
ok: [localhost]
TASK [Destroy molecule instance(s)] ***************************************************************************************
TASK [Wait for instance(s) deletion to complete] **************************************************************************
TASK [Populate instance config] *******************************************************************************************
ok: [localhost]
TASK [Dump instance config] ***********************************************************************************************
skipping: [localhost]
PLAY RECAP ****************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
INFO Running install_sensu > syntax
playbook: /home/gaian/snaplogic/AnsibleRoles.opensource-sensugo/molecule/install_sensu/converge.yml
INFO Running install_sensu > create
PLAY [Create] *************************************************************************************************************
TASK [Find the vpc for the subnet] ****************************************************************************************
ok: [localhost] => (item={'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'})
TASK [Create security groups] *********************************************************************************************
ok: [localhost] => (item={'subnets': [{'availability_zone': 'us-west-2c', 'availability_zone_id': 'usw2-az3', 'available_ip_address_count': 43, 'cidr_block': '172.27.75.128/26', 'default_for_az': False, 'map_public_ip_on_launch': False, 'map_customer_owned_ip_on_launch': False, 'state': 'available', 'subnet_id': 'subnet-3c6e3767', 'vpc_id': 'vpc-a2bd41c4', 'owner_id': '694702677705', 'assign_ipv6_address_on_creation': False, 'ipv6_cidr_block_association_set': [], 'tags': {'Application': 'magpie', 'Name': 'devqav2_vpc_x75_sn3_usw2c'}, 'subnet_arn': 'arn:aws:ec2:us-west-2:694702677705:subnet/subnet-3c6e3767', 'id': 'subnet-3c6e3767'}], 'invocation': {'module_args': {'subnet_ids': ['subnet-3c6e3767'], 'debug_botocore_endpoint_logs': False, 'validate_certs': True, 'filters': {}, 'ec2_url': None, 'aws_secret_key': None, 'aws_access_key': None, 'security_token': None, 'profile': None, 'region': None}}, 'failed': False, 'changed': False, 'item': {'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'}, 'ansible_loop_var': 'item'})
TASK [Test for presence of local key pair] ********************************************************************************
ok: [localhost]
TASK [Delete remote key pair] *********************************************************************************************
changed: [localhost]
TASK [Create key pair] ****************************************************************************************************
changed: [localhost]
TASK [Persist the key pair] ***********************************************************************************************
changed: [localhost]
TASK [Get the ec2 ami(s) by owner and name, if image not set] *************************************************************
skipping: [localhost] => (item={'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'})
TASK [Create molecule instance(s)] ****************************************************************************************
changed: [localhost] => (item={'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'})
TASK [Wait for instance(s) creation to complete] **************************************************************************
FAILED - RETRYING: Wait for instance(s) creation to complete (300 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (299 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (298 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (297 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (296 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (295 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (294 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (293 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (292 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (291 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (290 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (289 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (288 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (287 retries left).
FAILED - RETRYING: Wait for instance(s) creation to complete (286 retries left).
changed: [localhost] => (item={'started': 1, 'finished': 0, 'ansible_job_id': '49377413935.42539', 'results_file': '/home/gaian/.ansible_async/49377413935.42539', 'changed': True, 'failed': False, 'item': {'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'}, 'ansible_loop_var': 'item', 'index': 0, 'ansible_index_var': 'index'})
TASK [Populate instance config dict] **************************************************************************************
ok: [localhost] => (item={'changed': True, 'instance_ids': ['i-0df89ec85ea354cdf'], 'instances': [{'id': 'i-0df89ec85ea354cdf', 'ami_launch_index': '0', 'private_ip': '172.27.75.166', 'private_dns_name': 'ip-172-27-75-166.us-west-2.compute.internal', 'public_ip': None, 'dns_name': '', 'public_dns_name': '', 'state_code': 16, 'architecture': 'x86_64', 'image_id': 'ami-03d00124ebd4385ec', 'key_name': 'molecule_key', 'placement': 'us-west-2c', 'region': 'us-west-2', 'kernel': None, 'ramdisk': None, 'launch_time': '2021-08-09T14:21:57.000Z', 'instance_type': 't2.micro', 'root_device_type': 'ebs', 'root_device_name': '/dev/sda1', 'state': 'running', 'hypervisor': 'xen', 'tags': {'instance': 'instance'}, 'groups': {'sg-0667a485ce5b7a9c1': 'molecule_sg'}, 'virtualization_type': 'hvm', 'ebs_optimized': False, 'block_device_mapping': {'/dev/sda1': {'status': 'attached', 'volume_id': 'vol-013164544ded01f28', 'delete_on_termination': True}}, 'tenancy': 'default'}], 'tagged_instances': [{'id': 'i-0df89ec85ea354cdf', 'ami_launch_index': '0', 'private_ip': '172.27.75.166', 'private_dns_name': 'ip-172-27-75-166.us-west-2.compute.internal', 'public_ip': None, 'dns_name': '', 'public_dns_name': '', 'state_code': 16, 'architecture': 'x86_64', 'image_id': 'ami-03d00124ebd4385ec', 'key_name': 'molecule_key', 'placement': 'us-west-2c', 'region': 'us-west-2', 'kernel': None, 'ramdisk': None, 'launch_time': '2021-08-09T14:21:57.000Z', 'instance_type': 't2.micro', 'root_device_type': 'ebs', 'root_device_name': '/dev/sda1', 'state': 'running', 'hypervisor': 'xen', 'tags': {'instance': 'instance'}, 'groups': {'sg-0667a485ce5b7a9c1': 'molecule_sg'}, 'virtualization_type': 'hvm', 'ebs_optimized': False, 'block_device_mapping': {'/dev/sda1': {'status': 'attached', 'volume_id': 'vol-013164544ded01f28', 'delete_on_termination': True}}, 'tenancy': 'default'}], 'invocation': {'module_args': {'key_name': 'molecule_key', 'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'instance_profile_name': 'ec2_create_playbook', 'vpc_subnet_id': 'subnet-3c6e3767', 'group': ['molecule_sg'], 'instance_tags': {'instance': 'instance'}, 'wait': True, 'assign_public_ip': False, 'exact_count': 1, 'count_tag': {'instance': 'instance'}, 'debug_botocore_endpoint_logs': False, 'validate_certs': True, 'spot_type': 'one-time', 'count': 1, 'monitoring': False, 'wait_timeout': 300, 'spot_wait_timeout': 600, 'state': 'present', 'instance_initiated_shutdown_behavior': 'stop', 'ebs_optimized': False, 'tenancy': 'default', 'ec2_url': None, 'aws_secret_key': None, 'aws_access_key': None, 'security_token': None, 'profile': None, 'region': None, 'id': None, 'group_id': None, 'zone': None, 'spot_price': None, 'spot_launch_group': None, 'kernel': None, 'ramdisk': None, 'placement_group': None, 'user_data': None, 'private_ip': None, 'instance_ids': None, 'source_dest_check': None, 'termination_protection': None, 'volumes': None, 'network_interfaces': None}}, 'finished': 1, 'ansible_job_id': '49377413935.42539', 'failed': False, 'attempts': 16, 'item': {'started': 1, 'finished': 0, 'ansible_job_id': '49377413935.42539', 'results_file': '/home/gaian/.ansible_async/49377413935.42539', 'changed': True, 'failed': False, 'item': {'image': 'ami-03d00124ebd4385ec', 'instance_type': 't2.micro', 'name': 'instance', 'vpc_subnet_id': 'subnet-3c6e3767'}, 'ansible_loop_var': 'item', 'index': 0, 'ansible_index_var': 'index'}, 'ansible_loop_var': 'item'})
TASK [Convert instance config dict to a list] *****************************************************************************
ok: [localhost]
TASK [Dump instance config] ***********************************************************************************************
changed: [localhost]
TASK [Wait for SSH] *******************************************************************************************************
ok: [localhost] => (item={'instance': 'instance', 'address': '172.27.75.166', 'user': 'centos', 'port': 22, 'identity_file': '/home/gaian/.cache/molecule/AnsibleRoles.opensource-sensugo/install_sensu/ssh_key', 'instance_ids': ['i-0df89ec85ea354cdf']})
TASK [Wait for boot process to finish] ************************************************************************************
Pausing for 120 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [localhost]
PLAY RECAP ****************************************************************************************************************
localhost : ok=13 changed=6 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
INFO Running install_sensu > prepare
PLAY [Prepare] ************************************************************************************************************
TASK [Make sure python3 is installed] *************************************************************************************
changed: [instance]
PLAY RECAP ****************************************************************************************************************
instance : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
INFO Running install_sensu > converge
PLAY [Converge] ***********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [instance]
TASK [Include AnsibleRoles.opensource-sensugo] ****************************************************************************
TASK [install_sensu : download agent rpm] *********************************************************************************
changed: [instance]
TASK [install_sensu : uninstall sensu core agent rpm as its conflicting with sensu go] ************************************
changed: [instance]
TASK [install_sensu : Install rubygems, sensu-go-agent] *******************************************************************
ok: [instance]
TASK [install_sensu : Install sensu-go-agent] *****************************************************************************
changed: [instance]
TASK [install_sensu : Sensu agent configuration | Lookup sensu server password] *******************************************
ok: [instance]
TASK [install_sensu : copy configuration yml to /etc/sensu] ***************************************************************
changed: [instance]
TASK [install_sensu : copy sensu-agent] ***********************************************************************************
changed: [instance]
TASK [install_sensu : copy sensu] *****************************************************************************************
changed: [instance]
TASK [install_sensu : Creates directory] **********************************************************************************
changed: [instance]
TASK [install_sensu : copy plugins] ***************************************************************************************
changed: [instance] => (item=check-disk-free.rb)
changed: [instance] => (item=check-http.rb)
changed: [instance] => (item=check-procs.rb)
changed: [instance] => (item=cpu-pcnt-usage-metrics.rb)
changed: [instance] => (item=memory-metrics.rb)
changed: [instance] => (item=vmstat-metrics.rb)
changed: [instance] => (item=check-disk.rb)
changed: [instance] => (item=check-load.rb)
changed: [instance] => (item=check-ram.rb)
changed: [instance] => (item=disk-capacity-metrics.rb)
changed: [instance] => (item=mongodb-metrics.rb)
changed: [instance] => (item=check_ec2_events.py)
changed: [instance] => (item=check-mem.sh)
changed: [instance] => (item=check-swap.sh)
changed: [instance] => (item=iostat-metrics.rb)
changed: [instance] => (item=nginx-metrics.rb)
changed: [instance] => (item=check-es-cluster-status.rb)
changed: [instance] => (item=check_mongodb.py)
changed: [instance] => (item=check-tail.rb)
changed: [instance] => (item=load-metrics.rb)
changed: [instance] => (item=uwsgi-rps-metrics.rb)
changed: [instance] => (item=check-dir-size.rb)
TASK [install_sensu : Creates directory] **********************************************************************************
changed: [instance]
TASK [install_sensu : Sensu agent configuration | Lookup cert.pem in aws ssm] *********************************************
ok: [instance]
TASK [install_sensu : copy cert.pem file] *********************************************************************************
changed: [instance]
TASK [install_sensu : Sensu server configuration | Lookup key.pem in aws ssm] *********************************************
ok: [instance]
TASK [install_sensu : copy key.pem file] **********************************************************************************
changed: [instance]
TASK [install_sensu : Start Sensu go agent service] ***********************************************************************
changed: [instance]
PLAY RECAP ****************************************************************************************************************
instance : ok=17 changed=12 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
INFO Running install_sensu > idempotence
PLAY [Converge] ***********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************
ok: [instance]
TASK [Include AnsibleRoles.opensource-sensugo] ****************************************************************************
TASK [install_sensu : download agent rpm] *********************************************************************************
ok: [instance]
TASK [install_sensu : uninstall sensu core agent rpm as its conflicting with sensu go] ************************************
ok: [instance]
TASK [install_sensu : Install rubygems, sensu-go-agent] *******************************************************************
ok: [instance]
TASK [install_sensu : Install sensu-go-agent] *****************************************************************************
ok: [instance]
TASK [install_sensu : Sensu agent configuration | Lookup sensu server password] *******************************************
ok: [instance]
TASK [install_sensu : copy configuration yml to /etc/sensu] ***************************************************************
ok: [instance]
TASK [install_sensu : copy sensu-agent] ***********************************************************************************
ok: [instance]
TASK [install_sensu : copy sensu] *****************************************************************************************
ok: [instance]
TASK [install_sensu : Creates directory] **********************************************************************************
ok: [instance]
TASK [install_sensu : copy plugins] ***************************************************************************************
ok: [instance] => (item=check-disk-free.rb)
ok: [instance] => (item=check-http.rb)
ok: [instance] => (item=check-procs.rb)
ok: [instance] => (item=cpu-pcnt-usage-metrics.rb)
ok: [instance] => (item=memory-metrics.rb)
ok: [instance] => (item=vmstat-metrics.rb)
ok: [instance] => (item=check-disk.rb)
ok: [instance] => (item=check-load.rb)
ok: [instance] => (item=check-ram.rb)
ok: [instance] => (item=disk-capacity-metrics.rb)
ok: [instance] => (item=mongodb-metrics.rb)
ok: [instance] => (item=check_ec2_events.py)
ok: [instance] => (item=check-mem.sh)
ok: [instance] => (item=check-swap.sh)
ok: [instance] => (item=iostat-metrics.rb)
ok: [instance] => (item=nginx-metrics.rb)
ok: [instance] => (item=check-es-cluster-status.rb)
ok: [instance] => (item=check_mongodb.py)
ok: [instance] => (item=check-tail.rb)
ok: [instance] => (item=load-metrics.rb)
ok: [instance] => (item=uwsgi-rps-metrics.rb)
ok: [instance] => (item=check-dir-size.rb)
TASK [install_sensu : Creates directory] **********************************************************************************
ok: [instance]
TASK [install_sensu : Sensu agent configuration | Lookup cert.pem in aws ssm] *********************************************
ok: [instance]
TASK [install_sensu : copy cert.pem file] *********************************************************************************
ok: [instance]
TASK [install_sensu : Sensu server configuration | Lookup key.pem in aws ssm] *********************************************
ok: [instance]
TASK [install_sensu : copy key.pem file] **********************************************************************************
ok: [instance]
TASK [install_sensu : Start Sensu go agent service] ***********************************************************************
ok: [instance]
PLAY RECAP ****************************************************************************************************************
instance : ok=17 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
INFO Idempotence completed successfully.
INFO Running install_sensu > side_effect
WARNING Skipping, side effect playbook not configured.
INFO Running install_sensu > verify
INFO Executing Testinfra tests found in /home/gaian/snaplogic/AnsibleRoles.opensource-sensugo/molecule/install_sensu/tests/...
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /home/gaian
plugins: testinfra-6.4.0
collected 2 items
molecule/install_sensu/tests/test_default.py FF [100%]
=================================== FAILURES ===================================
_____ test_pkg[ansible://instance-sensu-go-agent-5.21.5-1.el7.x86_64.rpm] ______
host = <testinfra.host.Host ansible://instance>
pkg = 'sensu-go-agent-5.21.5-1.el7.x86_64.rpm'
@pytest.mark.parametrize('pkg', [
'sensu-go-agent-5.21.5-1.el7.x86_64.rpm'])
def test_pkg(host, pkg):
package = host.package(pkg)
> assert package.is_installed
E assert False
E + where False = <package sensu-go-agent-5.21.5-1.el7.x86_64.rpm>.is_installed
molecule/install_sensu/tests/test_default.py:16: AssertionError
___________________ test_svc[ansible://instance-sensu-agent] ___________________
host = <testinfra.host.Host ansible://instance>, svc = 'sensu-agent'
@pytest.mark.parametrize('svc', [
'sensu-agent'])
def test_svc(host, svc):
service = host.service(svc)
assert service.is_running
> assert service.is_enabled
E assert False
E + where False = <service sensu-agent>.is_enabled
molecule/install_sensu/tests/test_default.py:25: AssertionError
=========================== short test summary info ============================
FAILED molecule/install_sensu/tests/test_default.py::test_pkg[ansible:/instance-sensu-go-agent-5.21.5-1.el7.x86_64.rpm]
FAILED molecule/install_sensu/tests/test_default.py::test_svc[ansible:/instance-sensu-agent]
============================== 2 failed in 29.43s ==============================
WARNING An error occurred during the test sequence action: 'verify'. Cleaning up.
INFO Running install_sensu > cleanup
WARNING Skipping, cleanup playbook not configured.
INFO Running install_sensu > destroy
PLAY [Destroy] ************************************************************************************************************
TASK [Populate instance config] *******************************************************************************************
ok: [localhost]
TASK [Destroy molecule instance(s)] ***************************************************************************************
changed: [localhost] => (item={'instance': 'instance', 'address': '172.27.75.166', 'user': 'centos', 'port': 22, 'identity_file': '/home/gaian/.cache/molecule/AnsibleRoles.opensource-sensugo/install_sensu/ssh_key', 'instance_ids': ['i-0df89ec85ea354cdf']})
TASK [Wait for instance(s) deletion to complete] **************************************************************************
FAILED - RETRYING: Wait for instance(s) deletion to complete (300 retries left).
changed: [localhost] => (item={'started': 1, 'finished': 0, 'ansible_job_id': '644390836971.45552', 'results_file': '/home/gaian/.ansible_async/644390836971.45552', 'changed': True, 'failed': False, 'item': {'instance': 'instance', 'address': '172.27.75.166', 'user': 'centos', 'port': 22, 'identity_file': '/home/gaian/.cache/molecule/AnsibleRoles.opensource-sensugo/install_sensu/ssh_key', 'instance_ids': ['i-0df89ec85ea354cdf']}, 'ansible_loop_var': 'item'})
TASK [Populate instance config] *******************************************************************************************
ok: [localhost]
TASK [Dump instance config] ***********************************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************************************
localhost : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
INFO Pruning extra files from scenario ephemeral directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment