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