1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18 check "gopkg.in/check.v1"
21 type LoggingTestSuite struct {
22 client *arvados.Client
25 type TestTimestamper struct {
29 func (stamper *TestTimestamper) Timestamp(t time.Time) string {
31 t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", stamper.count), t.Location())
35 return RFC3339Timestamp(t)
38 // Gocheck boilerplate
39 var _ = Suite(&LoggingTestSuite{})
41 func (s *LoggingTestSuite) SetUpTest(c *C) {
42 s.client = arvados.NewClientFromEnv()
43 crunchLogUpdatePeriod = time.Hour * 24 * 365
44 crunchLogUpdateSize = 1 << 50
47 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
48 api := &ArvTestClient{}
49 kc := &KeepTestClient{}
51 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
53 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
55 cr.CrunchLog.Print("Hello world!")
56 cr.CrunchLog.Print("Goodbye")
59 c.Check(api.Calls, Equals, 1)
61 mt, err := cr.LogCollection.MarshalManifest(".")
63 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
65 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
66 "2015-12-29T15:51:45.000000002Z Goodbye\n"
68 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
69 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
70 c.Check(string(kc.Content), Equals, logtext)
73 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
77 api := &ArvTestClient{}
78 kc := &KeepTestClient{}
80 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
82 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
83 cr.CrunchLog.Immediate = nil
85 for i := 0; i < 2000000; i++ {
86 cr.CrunchLog.Printf("Hello %d", i)
88 cr.CrunchLog.Print("Goodbye")
91 c.Check(api.Calls > 0, Equals, true)
92 c.Check(api.Calls < 2000000, Equals, true)
94 mt, err := cr.LogCollection.MarshalManifest(".")
96 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
99 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
100 api := &ArvTestClient{}
101 kc := &KeepTestClient{}
103 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
105 ts := &TestTimestamper{}
106 cr.CrunchLog.Timestamper = ts.Timestamp
107 w, err := cr.NewLogWriter("stdout")
109 stdout := NewThrottledLogger(w)
110 stdout.Timestamper = ts.Timestamp
112 cr.CrunchLog.Print("Hello world!")
113 stdout.Print("Doing stuff")
114 cr.CrunchLog.Print("Goodbye")
115 stdout.Print("Blurb")
119 logText := make(map[string]string)
120 for _, content := range api.Content {
121 log := content["log"].(arvadosclient.Dict)
122 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
125 c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
126 2015-12-29T15:51:45.000000003Z Goodbye
128 c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
129 2015-12-29T15:51:45.000000004Z Blurb
132 mt, err := cr.LogCollection.MarshalManifest(".")
134 c.Check(mt, Equals, ". 48f9023dc683a850b1c9b482b14c4b97+163 0:83:crunch-run.txt 83:80:stdout.txt\n")
137 func (s *LoggingTestSuite) TestLogUpdate(c *C) {
138 for _, trial := range []struct {
140 maxDuration time.Duration
142 {1000, 10 * time.Second},
143 {1000000, time.Millisecond},
145 c.Logf("max %d bytes, %s", trial.maxBytes, trial.maxDuration)
146 crunchLogUpdateSize = trial.maxBytes
147 crunchLogUpdatePeriod = trial.maxDuration
149 api := &ArvTestClient{}
150 kc := &KeepTestClient{}
152 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
154 ts := &TestTimestamper{}
155 cr.CrunchLog.Timestamper = ts.Timestamp
156 w, err := cr.NewLogWriter("stdout")
158 stdout := NewThrottledLogger(w)
159 stdout.Timestamper = ts.Timestamp
161 c.Check(cr.logUUID, Equals, "")
162 cr.CrunchLog.Printf("Hello %1000s", "space")
163 for i, t := 0, time.NewTicker(time.Millisecond); i < 5000 && cr.logUUID == ""; i++ {
166 c.Check(cr.logUUID, Not(Equals), "")
167 cr.CrunchLog.Print("Goodbye")
168 fmt.Fprint(stdout, "Goodbye\n")
173 mt, err := cr.LogCollection.MarshalManifest(".")
175 // Block packing depends on whether there's an update
176 // between the two Goodbyes -- either way the first
177 // block will be 4dc76.
178 c.Check(mt, Matches, `. 4dc76e0a212bfa30c39d76d8c16da0c0\+1038 (afc503bc1b9a828b4bb543cb629e936c\+78|90699dc22545cd74a0664303f70bc05a\+39 276b49339fd5203d15a93ff3de11bfb9\+39) 0:1077:crunch-run.txt 1077:39:stdout.txt\n`)
182 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
183 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
186 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
187 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
190 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
191 s.testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
194 func (s *LoggingTestSuite) testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
195 discoveryMap[throttleParam] = float64(throttleValue)
197 discoveryMap[throttleParam] = float64(throttleDefault)
200 api := &ArvTestClient{}
201 kc := &KeepTestClient{}
203 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
205 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
207 cr.CrunchLog.Print("Hello world!")
208 cr.CrunchLog.Print("Goodbye")
211 c.Check(api.Calls, Equals, 1)
213 mt, err := cr.LogCollection.MarshalManifest(".")
215 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
217 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
218 "2015-12-29T15:51:45.000000002Z Goodbye\n"
220 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
221 stderrLog := api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]
222 c.Check(true, Equals, strings.Contains(stderrLog, expected))
223 c.Check(string(kc.Content), Equals, logtext)
226 type filterSuite struct{}
228 var _ = Suite(&filterSuite{})
230 func (*filterSuite) TestFilterKeepstoreErrorsOnly(c *check.C) {
232 f := filterKeepstoreErrorsOnly{WriteCloser: nopCloser{&buf}}
233 for _, s := range []string{
235 "son\n" + `{"msg":"foo"}` + "\n{}\n" + `{"msg":"request"}` + "\n" + `{"msg":1234}` + "\n\n",
237 `{"msg":"response","respStatusCode":404,"foo": "bar"}` + "\n",
238 `{"msg":"response","respStatusCode":206}` + "\n",
242 c.Check(buf.String(), check.Equals, `not json
247 {"msg":"response","respStatusCode":404,"foo": "bar"}
251 type nopCloser struct {
255 func (nopCloser) Close() error { return nil }