Skip to content

[TEST] Improve unit test coverage for apiserver pkg/model#3495

Merged
kevin85421 merged 10 commits intoray-project:masterfrom
JiangJiaWei1103:add-unit-tests-for-pkg-model
May 5, 2025
Merged

[TEST] Improve unit test coverage for apiserver pkg/model#3495
kevin85421 merged 10 commits intoray-project:masterfrom
JiangJiaWei1103:add-unit-tests-for-pkg-model

Conversation

@JiangJiaWei1103
Copy link
Contributor

Changes

  1. Pre-allocate memory to prevent re-allocation
  2. Add unit tests for functions that convert K8s-native CRD objects into KubeRay’s internal API representations:
    • FromCrdToAPIServices
    • FromCrdToAPIService
    • TestPopulateServeApplicationStatus
    • TestPopulateServeDeploymentStatus
    • TestPopulateRayServiceEvent

Why are these changes needed?

To ensure high confidence in code quality before releasing the APIServer to the public, we aim to improve unit test coverage. Currently, the unit test coverage of pkg/model package can be further improved, as shown below:

Before

Screenshot 2025-04-27 at 4 45 02 PM

After

After adding new tests above, the unit test coverage boosts from 79.3% to 90.1%.
Screenshot 2025-04-27 at 4 46 08 PM

Related issue number

Closes #3316.

Related PR

#3419

Checks

  • I've made sure the tests are passing.
  • Testing Strategy
    • Unit tests
    • Manual tests
    • This PR is not tested :(

@JiangJiaWei1103
Copy link
Contributor Author

@dentiny PTAL, thanks!

},
}

var serveAppStatus = rayv1api.AppStatus{
Copy link
Contributor

Choose a reason for hiding this comment

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

Two questions:

  • I'm wondering why do we need the variable here? All these variables are used once, it's clearer to put it inside of test?
  • Even if you insist they're necessary, I would suggest rename them to something like deployingAppStatus.

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 agree. Placing them inside the test functions would make things much clearer. Thanks a lot for your suggestion!

assert.Len(t, appStatuses, 1)

appStatus := appStatuses[0]
assert.Equal(t, "app0", appStatus.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't duplicate the code here, otherwise next time we update the app status, we need to change two places.

My suggestion would be, define a variable and put it into serveApplicationStatuses, use serveApplicationStatuses.Status as expected value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! We now define the expected result at the beginning, avoiding hardcoded expected values during assertions.

@JiangJiaWei1103 JiangJiaWei1103 requested a review from dentiny April 28, 2025 12:46
serviceEvents := PopulateRayServiceEvent("svc0", events)
assert.Len(t, serviceEvents, 1)

serviceEvent := serviceEvents[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if I suggested before, I would prefer to have an event variable.

event := &corev1.Event{
		{
			ObjectMeta: metav1.ObjectMeta{
				Name: "test",
			},
			Reason:  "test",
			Message: "test",
			Type:    "Normal",
			Count:   2,
		}

so we could reuse the fields inside of it, rather than hand-write ourselves.
The benefit is, if we want to update field value in the future, we only need to update in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! All checks now consistently reuse field values from the pre-defined objects. Thanks for catching that.

Copy link
Contributor

@dentiny dentiny left a comment

Choose a reason for hiding this comment

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

Just one comment.

@JiangJiaWei1103 JiangJiaWei1103 requested a review from dentiny May 3, 2025 08:16
Copy link
Contributor

@dentiny dentiny left a comment

Choose a reason for hiding this comment

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

Thanks!

@kevin85421
Copy link
Member

@JiangJiaWei1103 would you mind rebasing with the master branch? Thanks!

Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
Signed-off-by: jiangjiawei1103 <waynechuang97@gmail.com>
@JiangJiaWei1103 JiangJiaWei1103 force-pushed the add-unit-tests-for-pkg-model branch from bdc2375 to 0adf21a Compare May 4, 2025 07:59
@JiangJiaWei1103
Copy link
Contributor Author

@JiangJiaWei1103 would you mind rebasing with the master branch? Thanks!

Done. Thanks a lot!

@kevin85421 kevin85421 merged commit f21b999 into ray-project:master May 5, 2025
24 checks passed
laurafitzgerald pushed a commit to laurafitzgerald/kuberay that referenced this pull request May 7, 2025
laurafitzgerald pushed a commit to laurafitzgerald/kuberay that referenced this pull request May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Verify test coverage for apiserver pkg/model

3 participants