fleet/server/datastore/datastore_packs_test.go

150 lines
3.4 KiB
Go
Raw Normal View History

package datastore
import (
"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 testGetPackByName(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, ok, err := ds.PackByName(pack.Name)
require.Nil(t, err)
assert.True(t, ok)
assert.NotNil(t, pack)
assert.Equal(t, "foo", pack.Name)
pack, ok, err = ds.PackByName("bar")
require.Nil(t, err)
assert.False(t, ok)
assert.Nil(t, pack)
}
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 := test.NewHost(t, ds, "h1.local", "10.10.10.1", "1", "1", mockClock.Now())
err = ds.RecordLabelQueryExecutions(
h1,
map[uint]bool{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)
explicitHostsInPack, err := ds.ListExplicitHostsInPack(p1.ID, kolide.ListOptions{})
require.Nil(t, err)
require.Len(t, explicitHostsInPack, 0)
h2 := test.NewHost(t, ds, "h2.local", "10.10.10.2", "2", "2", mockClock.Now())
err = ds.RecordLabelQueryExecutions(
h2,
map[uint]bool{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)
h3 := test.NewHost(t, ds, "h3.local", "10.10.10.3", "3", "3", mockClock.Now())
err = ds.AddHostToPack(h3.ID, p1.ID)
require.Nil(t, err)
hostsInPack, err = ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
require.Nil(t, err)
require.Len(t, hostsInPack, 3)
explicitHostsInPack, err = ds.ListExplicitHostsInPack(p1.ID, kolide.ListOptions{})
require.Nil(t, err)
require.Len(t, explicitHostsInPack, 1)
}
Add host_ids and label_ids fields to the packs API (#737) This PR adds the `host_ids` and `label_ids` field to the packs HTTP API so that one can operate on the hosts/labels which a pack is scheduled to be executed on. This replaces (and deletes) the `/api/v1/kolide/packs/123/labels/456` API in favor of `PATCH /api/v1/packs/123` and specifying the `label_ids` field. This also allows for bulk operations. Consider the following API examples: ## Creating a pack with a known set of hosts and labels The key addition is the `host_ids` and `label_ids` field in both the request and the response. ### Request ``` POST /api/v1/kolide/packs ``` ```json { "name": "My new pack", "description": "The newest of the packs", "host_ids": [1, 2, 3], "label_ids": [1, 3, 5] } ``` ### Response ```json { "pack": { "id": 123, "name": "My new pack", "description": "The newest of the packs", "platform": "", "created_by": 1, "disabled": false, "query_count": 0, "total_hosts_count": 5, "host_ids": [1, 2, 3], "label_ids": [1, 3, 5] } } ``` ## Modifying the hosts and/or labels that a pack is scheduled to execute on ### Request ``` PATCH /api/v1/kolide/packs/123 ``` ```json { "host_ids": [1, 2, 3, 4, 5], "label_ids": [1, 3, 5, 7] } ``` ### Response ```json { "pack": { "id": 123, "name": "My new pack", "description": "The newest of the packs", "platform": "", "created_by": 1, "disabled": false, "query_count": 0, "total_hosts_count": 5, "host_ids": [1, 2, 3, 4, 5], "label_ids": [1, 3, 5, 7] } } ``` close #633
2017-01-03 17:32:06 +00:00
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)
}