Merge branch 'master' into 10979-cancelled-job-nodes
[arvados.git] / services / crunch-run / logging_test.go
1 package main
2
3 import (
4         "fmt"
5         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
6         . "gopkg.in/check.v1"
7         "testing"
8         "time"
9 )
10
11 type LoggingTestSuite struct{}
12
13 type TestTimestamper struct {
14         count int
15 }
16
17 func (this *TestTimestamper) Timestamp(t time.Time) string {
18         this.count += 1
19         t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", this.count), t.Location())
20         if err != nil {
21                 panic(err)
22         }
23         return RFC3339Timestamp(t)
24 }
25
26 // Gocheck boilerplate
27 var _ = Suite(&LoggingTestSuite{})
28
29 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
30         api := &ArvTestClient{}
31         kc := &KeepTestClient{}
32         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
33         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
34
35         cr.CrunchLog.Print("Hello world!")
36         cr.CrunchLog.Print("Goodbye")
37         cr.CrunchLog.Close()
38
39         c.Check(api.Calls, Equals, 1)
40
41         mt, err := cr.LogCollection.ManifestText()
42         c.Check(err, IsNil)
43         c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
44
45         logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
46                 "2015-12-29T15:51:45.000000002Z Goodbye\n"
47
48         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
49         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
50         c.Check(string(kc.Content), Equals, logtext)
51 }
52
53 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
54         if testing.Short() {
55                 return
56         }
57         api := &ArvTestClient{}
58         kc := &KeepTestClient{}
59         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
60         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
61         cr.CrunchLog.Immediate = nil
62
63         for i := 0; i < 2000000; i++ {
64                 cr.CrunchLog.Printf("Hello %d", i)
65         }
66         cr.CrunchLog.Print("Goodbye")
67         cr.CrunchLog.Close()
68
69         c.Check(api.Calls > 1, Equals, true)
70         c.Check(api.Calls < 2000000, Equals, true)
71
72         mt, err := cr.LogCollection.ManifestText()
73         c.Check(err, IsNil)
74         c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
75 }
76
77 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
78         api := &ArvTestClient{}
79         kc := &KeepTestClient{}
80         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
81         ts := &TestTimestamper{}
82         cr.CrunchLog.Timestamper = ts.Timestamp
83         stdout := NewThrottledLogger(cr.NewLogWriter("stdout"))
84         stdout.Timestamper = ts.Timestamp
85
86         cr.CrunchLog.Print("Hello world!")
87         stdout.Print("Doing stuff")
88         cr.CrunchLog.Print("Goodbye")
89         stdout.Print("Blurb")
90         cr.CrunchLog.Close()
91         stdout.Close()
92
93         logText := make(map[string]string)
94         for _, content := range api.Content {
95                 log := content["log"].(arvadosclient.Dict)
96                 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
97         }
98
99         c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
100 2015-12-29T15:51:45.000000003Z Goodbye
101 `)
102         c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
103 2015-12-29T15:51:45.000000004Z Blurb
104 `)
105
106         mt, err := cr.LogCollection.ManifestText()
107         c.Check(err, IsNil)
108         c.Check(mt, Equals, ""+
109                 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
110                 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
111 }