This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logFormat_test.go
111 lines (89 loc) · 2.61 KB
/
logFormat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"os"
"time"
"github.com/fsouza/go-dockerclient"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/robertkrimen/otto"
)
var _ = Describe("LogFormat", func() {
var (
benchmarkLogFormatScript = `
var message = dockerlogstream.Message;
var container_name = message.Container.Name.substring(1);
if (container_name == "skipme") {
dockerlogstream.SkipLogLine();
} else {
dockerlogstream.SendLogLine("<22> " + message.Time.Format("Jan 2 15:04:05") + " " +
dockerlogstream.Hostname + " " + container_name + ": " + message.Data);
}
`
err error
m *message
msgTime time.Time
testLoops = 10000
)
BeforeSuite(func() {
jsVM = otto.New()
jsLineConverter, err = jsVM.Compile("", benchmarkLogFormatScript)
Expect(err).NotTo(HaveOccurred())
msgTime = time.Now()
m = &message{
Container: &docker.Container{Name: "/testcontainer"},
Data: "I am a random log line with a few characters like I might be generated by some random program",
Time: msgTime,
}
})
Measure("it should handle the JavaScript call efficiently", func(b Benchmarker) {
runtime := b.Time("runtime", func() {
_, _, err := formatLogLine(m)
Expect(err).NotTo(HaveOccurred())
})
Expect(runtime.Nanoseconds()).Should(BeNumerically("<", 25000*testLoops), "JavaScript call should not exceed ø of 25000ns")
}, testLoops)
Context("Testing JavaScript call results", func() {
var (
outputLine string
skipLine bool
)
BeforeEach(func() {
outputLine, skipLine, err = formatLogLine(m)
})
It("should not have errored", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have marked that line for sending", func() {
Expect(skipLine).To(Equal(false))
})
It("should have generated the expected log line", func() {
hostname, _ := os.Hostname()
expect := fmt.Sprintf("<22> %s %s testcontainer: I am a random log line with a few characters like I might be generated by some random program",
msgTime.Format("Jan 2 15:04:05"),
hostname,
)
Expect(outputLine).To(Equal(expect))
})
})
Context("Testing line skipping", func() {
var (
outputLine string
skipLine bool
)
BeforeEach(func() {
m = &message{
Container: &docker.Container{Name: "/skipme"},
Data: "I am a random log line with a few characters like I might be generated by some random program",
Time: msgTime,
}
outputLine, skipLine, err = formatLogLine(m)
})
It("should not have errored", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should have skipped the line", func() {
Expect(skipLine).To(Equal(true))
})
})
})