fleet/server/datastore/datastore_packs_test.go
Mike Arpaia a036c5da9f Add attributes to packs (#524)
* Adds created_by attribute to packs

This PR also updated the distributed query code to use the pattern
established here (service checks context)

* add enable/disable state to packs

* add query_count to packs API responses

* add host_count to packs API responses (very, very poorly)

* pack description should not be required

* counting hosts in packs via mysql

* removing extraneous newline in test

* Switch case instead of if/if else

* add description to update query for SavePack method

* change AND to WHERE in query as per @zwass

* add ordering and list options as per @murphybytes' suggestion
2016-11-22 13:56:05 -08:00

150 lines
3.0 KiB
Go

package datastore
import (
"fmt"
"testing"
"github.com/WatchBeam/clock"
"github.com/kolide/kolide-ose/server/kolide"
"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 testAddAndRemoveQueryFromPack(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)
q1 := &kolide.Query{
Name: "bar",
Query: "bar",
}
q1, err = ds.NewQuery(q1)
assert.Nil(t, err)
assert.NotEqual(t, uint(0), q1.ID)
err = ds.AddQueryToPack(q1.ID, pack.ID)
assert.Nil(t, err)
q2 := &kolide.Query{
Name: "baz",
Query: "baz",
}
q2, err = ds.NewQuery(q2)
assert.Nil(t, err)
assert.NotEqual(t, uint(0), q2.ID)
assert.NotEqual(t, q1.ID, q2.ID)
err = ds.AddQueryToPack(q2.ID, pack.ID)
assert.Nil(t, err)
queries, err := ds.ListQueriesInPack(pack)
assert.Nil(t, err)
assert.Len(t, queries, 2)
err = ds.RemoveQueryFromPack(q1, pack)
assert.Nil(t, err)
queries, err = ds.ListQueriesInPack(pack)
assert.Nil(t, err)
assert.Len(t, queries, 1)
}
func testGetHostsInPack(t *testing.T, ds kolide.Datastore) {
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",
})
require.Nil(t, err)
q2, err := ds.NewQuery(&kolide.Query{
Name: "bar",
Query: "bar",
})
require.Nil(t, err)
err = ds.AddQueryToPack(q1.ID, p1.ID)
require.Nil(t, err)
err = ds.AddQueryToPack(q2.ID, p1.ID)
require.Nil(t, err)
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(),
HostName: "foobar.local",
NodeKey: "1",
UUID: "1",
PrimaryIP: "192.168.1.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(),
HostName: "foobaz.local",
NodeKey: "2",
UUID: "2",
PrimaryIP: "192.168.1.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)
}