2016-12-13 22:22:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-12-13 22:22:05 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-02-01 17:20:50 +00:00
|
|
|
"github.com/kolide/kolide/server/config"
|
|
|
|
"github.com/kolide/kolide/server/datastore/inmem"
|
|
|
|
"github.com/kolide/kolide/server/kolide"
|
|
|
|
"github.com/kolide/kolide/server/test"
|
2016-12-13 22:22:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetScheduledQueriesInPack(t *testing.T) {
|
|
|
|
ds, err := inmem.New(config.TestConfig())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
u1 := test.NewUser(t, ds, "Admin", "admin", "admin@kolide.co", true)
|
|
|
|
q1 := test.NewQuery(t, ds, "foo", "select * from time;", u1.ID, true)
|
|
|
|
q2 := test.NewQuery(t, ds, "bar", "select * from time;", u1.ID, true)
|
|
|
|
p1 := test.NewPack(t, ds, "baz")
|
|
|
|
sq1 := test.NewScheduledQuery(t, ds, p1.ID, q1.ID, 60, false, false)
|
|
|
|
|
|
|
|
queries, err := svc.GetScheduledQueriesInPack(ctx, p1.ID, kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, queries, 1)
|
|
|
|
assert.Equal(t, sq1.ID, queries[0].ID)
|
|
|
|
|
|
|
|
test.NewScheduledQuery(t, ds, p1.ID, q2.ID, 60, false, false)
|
|
|
|
test.NewScheduledQuery(t, ds, p1.ID, q2.ID, 60, true, false)
|
|
|
|
|
|
|
|
queries, err = svc.GetScheduledQueriesInPack(ctx, p1.ID, kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, queries, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetScheduledQuery(t *testing.T) {
|
|
|
|
ds, err := inmem.New(config.TestConfig())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
u1 := test.NewUser(t, ds, "Admin", "admin", "admin@kolide.co", true)
|
|
|
|
q1 := test.NewQuery(t, ds, "foo", "select * from time;", u1.ID, true)
|
|
|
|
p1 := test.NewPack(t, ds, "baz")
|
|
|
|
sq1 := test.NewScheduledQuery(t, ds, p1.ID, q1.ID, 60, false, false)
|
|
|
|
|
|
|
|
query, err := svc.GetScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(60), query.Interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModifyScheduledQuery(t *testing.T) {
|
|
|
|
ds, err := inmem.New(config.TestConfig())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
u1 := test.NewUser(t, ds, "Admin", "admin", "admin@kolide.co", true)
|
|
|
|
q1 := test.NewQuery(t, ds, "foo", "select * from time;", u1.ID, true)
|
|
|
|
p1 := test.NewPack(t, ds, "baz")
|
|
|
|
sq1 := test.NewScheduledQuery(t, ds, p1.ID, q1.ID, 60, false, false)
|
|
|
|
|
|
|
|
query, err := svc.GetScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(60), query.Interval)
|
|
|
|
|
2017-02-13 21:31:22 +00:00
|
|
|
interval := uint(120)
|
|
|
|
queryPayload := kolide.ScheduledQueryPayload{
|
|
|
|
Interval: &interval,
|
|
|
|
}
|
|
|
|
query, err = svc.ModifyScheduledQuery(ctx, sq1.ID, queryPayload)
|
2016-12-13 22:22:05 +00:00
|
|
|
assert.Equal(t, uint(120), query.Interval)
|
|
|
|
|
|
|
|
queryVerify, err := svc.GetScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(120), queryVerify.Interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteScheduledQuery(t *testing.T) {
|
|
|
|
ds, err := inmem.New(config.TestConfig())
|
|
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
u1 := test.NewUser(t, ds, "Admin", "admin", "admin@kolide.co", true)
|
|
|
|
q1 := test.NewQuery(t, ds, "foo", "select * from time;", u1.ID, true)
|
|
|
|
p1 := test.NewPack(t, ds, "baz")
|
|
|
|
sq1 := test.NewScheduledQuery(t, ds, p1.ID, q1.ID, 60, false, false)
|
|
|
|
|
|
|
|
query, err := svc.GetScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(60), query.Interval)
|
|
|
|
|
|
|
|
err = svc.DeleteScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
_, err = svc.GetScheduledQuery(ctx, sq1.ID)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|