config_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. package zap
  21. import (
  22. "io/ioutil"
  23. "os"
  24. "testing"
  25. "github.com/stretchr/testify/assert"
  26. "github.com/stretchr/testify/require"
  27. )
  28. func TestConfig(t *testing.T) {
  29. tests := []struct {
  30. desc string
  31. cfg Config
  32. expectN int64
  33. expectRe string
  34. }{
  35. {
  36. desc: "production",
  37. cfg: NewProductionConfig(),
  38. expectN: 2 + 100 + 1, // 2 from initial logs, 100 initial sampled logs, 1 from off-by-one in sampler
  39. expectRe: `{"level":"info","caller":"zap/config_test.go:\d+","msg":"info","k":"v","z":"zz"}` + "\n" +
  40. `{"level":"warn","caller":"zap/config_test.go:\d+","msg":"warn","k":"v","z":"zz"}` + "\n",
  41. },
  42. {
  43. desc: "development",
  44. cfg: NewDevelopmentConfig(),
  45. expectN: 3 + 200, // 3 initial logs, all 200 subsequent logs
  46. expectRe: "DEBUG\tzap/config_test.go:" + `\d+` + "\tdebug\t" + `{"k": "v", "z": "zz"}` + "\n" +
  47. "INFO\tzap/config_test.go:" + `\d+` + "\tinfo\t" + `{"k": "v", "z": "zz"}` + "\n" +
  48. "WARN\tzap/config_test.go:" + `\d+` + "\twarn\t" + `{"k": "v", "z": "zz"}` + "\n" +
  49. `testing.\w+`,
  50. },
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.desc, func(t *testing.T) {
  54. temp, err := ioutil.TempFile("", "zap-prod-config-test")
  55. require.NoError(t, err, "Failed to create temp file.")
  56. defer os.Remove(temp.Name())
  57. tt.cfg.OutputPaths = []string{temp.Name()}
  58. tt.cfg.EncoderConfig.TimeKey = "" // no timestamps in tests
  59. tt.cfg.InitialFields = map[string]interface{}{"z": "zz", "k": "v"}
  60. hook, count := makeCountingHook()
  61. logger, err := tt.cfg.Build(Hooks(hook))
  62. require.NoError(t, err, "Unexpected error constructing logger.")
  63. logger.Debug("debug")
  64. logger.Info("info")
  65. logger.Warn("warn")
  66. byteContents, err := ioutil.ReadAll(temp)
  67. require.NoError(t, err, "Couldn't read log contents from temp file.")
  68. logs := string(byteContents)
  69. assert.Regexp(t, tt.expectRe, logs, "Unexpected log output.")
  70. for i := 0; i < 200; i++ {
  71. logger.Info("sampling")
  72. }
  73. assert.Equal(t, tt.expectN, count.Load(), "Hook called an unexpected number of times.")
  74. })
  75. }
  76. }
  77. func TestConfigWithInvalidPaths(t *testing.T) {
  78. tests := []struct {
  79. desc string
  80. output string
  81. errOutput string
  82. }{
  83. {"output directory doesn't exist", "/tmp/not-there/foo.log", "stderr"},
  84. {"error output directory doesn't exist", "stdout", "/tmp/not-there/foo-errors.log"},
  85. {"neither output directory exists", "/tmp/not-there/foo.log", "/tmp/not-there/foo-errors.log"},
  86. }
  87. for _, tt := range tests {
  88. t.Run(tt.desc, func(t *testing.T) {
  89. cfg := NewProductionConfig()
  90. cfg.OutputPaths = []string{tt.output}
  91. cfg.ErrorOutputPaths = []string{tt.errOutput}
  92. _, err := cfg.Build()
  93. assert.Error(t, err, "Expected an error opening a non-existent directory.")
  94. })
  95. }
  96. }