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

Create unit tests for ssg.id_translate #9624

Merged
merged 11 commits into from
Oct 20, 2022

Conversation

jan-cerny
Copy link
Collaborator

Description:

Create unit tests for ssg.id_translate

Rationale:

Increases testing of build scripts.

@jan-cerny jan-cerny added the Infrastructure Our content build system label Oct 4, 2022
@jan-cerny jan-cerny added this to the 0.1.65 milestone Oct 4, 2022
@github-actions
Copy link

github-actions bot commented Oct 4, 2022

Start a new ephemeral environment with changes proposed in this pull request:

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

@jan-cerny
Copy link
Collaborator Author

I have rebased this PR on the top of the latest upstream branch to include the fix for the duplicate CCE problems.

Copy link
Member

@matejak matejak left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, I have some questions and suggestions though.
My main concern is that these tests are not really readable, and that they may be fragile.

It may be that there is no other way, but we need to look into that.

@@ -21,3 +26,172 @@ def test__split_namespace():
ns, n = sn("{}emptynamespace")
assert not ns
assert n == "emptynamespace"


def test_tagname_to_abbrev():
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to define a mapping `tagname->expected abbreviation, and just iterate over the pairs?

@@ -0,0 +1,52 @@
<?xml version="1.0"?>
Copy link
Member

Choose a reason for hiding this comment

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

What about editing the OVAL, so it contains less verbose and more versatile data? For example, the ID could be rewritten to RULE_ID, and things that are not tested and not required by the build system code for smooth finish could get removed from the XML file.

return ET.parse(draft_oval_path).getroot()


def test_idtranslator_translate_oval(idtranslator, oval_tree):
Copy link
Member

Choose a reason for hiding this comment

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

I see that this test takes an OVAL tree, and "translates" it, whatever it means.
I would expect this test to focus on expected differences between original and translated (or expected lack of differences). Instead, the test is mainly about making sure that the translated tree has certain properties, and it does so without referencing the original.

@jan-cerny
Copy link
Collaborator Author

I have refactor the test using a dictionary and I have removed some data from OVAL to reduce test data input and I have changed the unit tests for translate() and I have rebased on the latest upstream master.

@jan-cerny jan-cerny self-assigned this Oct 7, 2022
@jan-cerny jan-cerny requested a review from matejak October 7, 2022 13:39
@jan-cerny jan-cerny assigned matejak and unassigned jan-cerny Oct 10, 2022
@openshift-merge-robot openshift-merge-robot added the needs-rebase Used by openshift-ci bot. label Oct 12, 2022
We will remove some data from the fixture used in tests of
ssg.id_translate.IDTranslator.translate() method, because these
data aren't important for the behavior of the method. They only
add noise to the test, we should strive to form just minimal
fixtures in our units. Their purpose isn't to be a valid OVAL.
When we can define a dictionary of expected results we can iterate over
them in a for loop and have only 1 assert statement in the test.
This commit adds 2 new very simple unit tests for the
ssg.id_translate.IDTranslator.translate() method:
- test_idtranslator_translate_oval_ids - focus on translated
  IDs
- test_idtranslator_translate_oval_differences - focus on changes
  in OVAL tree caused by the translator
The test test_idtranslator_translate_oval_differences was missing
some asserts - let's make it more complete.
The xmldiff library compares 2 xml trees and returns a diff in a form of
a list of differences which describe specific changes such as added
element or changed attribute, and we can easily create a list of
expected differences and compare them.  Unfortunately, it works with
lxml trees, so we have to convert our xml.etree.elementree trees to lxml
trees. Also, it requires adding new dependencies to our CI.
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Used by openshift-ci bot. label Oct 12, 2022
@jan-cerny
Copy link
Collaborator Author

@matejak I have add a test that uses the xml diff using the xmldiff library.

Also, I have rebased this PR on the top of the latest upstream master branch.

Based on discussion with the team I have removed the tests that check
expected properties of the output XML tree from the translate method,
because we will prefer the test based on xmldiff that allows us to
compare the expected diff with the actual diff.
@jan-cerny
Copy link
Collaborator Author

Based on discussion with the team I have removed the tests that check
expected properties of the output XML tree from the translate method,
because we will prefer the test based on xmldiff that allows us to
compare the expected diff with the actual diff.

@jan-cerny
Copy link
Collaborator Author

@matejak Are there any further changes needed?

f'{o}:criteria/{o}:criterion[1]'),
name='test_ref',
value='oval:ssg-test_kerberos_disable_no_keytab:tst:1')
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@matejak said that it would be better if the expected items are removed from the diff and finally it would be checked if the diff is empty.

We will remove each found expected item from the diff and at the
end of the test we will test whether the diff is empty IOW that it
doesn't contain any unexpected items.
@jan-cerny
Copy link
Collaborator Author

I have removed the expected items from the diff

@codeclimate
Copy link

codeclimate bot commented Oct 20, 2022

Code Climate has analyzed commit 3c3d4bb and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 41.6% (0.5% change).

View more on Code Climate.

@matejak
Copy link
Member

matejak commented Oct 20, 2022

Thank you for the PR with those tests, I appreciate them, and we can think of any possible improvements of their readability later - it is important to start with something solid first, and this is what the PR brings.

@matejak matejak merged commit c599fd6 into ComplianceAsCode:master Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Infrastructure Our content build system XCCDF12
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants