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

Tests for AdminNetworkPolicy #388

Merged
merged 43 commits into from
Sep 3, 2024

Conversation

tanyaveksler
Copy link
Contributor

Added some ANP tests from policy-assistant.
Fixed a small bug in handling named ports in ANP

Fixed a small bug in handling named ports in ANP
@tanyaveksler tanyaveksler self-assigned this Jul 23, 2024
@shireenf-ibm
Copy link
Contributor

hi @tanyaveksler ,

  • why the tests where added in connlist pkg and not eval ?
  • also we prefer to avoid hard-coded expected output since it will be more difficult to maintain
  • in the base branch of admin-netpols, I have already added multiple tests to the connlist_test (with test directories (of yaml resources) and expected-output files)
  • thanks for fixing the bug with named-port (fixed it in the base branch too)

@tanyaveksler
Copy link
Contributor Author

Hi @shireenf-ibm,

  • the tests were added in connlist pkg and not eval, because they test all connections for the given configuration, rather then a connection between two specific nodes.
  • regarding the hardcoded tests and expected results, this is the first draft of adding tests from policy-assistant. Since in policy-assistant the tests are hardcoded, rather than in yamls (unlike the tests that you added in the base branch of admin-netpols), we decided with @adisos to add them hardcoded as well. Also, it is in sync with what we have in the eval pkg.

@tanyaveksler tanyaveksler requested a review from adisos July 28, 2024 09:07
@adisos
Copy link
Collaborator

adisos commented Jul 28, 2024

Hi @shireenf-ibm,

* the tests were added in `connlist` pkg and not `eval`, because they test all connections for the given configuration, rather then a  connection between two specific nodes.

* regarding the hardcoded tests and expected results, this is the first draft of adding tests from policy-assistant. Since in policy-assistant the tests are hardcoded, rather than in yamls (unlike the tests that you added in the base branch of admin-netpols), we decided with @adisos to add them hardcoded as well. Also, it is in sync with what we have in the `eval` pkg.

it's okay to have the hard-coded input instead of YAMLs, but the expected output should better be in a file and not hard-coded, as this is going to be more difficult to update following changes in output..
unless the expected result is in terms of vars/structs instead of output string. in that case you can encode the expected connectivity in certain test vars, and have some code that compares expected connectivity with the one returned from the analysis function..

…s - creating pod and namespace resources per test; reading expected results from file.

Added more tests from policy assistant.
@tanyaveksler
Copy link
Contributor Author

tanyaveksler commented Jul 28, 2024

@shireenf-ibm , please look at TestANPConnectivityFromParsedResources, the last test "multiple ANPs (priority order #2)".
It has two ANPs, the first one having priority 101, that allows TCP 80-81 and UDP 80-81 for all peers.
and the second one having priority 100, that denies the same ports/protocols for all peers.
According to my understanding, the result should be that all connections are allowed (and this is what I put in expected results), but in the actual run the result is denying TCP 80-81 and UDP 80-81 connections for all peers (as if the deny policy were of a higher priority).
Do I miss something, or is it a bug?

@shireenf-ibm
Copy link
Contributor

@shireenf-ibm , please look at TestANPConnectivityFromParsedResources, the last test "multiple ANPs (priority order #2)". It has two ANPs, the first one having priority 101, that allows TCP 80-81 and UDP 80-81 for all peers. and the second one having priority 100, that denies the same ports/protocols for all peers. According to my understanding, the result should be that all connections are allowed (and this is what I put in expected results), but in the actual run the result is denying TCP 80-81 and UDP 80-81 connections for all peers (as if the deny policy were of a higher priority). Do I miss something, or is it a bug?

from the description I think the actual results are correct , for ANPs the low priority takes precedence;
so if the ANP with priority 100 denies the connections that where allowed in an ANP with priority 101, I expect a deny.
you can find more here:

Priority is a value from 0 to 1000. Policies with lower priority values have higher precedence, and are checked before policies with higher priority values.

@shireenf-ibm
Copy link
Contributor

shireenf-ibm commented Jul 29, 2024

Hi @shireenf-ibm,

* the tests were added in `connlist` pkg and not `eval`, because they test all connections for the given configuration, rather then a  connection between two specific nodes.

* regarding the hardcoded tests and expected results, this is the first draft of adding tests from policy-assistant. Since in policy-assistant the tests are hardcoded, rather than in yamls (unlike the tests that you added in the base branch of admin-netpols), we decided with @adisos to add them hardcoded as well. Also, it is in sync with what we have in the `eval` pkg.

