8784: Fix test for latest firefox.
[arvados.git] / services / crunch-run / logging_test.go
1 package main
2
3 import (
4         "fmt"
5         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
6         . "gopkg.in/check.v1"
7         "strings"
8         "testing"
9         "time"
10 )
11
12 type LoggingTestSuite struct{}
13
14 type TestTimestamper struct {
15         count int
16 }
17
18 func (this *TestTimestamper) Timestamp(t time.Time) string {
19         this.count += 1
20         t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", this.count), t.Location())
21         if err != nil {
22                 panic(err)
23         }
24         return RFC3339Timestamp(t)
25 }
26
27 // Gocheck boilerplate
28 var _ = Suite(&LoggingTestSuite{})
29
30 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
31         api := &ArvTestClient{}
32         kc := &KeepTestClient{}
33         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
34         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
35
36         cr.CrunchLog.Print("Hello world!")
37         cr.CrunchLog.Print("Goodbye")
38         cr.CrunchLog.Close()
39
40         c.Check(api.Calls, Equals, 1)
41
42         mt, err := cr.LogCollection.ManifestText()
43         c.Check(err, IsNil)
44         c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
45
46         logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
47                 "2015-12-29T15:51:45.000000002Z Goodbye\n"
48
49         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
50         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
51         c.Check(string(kc.Content), Equals, logtext)
52 }
53
54 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
55         if testing.Short() {
56                 return
57         }
58         api := &ArvTestClient{}
59         kc := &KeepTestClient{}
60         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
61         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
62         cr.CrunchLog.Immediate = nil
63
64         for i := 0; i < 2000000; i++ {
65                 cr.CrunchLog.Printf("Hello %d", i)
66         }
67         cr.CrunchLog.Print("Goodbye")
68         cr.CrunchLog.Close()
69
70         c.Check(api.Calls > 1, Equals, true)
71         c.Check(api.Calls < 2000000, Equals, true)
72
73         mt, err := cr.LogCollection.ManifestText()
74         c.Check(err, IsNil)
75         c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
76 }
77
78 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
79         api := &ArvTestClient{}
80         kc := &KeepTestClient{}
81         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
82         ts := &TestTimestamper{}
83         cr.CrunchLog.Timestamper = ts.Timestamp
84         stdout := NewThrottledLogger(cr.NewLogWriter("stdout"))
85         stdout.Timestamper = ts.Timestamp
86
87         cr.CrunchLog.Print("Hello world!")
88         stdout.Print("Doing stuff")
89         cr.CrunchLog.Print("Goodbye")
90         stdout.Print("Blurb")
91         cr.CrunchLog.Close()
92         stdout.Close()
93
94         logText := make(map[string]string)
95         for _, content := range api.Content {
96                 log := content["log"].(arvadosclient.Dict)
97                 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
98         }
99
100         c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
101 2015-12-29T15:51:45.000000003Z Goodbye
102 `)
103         c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
104 2015-12-29T15:51:45.000000004Z Blurb
105 `)
106
107         mt, err := cr.LogCollection.ManifestText()
108         c.Check(err, IsNil)
109         c.Check(mt, Equals, ""+
110                 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
111                 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
112 }
113
114 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
115         testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
116 }
117
118 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
119         testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
120 }
121
122 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
123         testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
124 }
125
126 func testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
127         discoveryMap[throttleParam] = float64(throttleValue)
128         defer func() {
129                 discoveryMap[throttleParam] = float64(throttleDefault)
130         }()
131
132         api := &ArvTestClient{}
133         kc := &KeepTestClient{}
134         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
135         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
136
137         cr.CrunchLog.Print("Hello world!")
138         cr.CrunchLog.Print("Goodbye")
139         cr.CrunchLog.Close()
140
141         c.Check(api.Calls, Equals, 1)
142
143         mt, err := cr.LogCollection.ManifestText()
144         c.Check(err, IsNil)
145         c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
146
147         logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
148                 "2015-12-29T15:51:45.000000002Z Goodbye\n"
149
150         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
151         stderrLog := api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]
152         c.Check(true, Equals, strings.Contains(stderrLog, expected))
153         c.Check(string(kc.Content), Equals, logtext)
154 }