logging_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "testing"
  16. "github.com/coreos/pkg/capnslog"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestSetGlobalLogLevel(t *testing.T) {
  20. logger := capnslog.NewPackageLogger("github.com/rook/rook", "pkg/util/logging_test")
  21. tests := []struct {
  22. name string
  23. userLogLevelSelection string
  24. desiredLogLevel capnslog.LogLevel
  25. }{
  26. {"INFO is supported", "INFO", capnslog.INFO},
  27. {"DEBUG is supported", "DEBUG", capnslog.DEBUG},
  28. {"WARNING is supported", "WARNING", capnslog.WARNING},
  29. {"ERROR is supported", "ERROR", capnslog.ERROR},
  30. {"TRACE will be turned into DEBUG", "TRACE", capnslog.DEBUG},
  31. {"TRACE_INSECURE will be turned into TRACE", "TRACE_INSECURE", capnslog.TRACE},
  32. {"an invalid input will be turned into INFO", "INVALID", capnslog.INFO},
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. SetGlobalLogLevel(tt.userLogLevelSelection, logger)
  37. t.Logf("asserting that log level %d can be shown", tt.desiredLogLevel)
  38. assert.True(t, logger.LevelAt(tt.desiredLogLevel))
  39. // logger.LevelAt will only tell whether the logger will output logs at the desired log
  40. // level, but it won't actually tell what the log level is specifically. To make sure we
  41. // won't output MORE logs, we should check for the next more verbose log level also.
  42. nextMostVerboseLevel := tt.desiredLogLevel + 1
  43. t.Logf("asserting that log level %d can NOT be shown", nextMostVerboseLevel)
  44. assert.False(t, logger.LevelAt(nextMostVerboseLevel))
  45. })
  46. }
  47. }