Skip to content

test: add unit tests for controller util package#2395

Open
archy-rock3t-cloud wants to merge 1 commit intoopenkruise:masterfrom
sophotechlabs:test/controller-util-coverage
Open

test: add unit tests for controller util package#2395
archy-rock3t-cloud wants to merge 1 commit intoopenkruise:masterfrom
sophotechlabs:test/controller-util-coverage

Conversation

@archy-rock3t-cloud
Copy link
Copy Markdown

Ⅰ. Describe what this PR does

Add unit tests for the previously untested pkg/controller/util package.

Ⅱ. Does this pull request fix one issue?

NONE

Ⅲ. Describe how to verify it

go test ./pkg/controller/util/ -v -count=1

Ⅳ. Special notes for reviews

Copilot AI review requested due to automatic review settings March 30, 2026 18:05
@kruise-bot kruise-bot requested review from Fei-Guo and zmberg March 30, 2026 18:05
@kruise-bot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign furykerry for approval by writing /assign @furykerry in a comment. For more information see:The Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kruise-bot
Copy link
Copy Markdown

Welcome @archy-rock3t-cloud! It looks like this is your first PR to openkruise/kruise 🎉

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds unit tests for helper functions in pkg/controller/util, improving confidence in controller utility behaviors (workload object creation-by-key, StatefulSet pod ordinal parsing, and pod-condition timeout/message helpers).

Changes:

  • Add tests for GetEmptyObjectWithKey, including typed objects and unstructured.Unstructured GVK preservation.
  • Add tests for StatefulSet-style pod name parsing (getParentNameAndOrdinal, GetOrdinal).
  • Add tests for pod-condition message KV parsing/round-trip and pending/update timeout calculations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/controller/util/workload_utils_test.go Adds coverage for GetEmptyObjectWithKey across several supported workload types + unstructured objects.
pkg/controller/util/statefulset_utils_test.go Adds table-driven tests for StatefulSet pod name/ordinal parsing helpers.
pkg/controller/util/pod_condition_utils_test.go Adds tests for condition message KV parsing/marshaling and timeout helper logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +114 to +119
assert.True(t, ok)
assert.Equal(t, gvk, u.GroupVersionKind())
assert.Equal(t, "my-foo", u.GetName())
assert.Equal(t, "bar", u.GetNamespace())
// Labels should not carry over
assert.Empty(t, u.GetLabels())
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

assert.True(t, ok) doesn’t stop the test, so if the type assertion ever fails this test will panic on the next lines when u is nil. Consider using assert.IsType or guarding the rest of the assertions with if assert.True(t, ok) { ... } so failures are reported cleanly without a nil dereference.

Suggested change
assert.True(t, ok)
assert.Equal(t, gvk, u.GroupVersionKind())
assert.Equal(t, "my-foo", u.GetName())
assert.Equal(t, "bar", u.GetNamespace())
// Labels should not carry over
assert.Empty(t, u.GetLabels())
if assert.True(t, ok) {
assert.Equal(t, gvk, u.GroupVersionKind())
assert.Equal(t, "my-foo", u.GetName())
assert.Equal(t, "bar", u.GetNamespace())
// Labels should not carry over
assert.Empty(t, u.GetLabels())
}

Copilot uses AI. Check for mistakes.
Comment on lines +19 to +59
func TestGetEmptyObjectWithKey(t *testing.T) {
tests := []struct {
name string
object client.Object
wantType client.Object
}{
{
name: "Pod",
object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "my-pod", Namespace: "default", Labels: map[string]string{"app": "test"}},
Spec: v1.PodSpec{NodeName: "node-1"},
},
wantType: &v1.Pod{},
},
{
name: "Service",
object: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "my-svc", Namespace: "kube-system"},
},
wantType: &v1.Service{},
},
{
name: "Ingress",
object: &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{Name: "my-ingress", Namespace: "web"},
},
wantType: &networkingv1.Ingress{},
},
{
name: "Deployment",
object: &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "my-deploy", Namespace: "prod"},
},
wantType: &apps.Deployment{},
},
{
name: "ReplicaSet",
object: &apps.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "my-rs", Namespace: "default"},
},
wantType: &apps.ReplicaSet{},
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

These cases only cover the currently enumerated types. GetEmptyObjectWithKey will panic for any unsupported client.Object because empty stays nil and empty.SetName(...) is called. Consider adding a test case for an unsupported object type (and then either assert the panic explicitly, or—preferably—update the helper to return a safe default / error) so this behavior is exercised and doesn’t regress silently.

Copilot uses AI. Check for mistakes.
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.87%. Comparing base (749e8f2) to head (4dbdc29).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2395      +/-   ##
==========================================
+ Coverage   48.77%   48.87%   +0.09%     
==========================================
  Files         324      324              
  Lines       27928    27928              
==========================================
+ Hits        13623    13650      +27     
+ Misses      12775    12751      -24     
+ Partials     1530     1527       -3     
Flag Coverage Δ
unittests 48.87% <ø> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@archy-rock3t-cloud archy-rock3t-cloud force-pushed the test/controller-util-coverage branch from e45e6f6 to bc4c0fe Compare April 1, 2026 09:13
Signed-off-by: Artem Muterko <artem@sopho.tech>
@archy-rock3t-cloud archy-rock3t-cloud force-pushed the test/controller-util-coverage branch from bc4c0fe to 4dbdc29 Compare April 1, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L size/L: 100-499

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants