-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_test.go
175 lines (122 loc) · 3.95 KB
/
record_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package logfwd_test
import (
"time"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/logfwd"
)
type RecordSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&RecordSuite{})
func (s *RecordSuite) TestValidateValid(c *gc.C) {
rec := validRecord
err := rec.Validate()
c.Check(err, jc.ErrorIsNil)
}
func (s *RecordSuite) TestValidateZero(c *gc.C) {
var rec logfwd.Record
err := rec.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
}
func (s *RecordSuite) TestValidateBadOrigin(c *gc.C) {
rec := validRecord
rec.Origin.Name = "..."
err := rec.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `invalid Origin: invalid Name "...": bad user name`)
}
func (s *RecordSuite) TestValidateEmptyTimestamp(c *gc.C) {
rec := validRecord
rec.Timestamp = time.Time{}
err := rec.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `empty Timestamp`)
}
func (s *RecordSuite) TestValidateBadLocation(c *gc.C) {
rec := validRecord
rec.Location.Filename = ""
err := rec.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `invalid Location: Line set but Filename empty`)
}
type LocationSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&LocationSuite{})
func (s *LocationSuite) TestParseLocationTooLegitToQuit(c *gc.C) {
expected := validLocation
loc, err := logfwd.ParseLocation(expected.Module, expected.String())
c.Assert(err, jc.ErrorIsNil)
c.Check(loc, jc.DeepEquals, expected)
}
func (s *LocationSuite) TestParseLocationIsValid(c *gc.C) {
expected := validLocation
loc, err := logfwd.ParseLocation(expected.Module, expected.String())
c.Assert(err, jc.ErrorIsNil)
err = loc.Validate()
c.Check(err, jc.ErrorIsNil)
}
func (s *LocationSuite) TestParseLocationMissingFilename(c *gc.C) {
expected := validLocation
expected.Filename = ""
loc, err := logfwd.ParseLocation(expected.Module, ":42")
c.Assert(err, jc.ErrorIsNil)
c.Check(loc, jc.DeepEquals, expected)
}
func (s *LocationSuite) TestParseLocationBogusFilename(c *gc.C) {
expected := validLocation
expected.Filename = "..."
loc, err := logfwd.ParseLocation(expected.Module, "...:42")
c.Assert(err, jc.ErrorIsNil)
c.Check(loc, jc.DeepEquals, expected)
}
func (s *LocationSuite) TestParseLocationFilenameOnly(c *gc.C) {
expected := validLocation
expected.Line = -1
loc, err := logfwd.ParseLocation(expected.Module, expected.Filename)
c.Assert(err, jc.ErrorIsNil)
c.Check(loc, jc.DeepEquals, expected)
}
func (s *LocationSuite) TestParseLocationMissingLine(c *gc.C) {
_, err := logfwd.ParseLocation(validLocation.Module, "spam.go:")
c.Check(err, gc.ErrorMatches, `failed to parse sourceLine: missing line number after ":"`)
}
func (s *LocationSuite) TestParseLocationBogusLine(c *gc.C) {
_, err := logfwd.ParseLocation(validLocation.Module, "spam.go:xxx")
c.Check(err, gc.ErrorMatches, `failed to parse sourceLine: line number must be non-negative integer: strconv.(ParseInt|Atoi): parsing "xxx": invalid syntax`)
}
func (s *LocationSuite) TestValidateValid(c *gc.C) {
loc := validLocation
err := loc.Validate()
c.Check(err, jc.ErrorIsNil)
}
func (s *LocationSuite) TestValidateEmpty(c *gc.C) {
var loc logfwd.SourceLocation
err := loc.Validate()
c.Check(err, jc.ErrorIsNil)
}
func (s *LocationSuite) TestValidateBadLine(c *gc.C) {
loc := validLocation
loc.Filename = ""
err := loc.Validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `Line set but Filename empty`)
}
var validLocation = logfwd.SourceLocation{
Module: "spam",
Filename: "eggs.go",
Line: 42,
}
var validRecord = logfwd.Record{
Origin: validOrigin,
Timestamp: time.Now(),
Level: loggo.ERROR,
Location: validLocation,
Message: "uh-oh",
}