github.com/honeycombio/honeytail@v1.9.0/parsers/arangodb/arangodb_test.go (about)

     1  package arangodb
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/honeycombio/honeytail/event"
     9  )
    10  
    11  const (
    12  	DEBUGLINE = `2016-10-31T16:03:02Z [6402] DEBUG {requests} "http-request-begin","0x7f87ba86b290","127.0.0.1","GET","HTTP/1.1",/_api/version`
    13  
    14  	INFOLINE = `2016-10-31T16:03:02Z [6402] INFO {requests} "http-request-end","0x7f87ba86b290","127.0.0.1","GET","HTTP/1.1",200,0,64,"/_api/version",0.000139`
    15  
    16  	TIMESTRING = `2016-10-31T16:03:02Z`
    17  )
    18  
    19  var (
    20  	T1, _ = time.Parse(iso8601UTCTimeFormat, TIMESTRING)
    21  )
    22  
    23  type processed struct {
    24  	time        time.Time
    25  	includeData map[string]interface{}
    26  	excludeKeys []string
    27  }
    28  
    29  func TestProcessLines(t *testing.T) {
    30  	tlm := []struct {
    31  		line     string
    32  		expected processed
    33  	}{
    34  		{
    35  			line: DEBUGLINE,
    36  			expected: processed{
    37  				time: T1,
    38  				includeData: map[string]interface{}{
    39  					"pid":      "6402",
    40  					"logLevel": "DEBUG",
    41  					"id":       "0x7f87ba86b290",
    42  					"sourceIP": "127.0.0.1",
    43  					"method":   "GET",
    44  					"protocol": "HTTP/1.1",
    45  					"fullURL":  "/_api/version",
    46  				},
    47  			},
    48  		},
    49  
    50  		{
    51  			line: INFOLINE,
    52  			expected: processed{
    53  				time: T1,
    54  				includeData: map[string]interface{}{
    55  					"pid":          "6402",
    56  					"logLevel":     "INFO",
    57  					"id":           "0x7f87ba86b290",
    58  					"sourceIP":     "127.0.0.1",
    59  					"method":       "GET",
    60  					"protocol":     "HTTP/1.1",
    61  					"responseCode": int64(200),
    62  					"reqBodyLen":   int64(0),
    63  					"resBodyLen":   int64(64),
    64  					"fullURL":      "/_api/version",
    65  					"totalTime":    0.000139,
    66  				},
    67  			},
    68  		},
    69  	}
    70  	m := &Parser{
    71  		conf:       Options{numParsers: 1},
    72  		lineParser: &ArangoLineParser{},
    73  	}
    74  	lines := make(chan string)
    75  	send := make(chan event.Event)
    76  	// prep the incoming channel with test lines for the processor
    77  	go func() {
    78  		for _, pair := range tlm {
    79  			lines <- pair.line
    80  		}
    81  		close(lines)
    82  	}()
    83  	// spin up the processor to process our test lines
    84  	go m.ProcessLines(lines, send, nil)
    85  	for _, pair := range tlm {
    86  		ev := <-send
    87  
    88  		if ev.Timestamp.UnixNano() != pair.expected.time.UnixNano() {
    89  			t.Errorf("Parsed timestamp didn't match up for %s.\n  Expected: %+v\n  Actual: %+v",
    90  				pair.line, pair.expected.time, ev.Timestamp)
    91  		}
    92  
    93  		var missing []string
    94  		for k := range pair.expected.includeData {
    95  			if _, ok := ev.Data[k]; !ok {
    96  				missing = append(missing, k)
    97  			} else if !reflect.DeepEqual(ev.Data[k], pair.expected.includeData[k]) {
    98  				t.Errorf("  Parsed data value %s didn't match up for %s.\n  Expected: %+v\n  Actual: %+v",
    99  					k, pair.line, pair.expected.includeData[k], ev.Data[k])
   100  			}
   101  		}
   102  		if missing != nil {
   103  			t.Errorf("  Parsed data was missing keys for line: %s\n  Missing: %+v\n  Parsed data: %+v",
   104  				pair.line, missing, ev.Data)
   105  		}
   106  		for _, k := range pair.expected.excludeKeys {
   107  			if _, ok := ev.Data[k]; ok {
   108  				t.Errorf("  Parsed data included unexpected key %s for line: %s", k, pair.line)
   109  			}
   110  		}
   111  	}
   112  }