Skip to content

pkg/status: unit test-case for api.go#1595

Open
yashisrani wants to merge 3 commits intokmesh-net:mainfrom
yashisrani:test-case/api.go
Open

pkg/status: unit test-case for api.go#1595
yashisrani wants to merge 3 commits intokmesh-net:mainfrom
yashisrani:test-case/api.go

Conversation

@yashisrani
Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

  • These tests cover the "Adapter" layer, which is responsible for converting Kmesh's internal complex data structures (Protobufs and BPF map values) into JSON-friendly structures for use in high-level components like the dashboard.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Although full execution on macOS is limited by Linux-specific BPF dependencies in the package, the code is logically correct and follows the established cross-platform testing patterns in the Kmesh codebase.

Copilot AI review requested due to automatic review settings March 2, 2026 03:52
@kmesh-bot
Copy link
Copy Markdown
Collaborator

[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 hzxuzhonghu for approval. 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

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 a new set of unit tests for the pkg/status/api.go file. These tests are crucial for validating the "Adapter" layer, which is responsible for converting complex internal Kmesh data structures, such as Protobufs and BPF map values, into simpler, JSON-compatible formats. The addition of these tests enhances the reliability and correctness of the data transformation process, ensuring that high-level components like the dashboard receive accurately formatted information.

Highlights

  • Unit Test Coverage: Added comprehensive unit tests for the pkg/status/api.go conversion functions.
  • Adapter Layer Validation: Ensured the "Adapter" layer correctly transforms Kmesh's internal Protobuf and BPF map values into JSON-friendly structures.
  • Data Conversion Logic: Covered conversion logic for Workload, Service, AuthorizationPolicy, and BPF dump data, including various edge cases and full detail scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/status/api_test.go
    • Added unit tests for ConvertWorkload function, covering basic, waypoint address, waypoint hostname, and full workload details.
    • Added unit tests for ConvertService function, including basic service and service with load balancing and waypoint configurations.
    • Added unit tests for ConvertAuthorizationPolicy function to validate policy conversion.
    • Added unit tests for WorkloadBpfDump function, testing conversion of workload policies, backends, endpoints, and services from BPF cache values.
Activity
  • No human activity has occurred on this pull request yet.
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.


Code is made anew, Tests confirm its true purpose, Bugs flee from sight.

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.

Copy link
Copy Markdown

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

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 a comprehensive set of unit tests for the status API conversion logic in pkg/status/api.go. The tests cover various scenarios for Workload, Service, AuthorizationPolicy, and BPF data structures, which is a great addition for ensuring code quality and preventing regressions. However, I found a critical issue in one of the test cases for TestWorkloadBpfDump where the assertion is incorrect based on the current implementation, which will cause the test to fail. Please see the detailed comment.

Comment thread pkg/status/api_test.go
Comment on lines +327 to +328
expected := BpfWorkloadPolicyValue{PolicyIds: []string{"policy-1"}}
assert.Equal(t, []BpfWorkloadPolicyValue{expected}, res.WorkloadPolicies)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The assertion in this test case is incorrect given the current implementation of WithWorkloadPolicies in api.go, and it will cause the test to fail.

The PolicyIds field of bpfcache.WorkloadPolicyValue is a fixed-size array ([4]uint32). When initialized with a single value, the remaining elements are zero. The WithWorkloadPolicies function iterates over all elements but doesn't filter out the empty strings resulting from zero-valued IDs. Consequently, the actual result for PolicyIds will be []string{"policy-1", "", "", ""}.

To resolve this, the implementation of WithWorkloadPolicies should be updated to filter out zero IDs or empty strings, similar to how WithBackends is implemented. This would make the code more robust and consistent.

If you update api.go, this test will correctly catch the bug and then pass after the fix. If changing api.go is not intended, then this test's expectation needs to be corrected to match the actual output.

Copy link
Copy Markdown
Contributor

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 a new pkg/status/api_test.go unit test file to validate the “Adapter” conversion helpers in pkg/status/api.go (Workload/Service/AuthorizationPolicy conversions and WorkloadBpfDump formatting) that translate internal proto/BPF-shaped data into JSON-friendly structs used by higher-level components.

Changes:

  • Add table-driven tests for ConvertWorkload and ConvertService.
  • Add a direct test for ConvertAuthorizationPolicy.
  • Add tests for WorkloadBpfDump conversion helpers (WithWorkloadPolicies/WithBackends/WithEndpoints/WithServices).

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

Comment thread pkg/status/api_test.go
Comment on lines +37 to +55
{
name: "basic workload",
input: &workloadapi.Workload{
Uid: "uid-1",
Addresses: [][]byte{net.ParseIP("192.168.1.1")},
Name: "workload-1",
Namespace: "default",
Status: workloadapi.WorkloadStatus_HEALTHY,
},
expected: &Workload{
Uid: "uid-1",
Addresses: []string{"192.168.1.1"},
Name: "workload-1",
Namespace: "default",
Status: "HEALTHY",
Protocol: "HBONE", // Default enum value 0 is HBONE
WorkloadType: "POD", // Default enum value 0 is POD
},
},
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

ConvertWorkload uses w.TunnelProtocol.String() and w.WorkloadType.String(). In the proto, the zero values are TunnelProtocol_NONE and WorkloadType_DEPLOYMENT, so these test cases will fail unless you explicitly set TunnelProtocol: TunnelProtocol_HBONE and WorkloadType: WorkloadType_POD (or update the expected values/comments to match the real defaults).

Copilot uses AI. Check for mistakes.
Comment thread pkg/status/api_test.go
Comment on lines +56 to +99
{
name: "workload with waypoint address",
input: &workloadapi.Workload{
Uid: "uid-2",
Waypoint: &workloadapi.GatewayAddress{
Destination: &workloadapi.GatewayAddress_Address{
Address: &workloadapi.NetworkAddress{
Network: "network-1",
Address: net.ParseIP("10.0.0.1"),
},
},
},
},
expected: &Workload{
Uid: "uid-2",
Waypoint: "network-1/10.0.0.1",
Addresses: []string{},
Protocol: "HBONE",
WorkloadType: "POD",
Status: "HEALTHY",
},
},
{
name: "workload with waypoint hostname",
input: &workloadapi.Workload{
Uid: "uid-3",
Waypoint: &workloadapi.GatewayAddress{
Destination: &workloadapi.GatewayAddress_Hostname{
Hostname: &workloadapi.NamespacedHostname{
Namespace: "ns-1",
Hostname: "host-1",
},
},
},
},
expected: &Workload{
Uid: "uid-3",
Waypoint: "ns-1/host-1",
Addresses: []string{},
Protocol: "HBONE",
WorkloadType: "POD",
Status: "HEALTHY",
},
},
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

The expected Protocol/WorkloadType/Status values in these waypoint-only workloads rely on enum defaults. With the current proto definitions, an unset workload defaults to TunnelProtocol_NONE and WorkloadType_DEPLOYMENT (and Status_HEALTHY). Either set those fields on the input explicitly or change the expected values so the test reflects actual behavior.

Copilot uses AI. Check for mistakes.
Comment thread pkg/status/api_test.go
Comment on lines +167 to +170
// Null the slices for the equal check below
actual.Services = nil
tc.expected.Services = nil
assert.Equal(t, tc.expected, actual)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

This test mutates tc.expected.Services (and actual.Services) to make the final assert.Equal pass. That makes the table-driven cases harder to reason about and can cause surprises if these expected structs are reused later. Prefer comparing a copy (e.g., deep-copy expected into a local variable) or asserting the Services slice separately without mutating the expected value.

Suggested change
// Null the slices for the equal check below
actual.Services = nil
tc.expected.Services = nil
assert.Equal(t, tc.expected, actual)
// Compare the rest of the struct while leaving the original test case data unmodified.
expectedCopy := *tc.expected
actualCopy := *actual
expectedCopy.Services = nil
actualCopy.Services = nil
assert.Equal(t, expectedCopy, actualCopy)

Copilot uses AI. Check for mistakes.
Comment thread pkg/status/api_test.go
Comment on lines +313 to +318
hashName := utils.NewHashName()
policyId := hashName.Hash("policy-1")
backendUid := hashName.Hash("backend-1")
serviceId := hashName.Hash("service-1")

dump := NewWorkloadBpfDump(hashName)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

utils.NewHashName() reads/writes a persisted mapping at /mnt/hash_name.yaml. Without cleanup, this test can become order-dependent/flaky (e.g., if another test already populated an entry for ID 0, conversions that skip/keep zeros can change). Consider resetting/isolating the persisted state (e.g., call hashName.Reset() before/after, or construct a fresh HashName after a reset) so this test is hermetic.

Copilot uses AI. Check for mistakes.
Comment thread pkg/status/api_test.go
Comment on lines +323 to +327
PolicyIds: [4]uint32{policyId},
},
}
res := dump.WithWorkloadPolicies(policies)
expected := BpfWorkloadPolicyValue{PolicyIds: []string{"policy-1"}}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

