2016-12-13 22:22:05 +00:00
|
|
|
package test
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-12-06 18:22:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func NewQuery(t *testing.T, ds kolide.Datastore, name, q string, authorID uint, saved bool) *kolide.Query {
|
2016-12-06 18:22:28 +00:00
|
|
|
query, err := ds.NewQuery(&kolide.Query{
|
2016-12-07 20:22:31 +00:00
|
|
|
Name: name,
|
|
|
|
Query: q,
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &authorID,
|
2016-12-09 17:12:45 +00:00
|
|
|
Saved: saved,
|
2016-12-06 18:22:28 +00:00
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
|
|
|
query, err = ds.Query(query.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func NewPack(t *testing.T, ds kolide.Datastore, name string) *kolide.Pack {
|
2018-01-10 19:38:20 +00:00
|
|
|
err := ds.ApplyPackSpecs([]*kolide.PackSpec{&kolide.PackSpec{Name: name}})
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
2018-01-10 19:38:20 +00:00
|
|
|
pack, ok, err := ds.PackByName(name)
|
|
|
|
require.True(t, ok)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
return pack
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func NewCampaign(t *testing.T, ds kolide.Datastore, queryID uint, status kolide.DistributedQueryStatus, now time.Time) *kolide.DistributedQueryCampaign {
|
2016-12-06 18:22:28 +00:00
|
|
|
campaign, err := ds.NewDistributedQueryCampaign(&kolide.DistributedQueryCampaign{
|
|
|
|
UpdateCreateTimestamps: kolide.UpdateCreateTimestamps{
|
|
|
|
CreateTimestamp: kolide.CreateTimestamp{
|
|
|
|
CreatedAt: now,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryID: queryID,
|
|
|
|
Status: status,
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
|
|
|
campaign, err = ds.DistributedQueryCampaign(campaign.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
return campaign
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func AddHostToCampaign(t *testing.T, ds kolide.Datastore, campaignID, hostID uint) {
|
2016-12-06 18:22:28 +00:00
|
|
|
_, err := ds.NewDistributedQueryCampaignTarget(
|
|
|
|
&kolide.DistributedQueryCampaignTarget{
|
|
|
|
Type: kolide.TargetHost,
|
|
|
|
TargetID: hostID,
|
|
|
|
DistributedQueryCampaignID: campaignID,
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func AddLabelToCampaign(t *testing.T, ds kolide.Datastore, campaignID, labelID uint) {
|
2016-12-06 18:22:28 +00:00
|
|
|
_, err := ds.NewDistributedQueryCampaignTarget(
|
|
|
|
&kolide.DistributedQueryCampaignTarget{
|
|
|
|
Type: kolide.TargetLabel,
|
|
|
|
TargetID: labelID,
|
|
|
|
DistributedQueryCampaignID: campaignID,
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:10:20 +00:00
|
|
|
func AddAllHostsLabel(t *testing.T, ds kolide.Datastore) {
|
|
|
|
_, err := ds.NewLabel(
|
|
|
|
&kolide.Label{
|
2020-04-07 22:12:32 +00:00
|
|
|
Name: "All Hosts",
|
|
|
|
Query: "select 1",
|
|
|
|
LabelType: kolide.LabelTypeBuiltIn,
|
|
|
|
LabelMembershipType: kolide.LabelMembershipTypeManual,
|
2020-04-07 01:10:20 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func NewHost(t *testing.T, ds kolide.Datastore, name, ip, key, uuid string, now time.Time) *kolide.Host {
|
2016-12-06 19:51:11 +00:00
|
|
|
osqueryHostID, _ := kolide.RandomText(10)
|
2016-12-06 18:22:28 +00:00
|
|
|
h, err := ds.NewHost(&kolide.Host{
|
|
|
|
HostName: name,
|
|
|
|
NodeKey: key,
|
|
|
|
UUID: uuid,
|
|
|
|
DetailUpdateTime: now,
|
2020-04-07 01:10:20 +00:00
|
|
|
LabelUpdateTime: now,
|
2017-01-04 21:16:17 +00:00
|
|
|
SeenTime: now,
|
2016-12-06 19:51:11 +00:00
|
|
|
OsqueryHostID: osqueryHostID,
|
2016-12-06 18:22:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotZero(t, h.ID)
|
|
|
|
require.Nil(t, ds.MarkHostSeen(h, now))
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
func NewUser(t *testing.T, ds kolide.Datastore, name, username, email string, admin bool) *kolide.User {
|
2016-12-07 20:22:31 +00:00
|
|
|
u, err := ds.NewUser(&kolide.User{
|
|
|
|
Password: []byte("garbage"),
|
|
|
|
Salt: "garbage",
|
|
|
|
Name: name,
|
|
|
|
Username: username,
|
|
|
|
Email: email,
|
|
|
|
Admin: admin,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotZero(t, u.ID)
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
2018-06-15 14:13:11 +00:00
|
|
|
|
|
|
|
func NewScheduledQuery(t *testing.T, ds kolide.Datastore, pid, qid, interval uint, snapshot, removed bool) *kolide.ScheduledQuery {
|
|
|
|
sq, err := ds.NewScheduledQuery(&kolide.ScheduledQuery{
|
|
|
|
PackID: pid,
|
|
|
|
QueryID: qid,
|
|
|
|
Interval: interval,
|
|
|
|
Snapshot: &snapshot,
|
|
|
|
Removed: &removed,
|
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotZero(t, sq.ID)
|
|
|
|
|
|
|
|
return sq
|
|
|
|
}
|