I see, however, since multiple connlist tests are already added, and our goal is to test the new functionality in eval;
I still suggest to move these tests to the eval pkg (eval_test.go or a new test file under eval);

  • in eval_test.go we have examples with hard-coded input, including funcs for adding these hard-coded inputs to the policy-engine (for example: netpolFromYaml and podFromYaml , there are also funcs which return the hard-coded policy itself);
    these funcs can be helpful and eliminate the need to add new funcs to parser.go
  • and in order to get all conns results: you can loop the pods (see example: TestGeneralPerformance and get all conns by using pe.allAllowedConnections which also uses all new anp functions.(as if it was called from connlist)).

you may choose to write the results and compare with expected files
or instead you can add entries of "interesting" pods and expected "allowed_conns" between them.
in my opinion it is not critic to get all conns (like in connlist) for unit tests
@adisos , what do you think?

@tanyaveksler
Copy link
Contributor Author

@shireenf-ibm , please look at TestANPConnectivityFromParsedResources, the last test "multiple ANPs (priority order #2)". It has two ANPs, the first one having priority 101, that allows TCP 80-81 and UDP 80-81 for all peers. and the second one having priority 100, that denies the same ports/protocols for all peers. According to my understanding, the result should be that all connections are allowed (and this is what I put in expected results), but in the actual run the result is denying TCP 80-81 and UDP 80-81 connections for all peers (as if the deny policy were of a higher priority). Do I miss something, or is it a bug?

from the description I think the actual results are correct , for ANPs the low priority takes precedence; so if the ANP with priority 100 denies the connections that where allowed in an ANP with priority 101, I expect a deny. you can find more here:

Priority is a value from 0 to 1000. Policies with lower priority values have higher precedence, and are checked before policies with higher priority values.

OK, I see that lower priority has higher precedence. Then, another test results look incorrect. Look at the test "multiple ANPs (priority order #1)" - it is the opposite to the one described above: ANP with priority 99 allows TCP 80-81 and UDP 80-81, while ANP with priority 100 denies them. Yet, in the actual output those connections are denied.

@shireenf-ibm
Copy link
Contributor

shireenf-ibm commented Jul 30, 2024

@shireenf-ibm , please look at TestANPConnectivityFromParsedResources, the last test "multiple ANPs (priority order #2)". It has two ANPs, the first one having priority 101, that allows TCP 80-81 and UDP 80-81 for all peers. and the second one having priority 100, that denies the same ports/protocols for all peers. According to my understanding, the result should be that all connections are allowed (and this is what I put in expected results), but in the actual run the result is denying TCP 80-81 and UDP 80-81 connections for all peers (as if the deny policy were of a higher priority). Do I miss something, or is it a bug?

from the description I think the actual results are correct , for ANPs the low priority takes precedence; so if the ANP with priority 100 denies the connections that where allowed in an ANP with priority 101, I expect a deny. you can find more here:

Priority is a value from 0 to 1000. Policies with lower priority values have higher precedence, and are checked before policies with higher priority values.

OK, I see that lower priority has higher precedence. Then, another test results look incorrect. Look at the test "multiple ANPs (priority order #1)" - it is the opposite to the one described above: ANP with priority 99 allows TCP 80-81 and UDP 80-81, while ANP with priority 100 denies them. Yet, in the actual output those connections are denied.

by debugging I see that the problem is that the ANPs has no names, but when saving them in the policy-engine, I have a map from anp-name to its object; since both have the same empty ("") name string; so only the last ANP is saved in policy-engine as if it is the only ANP that was provided.
do you have the option to add names to the ANPs? (i.e. to add also metadata not just spec )

@shireenf-ibm
Copy link
Contributor

@tanyaveksler updated base branch to return error for anp objects without names

Added more tests, including BANP tests, currently commented out.
@tanyaveksler
Copy link
Contributor Author

@adisos and @shireenf-ibm: I addressed your comments, the PR is ready for your review.

// }
return res
}

func CreatePodK8sObject(pod *v1.Pod) parser.K8sObject {
Copy link
Contributor

Choose a reason for hiding this comment

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

should these funcs be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They shouldn't :)

Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

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

small typos..

pkg/internal/testutils/parsed_resources_tests.go Outdated Show resolved Hide resolved
pkg/internal/testutils/parsed_resources_tests.go Outdated Show resolved Hide resolved
tanyaveksler and others added 3 commits September 3, 2024 10:57
Co-authored-by: Adi Sosnovich <82078442+adisos@users.noreply.github.com>
Co-authored-by: Adi Sosnovich <82078442+adisos@users.noreply.github.com>
removed unneeded change.
@tanyaveksler tanyaveksler merged commit 0778788 into support_admin_netpolicy Sep 3, 2024
2 checks passed
@tanyaveksler tanyaveksler deleted the tests_for_admin_netpolicy branch September 3, 2024 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants