github.com/lablabs/operator-sdk@v0.8.2/pkg/log/zap/flags_test.go (about) 1 // Copyright 2019 The Operator-SDK Authors 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package zap 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "go.uber.org/zap/zapcore" 23 ) 24 25 func TestLevel(t *testing.T) { 26 testCases := []struct { 27 name string 28 input string 29 shouldErr bool 30 expStr string 31 expSet bool 32 expLevel zapcore.Level 33 }{ 34 { 35 name: "debug level set", 36 input: "debug", 37 expStr: "debug", 38 expSet: true, 39 expLevel: zapcore.DebugLevel, 40 }, 41 { 42 name: "info level set", 43 input: "info", 44 expStr: "info", 45 expSet: true, 46 expLevel: zapcore.InfoLevel, 47 }, 48 { 49 name: "error level set", 50 input: "error", 51 expStr: "error", 52 expSet: true, 53 expLevel: zapcore.ErrorLevel, 54 }, 55 { 56 name: "negative number should error", 57 input: "-10", 58 shouldErr: true, 59 expSet: false, 60 }, 61 { 62 name: "positive number level results in negative level set", 63 input: "8", 64 expStr: "Level(-8)", 65 expSet: true, 66 expLevel: zapcore.Level(int8(-8)), 67 }, 68 { 69 name: "non-integer should cause error", 70 input: "invalid", 71 shouldErr: true, 72 expSet: false, 73 }, 74 } 75 76 for _, tc := range testCases { 77 t.Run(tc.name, func(t *testing.T) { 78 lvl := levelValue{} 79 err := lvl.Set(tc.input) 80 if err != nil && !tc.shouldErr { 81 t.Fatalf("Unknown error - %v", err) 82 } 83 if err != nil && tc.shouldErr { 84 return 85 } 86 assert.Equal(t, tc.expSet, lvl.set) 87 assert.Equal(t, tc.expLevel, lvl.level) 88 assert.Equal(t, "level", lvl.Type()) 89 assert.Equal(t, tc.expStr, lvl.String()) 90 }) 91 } 92 } 93 94 func TestSample(t *testing.T) { 95 testCases := []struct { 96 name string 97 input string 98 shouldErr bool 99 expStr string 100 expSet bool 101 expValue bool 102 }{ 103 { 104 name: "enable sampling", 105 input: "true", 106 expStr: "true", 107 expSet: true, 108 expValue: true, 109 }, 110 { 111 name: "disable sampling", 112 input: "false", 113 expStr: "false", 114 expSet: true, 115 expValue: false, 116 }, 117 { 118 name: "invalid input", 119 input: "notaboolean", 120 shouldErr: true, 121 expSet: false, 122 }, 123 { 124 name: "UPPERCASE true input", 125 input: "true", 126 expStr: "true", 127 expSet: true, 128 expValue: true, 129 }, 130 { 131 name: "MiXeDCase true input", 132 input: "tRuE", 133 shouldErr: true, 134 expSet: false, 135 }, 136 } 137 138 for _, tc := range testCases { 139 t.Run(tc.name, func(t *testing.T) { 140 sample := sampleValue{} 141 err := sample.Set(tc.input) 142 if err != nil && !tc.shouldErr { 143 t.Fatalf("Unknown error - %v", err) 144 } 145 if err != nil && tc.shouldErr { 146 return 147 } 148 assert.Equal(t, tc.expSet, sample.set) 149 assert.Equal(t, tc.expValue, sample.sample) 150 assert.Equal(t, "sample", sample.Type()) 151 assert.Equal(t, tc.expStr, sample.String()) 152 assert.True(t, sample.IsBoolFlag()) 153 }) 154 } 155 } 156 func TestEncoder(t *testing.T) { 157 testCases := []struct { 158 name string 159 input string 160 shouldErr bool 161 expStr string 162 expSet bool 163 expEncoder zapcore.Encoder 164 }{ 165 { 166 name: "json encoder", 167 input: "json", 168 expStr: "json", 169 expSet: true, 170 expEncoder: jsonEncoder(), 171 }, 172 { 173 name: "console encoder", 174 input: "console", 175 expStr: "console", 176 expSet: true, 177 expEncoder: consoleEncoder(), 178 }, 179 { 180 name: "unknown encoder", 181 input: "unknown", 182 shouldErr: true, 183 expEncoder: nil, 184 }, 185 } 186 for _, tc := range testCases { 187 t.Run(tc.name, func(t *testing.T) { 188 encoder := encoderValue{} 189 err := encoder.Set(tc.input) 190 if err != nil && !tc.shouldErr { 191 t.Fatalf("Unknown error - %v", err) 192 } 193 if err != nil && tc.shouldErr { 194 return 195 } 196 assert.Equal(t, tc.expSet, encoder.set) 197 assert.Equal(t, "encoder", encoder.Type()) 198 assert.Equal(t, tc.expStr, encoder.String()) 199 assert.ObjectsAreEqual(tc.expEncoder, encoder.encoder) 200 }) 201 } 202 }