Files
Josh RoskosandLucas Manuel Rodriguez ba2c5b5e5c Add go_binaries table (#39877)
**Related issue:** Resolves #40138

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

Installed: 
```
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/tools/gopls@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

Validated:
```
osquery> SELECT * FROM go_packages;
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
| name          | version | module_path                       | import_path                                         | go_version | installed_path                   |
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
| goimports     | v0.42.0 | golang.org/x/tools                | golang.org/x/tools/cmd/goimports                    | go1.25.5   | /Users/josh/go/bin/goimports     |
| golangci-lint | v1.64.8 | github.com/golangci/golangci-lint | github.com/golangci/golangci-lint/cmd/golangci-lint | go1.25.5   | /Users/josh/go/bin/golangci-lint |
| gopls         | v0.21.1 | golang.org/x/tools/gopls          | golang.org/x/tools/gopls                            | go1.25.5   | /Users/josh/go/bin/gopls         |
+---------------+---------+-----------------------------------+-----------------------------------------------------+------------+----------------------------------+
```

## fleetd/orbit/Fleet Desktop

- [x] Verified compatibility with the latest released version of Fleet
(see [Must
rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md))
- [x] If the change applies to only one platform, confirmed that
`runtime.GOOS` is used as needed to isolate changes
- [x] Verified that fleetd runs on macOS, Linux and Windows

---------

Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2026-03-16 13:27:00 -05:00

78 lines
2.0 KiB
Go

package go_binaries
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadGoBinaryWithTestExecutable(t *testing.T) {
// The test binary itself is a Go binary, so this should succeed.
exe, err := os.Executable()
require.NoError(t, err)
row := readGoBinary(exe)
require.NotNil(t, row)
require.NotEmpty(t, row["go_version"])
require.Equal(t, filepath.Base(exe), row["name"])
require.Equal(t, exe, row["installed_path"])
}
func TestReadGoBinaryNonGo(t *testing.T) {
// /usr/bin/true is not a Go binary on any platform.
row := readGoBinary("/usr/bin/true")
require.Nil(t, row)
}
func TestReadGoBinaryNonExistent(t *testing.T) {
row := readGoBinary("/nonexistent/path/binary")
require.Nil(t, row)
}
func TestGenerateForDirsEmpty(t *testing.T) {
results := generateForDirs(nil)
require.Nil(t, results)
}
func TestGenerateForDirsNonExistentDir(t *testing.T) {
results := generateForDirs([]string{"/nonexistent/home/user"})
require.Nil(t, results)
}
func TestGenerateForDirsWithGoBinary(t *testing.T) {
// Get the test executable — it's a real Go binary with valid build info.
exe, err := os.Executable()
require.NoError(t, err)
// Create a temp directory structure: <tmpdir>/go/bin/
tmpHome := t.TempDir()
goBinDir := filepath.Join(tmpHome, "go", "bin")
require.NoError(t, os.MkdirAll(goBinDir, 0o755))
// Copy the test binary into the go/bin directory.
destPath := filepath.Join(goBinDir, "testbinary")
copyFile(t, exe, destPath)
results := generateForDirs([]string{tmpHome})
require.Len(t, results, 1)
require.Equal(t, "testbinary", results[0]["name"])
require.NotEmpty(t, results[0]["go_version"])
require.Equal(t, destPath, results[0]["installed_path"])
}
// copyFile copies src to dst for testing purposes.
func copyFile(t *testing.T, src, dst string) {
t.Helper()
in, err := os.Open(src)
require.NoError(t, err)
defer in.Close()
out, err := os.Create(dst)
require.NoError(t, err)
defer out.Close()
_, err = io.Copy(out, in)
require.NoError(t, err)
}