1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
9 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
16 type LoggingTestSuite struct{}
18 type TestTimestamper struct {
22 func (this *TestTimestamper) Timestamp(t time.Time) string {
24 t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", this.count), t.Location())
28 return RFC3339Timestamp(t)
31 // Gocheck boilerplate
32 var _ = Suite(&LoggingTestSuite{})
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
40 cr.CrunchLog.Print("Hello world!")
41 cr.CrunchLog.Print("Goodbye")
44 c.Check(api.Calls, Equals, 1)
46 mt, err := cr.LogCollection.ManifestText()
48 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
50 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
51 "2015-12-29T15:51:45.000000002Z Goodbye\n"
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)
58 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
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
68 for i := 0; i < 2000000; i++ {
69 cr.CrunchLog.Printf("Hello %d", i)
71 cr.CrunchLog.Print("Goodbye")
74 c.Check(api.Calls > 1, Equals, true)
75 c.Check(api.Calls < 2000000, Equals, true)
77 mt, err := cr.LogCollection.ManifestText()
79 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
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
91 cr.CrunchLog.Print("Hello world!")
92 stdout.Print("Doing stuff")
93 cr.CrunchLog.Print("Goodbye")
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"]
104 c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
105 2015-12-29T15:51:45.000000003Z Goodbye
107 c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
108 2015-12-29T15:51:45.000000004Z Blurb
111 mt, err := cr.LogCollection.ManifestText()
113 c.Check(mt, Equals, ""+
114 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
115 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
118 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
119 testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
122 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
123 testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
126 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
127 testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
130 func testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
131 discoveryMap[throttleParam] = float64(throttleValue)
133 discoveryMap[throttleParam] = float64(throttleDefault)
136 api := &ArvTestClient{}
137 kc := &KeepTestClient{}
138 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
139 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
141 cr.CrunchLog.Print("Hello world!")
142 cr.CrunchLog.Print("Goodbye")
145 c.Check(api.Calls, Equals, 1)
147 mt, err := cr.LogCollection.ManifestText()
149 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
151 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
152 "2015-12-29T15:51:45.000000002Z Goodbye\n"
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)