mirror of
https://github.com/fleetdm/fleet
synced 2026-05-18 14:38:53 +00:00
Copy the DefaultOptions in order to prevent a data race on the Targets
map. This race should only have effected testing.
Race detector output:
```
WARNING: DATA RACE
Read at 0x00c0000908d0 by goroutine 15:
runtime.mapaccess1_faststr()
/opt/hostedtoolcache/go/1.18.0/x64/src/runtime/map_faststr.go:13 +0x0
github.com/fleetdm/fleet/v4/orbit/pkg/update.TestMakeRepoPath.func1()
/home/runner/work/fleet/fleet/orbit/pkg/update/update_test.go:58 +0xb6
testing.tRunner()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1439 +0x213
testing.(*T).Run.func1()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486 +0x47
Previous write at 0x00c0000908d0 by goroutine 12:
runtime.mapassign_faststr()
/opt/hostedtoolcache/go/1.18.0/x64/src/runtime/map_faststr.go:203 +0x0
github.com/fleetdm/fleet/v4/orbit/pkg/update.TestMakeRepoPath.func1()
/home/runner/work/fleet/fleet/orbit/pkg/update/update_test.go:62 +0x1cb
testing.tRunner()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1439 +0x213
testing.(*T).Run.func1()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486 +0x47
Goroutine 15 (running) created at:
testing.(*T).Run()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486 +0x724
github.com/fleetdm/fleet/v4/orbit/pkg/update.TestMakeRepoPath()
/home/runner/work/fleet/fleet/orbit/pkg/update/update_test.go:53 +0x1a4
testing.tRunner()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1439 +0x213
testing.(*T).Run.func1()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486 +0x47
Goroutine 12 (running) created at:
testing.(*T).Run()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486 +0x724
github.com/fleetdm/fleet/v4/orbit/pkg/update.TestMakeRepoPath()
/home/runner/work/fleet/fleet/orbit/pkg/update/update_test.go:53 +0x1a4
testing.tRunner()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1439 +0x213
testing.(*T).Run.func1()
/opt/hostedtoolcache/go/1.18.0/x64/src/testing/testing.go:1486
+0x47
```
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package update
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
|
|
"github.com/jinzhu/copier"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestInitializeDirectories(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmpDir := t.TempDir()
|
|
require.NoError(t, os.Chmod(tmpDir, constant.DefaultDirMode))
|
|
|
|
opt := DefaultOptions
|
|
opt.RootDirectory = tmpDir
|
|
updater := Updater{opt: opt}
|
|
err := updater.initializeDirectories()
|
|
require.NoError(t, err)
|
|
assertDir(t, filepath.Join(tmpDir, binDir))
|
|
}
|
|
|
|
func assertDir(t *testing.T, path string) {
|
|
info, err := os.Stat(path)
|
|
assert.NoError(t, err, "stat should succeed")
|
|
assert.True(t, info.IsDir())
|
|
}
|
|
|
|
func TestMakeRepoPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
version string
|
|
platform string
|
|
expected string
|
|
}{
|
|
{platform: "linux", name: "osqueryd", version: "4.6.0", expected: "osqueryd/linux/4.6.0/osqueryd"},
|
|
{platform: "linux", name: "osqueryd", version: "3.3.2", expected: "osqueryd/linux/3.3.2/osqueryd"},
|
|
{platform: "macos", name: "osqueryd", version: "4.6.0", expected: "osqueryd/macos/4.6.0/osqueryd"},
|
|
{platform: "macos", name: "osqueryd", version: "3.3.2", expected: "osqueryd/macos/3.3.2/osqueryd"},
|
|
{platform: "macos-app", name: "osqueryd", version: "3.3.2", expected: "osqueryd/macos-app/3.3.2/osqueryd.app.tar.gz"},
|
|
{platform: "windows", name: "osqueryd", version: "4.6.0", expected: "osqueryd/windows/4.6.0/osqueryd.exe"},
|
|
{platform: "windows", name: "osqueryd", version: "3.3.2", expected: "osqueryd/windows/3.3.2/osqueryd.exe"},
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
tt := tt
|
|
t.Run(tt.expected, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var opt Options
|
|
// Must deep copy DefaultOptions, otherwise there is a race condition when modifying the
|
|
// opt.Targets map in parallel tests below.
|
|
err := copier.CopyWithOption(&opt, DefaultOptions, copier.Option{DeepCopy: true})
|
|
require.NoError(t, err)
|
|
|
|
osqueryd := opt.Targets[tt.name]
|
|
osqueryd.Platform = tt.platform
|
|
osqueryd.Channel = tt.version
|
|
osqueryd.TargetFile = filepath.Base(tt.expected)
|
|
opt.Targets[tt.name] = osqueryd
|
|
|
|
u := Updater{opt: opt}
|
|
repoPath, err := u.repoPath(tt.name)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.expected, repoPath)
|
|
})
|
|
}
|
|
}
|