Skip to content

Commit

Permalink
CIV: Add new test for otel package
Browse files Browse the repository at this point in the history
  • Loading branch information
sshmulev committed May 7, 2024
1 parent 4b66824 commit ebe1a19
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions test_suite/package/test_otel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import pytest
import logging
import time
import json

from test_suite.generic.test_generic import TestsSubscriptionManager
logger = logging.getLogger(__name__)


@pytest.fixture()
def check_instance_status(request, instance_data, host):
instance_id = instance_data['instance_id']
command_to_run = [
'aws', 'ec2', 'describe-instance-status',
'--instance-ids', instance_id
]

while True:
command_output = host.backend.run_local(" ".join(command_to_run)).stdout
cmd_output = json.loads(command_output)

instance_status = cmd_output["InstanceStatuses"][0]["InstanceStatus"]["Status"]
system_status = cmd_output["InstanceStatuses"][0]["SystemStatus"]["Status"]

if instance_status == "ok" and system_status == "ok":
break
logger.info("Instance status is not 'passed' yet. Waiting...")
time.sleep(5)


@pytest.fixture()
def modify_iam_role(request, instance_data, host):
instance_id = instance_data['instance_id']
iam_role_name = "CloudWatchAgentServerRole_2"

command_to_run = [
'aws', 'ec2', 'associate-iam-instance-profile',
'--instance-id', instance_id,
'--region', 'us-west-1',
'--iam-instance-profile', 'Name="{}"'.format(iam_role_name)
]
modify_iam_role_cmd = ' '.join(command_to_run)

assert host.backend.run_local(modify_iam_role_cmd), 'faild to update iam role'


@pytest.fixture()
def install_packages(request, host):
class_instance = request.node.cls
install_cmd = f'yum install -y {class_instance.package_name} --nogpgcheck'
repo_path = ('https://copr.fedorainfracloud.org/coprs/miyunari'
'/redhat-opentelemetry-collector/repo/rhel-9/miyunari-redhat-opentelemetry-collector-rhel-9.repo')
with host.sudo():
assert host.run_test(f'yum-config-manager --add-repo {repo_path}')
assert host.run_test(install_cmd), f'Failed to install the package {class_instance.package_name}'
output = host.check_output("rpm -qa | grep opentelemetry*")
print(output)


@pytest.fixture()
def start_service(request, host):
class_instance = request.node.cls
start_enable_service = (f'systemctl start {class_instance.package_name} && '
f'systemctl enable {class_instance.package_name}')
is_active = (f'systemctl is-active {class_instance.package_name}')

with host.sudo():
assert host.run(start_enable_service).succeeded, (f'Failed to start the service {class_instance.package_name}')
assert host.run(is_active).succeeded, (f'Failed to activate the service {class_instance.package_name}')

def finalizer():
logger.info(f'Removing the package {class_instance.package_name}')
assert host.run(f'sudo yum remove -y {class_instance.package_name}')
assert not host.check_output(f'rpm -q {class_instance.package_name}')
assert not host.run(f'ssh {class_instance.instance_dns}').succeeded
logger.info("Verify logs don't appear")
# TODO: verify logs don't appear.
request.addfinalizer(finalizer)


@pytest.fixture(scope="function", autouse=True)
def run_subscription_manager_auto(request, host, instance_data):
class_instance = request.node.cls
# Run the subscription manager auto test before any tests in this file
TestsSubscriptionManager.test_subscription_manager_auto(class_instance, host, instance_data)


@pytest.mark.package
@pytest.mark.run_on(['rhel9.1'])
class TestOtel:
package_name = 'opentelemetry-collector-cloudwatch-config'

def check_aws_cli_logs(self, host):
command_to_run = [
'aws', 'logs', 'filter-log-events',
'--log-stream-names', 'testing-integrations-stream-emf',
'--filter-pattern', 'Invalid',
'--log-group-name', 'testing-logs-emf'
]
run_aws_cli_cmd = ' '.join(command_to_run)

command_output = host.backend.run_local(run_aws_cli_cmd), 'Failed to get log output'
assert "Invalid" in command_output

@pytest.mark.usefixtures(
check_instance_status.__name__,
install_packages.__name__,
modify_iam_role.__name__,
start_service.__name__,
)
def test_otel(self, host, instance_data):
"""
Verify basic funstionality for Otel package:
- Install the package.
- Start the service.
- Modify IAM role.
- Make a failure ssh connection to the instance.
- Check for error messages in the ssh logs cotaining within the instance.
- Check the error logs with AWS CLI and compare it to the logs in "/var/log/secure".
Finalize:
- Remove the package from the instance and verify it's not present anymore.
- Try a failure ssh again and check that the logs don't appear.
"""
logger.info("Connect to the instance without a key in order to fail")
instance_dns = instance_data['public_dns']
assert not host.backend.run_local(f'ssh {instance_dns}')
logger.info("Check for error logs in the instance logs")
host_output = host.check_output('sudo tail -F /var/log/secure')
assert "Invalid" in host_output, ('no logs regarding ssh connection failure exist')

logger.info("Check for error logs in aws cli logs")
self.check_aws_cli_logs(host)

0 comments on commit ebe1a19

Please sign in to comment.