1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/sdk/go/arvados"
14 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18 type LoggingTestSuite struct {
19 client *arvados.Client
22 type TestTimestamper struct {
26 func (stamper *TestTimestamper) Timestamp(t time.Time) string {
28 t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", stamper.count), t.Location())
32 return RFC3339Timestamp(t)
35 // Gocheck boilerplate
36 var _ = Suite(&LoggingTestSuite{})
38 func (s *LoggingTestSuite) SetUpTest(c *C) {
39 s.client = arvados.NewClientFromEnv()
40 crunchLogUpdatePeriod = time.Hour * 24 * 365
41 crunchLogUpdateSize = 1 << 50
44 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
45 api := &ArvTestClient{}
46 kc := &KeepTestClient{}
48 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
50 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
52 cr.CrunchLog.Print("Hello world!")
53 cr.CrunchLog.Print("Goodbye")
56 c.Check(api.Calls, Equals, 1)
58 mt, err := cr.LogCollection.MarshalManifest(".")
60 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
62 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
63 "2015-12-29T15:51:45.000000002Z Goodbye\n"
65 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
66 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
67 c.Check(string(kc.Content), Equals, logtext)
70 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
74 api := &ArvTestClient{}
75 kc := &KeepTestClient{}
77 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
79 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
80 cr.CrunchLog.Immediate = nil
82 for i := 0; i < 2000000; i++ {
83 cr.CrunchLog.Printf("Hello %d", i)
85 cr.CrunchLog.Print("Goodbye")
88 c.Check(api.Calls > 0, Equals, true)
89 c.Check(api.Calls < 2000000, Equals, true)
91 mt, err := cr.LogCollection.MarshalManifest(".")
93 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
96 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
97 api := &ArvTestClient{}
98 kc := &KeepTestClient{}
100 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
102 ts := &TestTimestamper{}
103 cr.CrunchLog.Timestamper = ts.Timestamp
104 w, err := cr.NewLogWriter("stdout")
106 stdout := NewThrottledLogger(w)
107 stdout.Timestamper = ts.Timestamp
109 cr.CrunchLog.Print("Hello world!")
110 stdout.Print("Doing stuff")
111 cr.CrunchLog.Print("Goodbye")
112 stdout.Print("Blurb")
116 logText := make(map[string]string)
117 for _, content := range api.Content {
118 log := content["log"].(arvadosclient.Dict)
119 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
122 c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
123 2015-12-29T15:51:45.000000003Z Goodbye
125 c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
126 2015-12-29T15:51:45.000000004Z Blurb
129 mt, err := cr.LogCollection.MarshalManifest(".")
131 c.Check(mt, Equals, ". 48f9023dc683a850b1c9b482b14c4b97+163 0:83:crunch-run.txt 83:80:stdout.txt\n")
134 func (s *LoggingTestSuite) TestLogUpdate(c *C) {
135 for _, trial := range []struct {
137 maxDuration time.Duration
139 {1000, 10 * time.Second},
140 {1000000, time.Millisecond},
142 c.Logf("max %d bytes, %s", trial.maxBytes, trial.maxDuration)
143 crunchLogUpdateSize = trial.maxBytes
144 crunchLogUpdatePeriod = trial.maxDuration
146 api := &ArvTestClient{}
147 kc := &KeepTestClient{}
149 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
151 ts := &TestTimestamper{}
152 cr.CrunchLog.Timestamper = ts.Timestamp
153 w, err := cr.NewLogWriter("stdout")
155 stdout := NewThrottledLogger(w)
156 stdout.Timestamper = ts.Timestamp
158 c.Check(cr.logUUID, Equals, "")
159 cr.CrunchLog.Printf("Hello %1000s", "space")
160 for i, t := 0, time.NewTicker(time.Millisecond); i < 5000 && cr.logUUID == ""; i++ {
163 c.Check(cr.logUUID, Not(Equals), "")
164 cr.CrunchLog.Print("Goodbye")
165 fmt.Fprint(stdout, "Goodbye\n")
170 mt, err := cr.LogCollection.MarshalManifest(".")
172 // Block packing depends on whether there's an update
173 // between the two Goodbyes -- either way the first
174 // block will be 4dc76.
175 c.Check(mt, Matches, `. 4dc76e0a212bfa30c39d76d8c16da0c0\+1038 (afc503bc1b9a828b4bb543cb629e936c\+78|90699dc22545cd74a0664303f70bc05a\+39 276b49339fd5203d15a93ff3de11bfb9\+39) 0:1077:crunch-run.txt 1077:39:stdout.txt\n`)
179 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
180 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
183 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
184 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
187 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
188 s.testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
191 func (s *LoggingTestSuite) testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
192 discoveryMap[throttleParam] = float64(throttleValue)
194 discoveryMap[throttleParam] = float64(throttleDefault)
197 api := &ArvTestClient{}
198 kc := &KeepTestClient{}
200 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
202 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
204 cr.CrunchLog.Print("Hello world!")
205 cr.CrunchLog.Print("Goodbye")
208 c.Check(api.Calls, Equals, 1)
210 mt, err := cr.LogCollection.MarshalManifest(".")
212 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
214 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
215 "2015-12-29T15:51:45.000000002Z Goodbye\n"
217 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
218 stderrLog := api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]
219 c.Check(true, Equals, strings.Contains(stderrLog, expected))
220 c.Check(string(kc.Content), Equals, logtext)