Skip to content

Commit e7099ee

Browse files
authored
Add basic CI for unit testing (#7)
* Add basic CI for unit testing
1 parent 7b5498a commit e7099ee

20 files changed

+150
-101
lines changed

.github/workflows/build-and-test.yml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
tags:
7+
- 'v[0-9]+.[0-9]+.[0-9]+*'
8+
pull_request:
9+
10+
jobs:
11+
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Repo
16+
uses: actions/checkout@v2
17+
- name: Setup Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: 1.15
21+
- name: Cache Go Modules
22+
uses: actions/cache@v2
23+
env:
24+
cache-name: cache-go-modules
25+
with:
26+
path: ~/go/pkg/mod
27+
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
28+
- name: Install tools
29+
if: steps.tool-cache.outputs.cache-hit != 'true'
30+
run: make install-tools
31+
- name: Add Permissions to Tool Binaries
32+
run: chmod -R +x ~/go/bin
33+
- name: Vet
34+
run: make vet
35+
- name: Lint
36+
run: make lint
37+
38+
test-linux:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Checkout Repo
42+
uses: actions/checkout@v2
43+
- name: Setup Go
44+
uses: actions/setup-go@v2
45+
with:
46+
go-version: 1.15
47+
- name: Cache Go Modules
48+
uses: actions/cache@v2
49+
env:
50+
cache-name: cache-go-modules
51+
with:
52+
path: ~/go/bin
53+
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
54+
- name: Run Unit Tests
55+
run: make test
56+
57+
test-macos:
58+
runs-on: macos-latest
59+
steps:
60+
- name: Checkout Repo
61+
uses: actions/checkout@v2
62+
- name: Setup Go
63+
uses: actions/setup-go@v2
64+
with:
65+
go-version: 1.15
66+
- name: Cache Go Modules
67+
uses: actions/cache@v2
68+
env:
69+
cache-name: cache-go-modules
70+
with:
71+
path: ~/go/bin
72+
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
73+
- name: Run Unit Tests
74+
run: make test
75+
76+
test-windows:
77+
runs-on: windows-latest
78+
steps:
79+
- name: Checkout Repo
80+
uses: actions/checkout@v2
81+
- name: Set up Go
82+
uses: actions/[email protected]
83+
with:
84+
go-version: 1.15
85+
- name: Cache Go Modules
86+
uses: actions/cache@v2
87+
env:
88+
cache-name: cache-go-modules
89+
with:
90+
path: /Users/runneradmin/go/pkg/mod
91+
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
92+
- name: Run Unit Tests
93+
run: make test
94+

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ linters:
1313
- unparam
1414
- whitespace
1515
- ineffassign
16+
linters-settings:
17+
goconst:
18+
min-occurrences: 5

agent/builder.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ func (b *LogAgentBuilder) Build() (*LogAgent, error) {
8787

8888
if b.config != nil && len(b.configFiles) > 0 {
8989
return nil, errors.NewError("agent can be built WithConfig or WithConfigFiles, but not both", "")
90-
} else if b.config == nil && len(b.configFiles) == 0 {
90+
}
91+
if b.config == nil && len(b.configFiles) == 0 {
9192
return nil, errors.NewError("agent cannot be built without WithConfig or WithConfigFiles", "")
92-
} else if len(b.configFiles) > 0 {
93+
}
94+
if len(b.configFiles) > 0 {
9395
b.config, err = NewConfigFromGlobs(b.configFiles)
9496
if err != nil {
9597
return nil, errors.Wrap(err, "read configs from globs")

database/database_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ func TestOpenDatabase(t *testing.T) {
8484
require.Error(t, err)
8585
require.Nil(t, db)
8686
})
87-
8887
}
8988

9089
func TestStubDatabase(t *testing.T) {

operator/buffer/disk.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ type DiskBuffer struct {
122122
// NewDiskBuffer creates a new DiskBuffer
123123
func NewDiskBuffer(maxDiskSize int64) *DiskBuffer {
124124
return &DiskBuffer{
125-
maxBytes: int64(maxDiskSize),
125+
maxBytes: maxDiskSize,
126126
entryAdded: make(chan int64, 1),
127127
copyBuffer: make([]byte, 1<<16),
128-
diskSizeSemaphore: semaphore.NewWeighted(int64(maxDiskSize)),
128+
diskSizeSemaphore: semaphore.NewWeighted(maxDiskSize),
129129
}
130130
}
131131

operator/buffer/disk_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestDiskBuffer(t *testing.T) {
7474
readyDone := make(chan struct{})
7575
go func() {
7676
readyDone <- struct{}{}
77-
readWaitN(t, b, 20, 0)
77+
readWaitN(t, b, 20)
7878
readyDone <- struct{}{}
7979
}()
8080
<-readyDone
@@ -90,7 +90,7 @@ func TestDiskBuffer(t *testing.T) {
9090
readyDone := make(chan struct{})
9191
go func() {
9292
readyDone <- struct{}{}
93-
readWaitN(t, b, 15, 0)
93+
readWaitN(t, b, 15)
9494
readyDone <- struct{}{}
9595
}()
9696
<-readyDone
@@ -209,7 +209,7 @@ func TestDiskBuffer(t *testing.T) {
209209
c, n, err := b.Read(dst)
210210
require.NoError(t, err)
211211
require.Equal(t, 1, n)
212-
c.MarkAllAsFlushed()
212+
require.NoError(t, c.MarkAllAsFlushed())
213213
require.NoError(t, b.Compact())
214214

215215
// Now there should be space for another entry
@@ -243,7 +243,7 @@ func TestDiskBuffer(t *testing.T) {
243243
readCount := (writes - reads) / 2
244244
c := readN(t, b, readCount, reads)
245245
if j%2 == 0 {
246-
c.MarkAllAsFlushed()
246+
require.NoError(t, c.MarkAllAsFlushed())
247247
}
248248
reads += readCount
249249
default:
@@ -298,7 +298,7 @@ func BenchmarkDiskBuffer(b *testing.B) {
298298
cancel()
299299
panicOnErr(err)
300300
i += n
301-
c.MarkAllAsFlushed()
301+
require.NoError(b, c.MarkAllAsFlushed())
302302
}
303303
}()
304304

@@ -336,7 +336,7 @@ func BenchmarkDiskBuffer(b *testing.B) {
336336
cancel()
337337
panicOnErr(err)
338338
i += n
339-
c.MarkAllAsFlushed()
339+
require.NoError(b, c.MarkAllAsFlushed())
340340
}
341341
}()
342342

operator/buffer/memory_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestMemoryBuffer(t *testing.T) {
8787
readyDone := make(chan struct{})
8888
go func() {
8989
readyDone <- struct{}{}
90-
readWaitN(t, b, 20, 0)
90+
readWaitN(t, b, 20)
9191
readyDone <- struct{}{}
9292
}()
9393
<-readyDone
@@ -103,7 +103,7 @@ func TestMemoryBuffer(t *testing.T) {
103103
readyDone := make(chan struct{})
104104
go func() {
105105
readyDone <- struct{}{}
106-
readWaitN(t, b, 15, 0)
106+
readWaitN(t, b, 15)
107107
readyDone <- struct{}{}
108108
}()
109109
<-readyDone
@@ -190,7 +190,7 @@ func TestMemoryBuffer(t *testing.T) {
190190
c, n, err := b.Read(dst)
191191
require.NoError(t, err)
192192
require.Equal(t, 1, n)
193-
c.MarkAllAsFlushed()
193+
require.NoError(t, c.MarkAllAsFlushed())
194194

195195
// Now there should be space for another entry
196196
err = b.Add(context.Background(), entry.New())
@@ -220,7 +220,7 @@ func TestMemoryBuffer(t *testing.T) {
220220
readCount := (writes - reads) / 2
221221
c := readN(t, b, readCount, reads)
222222
if j%2 == 0 {
223-
c.MarkAllAsFlushed()
223+
require.NoError(t, c.MarkAllAsFlushed())
224224
}
225225
reads += readCount
226226
}
@@ -278,7 +278,7 @@ func BenchmarkMemoryBuffer(b *testing.B) {
278278
i += n
279279
go func() {
280280
time.Sleep(50 * time.Millisecond)
281-
c.MarkAllAsFlushed()
281+
require.NoError(b, c.MarkAllAsFlushed())
282282
}()
283283
}
284284
}()

operator/buffer/util_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,21 @@ func readN(t testing.TB, buffer Buffer, n, start int) Clearer {
4949
return f
5050
}
5151

52-
func readWaitN(t testing.TB, buffer Buffer, n, start int) Clearer {
52+
func readWaitN(t testing.TB, buffer Buffer, n int) {
5353
entries := make([]*entry.Entry, n)
5454
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
5555
defer cancel()
56-
f, readCount, err := buffer.ReadWait(ctx, entries)
56+
_, readCount, err := buffer.ReadWait(ctx, entries)
5757
require.NoError(t, err)
5858
require.Equal(t, n, readCount)
5959
for i := 0; i < n; i++ {
60-
require.Equal(t, intEntry(start+i), entries[i])
60+
require.Equal(t, intEntry(i), entries[i])
6161
}
62-
return f
6362
}
6463

6564
func flushN(t testing.TB, buffer Buffer, n, start int) {
6665
f := readN(t, buffer, n, start)
67-
f.MarkAllAsFlushed()
66+
require.NoError(t, f.MarkAllAsFlushed())
6867
}
6968

7069
func panicOnErr(err error) {

operator/build_context_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,5 @@ func TestBuildContext(t *testing.T) {
5353
bc2 := bc.WithDefaultOutputIDs([]string{"id1", "id2"})
5454
require.Equal(t, []string{"id1", "id2"}, bc2.DefaultOutputIDs)
5555
require.Equal(t, []string{"orig"}, bc.DefaultOutputIDs)
56-
5756
})
5857
}

operator/builtin/input/file/file_test.go

+9-41
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ func (rt rotationTest) run(tc rotationTest, copyTruncate, sequential bool) func(
784784
select {
785785
case e := <-logReceived:
786786
received = append(received, e.Record.(string))
787-
case <-time.After(100 * time.Millisecond):
787+
case <-time.After(200 * time.Millisecond):
788788
break LOOP
789789
}
790790
}
@@ -805,71 +805,39 @@ func TestRotation(t *testing.T) {
805805

806806
cases := []rotationTest{
807807
{
808-
name: "Fast/NoRotation",
808+
name: "NoRotation",
809809
totalLines: 10,
810810
maxLinesPerFile: 10,
811811
maxBackupFiles: 1,
812812
writeInterval: time.Millisecond,
813-
pollInterval: 20 * time.Millisecond,
813+
pollInterval: 10 * time.Millisecond,
814814
},
815815
{
816-
name: "Fast/NoDeletion",
816+
name: "NoDeletion",
817817
totalLines: 20,
818818
maxLinesPerFile: 10,
819819
maxBackupFiles: 1,
820820
writeInterval: time.Millisecond,
821-
pollInterval: 20 * time.Millisecond,
821+
pollInterval: 10 * time.Millisecond,
822822
},
823823
{
824-
name: "Fast/Deletion",
824+
name: "Deletion",
825825
totalLines: 30,
826826
maxLinesPerFile: 10,
827827
maxBackupFiles: 1,
828828
writeInterval: time.Millisecond,
829-
pollInterval: 20 * time.Millisecond,
829+
pollInterval: 10 * time.Millisecond,
830830
ephemeralLines: true,
831831
},
832832
{
833-
name: "Fast/Deletion/ExceedFingerprint",
833+
name: "Deletion/ExceedFingerprint",
834834
totalLines: 300,
835835
maxLinesPerFile: 100,
836836
maxBackupFiles: 1,
837837
writeInterval: time.Millisecond,
838-
pollInterval: 20 * time.Millisecond,
838+
pollInterval: 10 * time.Millisecond,
839839
ephemeralLines: true,
840840
},
841-
{
842-
name: "Slow/NoRotation",
843-
totalLines: 10,
844-
maxLinesPerFile: 10,
845-
maxBackupFiles: 1,
846-
writeInterval: 3 * time.Millisecond,
847-
pollInterval: 20 * time.Millisecond,
848-
},
849-
{
850-
name: "Slow/NoDeletion",
851-
totalLines: 20,
852-
maxLinesPerFile: 10,
853-
maxBackupFiles: 1,
854-
writeInterval: 3 * time.Millisecond,
855-
pollInterval: 20 * time.Millisecond,
856-
},
857-
{
858-
name: "Slow/Deletion",
859-
totalLines: 30,
860-
maxLinesPerFile: 10,
861-
maxBackupFiles: 1,
862-
writeInterval: 3 * time.Millisecond,
863-
pollInterval: 20 * time.Millisecond,
864-
},
865-
{
866-
name: "Slow/Deletion/ExceedFingerprint",
867-
totalLines: 100,
868-
maxLinesPerFile: 25, // ~20 is just enough to exceed 1000 bytes fingerprint at 50 chars per line
869-
maxBackupFiles: 2,
870-
writeInterval: 3 * time.Millisecond,
871-
pollInterval: 20 * time.Millisecond,
872-
},
873841
}
874842

875843
for _, tc := range cases {

operator/flusher/flusher_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
)
2727

2828
func TestFlusher(t *testing.T) {
29-
3029
// Override setting for test
3130
maxElapsedTime = 5 * time.Second
3231

@@ -56,7 +55,6 @@ func TestFlusher(t *testing.T) {
5655
}
5756

5857
func TestMaxElapsedTime(t *testing.T) {
59-
6058
// Override setting for test
6159
maxElapsedTime = 100 * time.Millisecond
6260

operator/helper/parser_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func TestParserInvalidTimeValidSeverityParse(t *testing.T) {
218218
}
219219

220220
func TestParserValidTimeInvalidSeverityParse(t *testing.T) {
221-
222221
// Hawaiian Standard Time
223222
hst, err := time.LoadLocation("HST")
224223
require.NoError(t, err)

operator/helper/persister_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestPersisterCache(t *testing.T) {
3434
func TestPersisterLoad(t *testing.T) {
3535
tempDir := testutil.NewTempDir(t)
3636
db, err := database.OpenDatabase(filepath.Join(tempDir, "test.db"))
37+
require.NoError(t, err)
3738
persister := NewScopedDBPersister(db, "test")
3839
persister.Set("key", []byte("value"))
3940

0 commit comments

Comments
 (0)