2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-09-26 18:48:55 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/datastore"
|
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
2016-09-04 05:13:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func TestListQueries(t *testing.T) {
|
2016-10-19 14:46:33 +00:00
|
|
|
ds, err := datastore.New("inmem", "")
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
queries, err := svc.ListQueries(ctx, kolide.ListOptions{})
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 0)
|
|
|
|
|
2016-09-30 01:19:51 +00:00
|
|
|
_, err = ds.NewQuery(&kolide.Query{
|
2016-09-04 05:13:42 +00:00
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from time;",
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
queries, err = svc.ListQueries(ctx, kolide.ListOptions{})
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetQuery(t *testing.T) {
|
2016-10-19 14:46:33 +00:00
|
|
|
ds, err := datastore.New("inmem", "")
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := &kolide.Query{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from time;",
|
|
|
|
}
|
2016-10-19 14:46:33 +00:00
|
|
|
query, err = ds.NewQuery(query)
|
2016-09-04 05:13:42 +00:00
|
|
|
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) {
|
2016-10-19 14:46:33 +00:00
|
|
|
ds, err := datastore.New("inmem", "")
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-04 05:13:42 +00:00
|
|
|
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)
|
|
|
|
|
2016-10-14 15:59:27 +00:00
|
|
|
queries, err := ds.ListQueries(kolide.ListOptions{})
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModifyQuery(t *testing.T) {
|
2016-10-19 14:46:33 +00:00
|
|
|
ds, err := datastore.New("inmem", "")
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := &kolide.Query{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from time;",
|
|
|
|
}
|
2016-10-19 14:46:33 +00:00
|
|
|
query, err = ds.NewQuery(query)
|
2016-09-04 05:13:42 +00:00
|
|
|
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) {
|
2016-10-19 14:46:33 +00:00
|
|
|
ds, err := datastore.New("inmem", "")
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-11-14 18:22:54 +00:00
|
|
|
svc, err := newTestService(ds, nil)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
query := &kolide.Query{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from time;",
|
|
|
|
}
|
2016-10-19 14:46:33 +00:00
|
|
|
query, err = ds.NewQuery(query)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, query.ID)
|
|
|
|
|
|
|
|
err = svc.DeleteQuery(ctx, query.ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-10-14 15:59:27 +00:00
|
|
|
queries, err := ds.ListQueries(kolide.ListOptions{})
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 0)
|
|
|
|
|
|
|
|
}
|