mirror of
https://github.com/empayre/fleet.git
synced 2024-11-08 01:38:57 +00:00
4f4185372d
This is just to pass down the context to the datastore layer, it doesn't use it just yet - this will be in a follow-up PR.
151 lines
4.8 KiB
Go
151 lines
4.8 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewGlobalPolicy(t *testing.T) {
|
|
ds := CreateMySQLDS(t)
|
|
defer ds.Close()
|
|
|
|
q, err := ds.NewQuery(context.Background(), &fleet.Query{
|
|
Name: "query1",
|
|
Description: "query1 desc",
|
|
Query: "select 1;",
|
|
Saved: true,
|
|
})
|
|
require.NoError(t, err)
|
|
p, err := ds.NewGlobalPolicy(context.Background(), q.ID)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "query1", p.QueryName)
|
|
|
|
q2, err := ds.NewQuery(context.Background(), &fleet.Query{
|
|
Name: "query2",
|
|
Description: "query2 desc",
|
|
Query: "select 42;",
|
|
Saved: true,
|
|
})
|
|
require.NoError(t, err)
|
|
_, err = ds.NewGlobalPolicy(context.Background(), q2.ID)
|
|
require.NoError(t, err)
|
|
|
|
policies, err := ds.ListGlobalPolicies(context.Background())
|
|
require.NoError(t, err)
|
|
require.Len(t, policies, 2)
|
|
assert.Equal(t, q.ID, policies[0].QueryID)
|
|
assert.Equal(t, q2.ID, policies[1].QueryID)
|
|
|
|
// Cannot delete a query if it's in a policy
|
|
require.Error(t, ds.DeleteQuery(context.Background(), q.Name))
|
|
|
|
_, err = ds.DeleteGlobalPolicies(context.Background(), []uint{policies[0].ID, policies[1].ID})
|
|
require.NoError(t, err)
|
|
|
|
policies, err = ds.ListGlobalPolicies(context.Background())
|
|
require.NoError(t, err)
|
|
require.Len(t, policies, 0)
|
|
|
|
// But you can delete the query if the policy is gone
|
|
require.NoError(t, ds.DeleteQuery(context.Background(), q.Name))
|
|
}
|
|
|
|
func TestPolicyMembershipView(t *testing.T) {
|
|
ds := CreateMySQLDS(t)
|
|
defer ds.Close()
|
|
|
|
host1, err := ds.NewHost(context.Background(), &fleet.Host{
|
|
OsqueryHostID: "1234",
|
|
DetailUpdatedAt: time.Now(),
|
|
LabelUpdatedAt: time.Now(),
|
|
SeenTime: time.Now(),
|
|
NodeKey: "1",
|
|
UUID: "1",
|
|
Hostname: "foo.local",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
host2, err := ds.NewHost(context.Background(), &fleet.Host{
|
|
OsqueryHostID: "5679",
|
|
DetailUpdatedAt: time.Now(),
|
|
LabelUpdatedAt: time.Now(),
|
|
SeenTime: time.Now(),
|
|
NodeKey: "2",
|
|
UUID: "2",
|
|
Hostname: "bar.local",
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
q, err := ds.NewQuery(context.Background(), &fleet.Query{
|
|
Name: "query1",
|
|
Description: "query1 desc",
|
|
Query: "select 1;",
|
|
Saved: true,
|
|
})
|
|
require.NoError(t, err)
|
|
p, err := ds.NewGlobalPolicy(context.Background(), q.ID)
|
|
require.NoError(t, err)
|
|
|
|
q2, err := ds.NewQuery(context.Background(), &fleet.Query{
|
|
Name: "query2",
|
|
Description: "query2 desc",
|
|
Query: "select 42;",
|
|
Saved: true,
|
|
})
|
|
require.NoError(t, err)
|
|
p2, err := ds.NewGlobalPolicy(context.Background(), q2.ID)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "query1", p.QueryName)
|
|
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host1, map[uint]*bool{p.ID: ptr.Bool(true)}, time.Now()))
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host1, map[uint]*bool{p.ID: ptr.Bool(true)}, time.Now()))
|
|
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{p.ID: nil}, time.Now()))
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{p.ID: ptr.Bool(false)}, time.Now()))
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{p.ID: ptr.Bool(true)}, time.Now()))
|
|
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{p2.ID: nil}, time.Now()))
|
|
|
|
policies, err := ds.ListGlobalPolicies(context.Background())
|
|
require.NoError(t, err)
|
|
require.Len(t, policies, 2)
|
|
|
|
assert.Equal(t, uint(2), policies[0].PassingHostCount)
|
|
assert.Equal(t, uint(0), policies[0].FailingHostCount)
|
|
|
|
assert.Equal(t, uint(0), policies[1].PassingHostCount)
|
|
assert.Equal(t, uint(0), policies[1].FailingHostCount)
|
|
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host1, map[uint]*bool{p.ID: ptr.Bool(false)}, time.Now()))
|
|
require.NoError(t, ds.RecordPolicyQueryExecutions(context.Background(), host2, map[uint]*bool{p2.ID: ptr.Bool(false)}, time.Now()))
|
|
|
|
policies, err = ds.ListGlobalPolicies(context.Background())
|
|
require.NoError(t, err)
|
|
require.Len(t, policies, 2)
|
|
|
|
assert.Equal(t, uint(1), policies[0].PassingHostCount)
|
|
assert.Equal(t, uint(1), policies[0].FailingHostCount)
|
|
|
|
assert.Equal(t, uint(0), policies[1].PassingHostCount)
|
|
assert.Equal(t, uint(1), policies[1].FailingHostCount)
|
|
|
|
policy, err := ds.Policy(context.Background(), policies[0].ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, policies[0], policy)
|
|
|
|
queries, err := ds.PolicyQueriesForHost(context.Background(), nil)
|
|
require.NoError(t, err)
|
|
require.Len(t, queries, 2)
|
|
assert.Equal(t, q.Query, queries[fmt.Sprint(q.ID)])
|
|
assert.Equal(t, q2.Query, queries[fmt.Sprint(q2.ID)])
|
|
}
|