fleet/server/datastore/datastore_packs_test.go
Mike Arpaia 704ddd424b Host summary endpoint (#742)
* Initial scaffolding of the host summary endpoint

* inmem datastore implementation of GenerateHostStatusStatistics

* HostSummary docstring

* changing the url of the host summary endpoint

* datastore tests for GenerateHostStatusStatistics

* MySQL datastore implementation of GenerateHostStatusStatistics

* <= and >= to catch exact time edge case

* removing clock interface method

* lowercase error wraps

* removin superfluous whitespace

* use updated_at

* adding a seen_at column to the hosts table

* moving the update of seen_time to the caller

* using db.Get instead of db.Select
2017-01-04 14:16:17 -07:00

129 lines
2.8 KiB
Go

package datastore
import (
"fmt"
"testing"
"github.com/WatchBeam/clock"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/kolide/kolide-ose/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testDeletePack(t *testing.T, ds kolide.Datastore) {
pack := &kolide.Pack{
Name: "foo",
}
_, err := ds.NewPack(pack)
assert.Nil(t, err)
assert.NotEqual(t, uint(0), pack.ID)
pack, err = ds.Pack(pack.ID)
require.Nil(t, err)
err = ds.DeletePack(pack.ID)
assert.Nil(t, err)
assert.NotEqual(t, uint(0), pack.ID)
pack, err = ds.Pack(pack.ID)
assert.NotNil(t, err)
}
func testGetHostsInPack(t *testing.T, ds kolide.Datastore) {
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
mockClock := clock.NewMockClock()
p1, err := ds.NewPack(&kolide.Pack{
Name: "foo",
})
require.Nil(t, err)
q1, err := ds.NewQuery(&kolide.Query{
Name: "foo",
Query: "foo",
AuthorID: user.ID,
})
require.Nil(t, err)
q2, err := ds.NewQuery(&kolide.Query{
Name: "bar",
Query: "bar",
AuthorID: user.ID,
})
require.Nil(t, err)
test.NewScheduledQuery(t, ds, p1.ID, q1.ID, 60, false, false)
test.NewScheduledQuery(t, ds, p1.ID, q2.ID, 60, false, false)
l1, err := ds.NewLabel(&kolide.Label{
Name: "foo",
})
require.Nil(t, err)
err = ds.AddLabelToPack(l1.ID, p1.ID)
require.Nil(t, err)
h1, err := ds.NewHost(&kolide.Host{
DetailUpdateTime: mockClock.Now(),
SeenTime: mockClock.Now(),
HostName: "foobar.local",
OsqueryHostID: "1",
NodeKey: "1",
UUID: "1",
})
require.Nil(t, err)
err = ds.RecordLabelQueryExecutions(
h1,
map[string]bool{fmt.Sprintf("%d", l1.ID): true},
mockClock.Now(),
)
require.Nil(t, err)
hostsInPack, err := ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
require.Nil(t, err)
require.Len(t, hostsInPack, 1)
h2, err := ds.NewHost(&kolide.Host{
DetailUpdateTime: mockClock.Now(),
SeenTime: mockClock.Now(),
HostName: "foobaz.local",
OsqueryHostID: "2",
NodeKey: "2",
UUID: "2",
})
require.Nil(t, err)
err = ds.RecordLabelQueryExecutions(
h2,
map[string]bool{fmt.Sprintf("%d", l1.ID): true},
mockClock.Now(),
)
require.Nil(t, err)
hostsInPack, err = ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
require.Nil(t, err)
require.Len(t, hostsInPack, 2)
}
func testAddLabelToPackTwice(t *testing.T, ds kolide.Datastore) {
l1 := test.NewLabel(t, ds, "l1", "select 1;")
p1 := test.NewPack(t, ds, "p1")
err := ds.AddLabelToPack(l1.ID, p1.ID)
assert.Nil(t, err)
labels, err := ds.ListLabelsForPack(p1.ID)
assert.Nil(t, err)
assert.Len(t, labels, 1)
err = ds.AddLabelToPack(l1.ID, p1.ID)
assert.Nil(t, err)
labels, err = ds.ListLabelsForPack(p1.ID)
assert.Nil(t, err)
assert.Len(t, labels, 1)
}