11package firetail
22
33import (
4+ "encoding/base32"
45 "encoding/base64"
56 "encoding/json"
7+ "firetail-lambda-extension/logsapi"
68 "testing"
9+ "time"
710
811 "github.com/stretchr/testify/assert"
912 "github.com/stretchr/testify/require"
@@ -26,3 +29,170 @@ func TestDecodeFiretailRecordResponse(t *testing.T) {
2629
2730 assert .Equal (t , testRecord .Response , * & decodedRecord .Response )
2831}
32+
33+ func TestDecodeFiretailRecordWithMissingPart (t * testing.T ) {
34+ encodedRecord := "firetail:log-ext"
35+
36+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
37+ assert .Nil (t , decodedRecord )
38+ require .NotNil (t , err )
39+
40+ assert .Equal (t , "record had 2 parts when split by ':'" , err .Error ())
41+ }
42+
43+ func TestDecodeFiretailRecordWithExtraPart (t * testing.T ) {
44+ testRecord := Record {
45+ Response : RecordResponse {
46+ StatusCode : 200 ,
47+ Body : "Test Body" ,
48+ },
49+ }
50+ testPayloadBytes , err := json .Marshal (testRecord )
51+ require .Nil (t , err )
52+
53+ encodedRecord := "firetail:log-ext:" + base64 .StdEncoding .EncodeToString (testPayloadBytes ) + ":extra"
54+
55+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
56+ assert .Nil (t , decodedRecord )
57+ require .NotNil (t , err )
58+
59+ assert .Equal (t , "record had 4 parts when split by ':'" , err .Error ())
60+ }
61+
62+ func TestDecodeFiretailRecordWithInvalidPrefix (t * testing.T ) {
63+ testRecord := Record {
64+ Response : RecordResponse {
65+ StatusCode : 200 ,
66+ Body : "Test Body" ,
67+ },
68+ }
69+ testPayloadBytes , err := json .Marshal (testRecord )
70+ require .Nil (t , err )
71+
72+ encodedRecord := "tailfire:log-ext:" + base64 .StdEncoding .EncodeToString (testPayloadBytes )
73+
74+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
75+ assert .Nil (t , decodedRecord )
76+ require .NotNil (t , err )
77+
78+ assert .Equal (t , "record did not have firetail prefix" , err .Error ())
79+ }
80+
81+ func TestDecodeFiretailRecordWithInvalidToken (t * testing.T ) {
82+ testRecord := Record {
83+ Response : RecordResponse {
84+ StatusCode : 200 ,
85+ Body : "Test Body" ,
86+ },
87+ }
88+ testPayloadBytes , err := json .Marshal (testRecord )
89+ require .Nil (t , err )
90+
91+ encodedRecord := "firetail:ext-log:" + base64 .StdEncoding .EncodeToString (testPayloadBytes )
92+
93+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
94+ assert .Nil (t , decodedRecord )
95+ require .NotNil (t , err )
96+
97+ assert .Equal (t , "firetail prefixed record did not have valid token" , err .Error ())
98+ }
99+
100+ func TestDecodeFiretailRecordWithInvalidPayloadEncoding (t * testing.T ) {
101+ testRecord := Record {
102+ Response : RecordResponse {
103+ StatusCode : 200 ,
104+ Body : "Test Body" ,
105+ },
106+ }
107+ testPayloadBytes , err := json .Marshal (testRecord )
108+ require .Nil (t , err )
109+
110+ encodedRecord := "firetail:log-ext:" + base32 .StdEncoding .EncodeToString (testPayloadBytes )
111+
112+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
113+ assert .Nil (t , decodedRecord )
114+ require .NotNil (t , err )
115+
116+ assert .Contains (t , err .Error (), "failed to b64 decode firetail record" )
117+ }
118+
119+ func TestDecodeFiretailRecordWithInvalidPayloadTypes (t * testing.T ) {
120+ type Event struct {
121+ Event string `json:"event"`
122+ Response int `json:"response"`
123+ ExecutionTime string `json:"execution_time"`
124+ }
125+ testRecord := Event {
126+ Event : "my birthday party" ,
127+ Response : 0 ,
128+ ExecutionTime : "tomorrow" ,
129+ }
130+ testPayloadBytes , err := json .Marshal (testRecord )
131+ require .Nil (t , err )
132+
133+ encodedRecord := "firetail:log-ext:" + base64 .StdEncoding .EncodeToString (testPayloadBytes )
134+
135+ decodedRecord , err := decodeFiretailRecord (encodedRecord )
136+ assert .Nil (t , decodedRecord )
137+ require .NotNil (t , err )
138+
139+ assert .Contains (t , err .Error (), "failed to unmarshal firetail event" )
140+ }
141+
142+ func TestExtractSingleRecord (t * testing.T ) {
143+ testRecord := Record {
144+ Response : RecordResponse {
145+ StatusCode : 200 ,
146+ Body : "Test Body" ,
147+ },
148+ }
149+ testPayloadBytes , err := json .Marshal (testRecord )
150+ require .Nil (t , err )
151+ encodedRecord := "firetail:log-ext:" + base64 .StdEncoding .EncodeToString (testPayloadBytes )
152+
153+ testMessage := logsapi.LogMessage {
154+ Time : time .Now ().Format ("2006-01-02T15:04:05.000Z" ),
155+ Type : "function" ,
156+ Record : json.RawMessage {},
157+ }
158+ testRecordBytes , err := json .Marshal (encodedRecord )
159+ require .Nil (t , err )
160+ testMessage .Record = testRecordBytes
161+
162+ decodedRecords , err := ExtractFiretailRecords ([]logsapi.LogMessage {testMessage })
163+ require .Nil (t , err )
164+
165+ require .Len (t , decodedRecords , 1 )
166+ assert .Equal (t , testRecord .Response , decodedRecords [0 ].Response )
167+ }
168+
169+ func TestExtractRecordOfInvalidType (t * testing.T ) {
170+ testMessage := logsapi.LogMessage {
171+ Type : "platform.start" ,
172+ }
173+
174+ decodedRecords , err := ExtractFiretailRecords ([]logsapi.LogMessage {testMessage })
175+ require .NotNil (t , err )
176+
177+ assert .Len (t , decodedRecords , 0 )
178+ assert .Contains (t , err .Error (), "logMessage type is 'platform.start', not 'function'" )
179+ }
180+
181+ func TestExtractRecordWithInvalidType (t * testing.T ) {
182+ invalidRecord := 3.14159
183+ testMessage := logsapi.LogMessage {
184+ Time : time .Now ().Format ("2006-01-02T15:04:05.000Z" ),
185+ Type : "function" ,
186+ Record : json.RawMessage {},
187+ }
188+ testRecordBytes , err := json .Marshal (invalidRecord )
189+ require .Nil (t , err )
190+ testMessage .Record = testRecordBytes
191+
192+ decodedRecords , err := ExtractFiretailRecords ([]logsapi.LogMessage {testMessage })
193+ require .NotNil (t , err )
194+
195+ assert .Len (t , decodedRecords , 0 )
196+ assert .Contains (t , err .Error (), "Err unmarshalling event record as string, err: json: cannot unmarshal number into Go value of type string" )
197+ assert .Contains (t , err .Error (), "Err decoding event record as firetail event, err: record had 1 parts when split by ':'" )
198+ }
0 commit comments