kubernetes/test/utils/ktesting/ktesting_test.go
Patrick Ohly 501a64df3f ktesting: rewrite log configuration
Depending on component-base/logs/testinit was convenient and avoided any doubts
about the init order, but isn't acceptable long-term as an additional
dependency because component-base is too big. The same functionality (flag
registration) can also be implemented directly in ktesting. Because Go 1.21
clarified the order in which independent packages get initialized, we know for
sure that "our" code runs after testinit and can handle a potential conflict.

While at it, introduce a KTESTING_VERBOSITY env variable to enable increasing
the default verbosity in CI jobs which run a mixture of tests where some don't
use ktesting and thus don't accept a -v=<something> parameter.
2026-04-24 21:54:19 +02:00

59 lines
1.6 KiB
Go

/*
Copyright The Kubernetes 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 ktesting
import (
"flag"
"os"
"strings"
"testing"
"github.com/onsi/gomega"
)
func TestLogFlags(t *testing.T) {
klogVFlag := flag.CommandLine.Lookup("v")
klogVModuleFlag := flag.CommandLine.Lookup("vmodule")
tCtx := Init(t)
tCtx.Assert(klogVFlag).NotTo(gomega.BeNil(), "v flag")
tCtx.Assert(klogVModuleFlag).NotTo(gomega.BeNil(), "vmodule flag")
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if f.Name == "v" ||
f.Name == "vmodule" ||
strings.HasPrefix(f.Name, "test.") {
return
}
tCtx.Errorf("unexpected command line flag: %s", f.Name)
})
// The behavior of init and SetDefaultVerbosity depend on whether
// "KTESTING_VERBOSITY" is set. We don't know how this test gets
// invoked, so we have to adapt the expectations based on that.
v, ok := os.LookupEnv("KTESTING_VERBOSITY")
if !ok {
v = "0"
}
tCtx.Assert(klogVFlag.Value.String()).To(gomega.Equal(v), "initial v")
SetDefaultVerbosity(42)
defer SetDefaultVerbosity(0)
if !ok {
v = "42"
}
tCtx.Assert(klogVFlag.Value.String()).To(gomega.Equal(v), "updated v")
}