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