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

Adding Tests and Refactoring for ruleWithKSOpaDependency Function in cautils #1487

Merged

Conversation

cbrom
Copy link
Contributor

@cbrom cbrom commented Nov 21, 2023

PR Type:

Tests


PR Description:

This PR introduces the following changes:

  • Adds unit tests for the ruleWithKSOpaDependency function in core/cautils/datastructuresmethods_test.go.
  • Refactors the ruleWithKSOpaDependency function in core/cautils/datastructuresmethods.go to handle type assertion of the "armoOpa" attribute.

PR Main Files Walkthrough:

files:
  • core/cautils/datastructuresmethods.go: Refactored the ruleWithKSOpaDependency function to handle type assertion of the 'armoOpa' attribute.
  • core/cautils/datastructuresmethods_test.go: Added unit tests for the ruleWithKSOpaDependency function, covering cases when attributes are nil, do not contain 'armoOpa' key, contain 'armoOpa' key with non-boolean value, and contain 'armoOpa' key with value 'true'.

Signed-off-by: cbrom <kb.cbrom@gmail.com>
Signed-off-by: cbrom <kb.cbrom@gmail.com>
@cbrom
Copy link
Contributor Author

cbrom commented Nov 21, 2023

/describe

@amirmalka
Copy link
Contributor

@vladklokun @Daniel-GrunbergerCA Looking up at the repo history I see you might have a clue if the armoOpa attribute is still relevant?
I searched for it in regolibrary and it looks like a thing of the past, if so, we can get rid of the ruleWithKSOpaDependency function

@Daniel-GrunbergerCA
Copy link
Collaborator

Doing a search into the regolibrary repo, I can't find any reference to it, so it seems like it's deprecated

@cbrom
Copy link
Contributor Author

cbrom commented Nov 22, 2023

@coditamar
Copy link

/describe

@amirmalka
Copy link
Contributor

@cbrom yes, it looks like it's not used in any of our controls

@github-actions github-actions bot changed the title Added tests to core cautils datastructuremethods Adding Tests and Refactoring for ruleWithKSOpaDependency Function in cautils Nov 22, 2023
@github-actions github-actions bot added the Tests label Nov 22, 2023
Signed-off-by: cbrom <kb.cbrom@gmail.com>
…ionCompatible function

Signed-off-by: cbrom <kb.cbrom@gmail.com>
…FromKubescapeVersion

Signed-off-by: cbrom <kb.cbrom@gmail.com>
@cbrom cbrom force-pushed the core_cautils_datastructuremethods_tests branch from cb45bcf to 4ae45cd Compare November 22, 2023 08:39
@cbrom
Copy link
Contributor Author

cbrom commented Nov 22, 2023

@amirmalka I've removed the function along with its usage and added type safe checks and tests to other function.

@matthyx
Copy link
Contributor

matthyx commented Nov 22, 2023

@amirmalka are you reviewing that?

@matthyx matthyx self-requested a review November 22, 2023 14:57
@matthyx
Copy link
Contributor

matthyx commented Nov 22, 2023

/review

Copy link

PR Analysis

  • 🎯 Main theme: Adding tests and refactoring for the ruleWithKSOpaDependency function in cautils
  • 📝 PR summary: This PR introduces unit tests for the ruleWithKSOpaDependency function in core/cautils/datastructuresmethods_test.go and refactors the ruleWithKSOpaDependency function in core/cautils/datastructuresmethods.go to handle type assertion of the "armoOpa" attribute.
  • 📌 Type of PR: Tests
  • 🧪 Relevant tests added: Yes
  • ⏱️ Estimated effort to review [1-5]: 2, because the PR is mainly about adding tests and refactoring one function, which is relatively straightforward to review.
  • 🔒 Security concerns: No security concerns found

PR Feedback

  • 💡 General suggestions: The PR is well-structured and the changes are clear. The addition of tests and the refactoring of the function to handle type assertion are good improvements. However, it would be beneficial to ensure that all edge cases are covered in the tests.

  • 🤖 Code feedback:

    • relevant file: core/cautils/datastructuresmethods.go
      suggestion: Consider adding error handling for the type assertion of the 'useFromKubescapeVersion' and 'useUntilKubescapeVersion' attributes. If the type assertion fails, it could lead to a panic. [important]
      relevant line: "+ if sfrom, ok := from.(string); ok {"

    • relevant file: core/cautils/datastructuresmethods.go
      suggestion: It would be beneficial to add a default case to the type assertion to handle unexpected types. This would improve the robustness of the code. [medium]
      relevant line: "+ if suntil, ok := until.(string); ok {"

How to use

Instructions

To invoke the PR-Agent, add a comment using one of the following commands:
/review: Request a review of your Pull Request.
/describe: Update the PR title and description based on the contents of the PR.
/improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
/ask <QUESTION>: Ask a question about the PR.
/update_changelog: Update the changelog based on the PR's contents.
/add_docs: Generate docstring for new components introduced in the PR.
/generate_labels: Generate labels for the PR based on the PR's contents.
see the tools guide for more details.

To edit any configuration parameter from the configuration.toml, add --config_path=new_value.
For example: /review --pr_reviewer.extra_instructions="focus on the file: ..."
To list the possible configuration parameters, add a /config comment.

@matthyx
Copy link
Contributor

matthyx commented Nov 22, 2023

@cbrom please check the code feedback on core/cautils/datastructuresmethods.go

@cbrom
Copy link
Contributor Author

cbrom commented Nov 22, 2023

@cbrom please check the code feedback on core/cautils/datastructuresmethods.go

The type assertion works fine for string and does not lead to panic.

                        if sfrom, ok := from.(string); ok {
				if semver.Compare(version, sfrom) == -1 {
					return false
				}
			} else {
				// Handle case where useFromKubescapeVersion is not a string
				return false
			}

@matthyx matthyx merged commit dc6c379 into kubescape:master Nov 22, 2023
10 of 11 checks passed
@cbrom
Copy link
Contributor Author

cbrom commented Nov 22, 2023

@matthyx I was doing a code improvement on this :( to use switch case for type assertion and remove unnecessary if else conditionals. Should I create a new PR?

@matthyx
Copy link
Contributor

matthyx commented Nov 22, 2023

ooops sorry, next time put it back to draft to indicate you don't want to merge it
yes open a follow-up PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants