Skip to content

ctl: add unit tests for all kmeshctl subcommands#1599

Open
Eeshu-Yadav wants to merge 2 commits intokmesh-net:mainfrom
Eeshu-Yadav:test/kmeshctl-ut-e2e-991
Open

ctl: add unit tests for all kmeshctl subcommands#1599
Eeshu-Yadav wants to merge 2 commits intokmesh-net:mainfrom
Eeshu-Yadav:test/kmeshctl-ut-e2e-991

Conversation

@Eeshu-Yadav
Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Adds comprehensive unit tests for kmeshctl CLI tool. Previously only version/version_test.go existed. This PR covers all remaining subcommands.

New test files:

Package Tests Coverage
common 1 Root command subcommand registration
dump 4 uint32ToIPStr, printDualEngineTable, command flags
log 5 GetJson, GetLoggerNames, GetLoggerLevel, SetLoggerLevel via httptest
monitoring 2 getKmeshDaemonPod arg parsing, command flags
authz 4 Command tree structure (enable/disable/status)
waypoint 3 namespaceOrDefault, subcommand registration, args validation

All tests run without a Kubernetes cluster using pure function tests, httptest servers, and cobra command structure validation.

go test ./ctl/... -v -count=1

Which issue(s) this PR fixes:

Closes #991

Copilot AI review requested due to automatic review settings March 2, 2026 09:52
@kmesh-bot kmesh-bot requested review from YaoZengzeng and nlgwcy March 2, 2026 09: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 yaozengzeng 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 comprehensive suite of unit tests for the kmeshctl command-line interface. The primary goal is to ensure the robustness and correctness of all CLI subcommands, from their command-line argument parsing and flag definitions to their internal logic and interactions. By expanding test coverage across various modules, this change enhances the overall reliability and maintainability of the kmeshctl tool, making it more resilient to future modifications and preventing regressions.

Highlights

  • Comprehensive Unit Test Coverage: Added extensive unit tests for all kmeshctl subcommands, significantly improving test coverage beyond the existing version tests.
  • Independent Testing Environment: All new tests are designed to run without requiring a Kubernetes cluster, utilizing pure function tests, httptest servers, and Cobra command structure validation.
  • Specific Subcommand Testing: New test files were introduced for authz, common, dump, log, monitoring, and waypoint subcommands, covering their core functionalities and argument handling.

🧠 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
  • ctl/authz/authz_test.go
    • Added tests for the authz subcommand's command structure and registration of enable, disable, and status subcommands.
  • ctl/common/common_test.go
    • Added tests for the root kmeshctl command, verifying its usage string, silence usage setting, completion options, and the registration of all expected subcommands.
  • ctl/dump/dump_test.go
    • Added tests for the uint32ToIPStr utility function, ensuring correct IP address conversion.
    • Added tests for printDualEngineTable, covering various scenarios including full dumps, empty dumps, and multiple addresses, as well as invalid JSON handling.
    • Added tests for the dump subcommand's command usage and the definition of its --output flag.
  • ctl/log/log_test.go
    • Added tests for the GetJson utility function, covering successful JSON retrieval, non-200 HTTP responses, invalid JSON, and connection errors.
    • Added tests for GetLoggerNames, verifying correct output of logger names.
    • Added tests for GetLoggerLevel, ensuring accurate display of logger levels.
    • Added tests for SetLoggerLevel, confirming proper HTTP POST requests for setting log levels.
    • Added tests for the log subcommand's command usage and the definition of its --set flag.
  • ctl/monitoring/monitoring_test.go
    • Added tests for getKmeshDaemonPod to validate correct parsing of pod names from arguments.
    • Added tests for the monitoring subcommand's command usage and the definition of its various flags like --accesslog, --all, --workloadMetrics, and --connectionMetrics.
  • ctl/waypoint/waypoint_test.go
    • Added tests for namespaceOrDefault to ensure correct default namespace handling.
    • Added tests for the waypoint subcommand's command structure, verifying subcommand registration (apply, delete, generate, list, status) and persistent flag definitions (namespace, name, image).
    • Added tests for waypoint subcommand argument validation, checking for errors with unknown subcommands and no arguments.
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 written, then Tests are added, bugs flee fast Software now shines bright.

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 good set of initial unit tests for the kmeshctl subcommands, focusing on the Cobra command structure and helper functions. However, a significant improvement would be to also test the execution logic within the Run functions for commands in packages like authz, dump, and monitoring. Currently, this core logic is untested. The tests for the log subcommand serve as an excellent template, using httptest to mock the backend and test the command's behavior end-to-end without a real Kubernetes cluster. Adopting this pattern across the other subcommands would greatly increase test coverage and the overall quality of the CLI tool.

Comment thread ctl/authz/authz_test.go
Comment on lines +23 to +59
func TestNewCmd(t *testing.T) {
cmd := NewCmd()
if cmd.Use != "authz" {
t.Fatalf("Use = %q, want %q", cmd.Use, "authz")
}

got := map[string]bool{}
for _, sub := range cmd.Commands() {
got[sub.Name()] = true
}
for _, want := range []string{"enable", "disable", "status"} {
if !got[want] {
t.Errorf("subcommand %q not registered", want)
}
}
}

