fleet/orbit/pkg/update/runner_test.go
Victor Lyuboslavsky b011418b71
Added initial randomization to update checker to prevent all agents updating at once. (#15583)
Added initial randomization to update checker to prevent all agents
updating at once.
#15476 

# Checklist for submitter
- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Sharon Katz <121527325+sharon-fdm@users.noreply.github.com>
2023-12-14 13:47:30 -06:00

61 lines
1.6 KiB
Go

package update
import (
"testing"
"time"
"github.com/fleetdm/fleet/v4/pkg/nettest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewRunner(t *testing.T) {
// TODO(lucas): Do not use our TUF remote repository
// but instead create local repository and serve with a httptest server.
// For that, we need to move and export some functionality currently in
// "ee/fleetctl/updates.go" (as it doesn't make sense to have such functionality
// there and import such eefleetctl package here).
nettest.Run(t)
rootDir := t.TempDir()
updateOpts := DefaultOptions
updateOpts.RootDirectory = rootDir
u, err := NewUpdater(updateOpts)
require.NoError(t, err)
err = u.UpdateMetadata()
require.NoError(t, err)
runnerOpts := RunnerOptions{
CheckInterval: 1 * time.Second,
Targets: []string{"osqueryd"},
}
// NewRunner should not fail if targets do not exist locally.
r, err := NewRunner(u, runnerOpts)
require.NoError(t, err)
execPath, err := u.ExecutableLocalPath("osqueryd")
require.NoError(t, err)
require.NoFileExists(t, execPath)
// r.UpdateAction should download osqueryd.
didUpdate, err := r.UpdateAction()
require.NoError(t, err)
require.True(t, didUpdate)
require.FileExists(t, execPath)
// Create another Runner but with the target already existing.
r2, err := NewRunner(u, runnerOpts)
require.NoError(t, err)
didUpdate, err = r2.UpdateAction()
require.NoError(t, err)
require.False(t, didUpdate)
}
func TestRandomizeDuration(t *testing.T) {
rand, err := randomizeDuration(10 * time.Minute)
require.NoError(t, err)
assert.True(t, rand >= 0)
assert.True(t, rand < 10*time.Minute)
}