Merge branch '19277-local-keep-from-ctr'
[arvados.git] / lib / crunchrun / logscanner_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         check "gopkg.in/check.v1"
9 )
10
11 var _ = check.Suite(&logScannerSuite{})
12
13 type logScannerSuite struct {
14 }
15
16 func (s *logScannerSuite) TestCallReportFuncOnce(c *check.C) {
17         var reported []string
18         ls := logScanner{
19                 Patterns: []string{"foobar", "barbaz"},
20                 ReportFunc: func(pattern, detail string) {
21                         reported = append(reported, pattern, detail)
22                 },
23         }
24         ls.Write([]byte("foo\nbar\n2021-01-01T00:00:00.000Z: bar"))
25         ls.Write([]byte("baz: it's a detail\nwaz\nqux"))
26         ls.Write([]byte("\nfoobar\n"))
27         c.Check(reported, check.DeepEquals, []string{"barbaz", "2021-01-01T00:00:00.000Z: barbaz: it's a detail"})
28 }
29
30 func (s *logScannerSuite) TestOneWritePerLine(c *check.C) {
31         var reported []string
32         ls := logScanner{
33                 Patterns: []string{"barbaz"},
34                 ReportFunc: func(pattern, detail string) {
35                         reported = append(reported, pattern, detail)
36                 },
37         }
38         ls.Write([]byte("foo\n"))
39         ls.Write([]byte("2021-01-01T00:00:00.000Z: barbaz: it's a detail\n"))
40         ls.Write([]byte("waz\n"))
41         c.Check(reported, check.DeepEquals, []string{"barbaz", "2021-01-01T00:00:00.000Z: barbaz: it's a detail"})
42 }
43
44 func (s *logScannerSuite) TestNoDetail(c *check.C) {
45         var reported []string
46         ls := logScanner{
47                 Patterns: []string{"barbaz"},
48                 ReportFunc: func(pattern, detail string) {
49                         reported = append(reported, pattern, detail)
50                 },
51         }
52         ls.Write([]byte("foo\n"))
53         ls.Write([]byte("barbaz\n"))
54         ls.Write([]byte("waz\n"))
55         c.Check(reported, check.DeepEquals, []string{"barbaz", "barbaz"})
56 }