bpfcache.WorkloadPolicyValue.PolicyIds is a fixed-size array; here only the first element is set and the rest remain 0. WorkloadBpfDump.WithWorkloadPolicies currently converts every element without filtering, so the output will include empty strings for the zero IDs (or potentially non-empty strings if ID 0 exists in the persisted HashName mapping). Either populate all IDs, update the expected value to include the extra entries, or change the converter to skip zero/unknown IDs (consistent with WithBackends).

Suggested change
PolicyIds: [4]uint32{policyId},
},
}
res := dump.WithWorkloadPolicies(policies)
expected := BpfWorkloadPolicyValue{PolicyIds: []string{"policy-1"}}
PolicyIds: [4]uint32{
hashName.Hash("policy-1"),
hashName.Hash("policy-2"),
hashName.Hash("policy-3"),
hashName.Hash("policy-4"),
},
},
}
res := dump.WithWorkloadPolicies(policies)
expected := BpfWorkloadPolicyValue{PolicyIds: []string{"policy-1", "policy-2", "policy-3", "policy-4"}}

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings March 2, 2026 06:00
Copy link
Copy Markdown
Contributor

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

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


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

Comment thread pkg/status/api_test.go
Comment on lines +72 to +76
Addresses: []string{},
Protocol: "NONE",
WorkloadType: "POD",
Status: "HEALTHY",
},
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

input.WorkloadType is not set here, so it defaults to WorkloadType_DEPLOYMENT (enum value 0). ConvertWorkload will therefore return WorkloadType: "DEPLOYMENT", not "POD". Update expected or set input.WorkloadType explicitly.

Copilot uses AI. Check for mistakes.
Comment thread pkg/status/api_test.go
Comment on lines +94 to +99
Addresses: []string{},
Protocol: "NONE",
WorkloadType: "POD",
Status: "HEALTHY",
},
},
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

input.WorkloadType is unset here (defaults to enum value 0 = DEPLOYMENT), so ConvertWorkload will return WorkloadType: "DEPLOYMENT". Adjust expected or set input.WorkloadType explicitly.

Copilot uses AI. Check for mistakes.
Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
Signed-off-by: Yash Israni <118755067+yashisrani@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 3, 2026 12:40
Copy link
Copy Markdown
Contributor

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants