test: add unit tests for pod resource - #111
Conversation
|
@ddc-baiye: The label(s) DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello @ddc-baiye, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces new unit tests for the pod resource API, significantly enhancing the reliability and coverage of the backend's pod listing capabilities. By leveraging a fake Kubernetes client, these tests ensure the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
6efcf7c to
5b61349
Compare
There was a problem hiding this comment.
Code Review
This pull request adds unit tests for the pod resource, which is a great step towards improving test coverage. The tests use a fake clientset and cover several important scenarios like listing pods in a specific namespace, across all namespaces, and for non-existent namespaces. My review includes suggestions to further improve the tests by adding a case for error handling and by making the test assertions more readable and explicit.
| tests := []struct { | ||
| name string | ||
| namespace string | ||
| expectedCount int | ||
| expectedError bool | ||
| }{ | ||
| { | ||
| name: "get pods from default namespace", | ||
| namespace: "default", | ||
| expectedCount: 2, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get pods from other namespace", | ||
| namespace: "other-ns", | ||
| expectedCount: 1, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get pods from non-existent namespace", | ||
| namespace: "non-existent", | ||
| expectedCount: 0, | ||
| expectedError: false, | ||
| }, | ||
| { | ||
| name: "get all pods (all namespaces)", | ||
| namespace: "", | ||
| expectedCount: 3, | ||
| expectedError: false, | ||
| }, | ||
| } |
There was a problem hiding this comment.
The test suite is great for covering the success scenarios. To make it more robust, consider adding a test case that verifies the error handling logic in GetPodList. Currently, if the Kubernetes client returns an error, it's not being tested.
You could add a test case to your table like this:
{
name: "client returns error",
namespace: "default",
expectedCount: 0,
expectedError: true,
},To make this test pass, you'll need to configure the fake client to return an error. This is typically done using a reactor. Since the client is created once for all tests, you might need to refactor the test to create a client per subtest or use a separate test function for the error case.
| if (err != nil) != tt.expectedError { | ||
| t.Errorf("GetPodList() error = %v, expectedError %v", err, tt.expectedError) | ||
| return | ||
| } | ||
|
|
||
| if list == nil { | ||
| if tt.expectedCount != 0 { | ||
| t.Errorf("GetPodList() returned nil list, expected count %d", tt.expectedCount) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if len(list.Items) != tt.expectedCount { | ||
| t.Errorf("GetPodList() returned %d pods, expected %d", len(list.Items), tt.expectedCount) | ||
| } |
There was a problem hiding this comment.
The current error and result checking logic using (err != nil) != tt.expectedError is a bit dense and can be hard to follow. It's generally better to have separate, explicit checks for expected errors versus unexpected errors, and for the results. This makes the test's intent clearer and easier to debug if it fails.
if tt.expectedError {
if err == nil {
t.Errorf("GetPodList() expected an error, but got none")
}
return
}
if err != nil {
t.Errorf("GetPodList() returned unexpected error: %v", err)
return
}
if list == nil {
t.Errorf("GetPodList() returned nil list without an error")
return
}
if len(list.Items) != tt.expectedCount {
t.Errorf("GetPodList() returned %d pods, expected %d", len(list.Items), tt.expectedCount)
}Signed-off-by: ddc-baiye <dongdong.chen@bluedotai.cn>
5b61349 to
82ffc8a
Compare
What type of PR is this?
/kind test
What this PR does / why we need it:
This PR adds unit tests for the
podresource inmodules/api/pkg/resource/pod.It utilizes
client-go/kubernetes/faketo mock the K8s client, allowing verification of the pod listing logic without requiring a running cluster. This improves the test coverage and stability of the backend API.Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
The added tests cover the following scenarios:
Does this PR introduce a user-facing change?: