mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
715d908613
Notable refactoring: - Use stdlib "context" in place of "golang.org/x/net/context" - Go-kit no longer wraps errors, so we remove the unwrap in transport_error.go - Use MakeHandler when setting up endpoint tests (fixes test bug caught during this refactoring) Closes #1411.
108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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"
|
|
"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)
|
|
|
|
interval := uint(120)
|
|
queryPayload := kolide.ScheduledQueryPayload{
|
|
Interval: &interval,
|
|
}
|
|
query, err = svc.ModifyScheduledQuery(ctx, sq1.ID, queryPayload)
|
|
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)
|
|
}
|