fleet/server/service_queries_test.go
Zachary Wasserman 885db1a597 Refactoring for config patterns (#159)
This PR refactors most of the codebase to use the new config patterns implemented in #149. Now the core service keeps a copy of the KolideConfig struct, and service methods can reference the configuration in that struct when they need it. The most significant refactoring is in the sessions code, separating the business logic from the storage layer.
2016-09-14 09:11:06 -07:00

136 lines
2.8 KiB
Go

package server
import (
"testing"
kitlog "github.com/go-kit/kit/log"
"github.com/kolide/kolide-ose/config"
"github.com/kolide/kolide-ose/datastore"
"github.com/kolide/kolide-ose/kolide"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
func TestGetAllQueries(t *testing.T) {
ds, err := datastore.New("gorm-sqlite3", ":memory:")
assert.Nil(t, err)
svc, err := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
assert.Nil(t, err)
ctx := context.Background()
queries, err := svc.GetAllQueries(ctx)
assert.Nil(t, err)
assert.Len(t, queries, 0)
err = ds.NewQuery(&kolide.Query{
Name: "foo",
Query: "select * from time;",
})
assert.Nil(t, err)
queries, err = svc.GetAllQueries(ctx)
assert.Nil(t, err)
assert.Len(t, queries, 1)
}
func TestGetQuery(t *testing.T) {
ds, err := datastore.New("gorm-sqlite3", ":memory:")
assert.Nil(t, err)
svc, err := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
err = ds.NewQuery(query)
assert.Nil(t, err)
assert.NotZero(t, query.ID)
queryVerify, err := svc.GetQuery(ctx, query.ID)
assert.Nil(t, err)
assert.Equal(t, query.ID, queryVerify.ID)
}
func TestNewQuery(t *testing.T) {
ds, err := datastore.New("gorm-sqlite3", ":memory:")
assert.Nil(t, err)
svc, err := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
assert.Nil(t, err)
ctx := context.Background()
name := "foo"
query := "select * from time;"
_, err = svc.NewQuery(ctx, kolide.QueryPayload{
Name: &name,
Query: &query,
})
assert.Nil(t, err)
queries, err := ds.Queries()
assert.Nil(t, err)
assert.Len(t, queries, 1)
}
func TestModifyQuery(t *testing.T) {
ds, err := datastore.New("gorm-sqlite3", ":memory:")
assert.Nil(t, err)
svc, err := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
err = ds.NewQuery(query)
assert.Nil(t, err)
assert.NotZero(t, query.ID)
newName := "bar"
queryVerify, err := svc.ModifyQuery(ctx, query.ID, kolide.QueryPayload{
Name: &newName,
})
assert.Nil(t, err)
assert.Equal(t, query.ID, queryVerify.ID)
assert.Equal(t, "bar", queryVerify.Name)
}
func TestDeleteQuery(t *testing.T) {
ds, err := datastore.New("gorm-sqlite3", ":memory:")
assert.Nil(t, err)
svc, err := NewService(ds, kitlog.NewNopLogger(), config.TestConfig())
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
err = ds.NewQuery(query)
assert.Nil(t, err)
assert.NotZero(t, query.ID)
err = svc.DeleteQuery(ctx, query.ID)
assert.Nil(t, err)
queries, err := ds.Queries()
assert.Nil(t, err)
assert.Len(t, queries, 0)
}