2025-05-09 14:26:57 +00:00
|
|
|
package preview
|
2021-10-07 13:19:10 +00:00
|
|
|
|
|
|
|
|
import (
|
2024-01-10 14:45:52 +00:00
|
|
|
"os/exec"
|
2021-10-07 13:19:10 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2025-05-09 14:26:57 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/cmd/fleetctl/fleetctl"
|
2022-05-10 14:52:33 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/pkg/nettest"
|
2021-10-07 13:19:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-11 13:57:10 +00:00
|
|
|
func TestPreviewFailsOnInvalidLicenseKey(t *testing.T) {
|
2025-05-09 14:26:57 +00:00
|
|
|
_, err := fleetctl.RunAppNoChecks([]string{"preview", "--license-key", "0xDEADBEEF"})
|
2024-12-11 13:57:10 +00:00
|
|
|
require.ErrorContains(t, err, "--license-key")
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 13:55:53 +00:00
|
|
|
func TestIntegrationsPreview(t *testing.T) {
|
2022-05-10 14:52:33 +00:00
|
|
|
nettest.Run(t)
|
2021-10-07 13:19:10 +00:00
|
|
|
|
2022-07-18 17:22:28 +00:00
|
|
|
t.Setenv("FLEET_SERVER_ADDRESS", "https://localhost:8412")
|
2025-05-09 14:26:57 +00:00
|
|
|
fleetctl.TestOverridePreviewDirectory = t.TempDir()
|
2021-10-07 13:19:10 +00:00
|
|
|
configPath := filepath.Join(t.TempDir(), "config")
|
|
|
|
|
t.Log("config path: ", configPath)
|
|
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2025-05-09 14:26:57 +00:00
|
|
|
require.Equal(t, "", fleetctl.RunAppForTest(t, []string{"preview", "--config", configPath, "stop"}))
|
2021-10-07 13:19:10 +00:00
|
|
|
})
|
|
|
|
|
|
2022-12-05 22:50:49 +00:00
|
|
|
require.NoError(t, nettest.RunWithNetRetry(t, func() error {
|
2025-07-16 14:12:32 +00:00
|
|
|
_, err := fleetctl.RunAppNoChecks([]string{
|
2024-01-10 14:45:52 +00:00
|
|
|
"preview",
|
|
|
|
|
"--config", configPath,
|
|
|
|
|
"--preview-config-path", filepath.Join(gitRootPath(t), "tools", "osquery", "in-a-box"),
|
2022-07-11 11:12:33 +00:00
|
|
|
"--tag", "main",
|
|
|
|
|
"--disable-open-browser",
|
|
|
|
|
})
|
|
|
|
|
return err
|
2022-12-05 22:50:49 +00:00
|
|
|
}))
|
2022-01-28 19:28:07 +00:00
|
|
|
|
2021-10-07 13:19:10 +00:00
|
|
|
// run some sanity checks on the preview environment
|
|
|
|
|
|
2025-07-16 14:12:32 +00:00
|
|
|
// starter library queries must have been loaded
|
2025-05-09 14:26:57 +00:00
|
|
|
queries := fleetctl.RunAppForTest(t, []string{"get", "queries", "--config", configPath, "--json"})
|
2021-10-07 13:19:10 +00:00
|
|
|
n := strings.Count(queries, `"kind":"query"`)
|
2025-07-16 14:12:32 +00:00
|
|
|
require.Greater(t, n, 0)
|
2021-10-07 13:19:10 +00:00
|
|
|
|
|
|
|
|
// app configuration must disable analytics
|
2025-05-09 14:26:57 +00:00
|
|
|
appConf := fleetctl.RunAppForTest(t, []string{"get", "config", "--include-server-config", "--config", configPath, "--yaml"})
|
2021-10-07 13:19:10 +00:00
|
|
|
ok := strings.Contains(appConf, `enable_analytics: false`)
|
|
|
|
|
require.True(t, ok, appConf)
|
|
|
|
|
|
|
|
|
|
// software inventory must be enabled
|
|
|
|
|
ok = strings.Contains(appConf, `enable_software_inventory: true`)
|
|
|
|
|
require.True(t, ok, appConf)
|
|
|
|
|
|
|
|
|
|
// current instance checks must be on
|
2021-10-12 15:49:25 +00:00
|
|
|
ok = strings.Contains(appConf, `current_instance_checks: "yes"`)
|
|
|
|
|
require.True(t, ok, appConf)
|
2021-10-07 13:19:10 +00:00
|
|
|
|
|
|
|
|
// a vulnerability database path must be set
|
|
|
|
|
ok = strings.Contains(appConf, `databases_path: /vulndb`)
|
2021-10-12 15:49:25 +00:00
|
|
|
require.True(t, ok, appConf)
|
2021-10-07 13:19:10 +00:00
|
|
|
}
|
2022-05-16 21:06:29 +00:00
|
|
|
|
2024-01-10 14:45:52 +00:00
|
|
|
func gitRootPath(t *testing.T) string {
|
|
|
|
|
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
return strings.TrimSpace(string(path))
|
|
|
|
|
}
|