8015: Don't use Docker logging driver. Attach stdout/stderr before starting container.
[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["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
44         c.Check(api.Content["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
54         for i := 0; i < 2000000; i += 1 {
55                 cr.CrunchLog.Printf("Hello %d", i)
56         }
57         cr.CrunchLog.Print("Goodbye")
58         cr.CrunchLog.Close()
59
60         c.Check(api.Calls > 1, Equals, true)
61         c.Check(api.Calls < 2000000, Equals, true)
62
63         mt, err := cr.LogCollection.ManifestText()
64         c.Check(err, IsNil)
65         c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
66 }
67
68 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
69         api := &ArvTestClient{}
70         kc := &KeepTestClient{}
71         cr := NewContainerRunner(api, kc, nil, "zzzzz-zzzzzzzzzzzzzzz")
72         ts := &TestTimestamper{}
73         cr.CrunchLog.Timestamper = ts.Timestamp
74         stdout := NewThrottledLogger(cr.NewLogWriter("stdout"))
75         stdout.Timestamper = ts.Timestamp
76
77         cr.CrunchLog.Print("Hello world!")
78         stdout.Print("Doing stuff")
79         cr.CrunchLog.Print("Goodbye")
80         stdout.Print("Blurb")
81
82         cr.CrunchLog.Close()
83         logtext1 := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
84                 "2015-12-29T15:51:45.000000003Z Goodbye\n"
85         c.Check(api.Content["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
86         c.Check(api.Content["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext1)
87
88         stdout.Close()
89         logtext2 := "2015-12-29T15:51:45.000000002Z Doing stuff\n" +
90                 "2015-12-29T15:51:45.000000004Z Blurb\n"
91         c.Check(api.Content["log"].(arvadosclient.Dict)["event_type"], Equals, "stdout")
92         c.Check(api.Content["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext2)
93
94         mt, err := cr.LogCollection.ManifestText()
95         c.Check(err, IsNil)
96         c.Check(mt, Equals, ""+
97                 ". 408672f5b5325f7d20edfbf899faee42+83 0:83:crunch-run.txt\n"+
98                 ". c556a293010069fa79a6790a931531d5+80 0:80:stdout.txt\n")
99 }