-
Notifications
You must be signed in to change notification settings - Fork 23
[codex] Improve dmesg tail flag error #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
WenLong Chen (stellhub)
wants to merge
1
commit into
cozystack:main
from
stellhub:codex/dmesg-tail-hint
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright Cozystack Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package commands | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/cockroachdb/errors" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestContract_WrapDmesgCommand_TailCountGetsActionableHint(t *testing.T) { | ||
| var tail bool | ||
|
|
||
| sourceCmd := &cobra.Command{ | ||
| Use: "dmesg", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| t.Fatal("dmesg command should not run when --tail has a non-bool value") | ||
| }, | ||
| } | ||
| sourceCmd.Flags().BoolVar(&tail, "tail", false, "specify if only new messages should be sent") | ||
|
|
||
| wrappedCmd := wrapTalosCommand(sourceCmd, "dmesg") | ||
| wrappedCmd.SetArgs([]string{"--tail=3"}) | ||
|
|
||
| err := wrappedCmd.Execute() | ||
| if err == nil { | ||
| t.Fatal("expected --tail=3 to fail") | ||
| } | ||
| if got := err.Error(); !strings.Contains(got, "--tail is a boolean") || strings.Contains(got, "strconv.ParseBool") { | ||
| t.Fatalf("expected actionable --tail error without pflag internals, got: %v", err) | ||
| } | ||
|
|
||
| hints := strings.Join(errors.GetAllHints(err), "\n") | ||
| for _, want := range []string{ | ||
| "talm dmesg --nodes <node> | tail -n 3", | ||
| "talm dmesg --follow --tail", | ||
| } { | ||
| if !strings.Contains(hints, want) { | ||
| t.Errorf("expected hint %q in:\n%s", want, hints) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestContract_WrapDmesgCommand_OtherFlagErrorsStayUnchanged(t *testing.T) { | ||
| sourceCmd := &cobra.Command{ | ||
| Use: "dmesg", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| t.Fatal("dmesg command should not run when an unknown flag is present") | ||
| }, | ||
| } | ||
|
|
||
| wrappedCmd := wrapTalosCommand(sourceCmd, "dmesg") | ||
| wrappedCmd.SetArgs([]string{"--unknown"}) | ||
|
|
||
| err := wrappedCmd.Execute() | ||
| if err == nil { | ||
| t.Fatal("expected unknown flag to fail") | ||
| } | ||
| if got := err.Error(); !strings.Contains(got, "unknown flag") || strings.Contains(got, "--tail is a boolean") { | ||
| t.Fatalf("expected ordinary cobra flag error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestContract_DmesgTailLineCountFromError(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| in string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "numeric", | ||
| in: `invalid argument "12" for "--tail" flag: strconv.ParseBool: parsing "12": invalid syntax`, | ||
| want: "12", | ||
| }, | ||
| { | ||
| name: "non_numeric", | ||
| in: `invalid argument "recent" for "--tail" flag: strconv.ParseBool: parsing "recent": invalid syntax`, | ||
| want: "N", | ||
| }, | ||
| { | ||
| name: "unrelated", | ||
| in: `unknown flag: --tail-lines`, | ||
| want: "N", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if got := dmesgTailLineCountFromError(tc.in); got != tc.want { | ||
| t.Errorf("dmesgTailLineCountFromError() = %q, want %q", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
originalFlagErrorFuncvariable will benilif no custom flag error function was previously set on the command. Calling anilfunction in theelseblock (line 207) will cause a panic whenever a user provides an invalid flag other than the specific--tailboolean error (e.g.,talm dmesg --unknown-flag).You should check if
originalFlagErrorFuncis non-nil before calling it, or simply return the error to let Cobra handle it with its default behavior.