github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/logfwd/record_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package logfwd_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/loggo" 11 "github.com/juju/testing" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/logfwd" 16 ) 17 18 type RecordSuite struct { 19 testing.IsolationSuite 20 } 21 22 var _ = gc.Suite(&RecordSuite{}) 23 24 func (s *RecordSuite) TestValidateValid(c *gc.C) { 25 rec := validRecord 26 27 err := rec.Validate() 28 29 c.Check(err, jc.ErrorIsNil) 30 } 31 32 func (s *RecordSuite) TestValidateZero(c *gc.C) { 33 var rec logfwd.Record 34 35 err := rec.Validate() 36 37 c.Check(err, jc.Satisfies, errors.IsNotValid) 38 } 39 40 func (s *RecordSuite) TestValidateBadOrigin(c *gc.C) { 41 rec := validRecord 42 rec.Origin.Name = "..." 43 44 err := rec.Validate() 45 46 c.Check(err, jc.Satisfies, errors.IsNotValid) 47 c.Check(err, gc.ErrorMatches, `invalid Origin: invalid Name "...": bad user name`) 48 } 49 50 func (s *RecordSuite) TestValidateEmptyTimestamp(c *gc.C) { 51 rec := validRecord 52 rec.Timestamp = time.Time{} 53 54 err := rec.Validate() 55 56 c.Check(err, jc.Satisfies, errors.IsNotValid) 57 c.Check(err, gc.ErrorMatches, `empty Timestamp`) 58 } 59 60 func (s *RecordSuite) TestValidateBadLocation(c *gc.C) { 61 rec := validRecord 62 rec.Location.Filename = "" 63 64 err := rec.Validate() 65 66 c.Check(err, jc.Satisfies, errors.IsNotValid) 67 c.Check(err, gc.ErrorMatches, `invalid Location: Line set but Filename empty`) 68 } 69 70 type LocationSuite struct { 71 testing.IsolationSuite 72 } 73 74 var _ = gc.Suite(&LocationSuite{}) 75 76 func (s *LocationSuite) TestParseLocationTooLegitToQuit(c *gc.C) { 77 expected := validLocation 78 79 loc, err := logfwd.ParseLocation(expected.Module, expected.String()) 80 c.Assert(err, jc.ErrorIsNil) 81 82 c.Check(loc, jc.DeepEquals, expected) 83 } 84 85 func (s *LocationSuite) TestParseLocationIsValid(c *gc.C) { 86 expected := validLocation 87 loc, err := logfwd.ParseLocation(expected.Module, expected.String()) 88 c.Assert(err, jc.ErrorIsNil) 89 90 err = loc.Validate() 91 92 c.Check(err, jc.ErrorIsNil) 93 } 94 95 func (s *LocationSuite) TestParseLocationMissingFilename(c *gc.C) { 96 expected := validLocation 97 expected.Filename = "" 98 99 loc, err := logfwd.ParseLocation(expected.Module, ":42") 100 c.Assert(err, jc.ErrorIsNil) 101 102 c.Check(loc, jc.DeepEquals, expected) 103 } 104 105 func (s *LocationSuite) TestParseLocationBogusFilename(c *gc.C) { 106 expected := validLocation 107 expected.Filename = "..." 108 109 loc, err := logfwd.ParseLocation(expected.Module, "...:42") 110 c.Assert(err, jc.ErrorIsNil) 111 112 c.Check(loc, jc.DeepEquals, expected) 113 } 114 115 func (s *LocationSuite) TestParseLocationFilenameOnly(c *gc.C) { 116 expected := validLocation 117 expected.Line = -1 118 119 loc, err := logfwd.ParseLocation(expected.Module, expected.Filename) 120 c.Assert(err, jc.ErrorIsNil) 121 122 c.Check(loc, jc.DeepEquals, expected) 123 } 124 125 func (s *LocationSuite) TestParseLocationMissingLine(c *gc.C) { 126 _, err := logfwd.ParseLocation(validLocation.Module, "spam.go:") 127 128 c.Check(err, gc.ErrorMatches, `failed to parse sourceLine: missing line number after ":"`) 129 } 130 131 func (s *LocationSuite) TestParseLocationBogusLine(c *gc.C) { 132 _, err := logfwd.ParseLocation(validLocation.Module, "spam.go:xxx") 133 134 c.Check(err, gc.ErrorMatches, `failed to parse sourceLine: line number must be non-negative integer: strconv.(ParseInt|Atoi): parsing "xxx": invalid syntax`) 135 } 136 137 func (s *LocationSuite) TestValidateValid(c *gc.C) { 138 loc := validLocation 139 140 err := loc.Validate() 141 142 c.Check(err, jc.ErrorIsNil) 143 } 144 145 func (s *LocationSuite) TestValidateEmpty(c *gc.C) { 146 var loc logfwd.SourceLocation 147 148 err := loc.Validate() 149 150 c.Check(err, jc.ErrorIsNil) 151 } 152 153 func (s *LocationSuite) TestValidateBadLine(c *gc.C) { 154 loc := validLocation 155 loc.Filename = "" 156 157 err := loc.Validate() 158 159 c.Check(err, jc.Satisfies, errors.IsNotValid) 160 c.Check(err, gc.ErrorMatches, `Line set but Filename empty`) 161 } 162 163 var validLocation = logfwd.SourceLocation{ 164 Module: "spam", 165 Filename: "eggs.go", 166 Line: 42, 167 } 168 169 var validRecord = logfwd.Record{ 170 Origin: validOrigin, 171 Timestamp: time.Now(), 172 Level: loggo.ERROR, 173 Location: validLocation, 174 Message: "uh-oh", 175 }