2021-05-12 01:15:16 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
|
|
|
"github.com/fleetdm/fleet/server/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgentOptionsForHost(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
|
|
|
svc, err := newTestService(ds, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
teamID := uint(1)
|
|
|
|
ds.TeamFunc = func(tid uint) (*kolide.Team, error) {
|
|
|
|
assert.Equal(t, teamID, tid)
|
|
|
|
opt := json.RawMessage(`{"config":{"foo":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override"}}}}`)
|
|
|
|
return &kolide.Team{AgentOptions: &opt}, nil
|
|
|
|
}
|
|
|
|
ds.AppConfigFunc = func() (*kolide.AppConfig, error) {
|
|
|
|
return &kolide.AppConfig{AgentOptions: json.RawMessage(`{"config":{"baz":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override2"}}}}`)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
host := &kolide.Host{
|
2021-05-25 22:46:46 +00:00
|
|
|
TeamID: &teamID,
|
2021-05-12 01:15:16 +00:00
|
|
|
Platform: "darwin",
|
|
|
|
}
|
|
|
|
|
|
|
|
opt, err := svc.AgentOptionsForHost(context.Background(), host)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"foo":"override"}`, string(opt))
|
|
|
|
|
|
|
|
host.Platform = "windows"
|
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host)
|
|
|
|
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
|
2021-05-12 01:15:16 +00:00
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"baz":"bar"}`, string(opt))
|
|
|
|
|
|
|
|
host.Platform = "darwin"
|
|
|
|
opt, err = svc.AgentOptionsForHost(context.Background(), host)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(t, `{"foo":"override2"}`, string(opt))
|
|
|
|
}
|