fleet/server/service/service_queries_test.go

134 lines
2.6 KiB
Go
Raw Normal View History

2016-09-26 18:48:55 +00:00
package service
import (
"testing"
2016-09-26 18:48:55 +00:00
"github.com/kolide/kolide-ose/server/datastore"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
2016-10-07 17:26:48 +00:00
func TestListQueries(t *testing.T) {
ds, err := datastore.New("inmem", "")
assert.Nil(t, err)
svc, err := newTestService(ds, nil)
assert.Nil(t, err)
ctx := context.Background()
queries, err := svc.ListQueries(ctx, kolide.ListOptions{})
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.ListQueries(ctx, kolide.ListOptions{})
assert.Nil(t, err)
assert.Len(t, queries, 1)
}
func TestGetQuery(t *testing.T) {
ds, err := datastore.New("inmem", "")
assert.Nil(t, err)
svc, err := newTestService(ds, nil)
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
query, 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("inmem", "")
assert.Nil(t, err)
svc, err := newTestService(ds, nil)
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.ListQueries(kolide.ListOptions{})
assert.Nil(t, err)
assert.Len(t, queries, 1)
}
func TestModifyQuery(t *testing.T) {
ds, err := datastore.New("inmem", "")
assert.Nil(t, err)
svc, err := newTestService(ds, nil)
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
query, 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("inmem", "")
assert.Nil(t, err)
svc, err := newTestService(ds, nil)
assert.Nil(t, err)
ctx := context.Background()
query := &kolide.Query{
Name: "foo",
Query: "select * from time;",
}
query, 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.ListQueries(kolide.ListOptions{})
assert.Nil(t, err)
assert.Len(t, queries, 0)
}