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