mirror of
https://github.com/fleetdm/fleet
synced 2026-05-22 16:39:01 +00:00
* Do caching of app config per instance instead of across all of them in redis * Add changes file * Simplify code based on review comment * Use go-cache instead of creating our own * Dont export consts * Copy app config before returning it * Fix lint * Update go sum * Update go sum
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package cached_mysql
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCachedAppConfig(t *testing.T) {
|
|
mockedDS := new(mock.Store)
|
|
ds := New(mockedDS)
|
|
|
|
var appConfigSet *fleet.AppConfig
|
|
mockedDS.NewAppConfigFunc = func(ctx context.Context, info *fleet.AppConfig) (*fleet.AppConfig, error) {
|
|
appConfigSet = info
|
|
return info, nil
|
|
}
|
|
mockedDS.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
|
return appConfigSet, nil
|
|
}
|
|
mockedDS.SaveAppConfigFunc = func(ctx context.Context, info *fleet.AppConfig) error {
|
|
appConfigSet = info
|
|
return nil
|
|
}
|
|
_, err := ds.NewAppConfig(context.Background(), &fleet.AppConfig{
|
|
HostSettings: fleet.HostSettings{
|
|
AdditionalQueries: ptr.RawMessage(json.RawMessage(`"TestCachedAppConfig"`)),
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
t.Run("NewAppConfig", func(t *testing.T) {
|
|
data, err := ds.AppConfig(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, data)
|
|
assert.Equal(t, json.RawMessage(`"TestCachedAppConfig"`), *data.HostSettings.AdditionalQueries)
|
|
})
|
|
|
|
t.Run("AppConfig", func(t *testing.T) {
|
|
require.False(t, mockedDS.AppConfigFuncInvoked)
|
|
ac, err := ds.AppConfig(context.Background())
|
|
require.NoError(t, err)
|
|
require.False(t, mockedDS.AppConfigFuncInvoked)
|
|
|
|
require.Equal(t, ptr.RawMessage(json.RawMessage(`"TestCachedAppConfig"`)), ac.HostSettings.AdditionalQueries)
|
|
})
|
|
|
|
t.Run("SaveAppConfig", func(t *testing.T) {
|
|
require.NoError(t, ds.SaveAppConfig(context.Background(), &fleet.AppConfig{
|
|
HostSettings: fleet.HostSettings{
|
|
AdditionalQueries: ptr.RawMessage(json.RawMessage(`"NewSAVED"`)),
|
|
},
|
|
}))
|
|
|
|
assert.True(t, mockedDS.SaveAppConfigFuncInvoked)
|
|
|
|
ac, err := ds.AppConfig(context.Background())
|
|
require.NoError(t, err)
|
|
require.NotNil(t, ac.HostSettings.AdditionalQueries)
|
|
assert.Equal(t, json.RawMessage(`"NewSAVED"`), *ac.HostSettings.AdditionalQueries)
|
|
})
|
|
|
|
t.Run("External SaveAppConfig gets caught", func(t *testing.T) {
|
|
mockedDS.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
|
return &fleet.AppConfig{
|
|
HostSettings: fleet.HostSettings{
|
|
AdditionalQueries: ptr.RawMessage(json.RawMessage(`"SavedSomewhereElse"`)),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
ac, err := ds.AppConfig(context.Background())
|
|
require.NoError(t, err)
|
|
require.NotNil(t, ac.HostSettings.AdditionalQueries)
|
|
assert.Equal(t, json.RawMessage(`"SavedSomewhereElse"`), *ac.HostSettings.AdditionalQueries)
|
|
})
|
|
}
|