2021-05-12 01:15:16 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-05-12 01:15:16 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgentOptionsForHost(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2021-05-12 01:15:16 +00:00
|
|
|
|
|
|
|
teamID := uint(1)
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-08-20 15:27:41 +00:00
|
|
|
return &fleet.AppConfig{
|
|
|
|
AgentOptions: ptr.RawMessage(json.RawMessage(`{"config":{"baz":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override2"}}}}`)),
|
|
|
|
}, nil
|
2021-05-12 01:15:16 +00:00
|
|
|
}
|
2022-01-18 01:52:09 +00:00
|
|
|
ds.TeamAgentOptionsFunc = func(ctx context.Context, id uint) (*json.RawMessage, error) {
|
|
|
|
return ptr.RawMessage(json.RawMessage(`{"config":{"foo":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override"}}}}`)), nil
|
|
|
|
}
|
2021-05-12 01:15:16 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
host := &fleet.Host{
|
2021-05-25 22:46:46 +00:00
|
|
|
TeamID: &teamID,
|
2021-05-12 01:15:16 +00:00
|
|
|
Platform: "darwin",
|
|
|
|
}
|
|
|
|
|
2022-01-18 01:52:09 +00:00
|
|
|
opt, err := svc.AgentOptionsForHost(context.Background(), host.TeamID, host.Platform)
|
2021-05-12 01:15:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"foo":"override"}`, string(opt))
|
|
|
|
|
|
|
|
host.Platform = "windows"
|
2022-01-18 01:52:09 +00:00
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host.TeamID, host.Platform)
|
2021-05-12 01:15:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"foo":"bar"}`, string(opt))
|
|
|
|
|
|
|
|
// Should take gobal option with no team
|
2021-05-25 22:46:46 +00:00
|
|
|
host.TeamID = nil
|
2022-01-18 01:52:09 +00:00
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host.TeamID, host.Platform)
|
2021-05-12 01:15:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"baz":"bar"}`, string(opt))
|
|
|
|
|
|
|
|
host.Platform = "darwin"
|
2022-01-18 01:52:09 +00:00
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host.TeamID, host.Platform)
|
2021-05-12 01:15:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"foo":"override2"}`, string(opt))
|
|
|
|
}
|