2020-12-04 17:50:39 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2020-12-04 17:50:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScheduleQuery(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2020-12-04 17:50:39 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
expectedQuery := &fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
Name: "foobar",
|
|
|
|
QueryName: "foobar",
|
|
|
|
QueryID: 3,
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.NewScheduledQueryFunc = func(q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
assert.Equal(t, expectedQuery, q)
|
|
|
|
return expectedQuery, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := svc.ScheduleQuery(test.UserContext(test.UserAdmin), expectedQuery)
|
2020-12-04 17:50:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, ds.NewScheduledQueryFuncInvoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScheduleQueryNoName(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2020-12-04 17:50:39 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
expectedQuery := &fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
Name: "foobar",
|
|
|
|
QueryName: "foobar",
|
|
|
|
QueryID: 3,
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.QueryFunc = func(qid uint) (*fleet.Query, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
require.Equal(t, expectedQuery.QueryID, qid)
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.Query{Name: expectedQuery.QueryName}, nil
|
2020-12-04 17:50:39 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListScheduledQueriesInPackFunc = func(id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
// No matching query
|
2021-06-06 22:07:29 +00:00
|
|
|
return []*fleet.ScheduledQuery{
|
|
|
|
&fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
Name: "froobling",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.NewScheduledQueryFunc = func(q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
assert.Equal(t, expectedQuery, q)
|
|
|
|
return expectedQuery, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := svc.ScheduleQuery(
|
|
|
|
test.UserContext(test.UserAdmin),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.ScheduledQuery{QueryID: expectedQuery.QueryID},
|
2020-12-04 17:50:39 +00:00
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, ds.NewScheduledQueryFuncInvoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScheduleQueryNoNameMultiple(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2020-12-04 17:50:39 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
expectedQuery := &fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
Name: "foobar-1",
|
|
|
|
QueryName: "foobar",
|
|
|
|
QueryID: 3,
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.QueryFunc = func(qid uint) (*fleet.Query, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
require.Equal(t, expectedQuery.QueryID, qid)
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.Query{Name: expectedQuery.QueryName}, nil
|
2020-12-04 17:50:39 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListScheduledQueriesInPackFunc = func(id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
// No matching query
|
2021-06-06 22:07:29 +00:00
|
|
|
return []*fleet.ScheduledQuery{
|
|
|
|
&fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
Name: "foobar",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.NewScheduledQueryFunc = func(q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
|
2020-12-04 17:50:39 +00:00
|
|
|
assert.Equal(t, expectedQuery, q)
|
|
|
|
return expectedQuery, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := svc.ScheduleQuery(
|
|
|
|
test.UserContext(test.UserAdmin),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.ScheduledQuery{QueryID: expectedQuery.QueryID},
|
2020-12-04 17:50:39 +00:00
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, ds.NewScheduledQueryFuncInvoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindNextNameForQuery(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
2021-06-06 22:07:29 +00:00
|
|
|
scheduled []*fleet.ScheduledQuery
|
2020-12-04 17:50:39 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "foobar",
|
2021-06-06 22:07:29 +00:00
|
|
|
scheduled: []*fleet.ScheduledQuery{},
|
2020-12-04 17:50:39 +00:00
|
|
|
expected: "foobar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "foobar",
|
2021-06-06 22:07:29 +00:00
|
|
|
scheduled: []*fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
{
|
|
|
|
Name: "foobar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "foobar-1",
|
|
|
|
}, {
|
|
|
|
name: "foobar",
|
2021-06-06 22:07:29 +00:00
|
|
|
scheduled: []*fleet.ScheduledQuery{
|
2020-12-04 17:50:39 +00:00
|
|
|
{
|
|
|
|
Name: "foobar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foobar-1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "foobar-1-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
assert.Equal(t, tt.expected, findNextNameForQuery(tt.name, tt.scheduled))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|