1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
17 type LoggingTestSuite struct{}
19 type TestTimestamper struct {
23 func (this *TestTimestamper) Timestamp(t time.Time) string {
25 t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", this.count), t.Location())
29 return RFC3339Timestamp(t)
32 // Gocheck boilerplate
33 var _ = Suite(&LoggingTestSuite{})
35 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
36 api := &ArvTestClient{}
37 kc := &KeepTestClient{}
39 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
40 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
42 cr.CrunchLog.Print("Hello world!")
43 cr.CrunchLog.Print("Goodbye")
46 c.Check(api.Calls, Equals, 1)
48 mt, err := cr.LogCollection.ManifestText()
50 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
52 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
53 "2015-12-29T15:51:45.000000002Z Goodbye\n"
55 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
56 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
57 c.Check(string(kc.Content), Equals, logtext)
60 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
64 api := &ArvTestClient{}
65 kc := &KeepTestClient{}
67 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
68 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
69 cr.CrunchLog.Immediate = nil
71 for i := 0; i < 2000000; i++ {
72 cr.CrunchLog.Printf("Hello %d", i)
74 cr.CrunchLog.Print("Goodbye")
77 c.Check(api.Calls > 1, Equals, true)
78 c.Check(api.Calls < 2000000, Equals, true)
80 mt, err := cr.LogCollection.ManifestText()
82 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
85 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
86 api := &ArvTestClient{}
87 kc := &KeepTestClient{}
89 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
90 ts := &TestTimestamper{}
91 cr.CrunchLog.Timestamper = ts.Timestamp
92 stdout := NewThrottledLogger(cr.NewLogWriter("stdout"))
93 stdout.Timestamper = ts.Timestamp
95 cr.CrunchLog.Print("Hello world!")
96 stdout.Print("Doing stuff")
97 cr.CrunchLog.Print("Goodbye")
102 logText := make(map[string]string)
103 for _, content := range api.Content {
104 log := content["log"].(arvadosclient.Dict)
105 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
108 c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
109 2015-12-29T15:51:45.000000003Z Goodbye
111 c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
112 2015-12-29T15:51:45.000000004Z Blurb
115 mt, err := cr.LogCollection.ManifestText()
117 c.Check(mt, Equals, ""+
118 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
119 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
122 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
123 testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
126 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
127 testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
130 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
131 testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
134 func testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
135 discoveryMap[throttleParam] = float64(throttleValue)
137 discoveryMap[throttleParam] = float64(throttleDefault)
140 api := &ArvTestClient{}
141 kc := &KeepTestClient{}
143 cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
144 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
146 cr.CrunchLog.Print("Hello world!")
147 cr.CrunchLog.Print("Goodbye")
150 c.Check(api.Calls, Equals, 1)
152 mt, err := cr.LogCollection.ManifestText()
154 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
156 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
157 "2015-12-29T15:51:45.000000002Z Goodbye\n"
159 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
160 stderrLog := api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]
161 c.Check(true, Equals, strings.Contains(stderrLog, expected))
162 c.Check(string(kc.Content), Equals, logtext)