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

Updates TestUtil.pm for unit tests #1340

Merged
merged 32 commits into from
May 22, 2024

Conversation

matsduf
Copy link
Contributor

@matsduf matsduf commented May 6, 2024

Purpose

This PR introduces two changes to function in TestUtil.pm:

  • Support of undelegated data (both NS and DS) for unit tests.
  • Makes the function more flexible to avoid long lists of tags when not needed.
  • Do not use diag/fail when there is an error in the t file. Use croak() instead.
  • More verifications of data.

The unit test files using TestUtil.pm have been updated to match the new format.

Five new scenarios have been added to Consistency05 and two to Consistency06. Both updates are to unit tests that uses the new function in TestUtil.pm that supports undelegated data.

The following new scenario fail:

  • MULT-SOA-MNAMES-NO-DEL-UNDEL-2 (Consistency06)

Context

The update is needed for zonemaster/zonemaster#1255.

How to test this PR

Review TestUtil.pm. If the unit test files using TestUtil.pm then they should be fine.

@matsduf matsduf added this to the v2024.1 milestone May 6, 2024
t/TestUtil.pm Outdated
Comment on lines 47 to 72
(in all uppercase), and their corresponding values are an array of:

=over

=item *
a boolean (testable)

=item *
a string (zone name)

=item *
an array of strings (all test case message tags)

=item *
an array of strings (mandatory message tags)

=item *
an array of strings (forbidden message tags)

=item *
an array of name server expressions for undelegated name servers

=item *
an array of DS expressions for "undelegated" DS

=back
Copy link
Member

Choose a reason for hiding this comment

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

These are a lot of positional arguments. I suggest replacing the arrayref of positional arguments with a hashref of named arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that I understand what you mean, but that will make it more complex to create e.g. t/Test-nameserver15.t and harder to read it. Please note that "all test case message tags" will just be a variable, and that the two last arguments will not be used in most cases.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how you're measuring complexity in the call site. The calling code would be easier to read and less error prone because you wouldn't run the risk of mixing up the arguments. There would be more code inside perform_testcase_testing() but this is a quite good investment.

t/TestUtil.pm Outdated
Comment on lines 119 to 123
if ( ref( $testable ) ne '' ) {
diag("Scenario $scenario: Type of testable must not be a reference");
fail("Testable is of the correct type");
next;
}
Copy link
Member

Choose a reason for hiding this comment

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

This is problematic for a couple of reasons, but the biggest one is that the diag() and fail() methods are uses here for complaining about the test script. They should only be used for complaining about the object under test. When complaining about the test script it's better to use croak().

There are more of these below.

I do realize you followed the established pattern, but it would be a really good to break this pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not new code. It is the same code as in the current version, but moved up. Could you suggest the replacement code?

Copy link
Member

Choose a reason for hiding this comment

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

Just replace the diag, fail and next with a croak.

t/TestUtil.pm Outdated

if ( ref( $zone_name ) ne '' ) {
diag("Scenario $scenario: Type of zone name must not be a reference");
fail("Zone name is of the correct type");
next;
}

