Skip to content

test: add unit tests for pod resource - #111

Open
ddc-baiye wants to merge 1 commit into
kubeedge:mainfrom
ddc-baiye:feat/add-pod-unit-test
Open

test: add unit tests for pod resource#111
ddc-baiye wants to merge 1 commit into
kubeedge:mainfrom
ddc-baiye:feat/add-pod-unit-test

Conversation

@ddc-baiye

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind test

What this PR does / why we need it:
This PR adds unit tests for the pod resource in modules/api/pkg/resource/pod.
It utilizes client-go/kubernetes/fake to 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:

  • Listing pods in a specific namespace.
  • Listing pods across all namespaces.
  • Handling queries for non-existent namespaces.

Does this PR introduce a user-facing change?:

NONE

@kubeedge-bot

Copy link
Copy Markdown
Collaborator

@ddc-baiye: The label(s) kind/test cannot be applied, because the repository doesn't have them

Details

In response to this:

What type of PR is this?
/kind test

What this PR does / why we need it:
This PR adds unit tests for the pod resource in modules/api/pkg/resource/pod.
It utilizes client-go/kubernetes/fake to 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:

  • Listing pods in a specific namespace.
  • Listing pods across all namespaces.
  • Handling queries for non-existent namespaces.

Does this PR introduce a user-facing change?:

NONE

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.

@kubeedge-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign kevin-wangzefeng after the PR has been reviewed.
You can assign the PR to them by writing /assign @kevin-wangzefeng in a comment when ready.

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

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @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 GetPodList function behaves as expected across various namespace scenarios, improving code quality and maintainability without requiring a live cluster.

Highlights

  • Unit Tests for Pod Resource: Adds comprehensive unit tests for the GetPodList function within the modules/api/pkg/resource/pod module.
  • Mocking Kubernetes Client: Employs client-go/kubernetes/fake to simulate a Kubernetes cluster environment, enabling isolated testing of pod listing logic without external dependencies.
  • Test Scenarios Covered: The new tests validate pod listing functionality for specific namespaces, all namespaces, and gracefully handling requests for non-existent namespaces.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@kubeedge-bot kubeedge-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 16, 2025
@ddc-baiye
ddc-baiye force-pushed the feat/add-pod-unit-test branch from 6efcf7c to 5b61349 Compare December 16, 2025 09:31

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment on lines +52 to +82
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,
},
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Comment on lines +88 to +102
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)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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>
@ddc-baiye
ddc-baiye force-pushed the feat/add-pod-unit-test branch from 5b61349 to 82ffc8a Compare December 22, 2025 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants