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

[test] Improve test coverage and performance in apimachinery_status_conditions #172

Merged

Conversation

art-tapin
Copy link
Contributor

This pull request adds unit tests to verify the correctness of the CopyConditions and ConditionMarshal functions in the 'common' package's apimachinery_status_conditions.go file.

It also refactors the CopyConditions function to avoid unnecessary appends and allocate a slice with the exact length.

Finally, it reuses the CopyConditions function in ConditionMarshal instead of using append for performance improvement and null safety. This work partly addresses issue #167, and more work is still in progress.

@art-tapin art-tapin force-pushed the issue167/unit-tests-for-apimachinery_status_conditions branch from a08e581 to 379dcdc Compare April 27, 2023 12:36
@art-tapin art-tapin marked this pull request as ready for review April 27, 2023 12:54
@art-tapin art-tapin requested a review from a team as a code owner April 27, 2023 12:54
Message: "Object validation failed",
},
},
expectedJSONOutput: `[{"type":"Installed","status":"True","observedGeneration":1,"lastTransitionTime":"` + nowUTC + `","reason":"InstallSuccessful","message":"Object successfully installed"},{"type":"Ready","status":"False","observedGeneration":2,"lastTransitionTime":"` + nowUTC + `","reason":"ComponentsNotReady","message":"Object components are not ready"},{"type":"Validated","status":"Unknown","observedGeneration":3,"lastTransitionTime":"` + nowUTC + `","reason":"ValidationError","message":"Object validation failed"}]`,
Copy link
Contributor

Choose a reason for hiding this comment

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

what? Order does not keep consistent?? First Ready, then Installed and lastly Validated. And for the serialized string: Installed, Ready, Validated. Weird.

HAve you run the test multiple times? if order is not maintained, on multiple runs, some of them could fail

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the concern! Conditions are sorted based on their Condition.Type field in the ConditionMarshal function before they're serialized. So, the order is consistent and not random. To answer your question, running the test multiple times should not result in any failures due to the consistent ordering provided by the sorting mechanism. Heer is ConditionMarhsal implementation, pay attention to the sorting part:

func ConditionMarshal(conditions []metav1.Condition) ([]byte, error) {
	condCopy := CopyConditions(conditions)
	sort.Slice(condCopy, func(a, b int) bool {
		return condCopy[a].Type < condCopy[b].Type
	})
	return json.Marshal(condCopy)
}

Let me know if you have any other questions! 👼

Copy link
Contributor

Choose a reason for hiding this comment

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

🤦 Sorry for that. Once again I took some assumptions that are wrong.

Looking good

@art-tapin art-tapin merged commit 425a4f7 into main Apr 28, 2023
@art-tapin art-tapin deleted the issue167/unit-tests-for-apimachinery_status_conditions branch April 28, 2023 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants