Skip to content

Commit 879b627

Browse files
author
Matthew Fisher
committed
Merge pull request #1132 from deis/log-formatting
test(logger): add syslog tests
2 parents 546d2bb + 762d15f commit 879b627

8 files changed

Lines changed: 156 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pip-log.txt
2424
.coverage
2525
.tox
2626
nosetests.xml
27+
logger/coverage.out
2728

2829
# Translations
2930
*.mo

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ before_install:
1818
install:
1919
- pip install -r controller/requirements.txt
2020
- pip install -r controller/dev_requirements.txt coveralls
21+
- go get code.google.com/p/go.tools/cmd/cover
2122

2223
before_script:
2324
- "psql -c 'create database deis_testing;' -U postgres"
@@ -40,6 +41,7 @@ script:
4041
- make -C controller flake8
4142
- make -C controller coverage
4243
- make -C docs
44+
- make -C logger test
4345

4446
after_success:
4547
- cd controller && coveralls

client/deis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def apps_logs(self, args):
566566
response = self._dispatch('post',
567567
"/api/apps/{}/logs".format(app))
568568
if response.status_code == requests.codes.ok: # @UndefinedVariable
569-
print(response.json())
569+
sys.stdout.write(response.json())
570570
else:
571571
raise ResponseError(response)
572572

logger/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ full-clean: clean
3030
test: test-unit test-functional
3131

3232
test-unit:
33-
@echo no unit tests
33+
go test -v -cover ./syslog
3434

3535
test-functional:
3636
GOPATH=$(CURDIR)/../tests/_vendor:$(GOPATH) go test -v ./tests/...
37+
38+
coverage:
39+
go test -coverprofile coverage.out ./syslog
40+
go tool cover -html=coverage.out

logger/syslog/filehandler_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package syslog
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
type TestLogger struct {}
9+
10+
func (tl TestLogger) Print(...interface{}) {}
11+
func (tl TestLogger) Printf(format string, v ...interface{}) {}
12+
func (tl TestLogger) Println(...interface{}) {}
13+
14+
func TestNewFileHandler(t *testing.T) {
15+
fh := NewFileHandler("", 1, func (m *Message) bool {return true}, true)
16+
if fh == nil {
17+
t.Errorf("expected filehandler, got nil")
18+
}
19+
}
20+
21+
func TestSetLogger(t *testing.T) {
22+
tl := TestLogger{}
23+
fh := NewFileHandler("", 1, func (m *Message) bool {return true}, true)
24+
fh.SetLogger(tl)
25+
if fh.l != tl {
26+
t.Errorf("expected the logger to be set")
27+
}
28+
}
29+
30+
func TestHandle(t *testing.T) {
31+
fh := NewFileHandler("/tmp/test", 1, func (m *Message) bool {return true}, true)
32+
handle := fh.Handle(&Message{time.Now(), nil, 0, 0, time.Now(), "localhost", "test", "message", "", ""})
33+
if handle == nil {
34+
t.Errorf("expected a handle, got nil")
35+
}
36+
}

logger/syslog/message.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func (m *Message) NetSrc() string {
3636
func (m *Message) String() string {
3737
timeLayout := "2006-01-02 15:04:05"
3838
return fmt.Sprintf(
39-
"%s %s %s %s%s",
40-
m.Time.Format(timeLayout), m.Source,
41-
m.Hostname, m.Tag, m.Content,
39+
"%s %s %s",
40+
m.Time.Format(timeLayout),
41+
m.Hostname,
42+
m.Content,
4243
)
4344
}

logger/syslog/message_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package syslog
2+
3+
import (
4+
"net"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestMessageNetSrc(t *testing.T) {
10+
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")
11+
12+
if err != nil {
13+
t.Errorf("could not resolve TCP address")
14+
}
15+
16+
m := &Message{time.Now(), tcpAddress, 0, 0, time.Now(), "", "", "", "", ""}
17+
if m.NetSrc() != "127.0.0.1" {
18+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
19+
}
20+
21+
udpAddress, err := net.ResolveUDPAddr("udp", "localhost:1234")
22+
23+
if err != nil {
24+
t.Errorf("could not resolve UDP address")
25+
}
26+
27+
m.Source = udpAddress
28+
if m.NetSrc() != "127.0.0.1" {
29+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
30+
}
31+
32+
unixAddress, err := net.ResolveUnixAddr("unix", "/tmp/str")
33+
34+
if err != nil {
35+
t.Errorf("could not resolve unix address")
36+
}
37+
38+
m.Source = unixAddress
39+
if m.NetSrc() != "/tmp/str" {
40+
t.Errorf("expected /tmp/str, got ", m.NetSrc())
41+
}
42+
43+
unknownAddress, err := net.ResolveIPAddr("ip4", "localhost")
44+
45+
if err != nil {
46+
t.Errorf("could not resolve unknown address")
47+
}
48+
49+
m.Source = unknownAddress
50+
if m.NetSrc() != "127.0.0.1" {
51+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
52+
}
53+
}
54+
55+
func TestMessageFormat(t *testing.T) {
56+
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")
57+
58+
if err != nil {
59+
t.Errorf("could not resolve TCP address")
60+
}
61+
62+
m := &Message{
63+
time.Now(),
64+
tcpAddress,
65+
0,
66+
0,
67+
time.Now(),
68+
"localhost",
69+
"TEST",
70+
"hello world",
71+
"",
72+
"",
73+
}
74+
75+
timeLayout := "2006-01-02 15:04:05"
76+
expectedOutput := m.Time.Format(timeLayout) + " localhost hello world"
77+
if m.String() != expectedOutput {
78+
t.Errorf("expected '" + expectedOutput + "', got '" + m.String() + "'.")
79+
}
80+
}

logger/syslog/priority_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package syslog
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFacilityToString(t *testing.T) {
8+
var fac Facility = 100
9+
if fac.String() != "unknown" {
10+
t.Errorf("facility != unknown; got %s", fac.String())
11+
}
12+
fac = 20
13+
if fac.String() != "local4" {
14+
t.Errorf("facility != local4; got %s", fac.String())
15+
}
16+
}
17+
18+
func TestSeverityToString(t *testing.T) {
19+
var sev Severity = 100
20+
if sev.String() != "unknown" {
21+
t.Errorf("severity != unknown; got %s", sev.String())
22+
}
23+
sev = 5
24+
if sev.String() != "notice" {
25+
t.Errorf("severity != local4; got %s", sev.String())
26+
}
27+
}

0 commit comments

Comments
 (0)