|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package logging |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/pflag" |
| 24 | + uberzap "go.uber.org/zap" |
| 25 | + "go.uber.org/zap/zapcore" |
| 26 | +) |
| 27 | + |
| 28 | +func TestNewOptions(t *testing.T) { |
| 29 | + opts := NewOptions() |
| 30 | + if opts.LogVerbosity != DEFAULT { |
| 31 | + t.Errorf("Expected LogVerbosity to be %d, got %d", DEFAULT, opts.LogVerbosity) |
| 32 | + } |
| 33 | + if !opts.ZapOptions.Development { |
| 34 | + t.Error("Expected ZapOptions.Development to be true") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestAddFlags(t *testing.T) { |
| 39 | + opts := NewOptions() |
| 40 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 41 | + opts.AddFlags(fs) |
| 42 | + |
| 43 | + // Check that the -v flag was added |
| 44 | + if fs.Lookup("v") == nil { |
| 45 | + t.Error("Expected -v flag to be added") |
| 46 | + } |
| 47 | + |
| 48 | + // Check that zap flags were added |
| 49 | + if fs.Lookup(ZapLogLevelFlagName) == nil { |
| 50 | + t.Errorf("Expected %s flag to be added", ZapLogLevelFlagName) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestComplete(t *testing.T) { |
| 55 | + tests := []struct { |
| 56 | + name string |
| 57 | + args []string |
| 58 | + expectedVerbosity int |
| 59 | + expectedZapLevel zapcore.Level |
| 60 | + zapShouldDerive bool |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "derive zap level from v flag", |
| 64 | + args: []string{"-v=3"}, |
| 65 | + expectedVerbosity: 3, |
| 66 | + expectedZapLevel: zapcore.Level(-3), |
| 67 | + zapShouldDerive: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "explicit zap level takes precedence", |
| 71 | + args: []string{"-v=5", "--zap-log-level=info"}, |
| 72 | + expectedVerbosity: 5, |
| 73 | + expectedZapLevel: zapcore.InfoLevel, |
| 74 | + zapShouldDerive: false, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + opts := NewOptions() |
| 81 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 82 | + opts.AddFlags(fs) |
| 83 | + |
| 84 | + err := fs.Parse(tt.args) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("Failed to parse flags: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + err = opts.Complete() |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Complete() failed: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + if opts.LogVerbosity != tt.expectedVerbosity { |
| 95 | + t.Errorf("Expected LogVerbosity to be %d, got %d", tt.expectedVerbosity, opts.LogVerbosity) |
| 96 | + } |
| 97 | + |
| 98 | + atomicLevel, ok := opts.ZapOptions.Level.(uberzap.AtomicLevel) |
| 99 | + if !ok { |
| 100 | + t.Fatalf("Expected ZapOptions.Level to be zap.AtomicLevel, got %T", opts.ZapOptions.Level) |
| 101 | + } |
| 102 | + actualLevel := atomicLevel.Level() |
| 103 | + if actualLevel != tt.expectedZapLevel { |
| 104 | + t.Errorf("Expected zap level to be %v, got %v", tt.expectedZapLevel, actualLevel) |
| 105 | + } |
| 106 | + |
| 107 | + zapLogLevelFlag := fs.Lookup(ZapLogLevelFlagName) |
| 108 | + if zapLogLevelFlag == nil { |
| 109 | + t.Fatal("zap-log-level flag not found") |
| 110 | + } |
| 111 | + if !zapLogLevelFlag.Changed { |
| 112 | + t.Error("Expected zap-log-level flag to be marked as changed after Complete()") |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestValidate(t *testing.T) { |
| 119 | + tests := []struct { |
| 120 | + name string |
| 121 | + verbosity int |
| 122 | + expectError bool |
| 123 | + }{ |
| 124 | + {"valid verbosity 0", 0, false}, |
| 125 | + {"valid verbosity 2", 2, false}, |
| 126 | + {"valid verbosity 5", 5, false}, |
| 127 | + {"negative verbosity corrected to default", -1, false}, |
| 128 | + } |
| 129 | + |
| 130 | + for _, tt := range tests { |
| 131 | + t.Run(tt.name, func(t *testing.T) { |
| 132 | + opts := NewOptions() |
| 133 | + opts.LogVerbosity = tt.verbosity |
| 134 | + |
| 135 | + err := opts.Validate() |
| 136 | + if tt.expectError && err == nil { |
| 137 | + t.Error("Expected error but got nil") |
| 138 | + } |
| 139 | + if !tt.expectError && err != nil { |
| 140 | + t.Errorf("Expected no error but got: %v", err) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestValidate_SetsDefaultForNegativeVerbosity(t *testing.T) { |
| 147 | + opts := NewOptions() |
| 148 | + opts.LogVerbosity = -1 |
| 149 | + |
| 150 | + err := opts.Validate() |
| 151 | + if err != nil { |
| 152 | + t.Errorf("Expected no error, got %v", err) |
| 153 | + } |
| 154 | + if opts.LogVerbosity != DEFAULT { |
| 155 | + t.Errorf("Expected LogVerbosity to be set to DEFAULT (%d), got %d", DEFAULT, opts.LogVerbosity) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func init() { |
| 160 | + // Clear any global flags from other tests |
| 161 | + flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError) |
| 162 | +} |
0 commit comments