2016-10-04 20:34:36 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-11-22 21:56:05 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
|
|
|
"github.com/kolide/fleet/server/test"
|
2016-10-04 20:34:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testDeletePack(t *testing.T, ds kolide.Datastore) {
|
2018-01-10 19:38:20 +00:00
|
|
|
pack := test.NewPack(t, ds, "foo")
|
2016-10-17 19:30:47 +00:00
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
pack, err := ds.Pack(pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
err = ds.DeletePack(pack.Name)
|
2016-10-04 20:34:36 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-10-17 19:30:47 +00:00
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
pack, err = ds.Pack(pack.ID)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:35:25 +00:00
|
|
|
func testGetPackByName(t *testing.T, ds kolide.Datastore) {
|
2018-01-10 19:38:20 +00:00
|
|
|
pack := test.NewPack(t, ds, "foo")
|
2017-01-13 18:35:25 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
func testListPacks(t *testing.T, ds kolide.Datastore) {
|
|
|
|
p1 := &kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "foo_pack",
|
|
|
|
}
|
|
|
|
p2 := &kolide.PackSpec{
|
|
|
|
ID: 2,
|
|
|
|
Name: "bar_pack",
|
|
|
|
}
|
|
|
|
err := ds.ApplyPackSpecs([]*kolide.PackSpec{p1})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err := ds.ListPacks(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 1)
|
|
|
|
|
|
|
|
err = ds.ApplyPackSpecs([]*kolide.PackSpec{p1, p2})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacks(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testListHostsInPack(t *testing.T, ds kolide.Datastore) {
|
2017-03-01 19:56:13 +00:00
|
|
|
if ds.Name() == "inmem" {
|
|
|
|
t.Skip("inmem is deprecated")
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:56:05 +00:00
|
|
|
mockClock := clock.NewMockClock()
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
l1 := kolide.LabelSpec{
|
|
|
|
ID: 1,
|
2016-11-22 21:56:05 +00:00
|
|
|
Name: "foo",
|
2018-01-10 19:38:20 +00:00
|
|
|
}
|
|
|
|
err := ds.ApplyLabelSpecs([]*kolide.LabelSpec{&l1})
|
2016-11-22 21:56:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
p1 := &kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "foo_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
l1.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs([]*kolide.PackSpec{p1})
|
2016-11-22 21:56:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2017-01-11 22:24:32 +00:00
|
|
|
h1 := test.NewHost(t, ds, "h1.local", "10.10.10.1", "1", "1", mockClock.Now())
|
2016-11-22 21:56:05 +00:00
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
hostsInPack, err := ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, hostsInPack, 0)
|
|
|
|
|
2016-11-22 21:56:05 +00:00
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
2017-01-17 06:03:51 +00:00
|
|
|
map[uint]bool{l1.ID: true},
|
2016-11-22 21:56:05 +00:00
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
hostsInPack, err = ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
|
2016-11-22 21:56:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, hostsInPack, 1)
|
|
|
|
|
2017-01-11 22:24:32 +00:00
|
|
|
explicitHostsInPack, err := ds.ListExplicitHostsInPack(p1.ID, kolide.ListOptions{})
|
2016-11-22 21:56:05 +00:00
|
|
|
require.Nil(t, err)
|
2017-01-11 22:24:32 +00:00
|
|
|
require.Len(t, explicitHostsInPack, 0)
|
|
|
|
|
|
|
|
h2 := test.NewHost(t, ds, "h2.local", "10.10.10.2", "2", "2", mockClock.Now())
|
2016-11-22 21:56:05 +00:00
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h2,
|
2017-01-17 06:03:51 +00:00
|
|
|
map[uint]bool{l1.ID: true},
|
2016-11-22 21:56:05 +00:00
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
hostsInPack, err = ds.ListHostsInPack(p1.ID, kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, hostsInPack, 2)
|
|
|
|
}
|
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) {
|
2018-01-10 19:38:20 +00:00
|
|
|
l1 := kolide.LabelSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "l1",
|
|
|
|
Query: "select 1",
|
|
|
|
}
|
|
|
|
err := ds.ApplyLabelSpecs([]*kolide.LabelSpec{&l1})
|
|
|
|
require.Nil(t, err)
|
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
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
p1 := &kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "pack1",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
l1.Name,
|
|
|
|
l1.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs([]*kolide.PackSpec{p1})
|
|
|
|
require.NotNil(t, err)
|
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
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
func setupPackSpecsTest(t *testing.T, ds kolide.Datastore) []*kolide.PackSpec {
|
2018-01-03 19:18:05 +00:00
|
|
|
zwass := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
|
|
|
queries := []*kolide.Query{
|
|
|
|
{Name: "foo", Description: "get the foos", Query: "select * from foo"},
|
|
|
|
{Name: "bar", Description: "do some bars", Query: "select baz from bar"},
|
|
|
|
}
|
|
|
|
// Zach creates some queries
|
|
|
|
err := ds.ApplyQueries(zwass.ID, queries)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
labels := []*kolide.LabelSpec{
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from foo",
|
|
|
|
},
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "bar",
|
|
|
|
Query: "select * from bar",
|
|
|
|
},
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "bing",
|
|
|
|
Query: "select * from bing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyLabelSpecs(labels)
|
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
|
|
|
|
boolPtr := func(b bool) *bool { return &b }
|
|
|
|
uintPtr := func(x uint) *uint { return &x }
|
|
|
|
stringPtr := func(s string) *string { return &s }
|
2018-05-08 01:54:29 +00:00
|
|
|
expectedSpecs := []*kolide.PackSpec{
|
2018-01-03 19:18:05 +00:00
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "test_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[0].Name,
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q0",
|
2018-01-03 19:18:05 +00:00
|
|
|
Description: "test_foo",
|
|
|
|
Interval: 42,
|
|
|
|
},
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[0].Name,
|
2020-10-21 23:29:27 +00:00
|
|
|
Name: "foo_snapshot",
|
|
|
|
Interval: 600,
|
|
|
|
Snapshot: boolPtr(true),
|
|
|
|
},
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
Name: "q2",
|
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 600,
|
|
|
|
Removed: boolPtr(false),
|
|
|
|
Shard: uintPtr(73),
|
|
|
|
Platform: stringPtr("foobar"),
|
|
|
|
Version: stringPtr("0.0.0.0.0.1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 2,
|
|
|
|
Name: "test_pack_disabled",
|
|
|
|
Disabled: true,
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[0].Name,
|
|
|
|
Name: "q0",
|
|
|
|
Description: "test_foo",
|
|
|
|
Interval: 42,
|
|
|
|
},
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[0].Name,
|
2018-01-03 19:18:05 +00:00
|
|
|
Name: "foo_snapshot",
|
|
|
|
Interval: 600,
|
|
|
|
Snapshot: boolPtr(true),
|
|
|
|
},
|
|
|
|
kolide.PackSpecQuery{
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q2",
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 600,
|
|
|
|
Removed: boolPtr(false),
|
|
|
|
Shard: uintPtr(73),
|
|
|
|
Platform: stringPtr("foobar"),
|
|
|
|
Version: stringPtr("0.0.0.0.0.1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
err = ds.ApplyPackSpecs(expectedSpecs)
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
2018-05-08 01:54:29 +00:00
|
|
|
return expectedSpecs
|
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
func testApplyPackSpecRoundtrip(t *testing.T, ds kolide.Datastore) {
|
|
|
|
expectedSpecs := setupPackSpecsTest(t, ds)
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
gotSpec, err := ds.GetPackSpecs()
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
2018-05-08 01:54:29 +00:00
|
|
|
assert.Equal(t, expectedSpecs, gotSpec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetPackSpec(t *testing.T, ds kolide.Datastore) {
|
|
|
|
expectedSpecs := setupPackSpecsTest(t, ds)
|
|
|
|
|
|
|
|
for _, s := range expectedSpecs {
|
|
|
|
spec, err := ds.GetPackSpec(s.Name)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, s, spec)
|
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testApplyPackSpecMissingQueries(t *testing.T, ds kolide.Datastore) {
|
|
|
|
// Do not define queries mentioned in spec
|
|
|
|
specs := []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "test_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{},
|
|
|
|
},
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: "bar",
|
|
|
|
Interval: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should error due to unkown query
|
|
|
|
err := ds.ApplyPackSpecs(specs)
|
|
|
|
if assert.NotNil(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unknown query 'bar'")
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 19:38:20 +00:00
|
|
|
|
2019-01-17 23:59:42 +00:00
|
|
|
func testApplyPackSpecMissingName(t *testing.T, ds kolide.Datastore) {
|
|
|
|
setupPackSpecsTest(t, ds)
|
|
|
|
|
|
|
|
specs := []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
Name: "test2",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{},
|
|
|
|
},
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: "foo",
|
|
|
|
Interval: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ds.ApplyPackSpecs(specs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Query name should have been copied into name field
|
|
|
|
spec, err := ds.GetPackSpec("test2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", spec.Queries[0].Name)
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
func testListLabelsForPack(t *testing.T, ds kolide.Datastore) {
|
|
|
|
labelSpecs := []*kolide.LabelSpec{
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from foo",
|
|
|
|
},
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "bar",
|
|
|
|
Query: "select * from bar",
|
|
|
|
},
|
|
|
|
&kolide.LabelSpec{
|
|
|
|
Name: "bing",
|
|
|
|
Query: "select * from bing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ds.ApplyLabelSpecs(labelSpecs)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
specs := []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "test_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 2,
|
|
|
|
Name: "test 2",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&kolide.PackSpec{
|
|
|
|
ID: 3,
|
|
|
|
Name: "test 3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs(specs)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
labels, err := ds.ListLabelsForPack(specs[0].ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, labels, 3)
|
|
|
|
|
|
|
|
labels, err = ds.ListLabelsForPack(specs[1].ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, labels, 1)
|
|
|
|
assert.Equal(t, "bing", labels[0].Name)
|
|
|
|
|
|
|
|
labels, err = ds.ListLabelsForPack(specs[2].ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, labels, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testListPacksForHost(t *testing.T, ds kolide.Datastore) {
|
|
|
|
if ds.Name() == "inmem" {
|
|
|
|
t.Skip("inmem is deprecated")
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClock := clock.NewMockClock()
|
|
|
|
|
|
|
|
l1 := &kolide.LabelSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
l2 := &kolide.LabelSpec{
|
|
|
|
ID: 2,
|
|
|
|
Name: "bar",
|
|
|
|
}
|
|
|
|
err := ds.ApplyLabelSpecs([]*kolide.LabelSpec{l1, l2})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
p1 := &kolide.PackSpec{
|
|
|
|
ID: 1,
|
|
|
|
Name: "foo_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
l1.Name,
|
|
|
|
l2.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p2 := &kolide.PackSpec{
|
|
|
|
ID: 2,
|
|
|
|
Name: "shmoo_pack",
|
|
|
|
Targets: kolide.PackSpecTargets{
|
|
|
|
Labels: []string{
|
|
|
|
l2.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs([]*kolide.PackSpec{p1, p2})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
h1 := test.NewHost(t, ds, "h1.local", "10.10.10.1", "1", "1", mockClock.Now())
|
|
|
|
|
|
|
|
packs, err := ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, packs, 0)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, packs, 1) {
|
|
|
|
assert.Equal(t, "foo_pack", packs[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: false, l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: true, l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
h2 := test.NewHost(t, ds, "h2.local", "10.10.10.2", "2", "2", mockClock.Now())
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h2,
|
|
|
|
map[uint]bool{l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l2.ID: false},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, packs, 1) {
|
|
|
|
assert.Equal(t, "foo_pack", packs[0].Name)
|
|
|
|
}
|
2018-08-13 17:07:10 +00:00
|
|
|
|
|
|
|
// Add host directly to pack
|
|
|
|
err = ds.AddHostToPack(h1.ID, p2.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
// Remove label membership for both
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l2.ID: false, l1.ID: false},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l2.ID: false},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, packs, 1) {
|
|
|
|
assert.Equal(t, p2.Name, packs[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now host is added directly to both packs
|
|
|
|
err = ds.AddHostToPack(h1.ID, p1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
2018-01-10 19:38:20 +00:00
|
|
|
}
|