-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: k8s warnings being handled improperly (#74)
- Loading branch information
1 parent
0bca17b
commit 0875dca
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,22 @@ | ||
package k8s | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pterm/pterm" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
var _ rest.WarningHandler = (*Logger)(nil) | ||
|
||
// Logger is an implementation of the WarningHandler that converts the k8s warning messages | ||
// into abctl debug messages. | ||
type Logger struct { | ||
} | ||
|
||
func (x Logger) HandleWarningHeader(code int, _ string, msg string) { | ||
// code and length check are taken from the default WarningLogger implementation | ||
if code != 299 || len(msg) == 0 { | ||
return | ||
} | ||
pterm.Debug.Println(fmt.Sprintf("k8s - WARN: %s", msg)) | ||
} |
This file contains 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,56 @@ | ||
package k8s | ||
|
||
import ( | ||
"bytes" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/pterm/pterm" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestLogger_HandleWarningHeader(t *testing.T) { | ||
b := bytes.NewBufferString("") | ||
pterm.SetDefaultOutput(b) | ||
pterm.EnableDebugMessages() | ||
// remove color codes from output | ||
pterm.DisableColor() | ||
t.Cleanup(func() { | ||
pterm.SetDefaultOutput(os.Stdout) | ||
pterm.DisableDebugMessages() | ||
pterm.EnableColor() | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
code int | ||
msg string | ||
want string | ||
}{ | ||
{ | ||
name: "non 299 code", | ||
code: 300, | ||
msg: "test msg", | ||
}, | ||
{ | ||
name: "empty msg", | ||
code: 299, | ||
}, | ||
{ | ||
name: "happy path", | ||
code: 299, | ||
msg: "test msg", | ||
want: " DEBUG k8s - WARN: test msg\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
logger := Logger{} | ||
logger.HandleWarningHeader(tt.code, "", tt.msg) | ||
|
||
if d := cmp.Diff(tt.want, b.String()); d != "" { | ||
t.Error("unexpected output (-want, +got) =", d) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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