if ( ref( $mandatory_message_tags ) ne 'ARRAY' ) {
if ( ref( $all_test_case_tags ) ne 'ARRAY' ) {
Copy link
Member

Choose a reason for hiding this comment

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

If we care to verify that it's an array, shouldn't we also check the type of the array elements?

There are more of these below.

Copy link
Contributor Author

@matsduf matsduf May 6, 2024

Choose a reason for hiding this comment

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

This is not new code. It is the same code as in the current version, but maybe moved. What you suggest is that all elements in the array should be matching the tag format? I guess that could be done. Should that also use croak() instead of diag/fail? If so, what is the benefit of croak?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think it’s useful to check the type of the array elements: because the array is usually prepared using qw(), it is reasonable to assume that each element can be treated as a string. I’m not saying this kind of thorough checking shouldn’t be done; only that I don’t mind if it isn’t done.

However, I do agree that we should use croak instead of diag and fail here. We don’t want to raise an issue with the domain under test here, but with the code that tests the code. In that case, aborting early gives a stronger signal that the problem is with the testing code.

t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
t/Test-nameserver15.t Outdated Show resolved Hide resolved
matsduf and others added 7 commits May 6, 2024 23:31
Co-authored-by: Mattias Päivärinta <mattias@paivarinta.se>
* Replaces diag/fail with croak().
* Additional verification of test data from t file.
* Moved "all tags" to be a common parameter, not scenario specific.
@matsduf matsduf requested a review from mattias-p May 7, 2024 13:20
@matsduf
Copy link
Contributor Author

matsduf commented May 7, 2024

@mattias-p, please re-review.

@matsduf
Copy link
Contributor Author

matsduf commented May 13, 2024

@mattias-p, could your re-review?

t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
my %subtests = (
'NO-RESPONSE-MX-QUERY' => [
q(no-response-mx-query.zone09.xa),
1,
q(no-response-mx-query.zone09.xa),#
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there a trailing # here? (many other occurrences below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tagged scenarios where MANDATORY and FORBIDDEN did not cover all tags. I'll make it explicit.

t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
matsduf and others added 2 commits May 14, 2024 13:59
Co-authored-by: tgreenx <96772376+tgreenx@users.noreply.github.com>
@matsduf matsduf requested a review from tgreenx May 14, 2024 12:28
@matsduf
Copy link
Contributor Author

matsduf commented May 14, 2024

@tgreenx and @mattias-p, please re-review.

t/TestUtil.pm Outdated Show resolved Hide resolved
@matsduf matsduf added the V-Patch Versioning: The change gives an update of patch in version. label May 18, 2024
@matsduf matsduf requested a review from tgreenx May 18, 2024 20:45
@matsduf
Copy link
Contributor Author

matsduf commented May 21, 2024

@mattias-p, @tgreenx and @marc-vanderwal, can you approve this? I would like to merge it. It updates TestUtils.pm and the unit tests that use it. They pass as expected.

t/Test-zone11.t Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
t/TestUtil.pm Show resolved Hide resolved
@marc-vanderwal
Copy link
Contributor

I’ve had a look. I see no issues that haven’t already been brought up by @tgreenx.

matsduf and others added 2 commits May 21, 2024 15:09
Co-authored-by: tgreenx <96772376+tgreenx@users.noreply.github.com>
@matsduf
Copy link
Contributor Author

matsduf commented May 21, 2024

@marc-vanderwal and @tgreenx, please re-review after my latest updates.

@matsduf matsduf requested a review from tgreenx May 21, 2024 13:22
Copy link
Contributor

@marc-vanderwal marc-vanderwal left a comment

Choose a reason for hiding this comment

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

Looks fine. I do think that croak is better than diag/fail when there is a problem with the data in the test script. It’s better to have a specific failure mode for problems with the testing code, that can’t be confused with the outcome of a failing unit test suite for reasons unrelated to the testing code itself.

t/TestUtil.pm Outdated

if ( ref( $zone_name ) ne '' ) {
diag("Scenario $scenario: Type of zone name must not be a reference");
fail("Zone name is of the correct type");
next;
}

if ( ref( $mandatory_message_tags ) ne 'ARRAY' ) {
if ( ref( $all_test_case_tags ) ne 'ARRAY' ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think it’s useful to check the type of the array elements: because the array is usually prepared using qw(), it is reasonable to assume that each element can be treated as a string. I’m not saying this kind of thorough checking shouldn’t be done; only that I don’t mind if it isn’t done.

However, I do agree that we should use croak instead of diag and fail here. We don’t want to raise an issue with the domain under test here, but with the code that tests the code. In that case, aborting early gives a stronger signal that the problem is with the testing code.

t/TestUtil.pm Outdated
}

if ( ! defined( $mandatory_message_tags ) and !defined( $forbidden_message_tags ) ) {
diag("Scenario $scenario: Not both array of mandatory tags and array of forbidden tags can be undefined");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
diag("Scenario $scenario: Not both array of mandatory tags and array of forbidden tags can be undefined");
diag("Scenario $scenario: Arrays of mandatory tags and forbidden tags cannot be both undefined");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your proposal must be based on old code. The current code uses "croak" for such tests.

t/TestUtil.pm Show resolved Hide resolved
@matsduf matsduf requested a review from marc-vanderwal May 21, 2024 14:59
@matsduf
Copy link
Contributor Author

matsduf commented May 22, 2024

@marc-vanderwal and @tgreenx, please re-review after my latest updates. All tests pass. TestUtil now treats empty-empty as an error. Can this be merged now?

Copy link
Contributor

@tgreenx tgreenx left a comment

Choose a reason for hiding this comment

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

@marc-vanderwal and @tgreenx, please re-review after my latest updates. All tests pass. TestUtil now treats empty-empty as an error. Can this be merged now?

Almost! A few nits left:

t/TestUtil.pm Show resolved Hide resolved
t/TestUtil.pm Outdated Show resolved Hide resolved
matsduf and others added 2 commits May 22, 2024 10:56
Co-authored-by: tgreenx <96772376+tgreenx@users.noreply.github.com>
@matsduf
Copy link
Contributor Author

matsduf commented May 22, 2024

I have commited the proposal from @tgreenx and I have updated the documentation so that it states that empty-empty is not permitted. @marc-vanderwal and @tgreenx, please re-review.

@matsduf matsduf requested a review from tgreenx May 22, 2024 09:02
t/TestUtil.pm Outdated Show resolved Hide resolved
@matsduf matsduf requested a review from marc-vanderwal May 22, 2024 09:03
Co-authored-by: Marc van der Wal <103426270+marc-vanderwal@users.noreply.github.com>
@matsduf
Copy link
Contributor Author

matsduf commented May 22, 2024

@marc-vanderwal and @tgreenx, please re-review.

@matsduf matsduf merged commit aeb9b63 into zonemaster:develop May 22, 2024
3 checks passed
@matsduf matsduf deleted the update-unit-test-testutil branch May 22, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V-Patch Versioning: The change gives an update of patch in version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants