You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 lines
12 KiB

  1. // Copyright (c) 2016 Pani Networks
  2. // All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // 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, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package rlog
  16. import (
  17. "bufio"
  18. "fmt"
  19. "os"
  20. "path"
  21. "runtime"
  22. "strconv"
  23. "testing"
  24. "time"
  25. )
  26. var logfile string
  27. // These two flags are used to quickly change behaviour of our tests, so that
  28. // we can manually check and test things during development of those tests.
  29. // The settings here reflect the correct behaviour for normal test runs.
  30. var removeLogfile = true
  31. var fixedLogfileName = false
  32. // setup is called at the start of each test and prepares a new log file. It
  33. // also returns a new configuration, as it may have been supplied by the user
  34. // in environment variables, which can be used by this test.
  35. func setup() rlogConfig {
  36. if fixedLogfileName {
  37. logfile = "/tmp/rlog-test.log"
  38. } else {
  39. logfile = fmt.Sprintf("/tmp/rlog-test-%d.log", time.Now().UnixNano())
  40. }
  41. // If there's a logfile with that name already, remove it so that our tests
  42. // always start from scratch.
  43. os.Remove(logfile)
  44. // Provide a default config, which can be used or modified by the tests
  45. return rlogConfig{
  46. logLevel: "",
  47. traceLevel: "",
  48. logTimeFormat: "",
  49. confFile: "",
  50. logFile: logfile,
  51. logStream: "NONE",
  52. logNoTime: "true",
  53. showCallerInfo: "false",
  54. }
  55. }
  56. // cleanup is called at the end of each test.
  57. func cleanup() {
  58. if removeLogfile {
  59. os.Remove(logfile)
  60. }
  61. }
  62. // fileMatch compares entries in the logfile with expected entries provided as
  63. // a list of strings (one for each line). If a timeLayout string is provided
  64. // then we will assume the first part of the line is a timestamp, which we will
  65. // check to see if it's correctly formatted according to the specified time
  66. // layout.
  67. func fileMatch(t *testing.T, checkLines []string, timeLayout string) {
  68. // We need to know how many characters at the start of the line we should
  69. // assume to belong to the time stamp. The formatted time stamp can
  70. // actually be of different length than the time layout string, because
  71. // actual timezone names can have more characters than the TZ specified in
  72. // the layout. So we create the current time in the specified layout, which
  73. // should be very similar to the timestamps in the log lines.
  74. currentSampleTimestamp := time.Now().Format(timeLayout)
  75. timeStampLen := len(currentSampleTimestamp)
  76. // Scan over the logfile, line by line and compare to the lines provided in
  77. // checkLines.
  78. file, err := os.Open(logfile)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer file.Close()
  83. scanner := bufio.NewScanner(file)
  84. i := 0
  85. for scanner.Scan() {
  86. line := scanner.Text()
  87. // Process and strip off the time stamp at the start, if a time layout
  88. // string was provided.
  89. if timeLayout != "" {
  90. dateTime := line[:timeStampLen]
  91. line = line[timeStampLen+1:]
  92. _, err := time.Parse(timeLayout, dateTime)
  93. if err != nil {
  94. t.Fatalf("Incorrect date/time format.\nSHOULD: %s\nIS: %s\n", timeLayout, dateTime)
  95. }
  96. }
  97. t.Logf("\n-- fileMatch: SHOULD %s\n IS %s\n",
  98. checkLines[i], line)
  99. if i >= len(checkLines) {
  100. t.Fatal("Not enough lines provided in checkLines.")
  101. }
  102. if line != checkLines[i] {
  103. t.Fatalf("Log line %d does not match check line.\nSHOULD: %s\nIS: %s\n", i, checkLines[i], line)
  104. }
  105. i++
  106. }
  107. if len(checkLines) > i {
  108. t.Fatalf("Only %d of %d checklines found in output file.", i, len(checkLines))
  109. }
  110. if i == 0 {
  111. t.Fatal("No input scanned")
  112. }
  113. }
  114. // ---------- Tests -----------
  115. // TestLogLevels performs some basic tests for each known log level.
  116. func TestLogLevels(t *testing.T) {
  117. conf := setup()
  118. defer cleanup()
  119. conf.logLevel = "DEBUG"
  120. initialize(conf, true) // re-initialize the environment variable config
  121. Debug("Test Debug")
  122. Info("Test Info")
  123. Warn("Test Warning")
  124. Error("Test Error")
  125. Critical("Test Critical")
  126. checkLines := []string{
  127. "DEBUG : Test Debug",
  128. "INFO : Test Info",
  129. "WARN : Test Warning",
  130. "ERROR : Test Error",
  131. "CRITICAL : Test Critical",
  132. }
  133. fileMatch(t, checkLines, "")
  134. }
  135. // TestLogLevelsLimited checks that we can limit the output of log and trace
  136. // messages that don't meed the minimum configured logging levels.
  137. func TestLogLevelsLimited(t *testing.T) {
  138. conf := setup()
  139. defer cleanup()
  140. conf.logLevel = "WARN"
  141. conf.traceLevel = "3"
  142. initialize(conf, true)
  143. Debug("Test Debug")
  144. Info("Test Info")
  145. Warn("Test Warning")
  146. Error("Test Error")
  147. Critical("Test Critical")
  148. Trace(1, "Trace 1")
  149. Trace(2, "Trace 2")
  150. Trace(3, "Trace 3")
  151. Trace(4, "Trace 4")
  152. checkLines := []string{
  153. "WARN : Test Warning",
  154. "ERROR : Test Error",
  155. "CRITICAL : Test Critical",
  156. "TRACE(1) : Trace 1",
  157. "TRACE(2) : Trace 2",
  158. "TRACE(3) : Trace 3",
  159. }
  160. fileMatch(t, checkLines, "")
  161. }
  162. // TestLogFormatted checks whether the *f functions for formatted output work
  163. // as expected.
  164. func TestLogFormatted(t *testing.T) {
  165. conf := setup()
  166. defer cleanup()
  167. conf.logLevel = "DEBUG"
  168. conf.traceLevel = "1"
  169. initialize(conf, true)
  170. Debugf("Test Debug %d", 123)
  171. Infof("Test Info %d", 123)
  172. Warnf("Test Warning %d", 123)
  173. Errorf("Test Error %d", 123)
  174. Criticalf("Test Critical %d", 123)
  175. Tracef(1, "Trace 1 %d", 123)
  176. Tracef(2, "Trace 2 %d", 123)
  177. checkLines := []string{
  178. "DEBUG : Test Debug 123",
  179. "INFO : Test Info 123",
  180. "WARN : Test Warning 123",
  181. "ERROR : Test Error 123",
  182. "CRITICAL : Test Critical 123",
  183. "TRACE(1) : Trace 1 123",
  184. }
  185. fileMatch(t, checkLines, "")
  186. }
  187. // TestLogTimestamp checks that the time stamp format can be changed and that
  188. // we indeed get a properly formatted timestamp output.
  189. func TestLogTimestamp(t *testing.T) {
  190. conf := setup()
  191. conf.logNoTime = "false"
  192. defer cleanup()
  193. checkLines := []string{
  194. "INFO : Test Info",
  195. }
  196. // Map of various 'user specified' time layouts and the actual time layout
  197. // to which they should be mapped. Some of the capitalization in the well
  198. // known time stamps is off, to show that those names can be specified in a
  199. // case insensitive manner.
  200. checkTimeStamps := map[string]string{
  201. "ansIC": time.ANSIC,
  202. "UNIXDATE": time.UnixDate,
  203. "rubydate": time.RubyDate,
  204. "rfc822": time.RFC822,
  205. "rfc822z": time.RFC822Z,
  206. "rfc1123": time.RFC1123,
  207. "rfc1123z": time.RFC1123Z,
  208. "RFC3339": time.RFC3339,
  209. //"RFC3339Nano": time.RFC3339Nano, // Not included in the tests, since
  210. // output length can vary depending on whether there are trailing zeros.
  211. // Not worth the trouble.
  212. "Kitchen": time.Kitchen,
  213. "": time.RFC3339, // If nothing specified, default is RFC3339
  214. "2006/01/02 15:04:05": "2006/01/02 15:04:05", // custom format
  215. }
  216. for tsUserSpecified, tsActualFormat := range checkTimeStamps {
  217. os.Remove(logfile)
  218. // Specify a time layout...
  219. conf.logTimeFormat = tsUserSpecified
  220. initialize(conf, true)
  221. Info("Test Info")
  222. // We can specify a time layout to fileMatch, which then performs the extra
  223. // check for the correctly formatted time stamp.
  224. fileMatch(t, checkLines, tsActualFormat)
  225. }
  226. }
  227. // TestLogCallerInfo manually figures out the caller info, which should be
  228. // displayed by rlog. The code that's creating the expected caller info
  229. // within the test is pretty much exactly the code that should be at work
  230. // within rlog.
  231. func TestLogCallerInfo(t *testing.T) {
  232. conf := setup()
  233. defer cleanup()
  234. conf.showCallerInfo = "true"
  235. initialize(conf, true)
  236. Info("Test Info")
  237. pc, fullFilePath, line, _ := runtime.Caller(0)
  238. line-- // The log was called in the line before, so... -1
  239. // The following lines simply format the caller info in the way that it
  240. // should be formatted by rlog
  241. callingFuncName := runtime.FuncForPC(pc).Name()
  242. dirPath, fileName := path.Split(fullFilePath)
  243. var moduleName string
  244. if dirPath != "" {
  245. dirPath = dirPath[:len(dirPath)-1]
  246. _, moduleName = path.Split(dirPath)
  247. }
  248. moduleAndFileName := moduleName + "/" + fileName
  249. shouldLine := fmt.Sprintf("INFO : [%d %s:%d (%s)] Test Info",
  250. os.Getpid(), moduleAndFileName, line, callingFuncName)
  251. checkLines := []string{shouldLine}
  252. fileMatch(t, checkLines, "")
  253. }
  254. // TestLogLevelsFiltered checks whether the per-module filtering works
  255. // correctly. For that, we provide a log-level filter that names this
  256. // executable here, so that log messages should be displayed, and a trace level
  257. // filter for a non-existent module, so that trace messages should not be
  258. // displayed.
  259. func TestLogLevelsFiltered(t *testing.T) {
  260. conf := setup()
  261. defer cleanup()
  262. conf.logLevel = "rlog_test.go=WARN"
  263. conf.traceLevel = "foobar.go=2" // should not see any of those
  264. initialize(conf, true)
  265. Debug("Test Debug")
  266. Info("Test Info")
  267. Warn("Test Warning")
  268. Error("Test Error")
  269. Critical("Test Critical")
  270. Trace(1, "Trace 1")
  271. Trace(2, "Trace 2")
  272. Trace(3, "Trace 3")
  273. Trace(4, "Trace 4")
  274. checkLines := []string{
  275. "WARN : Test Warning",
  276. "ERROR : Test Error",
  277. "CRITICAL : Test Critical",
  278. }
  279. fileMatch(t, checkLines, "")
  280. }
  281. // writeLogfile is a small utility function for the creation of unique config
  282. // files for these tests.
  283. func writeLogfile(lines []string) string {
  284. confFile := fmt.Sprintf("/tmp/rlog-test-%d.conf", time.Now().UnixNano())
  285. cf, _ := os.Create(confFile)
  286. defer cf.Close()
  287. for _, l := range lines {
  288. cf.WriteString(l + "\n")
  289. }
  290. return confFile
  291. }
  292. // checkLogFilter simplifies the checking of correct log levels in the tests.
  293. func checkLogFilter(t *testing.T, shouldPattern string, shouldLevel int) {
  294. f := logFilterSpec.filters[0]
  295. if f.Pattern != shouldPattern || f.Level != shouldLevel {
  296. t.Fatalf("Incorrect default filter '%s' / %d. Should be: '%s' / %d",
  297. f.Pattern, f.Level, shouldPattern, shouldLevel)
  298. }
  299. }
  300. // TestConfFile tests the reading of an rlog config file and the proper
  301. // processing of settings from a config file.
  302. func TestConfFile(t *testing.T) {
  303. conf := setup()
  304. defer cleanup()
  305. // Set the default configuration and check how this is reflected in the
  306. // internal settings variables.
  307. initialize(conf, true)
  308. checkLogFilter(t, "", levelInfo)
  309. t.Log("trace filter = ", traceFilterSpec)
  310. if len(traceFilterSpec.filters) > 0 {
  311. t.Fatal("Incorrect trace filters: ", traceFilterSpec.filters)
  312. }
  313. conf.confFile = writeLogfile([]string{"RLOG_LOG_LEVEL=DEBUG"})
  314. defer os.Remove(conf.confFile)
  315. initialize(conf, true)
  316. // No explicit log level was set in the initial, default config. Therefore,
  317. // the conf file value should have overwritten that.
  318. checkLogFilter(t, "", levelDebug)
  319. // Now we test with an initial config, which contains an explicit value for
  320. // the log level. The INFO value should remain.
  321. conf.logLevel = "INFO"
  322. initialize(conf, true)
  323. checkLogFilter(t, "", levelInfo)
  324. // Now we test the 'override' option (start the config in the conf file
  325. // with a '!'). With that, the conf file takes precedence.
  326. conf.confFile = writeLogfile([]string{"!RLOG_LOG_LEVEL=DEBUG"})
  327. defer os.Remove(conf.confFile)
  328. initialize(conf, true)
  329. checkLogFilter(t, "", levelDebug)
  330. // Test that a full filter spec can be read from logfile and also test that
  331. // space trimming worked correctly.
  332. conf.confFile = writeLogfile([]string{
  333. " !RLOG_LOG_LEVEL = foo.go=DEBUG ",
  334. })
  335. defer os.Remove(conf.confFile)
  336. initialize(conf, true)
  337. checkLogFilter(t, "foo.go", levelDebug)
  338. }
  339. // TestRaceConditions stress tests thread safety of rlog. Useful when running
  340. // with the race detector flag (--race).
  341. func TestRaceConditions(t *testing.T) {
  342. conf := setup()
  343. defer cleanup()
  344. for i := 0; i < 1000; i++ {
  345. go func(conf rlogConfig, i int) {
  346. for j := 0; j < 100; j++ {
  347. // Change behaviour and config around a little
  348. if j%2 == 0 {
  349. conf.showCallerInfo = "true"
  350. }
  351. conf.traceLevel = strconv.Itoa(j%10 - 1) // sometimes this will be -1
  352. //initialize(conf, j%3 == 0)
  353. initialize(conf, false)
  354. Debug("Test Debug")
  355. Info("Test Info")
  356. Trace(1, "Some trace")
  357. Trace(2, "Some trace")
  358. Trace(3, "Some trace")
  359. Trace(4, "Some trace")
  360. }
  361. }(conf, i)
  362. }
  363. }