1// Copyright (c) 2016 Uber Technologies, Inc. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a copy 4// of this software and associated documentation files (the "Software"), to deal 5// in the Software without restriction, including without limitation the rights 6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7// copies of the Software, and to permit persons to whom the Software is 8// furnished to do so, subject to the following conditions: 9// 10// The above copyright notice and this permission notice shall be included in 11// all copies or substantial portions of the Software. 12// 13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19// THE SOFTWARE. 20 21package zap 22 23import ( 24 "io/ioutil" 25 "os" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30) 31 32func TestConfig(t *testing.T) { 33 tests := []struct { 34 desc string 35 cfg Config 36 expectN int64 37 expectRe string 38 }{ 39 { 40 desc: "production", 41 cfg: NewProductionConfig(), 42 expectN: 2 + 100 + 1, // 2 from initial logs, 100 initial sampled logs, 1 from off-by-one in sampler 43 expectRe: `{"level":"info","caller":"zap/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" + 44 `{"level":"warn","caller":"zap/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n", 45 }, 46 { 47 desc: "development", 48 cfg: NewDevelopmentConfig(), 49 expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs 50 expectRe: "DEBUG\tzap/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" + 51 "INFO\tzap/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" + 52 "WARN\tzap/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" + 53 `testing.\w+`, 54 }, 55 } 56 57 for _, tt := range tests { 58 t.Run(tt.desc, func(t *testing.T) { 59 temp, err := ioutil.TempFile("", "zap-prod-config-test") 60 require.NoError(t, err, "Failed to create temp file.") 61 defer os.Remove(temp.Name()) 62 63 tt.cfg.OutputPaths = []string{temp.Name()} 64 tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests 65 tt.cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"} 66 67 hook, count := makeCountingHook() 68 logger, err := tt.cfg.Build(Hooks(hook)) 69 require.NoError(t, err, "Unexpected error constructing logger.") 70 71 logger.Debug("debug") 72 logger.Info("info") 73 logger.Warn("warn") 74 75 byteContents, err := ioutil.ReadAll(temp) 76 require.NoError(t, err, "Couldn't read log contents from temp file.") 77 logs := string(byteContents) 78 assert.Regexp(t, tt.expectRe, logs, "Unexpected log output.") 79 80 for i := 0; i < 200; i++ { 81 logger.Info("sampling") 82 } 83 assert.Equal(t, tt.expectN, count.Load(), "Hook called an unexpected number of times.") 84 }) 85 } 86} 87 88func TestConfigWithInvalidPaths(t *testing.T) { 89 tests := []struct { 90 desc string 91 output string 92 errOutput string 93 }{ 94 {"output directory doesn't exist", "/tmp/not-there/foo.log", "stderr"}, 95 {"error output directory doesn't exist", "stdout", "/tmp/not-there/foo-errors.log"}, 96 {"neither output directory exists", "/tmp/not-there/foo.log", "/tmp/not-there/foo-errors.log"}, 97 } 98 99 for _, tt := range tests { 100 t.Run(tt.desc, func(t *testing.T) { 101 cfg := NewProductionConfig() 102 cfg.OutputPaths = []string{tt.output} 103 cfg.ErrorOutputPaths = []string{tt.errOutput} 104 _, err := cfg.Build() 105 assert.Error(t, err, "Expected an error opening a non-existent directory.") 106 }) 107 } 108} 109