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

Assert for exact facts match on fact related tests #36

Merged
merged 1 commit into from
Sep 22, 2017
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
75 changes: 75 additions & 0 deletions camayoc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,81 @@
"""Values usable by multiple test modules."""
from camayoc import utils

RHO_CONNECTION_FACTS = (
'connection.host',
'connection.port',
'connection.uuid',
)
"""List of RHO's connection facts."""

RHO_JBOSS_FACTS = (
'jboss.installed-versions',
'jboss.deploy-dates',
'jboss.running-versions',
'jboss.brms.kie-api-ver',
'jboss.brms.drools-core-ver',
'jboss.brms.kie-war-ver',
'jboss.fuse.activemq-ver',
'jboss.fuse.camel-ver',
'jboss.fuse.cxf-ver',
)
"""List of RHO's jboss facts."""

RHO_RHEL_FACTS = (
'cpu.bogomips',
'cpu.count',
'cpu.cpu_family',
'cpu.model_name',
'cpu.model_ver',
'cpu.socket_count',
'cpu.vendor_id',
'date.anaconda_log',
'date.date',
'date.filesystem_create',
'date.machine_id',
'date.yum_history',
'dmi.bios-vendor',
'dmi.bios-version',
'dmi.processor-family',
'dmi.system-manufacturer',
'etc-issue.etc-issue',
'etc_release.name',
'etc_release.release',
'etc_release.version',
'instnum.instnum',
'redhat-packages.is_redhat',
'redhat-packages.last_built',
'redhat-packages.last_installed',
'redhat-packages.num_installed_packages',
'redhat-packages.num_rh_packages',
'redhat-release.name',
'redhat-release.release',
'redhat-release.version',
'subman.cpu.core(s)_per_socket',
'subman.cpu.cpu(s)',
'subman.cpu.cpu_socket(s)',
'subman.has_facts_file',
'subman.virt.host_type',
'subman.virt.is_guest',
'subman.virt.uuid',
'systemid.system_id',
'systemid.username',
'uname.all',
'uname.hardware_platform',
'uname.hostname',
'uname.kernel',
'uname.os',
'uname.processor',
'virt-what.type',
'virt.num_guests',
'virt.num_running_guests',
'virt.type',
'virt.virt',
)
"""List of RHO's RHEL facts."""

RHO_DEFAULT_FACTS = RHO_CONNECTION_FACTS + RHO_JBOSS_FACTS + RHO_RHEL_FACTS
"""List of RHO's default facts."""

MASKED_PASSWORD_OUTPUT = '\*{8}'
"""Regex that matches password on outputs."""
Expand Down
77 changes: 73 additions & 4 deletions camayoc/tests/rho/test_fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
"""
import csv
import hashlib
import random
from collections import OrderedDict
from io import BytesIO

import pexpect
import pytest

from camayoc import utils
from camayoc.constants import (
RHO_CONNECTION_FACTS,
RHO_DEFAULT_FACTS,
RHO_JBOSS_FACTS,
)


def test_fact_list():
Expand All @@ -30,10 +37,11 @@ def test_fact_list():
rho_fact_list.logfile = BytesIO()
rho_fact_list.expect(pexpect.EOF)
output = rho_fact_list.logfile.getvalue().decode('utf-8')
assert len(output.splitlines()) == 60
rho_fact_list.logfile.close()
rho_fact_list.close()
assert rho_fact_list.exitstatus == 0
facts = [line.split(' - ')[0].strip() for line in output.splitlines()]
assert sorted(facts) == sorted(RHO_DEFAULT_FACTS)


def test_fact_list_filter():
Expand All @@ -45,12 +53,15 @@ def test_fact_list_filter():
:steps: Run ``rho fact list --filter <filter>``
:expectedresults: Only the facts that match the filter are printed.
"""
rho_fact_list = pexpect.spawn('rho fact list --filter connection')
facts = random.choice((RHO_CONNECTION_FACTS, RHO_JBOSS_FACTS))
fact_filter = facts[0].split('.')[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like you are randomly chooses which to test.
What motivated that choice? Will it be easy to tell which once failed if we get a failure?

I would think instead to parametrize the test on this list and test both, that way if we get failures easy to figure out which one failed?

Maybe it would be clear if it failed, I'm not 100% certain what failure out put would be for this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial thought for this test was to ensure the fact list can be filtered, so it does really matter which fact subset we are looking for. I decided to use random to make sure we can test different values and try to reduce the chance to work only for an specific subset.

Having a parametrized test is a good idea. But I would recommend something slightly different, since it accepts a regex I would parametrize by a string as a field or a real regex. This improvement deserve an issue by its own I think.

If you agree, let's open an issue to test using a string or a regex and we can leave this as it is and improve later. The goal here is to match for the exact expected facts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
You'll open the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just created #37 to track this

rho_fact_list = pexpect.spawn(
'rho fact list --filter {}'.format(fact_filter))
rho_fact_list.logfile = BytesIO()
rho_fact_list.expect(pexpect.EOF)
output = rho_fact_list.logfile.getvalue().decode('utf-8')
assert len(output.splitlines()) == 3
for fact in ('connection.port', 'connection.port', 'connection.uuid'):
assert len(output.splitlines()) == len(facts)
for fact in facts:
assert fact in output
rho_fact_list.logfile.close()
rho_fact_list.close()
Expand Down Expand Up @@ -99,3 +110,61 @@ def test_fact_hash(isolated_filesystem):
assert len(rows) == 1
parsed_facts = rows[0]
assert parsed_facts == expected_hashed_facts


@pytest.mark.parametrize('facts_value', ('file', 'string'))
def test_fact_hash_with_facts(isolated_filesystem, facts_value):
"""Hash only the specified facts from a generated report.

:id: 3ae721ff-f910-4f09-971e-bf96d0832df5
:description: Hash only the specified facts from a generated report.
:steps: Run ``rho fact hash --facts <facts> --reportfile <reportfile>
--outputfile <outputfile>``
:expectedresults: Only the specified facts are hashed.
"""
facts = OrderedDict({
'connection.host': utils.uuid4(),
'connection.port': utils.uuid4(),
'uname.all': utils.uuid4(),
'uname.hostname': utils.uuid4(),
})
facts_to_hash = [
'connection.host',
'connection.port',
]
reportfile = utils.uuid4()
outputfile = utils.uuid4()
with open(reportfile, 'w') as f:
f.write(','.join(facts.keys()) + '\n')
f.write(','.join(facts.values()) + '\n')

if facts_value == 'file':
facts_value = 'facts_file'
with open(facts_value, 'w') as handler:
for fact in facts_to_hash:
handler.write(fact + '\n')
else:
facts_value = ' '.join(facts_to_hash)
rho_fact_hash = pexpect.spawn(
'rho fact hash --facts {} --reportfile {} --outputfile {}'
.format(facts_value, reportfile, outputfile)
)
rho_fact_hash.logfile = BytesIO()
rho_fact_hash.expect(pexpect.EOF)
output = rho_fact_hash.logfile.getvalue().decode('utf-8')
assert len(output.splitlines()) == len(facts_to_hash)
rho_fact_hash.logfile.close()
rho_fact_hash.close()
assert rho_fact_hash.exitstatus == 0

expected_hashed_facts = facts.copy()
for fact in facts_to_hash:
expected_hashed_facts[fact] = hashlib.sha256(
expected_hashed_facts[fact].encode('utf-8')).hexdigest()

with open(outputfile) as f:
reader = csv.DictReader(f)
rows = list(reader)
assert len(rows) == 1
parsed_facts = rows[0]
assert parsed_facts == expected_hashed_facts