Skip to content

Commit

Permalink
k8s.io/component-base/logs: unit test for command line help output
Browse files Browse the repository at this point in the history
Both pflag and standard FlagSet are covered.
  • Loading branch information
pohly committed Jan 20, 2023
1 parent 6495dd0 commit ce665b2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions staging/src/k8s.io/component-base/logs/api/v1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1
import (
"bytes"
"context"
"flag"
"testing"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -100,6 +101,45 @@ func TestOptions(t *testing.T) {
}
}

func TestFlagSet(t *testing.T) {
t.Run("pflag", func(t *testing.T) {
newOptions := NewLoggingConfiguration()
var fs pflag.FlagSet
AddFlags(newOptions, &fs)
var buffer bytes.Buffer
fs.SetOutput(&buffer)
fs.PrintDefaults()
assert.Equal(t, ` --logging-format string Sets the log format. Permitted formats: "text". (default "text")
--log-flush-frequency duration Maximum number of seconds between log flushes (default 5s)
-v, --v Level number for the log level verbosity
--vmodule pattern=N,... comma-separated list of pattern=N settings for file-filtered logging (only works for text log format)
`, buffer.String())
})

t.Run("flag", func(t *testing.T) {
newOptions := NewLoggingConfiguration()
var pfs pflag.FlagSet
AddFlags(newOptions, &pfs)
var fs flag.FlagSet
pfs.VisitAll(func(f *pflag.Flag) {
fs.Var(f.Value, f.Name, f.Usage)
})
var buffer bytes.Buffer
fs.SetOutput(&buffer)
fs.PrintDefaults()
assert.Equal(t, ` -log-flush-frequency value
Maximum number of seconds between log flushes (default 5s)
-logging-format value
Sets the log format. Permitted formats: "text". (default text)
-v value
number for the log level verbosity
-vmodule value
comma-separated list of pattern=N settings for file-filtered logging (only works for text log format)
`, buffer.String())
})

}

func TestContextualLogging(t *testing.T) {
t.Run("enabled", func(t *testing.T) {
testContextualLogging(t, true)
Expand Down

0 comments on commit ce665b2

Please sign in to comment.