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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// Copyright 2015 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package runtime
import (
"path/filepath"
"strings"
"sync"
"testing"
"github.com/golang/glog"
"github.com/jaqx0r/mtail/internal/logline"
"github.com/jaqx0r/mtail/internal/metrics"
"github.com/jaqx0r/mtail/internal/testutil"
)
func TestNewRuntime(t *testing.T) {
store := metrics.NewStore()
lines := make(chan *logline.LogLine)
var wg sync.WaitGroup
_, err := New(lines, &wg, "", store)
testutil.FatalIfErr(t, err)
close(lines)
wg.Wait()
}
func TestNewRuntimeErrors(t *testing.T) {
store := metrics.NewStore()
lines := make(chan *logline.LogLine)
var wg sync.WaitGroup
_, err := New(lines, nil, "", store)
if err == nil {
t.Error("New(..., nil) expecting error, got nil")
}
_, err = New(lines, &wg, "", nil)
if err == nil {
t.Error("New(..., nil) expecting error, got nil")
}
}
func TestCompileAndRun(t *testing.T) {
testProgram := "/$/ {}\n"
store := metrics.NewStore()
lines := make(chan *logline.LogLine)
var wg sync.WaitGroup
l, err := New(lines, &wg, "", store)
testutil.FatalIfErr(t, err)
if err := l.CompileAndRun("Test", strings.NewReader(testProgram)); err != nil {
t.Errorf("CompileAndRun returned error: %s", err)
}
l.handleMu.Lock()
if len(l.handles) < 1 {
t.Errorf("no vm handles: %v", l.handles)
}
l.handleMu.Unlock()
l.handleMu.Lock()
h := l.handles["Test"]
if h == nil {
t.Errorf("No handle for Test: %v", l.handles)
}
l.handleMu.Unlock()
close(lines)
wg.Wait()
}
var testProgram = "/$/ {}\n"
var testProgFiles = []string{
"test.wrongext",
"test.mtail",
".test",
}
func TestLoadProg(t *testing.T) {
store := metrics.NewStore()
tmpDir := testutil.TestTempDir(t)
lines := make(chan *logline.LogLine)
var wg sync.WaitGroup
l, err := New(lines, &wg, tmpDir, store)
testutil.FatalIfErr(t, err)
for _, name := range testProgFiles {
f := testutil.TestOpenFile(t, filepath.Join(tmpDir, name))
n, err := f.WriteString(testProgram)
testutil.FatalIfErr(t, err)
glog.Infof("Wrote %d bytes", n)
err = l.LoadProgram(filepath.Join(tmpDir, name))
testutil.FatalIfErr(t, err)
f.Close()
}
close(lines)
wg.Wait()
}
func TestLogLineFilter(t *testing.T) {
tests := []struct {
lines []*logline.LogLine
logmappings map[uint32]struct{}
expected []*logline.LogLine
}{
{
// Test case where one file is processed and one not.
lines: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
{
Filenamehash: 67890,
Line: "This log line should be ignored",
},
},
expected: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
},
logmappings: map[uint32]struct{}{
12345: {}, // This maps to the first line.
},
},
{
// Test case where both file are processed.
lines: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
{
Filenamehash: 67890,
Line: "This is a valid log line",
},
},
expected: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
{
Filenamehash: 67890,
Line: "This is a valid log line",
},
},
logmappings: map[uint32]struct{}{
12345: {}, // This maps to the first line.
67890: {}, // This maps to the first line.
},
},
{
// Test case where file process because no mapping.
lines: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
},
expected: []*logline.LogLine{
{
Filenamehash: 12345,
Line: "This is a valid log line",
},
},
logmappings: map[uint32]struct{}{}, // empty to all logs match
},
{
// empty test case
lines: []*logline.LogLine{},
expected: []*logline.LogLine{},
logmappings: map[uint32]struct{}{}, // empty to all logs match
},
}
for _, tc := range tests {
// Create a channel for log lines.
lines := make(chan *logline.LogLine, len(tc.lines))
for _, line := range tc.lines {
lines <- line
}
close(lines)
var wg sync.WaitGroup
store := metrics.NewStore()
// Create a Runtime instance with a logmapping for the first line.
r, err := New(lines, &wg, "", store)
testutil.FatalIfErr(t, err)
// Add a logmapping for the first line.
r.logmappings["test_program"] = tc.logmappings
// Add a mock program handle for "test_program".
linesReceived := make(chan *logline.LogLine, len(tc.expected))
// Start processing lines.
wg.Add(1)
go func() {
defer wg.Done()
for line := range lines {
if _, ok := r.logmappings["test_program"][line.Filenamehash]; ok {
linesReceived <- line
}
}
}()
// Wait for the Runtime to finish processing.
wg.Wait()
// Validate the lines received by the "test_program" handle.
testutil.ExpectLinesReceivedNoDiff(t, tc.expected, linesReceived)
}
}
|