9374: Use arvados.Collection instead of own CollectionRecord.
[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         return fmt.Sprintf("2015-12-29T15:51:45.%09dZ", this.count)
20 }
21
22 // Gocheck boilerplate
23 var _ = Suite(&LoggingTestSuite{})
24
25 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
26         api := &ArvTestClient{}
27         kc := &KeepTestClient{}
28         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
29         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
30
31         cr.CrunchLog.Print("Hello world!")
32         cr.CrunchLog.Print("Goodbye")
33         cr.CrunchLog.Close()
34
35         c.Check(api.Calls, Equals, 1)
36
37         mt, err := cr.LogCollection.ManifestText()
38         c.Check(err, IsNil)
39         c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
40
41         logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
42                 "2015-12-29T15:51:45.000000002Z Goodbye\n"
43
44         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
45         c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
46         c.Check(string(kc.Content), Equals, logtext)
47 }
48
49 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
50         if testing.Short() {
51                 return
52         }
53         api := &ArvTestClient{}
54         kc := &KeepTestClient{}
55         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
56         cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
57         cr.CrunchLog.Immediate = nil
58
59         for i := 0; i < 2000000; i++ {
60                 cr.CrunchLog.Printf("Hello %d", i)
61         }
62         cr.CrunchLog.Print("Goodbye")
63         cr.CrunchLog.Close()
64
65         c.Check(api.Calls > 1, Equals, true)
66         c.Check(api.Calls < 2000000, Equals, true)
67
68         mt, err := cr.LogCollection.ManifestText()
69         c.Check(err, IsNil)
70         c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
71 }
72
73 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
74         api := &ArvTestClient{}
75         kc := &KeepTestClient{}
76         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
77         ts := &TestTimestamper{}
78         cr.CrunchLog.Timestamper = ts.Timestamp
79         stdout := NewThrottledLogger(cr.NewLogWriter("stdout"))
80         stdout.Timestamper = ts.Timestamp
81
82         cr.CrunchLog.Print("Hello world!")
83         stdout.Print("Doing stuff")
84         cr.CrunchLog.Print("Goodbye")
85         stdout.Print("Blurb")
86         cr.CrunchLog.Close()
87         stdout.Close()
88
89         logText := make(map[string]string)
90         for _, content := range api.Content {
91                 log := content["log"].(arvadosclient.Dict)
92                 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
93         }
94
95         c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
96 2015-12-29T15:51:45.000000003Z Goodbye
97 `)
98         c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
99 2015-12-29T15:51:45.000000004Z Blurb
100 `)
101
102         mt, err := cr.LogCollection.ManifestText()
103         c.Check(err, IsNil)
104         c.Check(mt, Equals, ""+
105                 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
106                 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
107 }