Skip to content

Commit

Permalink
Add basic CI for unit testing (#7)
Browse files Browse the repository at this point in the history
* Add basic CI for unit testing
  • Loading branch information
djaglowski authored Feb 1, 2021
1 parent 7b5498a commit e7099ee
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 101 deletions.
94 changes: 94 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Go

on:
push:
branches: [ main ]
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
pull_request:

jobs:

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Cache Go Modules
uses: actions/cache@v2
env:
cache-name: cache-go-modules
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
- name: Install tools
if: steps.tool-cache.outputs.cache-hit != 'true'
run: make install-tools
- name: Add Permissions to Tool Binaries
run: chmod -R +x ~/go/bin
- name: Vet
run: make vet
- name: Lint
run: make lint

test-linux:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Cache Go Modules
uses: actions/cache@v2
env:
cache-name: cache-go-modules
with:
path: ~/go/bin
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
- name: Run Unit Tests
run: make test

test-macos:
runs-on: macos-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Cache Go Modules
uses: actions/cache@v2
env:
cache-name: cache-go-modules
with:
path: ~/go/bin
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
- name: Run Unit Tests
run: make test

test-windows:
runs-on: windows-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Set up Go
uses: actions/[email protected]
with:
go-version: 1.15
- name: Cache Go Modules
uses: actions/cache@v2
env:
cache-name: cache-go-modules
with:
path: /Users/runneradmin/go/pkg/mod
key: ${{ runner.os }}-${{ hashFiles('**/go.mod') }}
- name: Run Unit Tests
run: make test

3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ linters:
- unparam
- whitespace
- ineffassign
linters-settings:
goconst:
min-occurrences: 5
6 changes: 4 additions & 2 deletions agent/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ func (b *LogAgentBuilder) Build() (*LogAgent, error) {

if b.config != nil && len(b.configFiles) > 0 {
return nil, errors.NewError("agent can be built WithConfig or WithConfigFiles, but not both", "")
} else if b.config == nil && len(b.configFiles) == 0 {
}
if b.config == nil && len(b.configFiles) == 0 {
return nil, errors.NewError("agent cannot be built without WithConfig or WithConfigFiles", "")
} else if len(b.configFiles) > 0 {
}
if len(b.configFiles) > 0 {
b.config, err = NewConfigFromGlobs(b.configFiles)
if err != nil {
return nil, errors.Wrap(err, "read configs from globs")
Expand Down
1 change: 0 additions & 1 deletion database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func TestOpenDatabase(t *testing.T) {
require.Error(t, err)
require.Nil(t, db)
})

}

func TestStubDatabase(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions operator/buffer/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ type DiskBuffer struct {
// NewDiskBuffer creates a new DiskBuffer
func NewDiskBuffer(maxDiskSize int64) *DiskBuffer {
return &DiskBuffer{
maxBytes: int64(maxDiskSize),
maxBytes: maxDiskSize,
entryAdded: make(chan int64, 1),
copyBuffer: make([]byte, 1<<16),
diskSizeSemaphore: semaphore.NewWeighted(int64(maxDiskSize)),
diskSizeSemaphore: semaphore.NewWeighted(maxDiskSize),
}
}

Expand Down
12 changes: 6 additions & 6 deletions operator/buffer/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDiskBuffer(t *testing.T) {
readyDone := make(chan struct{})
go func() {
readyDone <- struct{}{}
readWaitN(t, b, 20, 0)
readWaitN(t, b, 20)
readyDone <- struct{}{}
}()
<-readyDone
Expand All @@ -90,7 +90,7 @@ func TestDiskBuffer(t *testing.T) {
readyDone := make(chan struct{})
go func() {
readyDone <- struct{}{}
readWaitN(t, b, 15, 0)
readWaitN(t, b, 15)
readyDone <- struct{}{}
}()
<-readyDone
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestDiskBuffer(t *testing.T) {
c, n, err := b.Read(dst)
require.NoError(t, err)
require.Equal(t, 1, n)
c.MarkAllAsFlushed()
require.NoError(t, c.MarkAllAsFlushed())
require.NoError(t, b.Compact())

// Now there should be space for another entry
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestDiskBuffer(t *testing.T) {
readCount := (writes - reads) / 2
c := readN(t, b, readCount, reads)
if j%2 == 0 {
c.MarkAllAsFlushed()
require.NoError(t, c.MarkAllAsFlushed())
}
reads += readCount
default:
Expand Down Expand Up @@ -298,7 +298,7 @@ func BenchmarkDiskBuffer(b *testing.B) {
cancel()
panicOnErr(err)
i += n
c.MarkAllAsFlushed()
require.NoError(b, c.MarkAllAsFlushed())
}
}()

Expand Down Expand Up @@ -336,7 +336,7 @@ func BenchmarkDiskBuffer(b *testing.B) {
cancel()
panicOnErr(err)
i += n
c.MarkAllAsFlushed()
require.NoError(b, c.MarkAllAsFlushed())
}
}()

Expand Down
10 changes: 5 additions & 5 deletions operator/buffer/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestMemoryBuffer(t *testing.T) {
readyDone := make(chan struct{})
go func() {
readyDone <- struct{}{}
readWaitN(t, b, 20, 0)
readWaitN(t, b, 20)
readyDone <- struct{}{}
}()
<-readyDone
Expand All @@ -103,7 +103,7 @@ func TestMemoryBuffer(t *testing.T) {
readyDone := make(chan struct{})
go func() {
readyDone <- struct{}{}
readWaitN(t, b, 15, 0)
readWaitN(t, b, 15)
readyDone <- struct{}{}
}()
<-readyDone
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestMemoryBuffer(t *testing.T) {
c, n, err := b.Read(dst)
require.NoError(t, err)
require.Equal(t, 1, n)
c.MarkAllAsFlushed()
require.NoError(t, c.MarkAllAsFlushed())

// Now there should be space for another entry
err = b.Add(context.Background(), entry.New())
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestMemoryBuffer(t *testing.T) {
readCount := (writes - reads) / 2
c := readN(t, b, readCount, reads)
if j%2 == 0 {
c.MarkAllAsFlushed()
require.NoError(t, c.MarkAllAsFlushed())
}
reads += readCount
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func BenchmarkMemoryBuffer(b *testing.B) {
i += n
go func() {
time.Sleep(50 * time.Millisecond)
c.MarkAllAsFlushed()
require.NoError(b, c.MarkAllAsFlushed())
}()
}
}()
Expand Down
9 changes: 4 additions & 5 deletions operator/buffer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,21 @@ func readN(t testing.TB, buffer Buffer, n, start int) Clearer {
return f
}

func readWaitN(t testing.TB, buffer Buffer, n, start int) Clearer {
func readWaitN(t testing.TB, buffer Buffer, n int) {
entries := make([]*entry.Entry, n)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
f, readCount, err := buffer.ReadWait(ctx, entries)
_, readCount, err := buffer.ReadWait(ctx, entries)
require.NoError(t, err)
require.Equal(t, n, readCount)
for i := 0; i < n; i++ {
require.Equal(t, intEntry(start+i), entries[i])
require.Equal(t, intEntry(i), entries[i])
}
return f
}

func flushN(t testing.TB, buffer Buffer, n, start int) {
f := readN(t, buffer, n, start)
f.MarkAllAsFlushed()
require.NoError(t, f.MarkAllAsFlushed())
}

func panicOnErr(err error) {
Expand Down
1 change: 0 additions & 1 deletion operator/build_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ func TestBuildContext(t *testing.T) {
bc2 := bc.WithDefaultOutputIDs([]string{"id1", "id2"})
require.Equal(t, []string{"id1", "id2"}, bc2.DefaultOutputIDs)
require.Equal(t, []string{"orig"}, bc.DefaultOutputIDs)

})
}
50 changes: 9 additions & 41 deletions operator/builtin/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func (rt rotationTest) run(tc rotationTest, copyTruncate, sequential bool) func(
select {
case e := <-logReceived:
received = append(received, e.Record.(string))
case <-time.After(100 * time.Millisecond):
case <-time.After(200 * time.Millisecond):
break LOOP
}
}
Expand All @@ -805,71 +805,39 @@ func TestRotation(t *testing.T) {

cases := []rotationTest{
{
name: "Fast/NoRotation",
name: "NoRotation",
totalLines: 10,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: time.Millisecond,
pollInterval: 20 * time.Millisecond,
pollInterval: 10 * time.Millisecond,
},
{
name: "Fast/NoDeletion",
name: "NoDeletion",
totalLines: 20,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: time.Millisecond,
pollInterval: 20 * time.Millisecond,
pollInterval: 10 * time.Millisecond,
},
{
name: "Fast/Deletion",
name: "Deletion",
totalLines: 30,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: time.Millisecond,
pollInterval: 20 * time.Millisecond,
pollInterval: 10 * time.Millisecond,
ephemeralLines: true,
},
{
name: "Fast/Deletion/ExceedFingerprint",
name: "Deletion/ExceedFingerprint",
totalLines: 300,
maxLinesPerFile: 100,
maxBackupFiles: 1,
writeInterval: time.Millisecond,
pollInterval: 20 * time.Millisecond,
pollInterval: 10 * time.Millisecond,
ephemeralLines: true,
},
{
name: "Slow/NoRotation",
totalLines: 10,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: 3 * time.Millisecond,
pollInterval: 20 * time.Millisecond,
},
{
name: "Slow/NoDeletion",
totalLines: 20,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: 3 * time.Millisecond,
pollInterval: 20 * time.Millisecond,
},
{
name: "Slow/Deletion",
totalLines: 30,
maxLinesPerFile: 10,
maxBackupFiles: 1,
writeInterval: 3 * time.Millisecond,
pollInterval: 20 * time.Millisecond,
},
{
name: "Slow/Deletion/ExceedFingerprint",
totalLines: 100,
maxLinesPerFile: 25, // ~20 is just enough to exceed 1000 bytes fingerprint at 50 chars per line
maxBackupFiles: 2,
writeInterval: 3 * time.Millisecond,
pollInterval: 20 * time.Millisecond,
},
}

for _, tc := range cases {
Expand Down
2 changes: 0 additions & 2 deletions operator/flusher/flusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

func TestFlusher(t *testing.T) {

// Override setting for test
maxElapsedTime = 5 * time.Second

Expand Down Expand Up @@ -56,7 +55,6 @@ func TestFlusher(t *testing.T) {
}

func TestMaxElapsedTime(t *testing.T) {

// Override setting for test
maxElapsedTime = 100 * time.Millisecond

Expand Down
1 change: 0 additions & 1 deletion operator/helper/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ func TestParserInvalidTimeValidSeverityParse(t *testing.T) {
}

func TestParserValidTimeInvalidSeverityParse(t *testing.T) {

// Hawaiian Standard Time
hst, err := time.LoadLocation("HST")
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions operator/helper/persister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestPersisterCache(t *testing.T) {
func TestPersisterLoad(t *testing.T) {
tempDir := testutil.NewTempDir(t)
db, err := database.OpenDatabase(filepath.Join(tempDir, "test.db"))
require.NoError(t, err)
persister := NewScopedDBPersister(db, "test")
persister.Set("key", []byte("value"))

Expand Down
Loading

0 comments on commit e7099ee

Please sign in to comment.