Skip to content

Commit

Permalink
Made a database package and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwilliams89 committed Sep 7, 2020
1 parent 16358e3 commit 2fb4b20
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 54 deletions.
4 changes: 2 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package agent
import (
"sync"

"github.com/observiq/stanza/operator"
"github.com/observiq/stanza/database"
"github.com/observiq/stanza/pipeline"
"go.uber.org/zap"
)

// LogAgent is an entity that handles log monitoring.
type LogAgent struct {
database operator.Database
database database.Database
pipeline pipeline.Pipeline

startOnce sync.Once
Expand Down
7 changes: 4 additions & 3 deletions agent/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"github.com/observiq/stanza/database"
"github.com/observiq/stanza/errors"
"github.com/observiq/stanza/operator"
"go.uber.org/zap"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (b *LogAgentBuilder) WithDefaultOutput(defaultOutput operator.Operator) *Lo

// Build will build a new log agent using the values defined on the builder
func (b *LogAgentBuilder) Build() (*LogAgent, error) {
database, err := operator.OpenDatabase(b.databaseFile)
db, err := database.OpenDatabase(b.databaseFile)
if err != nil {
return nil, errors.Wrap(err, "open database")
}
Expand All @@ -56,7 +57,7 @@ func (b *LogAgentBuilder) Build() (*LogAgent, error) {
buildContext := operator.BuildContext{
Logger: b.logger,
PluginRegistry: registry,
Database: database,
Database: db,
}

pipeline, err := b.cfg.Pipeline.BuildPipeline(buildContext, b.defaultOutput)
Expand All @@ -66,7 +67,7 @@ func (b *LogAgentBuilder) Build() (*LogAgent, error) {

return &LogAgent{
pipeline: pipeline,
database: database,
database: db,
SugaredLogger: b.logger,
}, nil
}
2 changes: 1 addition & 1 deletion operator/database.go → database/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:generate mockery -name=^(Database)$ -output=../testutil -outpkg=testutil -case=snake

package operator
package database

import (
"fmt"
Expand Down
89 changes: 89 additions & 0 deletions database/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package database

import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/require"
)

// NewTempDir will return a new temp directory for testing
func NewTempDir(t testing.TB) string {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf(err.Error())
t.FailNow()
}

t.Cleanup(func() {
os.RemoveAll(tempDir)
})

return tempDir
}

func TestOpenDatabase(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
tempDir := NewTempDir(t)
db, err := OpenDatabase(filepath.Join(tempDir, "test.db"))
require.NoError(t, err)
require.NotNil(t, db)
})

t.Run("NoFile", func(t *testing.T) {
db, err := OpenDatabase("")
require.NoError(t, err)
require.NotNil(t, db)
require.IsType(t, &StubDatabase{}, db)
})

t.Run("NonexistantPathIsCreated", func(t *testing.T) {
tempDir := NewTempDir(t)
db, err := OpenDatabase(filepath.Join(tempDir, "nonexistdir", "test.db"))
require.NoError(t, err)
require.NotNil(t, db)
})

t.Run("BadPermissions", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows does not have the same kind of file permissions")
}
tempDir := NewTempDir(t)
err := os.MkdirAll(filepath.Join(tempDir, "badperms"), 0666)
require.NoError(t, err)
db, err := OpenDatabase(filepath.Join(tempDir, "badperms", "nonexistdir", "test.db"))
require.Error(t, err)
require.Nil(t, db)
})

t.Run("ExecuteOnlyPermissions", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows does not have the same kind of file permissions")
}
tempDir := NewTempDir(t)
err := os.MkdirAll(filepath.Join(tempDir, "badperms"), 0111)
require.NoError(t, err)
db, err := OpenDatabase(filepath.Join(tempDir, "badperms", "nonexistdir", "test.db"))
require.Error(t, err)
require.Nil(t, db)
})

}

func TestStubDatabase(t *testing.T) {
stubDatabase := NewStubDatabase()
err := stubDatabase.Close()
require.NoError(t, err)

err = stubDatabase.Sync()
require.NoError(t, err)

err = stubDatabase.Update(nil)
require.NoError(t, err)

err = stubDatabase.View(nil)
require.NoError(t, err)
}
5 changes: 1 addition & 4 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ func (e AgentError) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
}

if len(e.Details) != 0 {
err := encoder.AddObject("details", e.Details)
if err != nil {
return err
}
_ = encoder.AddObject("details", e.Details)
}

return nil
Expand Down
29 changes: 29 additions & 0 deletions internal/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package version

import (
"testing"

"github.com/stretchr/testify/require"
)

func resetVersion() {
Version = ""
GitHash = ""
}

func TestGetVersionWithVersion(t *testing.T) {
Version = "0.1.1"
defer resetVersion()
require.Equal(t, Version, GetVersion())
}

func TestGetVersionWithGitHash(t *testing.T) {
GitHash = "git hash"
defer resetVersion()
require.Equal(t, GitHash, GetVersion())
}

func TestGetVersionWithUnknownVersion(t *testing.T) {
defer resetVersion()
require.Equal(t, "unknown", GetVersion())
}
3 changes: 2 additions & 1 deletion operator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/observiq/stanza/database"
"go.uber.org/zap"
)

Expand All @@ -23,7 +24,7 @@ type Builder interface {
// BuildContext supplies contextual resources when building an operator.
type BuildContext struct {
PluginRegistry PluginRegistry
Database Database
Database database.Database
Parameters map[string]interface{}
Logger *zap.SugaredLogger
}
Expand Down
3 changes: 2 additions & 1 deletion operator/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"encoding/json"
"testing"

"github.com/observiq/stanza/database"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

func TestStubDatabase(t *testing.T) {
stub := &StubDatabase{}
stub := &database.StubDatabase{}

err := stub.Close()
require.NoError(t, err)
Expand Down
38 changes: 0 additions & 38 deletions operator/database_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions operator/helper/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package helper
import (
"sync"

"github.com/observiq/stanza/operator"
"github.com/observiq/stanza/database"
"go.etcd.io/bbolt"
)

Expand All @@ -18,13 +18,13 @@ type Persister interface {
// ScopedBBoltPersister is a persister that uses a database for the backend
type ScopedBBoltPersister struct {
scope []byte
db operator.Database
db database.Database
cache map[string][]byte
cacheMux sync.Mutex
}

// NewScopedDBPersister returns a new ScopedBBoltPersister
func NewScopedDBPersister(db operator.Database, scope string) *ScopedBBoltPersister {
func NewScopedDBPersister(db database.Database, scope string) *ScopedBBoltPersister {
return &ScopedBBoltPersister{
scope: []byte(scope),
db: db,
Expand Down
3 changes: 2 additions & 1 deletion testutil/database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2fb4b20

Please sign in to comment.