func TestNewEnableCmd(t *testing.T) {
cmd := NewEnableCmd()
if cmd.Use != "enable [podNames...]" {
t.Fatalf("Use = %q, want %q", cmd.Use, "enable [podNames...]")
}
}

func TestNewDisableCmd(t *testing.T) {
cmd := NewDisableCmd()
if cmd.Use != "disable [podNames...]" {
t.Fatalf("Use = %q, want %q", cmd.Use, "disable [podNames...]")
}
}

func TestNewStatusCmd(t *testing.T) {
cmd := NewStatusCmd()
if cmd.Use != "status [podNames...]" {
t.Fatalf("Use = %q, want %q", cmd.Use, "status [podNames...]")
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

While these tests are good for verifying the command structure, they don't test the execution logic within the Run functions of the commands (e.g., for enable, disable, status). This leaves the core functionality of the authz subcommand untested.

Consider refactoring the authz package to separate the command logic from the Kubernetes client and port-forwarding setup. This would allow you to write unit tests for the logic using an httptest server, similar to how it's done in ctl/log/log_test.go. This would make the tests truly comprehensive.

For example, you could add tests that execute the command and verify that the correct HTTP request is made to the mock server.

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 missing unit test coverage for kmeshctl CLI subcommands, expanding beyond the previously existing version tests by validating cobra command wiring, flags, and a few pure-function/output behaviors without requiring a live Kubernetes cluster.

Changes:

  • Added unit tests for root command subcommand registration (ctl/common).
  • Added unit tests for multiple subcommands’ cobra structure/flags and helper functions (dump, log, monitoring, authz, waypoint).
  • Added output/HTTP behavior tests using httptest and stdout capture where needed.

Reviewed changes

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

Show a summary per file
File Description
ctl/waypoint/waypoint_test.go Tests waypoint namespace defaulting, subcommand registration, persistent flags, and Args validation.
ctl/monitoring/monitoring_test.go Tests daemon pod arg parsing helper and monitoring command flags.
ctl/log/log_test.go Tests JSON fetch helper and logger get/set behaviors via httptest, plus command flag presence.
ctl/dump/dump_test.go Tests IP conversion helper, table printing output behavior, invalid JSON fallback, and dump command flags.
ctl/common/common_test.go Tests root command config and expected subcommand registration.
ctl/authz/authz_test.go Tests authz command tree structure and Use strings for subcommands.

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

Comment thread ctl/dump/dump_test.go Outdated
Comment thread ctl/log/log_test.go Outdated
@Eeshu-Yadav Eeshu-Yadav force-pushed the test/kmeshctl-ut-e2e-991 branch from 9f0797c to da0a465 Compare March 2, 2026 09:59
Copilot AI review requested due to automatic review settings March 2, 2026 10:09
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 6 out of 6 changed files in this pull request and generated 1 comment.


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

Comment thread ctl/monitoring/monitoring_test.go Outdated
@LiZhenCheng9527
Copy link
Copy Markdown
Contributor

Please update your base branch.

@Eeshu-Yadav
Copy link
Copy Markdown
Contributor Author

Please update your base branch.

updating it

Copilot AI review requested due to automatic review settings March 3, 2026 08:08
@Eeshu-Yadav Eeshu-Yadav force-pushed the test/kmeshctl-ut-e2e-991 branch from 90538d5 to a1cfb44 Compare March 3, 2026 08:08
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 6 out of 6 changed files in this pull request and generated 1 comment.


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

Comment thread ctl/log/log_test.go Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.48%. Comparing base (64f1777) to head (3f28612).
⚠️ Report is 6 commits behind head on main.


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3c29dcb...3f28612. Read the comment docs.

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

Add comprehensive unit tests for kmeshctl covering:
- common: root command subcommand registration
- dump: uint32ToIPStr, printDualEngineTable, command flags
- log: GetJson, GetLoggerNames, GetLoggerLevel, SetLoggerLevel via httptest
- monitoring: getKmeshDaemonPod arg parsing, command flags
- authz: command tree structure (enable/disable/status)
- waypoint: namespaceOrDefault, subcommand registration, args validation

Signed-off-by: Eeshu-Yadav <eeshuyadav123@gmail.com>
@Eeshu-Yadav Eeshu-Yadav force-pushed the test/kmeshctl-ut-e2e-991 branch from a1cfb44 to 6af59d7 Compare March 3, 2026 17:53
Test kernel-native config dump table formatting using real protobuf
ConfigDump types, covering populated data, empty dump, and invalid
JSON fallback.

Signed-off-by: Eeshu-Yadav <eeshuyadav123@gmail.com>
Copilot AI review requested due to automatic review settings March 3, 2026 17:59
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 5 out of 5 changed files in this pull request and generated 1 comment.


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

Comment thread ctl/common/common_test.go
Comment on lines +36 to +39
want := map[string]bool{
"log": false, "dump": false, "waypoint": false, "version": false,
"monitoring": false, "authz": false, "secret": false,
}
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

PR title/description says unit tests were added for all kmeshctl subcommands, but this PR still has no tests for the secret subcommand (ctl/secret has no *_test.go). Either add a basic ctl/secret/secret_test.go (e.g., validates command tree/flags like other packages) or adjust the PR title/description to exclude secret from the claim.

Copilot uses AI. Check for mistakes.
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.

Add UT and E2E test for kmeshctl

4 participants