From 0216434dcd1f1327c2d93cd9f62020cafff13487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 5 Mar 2024 16:13:32 +0100 Subject: [PATCH] test(install-script): refactor tests in prep for Windows --- pkg/scripts_test/Makefile | 1 + pkg/scripts_test/check.go | 53 ----------- pkg/scripts_test/check_unix.go | 64 +++++++++++++ .../{command.go => command_unix.go} | 0 pkg/scripts_test/common.go | 88 ------------------ pkg/scripts_test/common_linux.go | 2 + pkg/scripts_test/common_unix.go | 91 +++++++++++++++++++ .../{consts.go => consts_unix.go} | 2 + 8 files changed, 160 insertions(+), 141 deletions(-) create mode 100644 pkg/scripts_test/check_unix.go rename pkg/scripts_test/{command.go => command_unix.go} (100%) create mode 100644 pkg/scripts_test/common_unix.go rename pkg/scripts_test/{consts.go => consts_unix.go} (97%) diff --git a/pkg/scripts_test/Makefile b/pkg/scripts_test/Makefile index aecfc08869..4586b9c7dd 100644 --- a/pkg/scripts_test/Makefile +++ b/pkg/scripts_test/Makefile @@ -4,6 +4,7 @@ ifneq ($(OS),windows) GOTESTPREFIX ?= sudo env PATH="${PATH}" endif +GOTEST=go test GOTESTBINARY=sumologic_scripts_tests.test # We build the test binary separately to avoid downloading modules as root diff --git a/pkg/scripts_test/check.go b/pkg/scripts_test/check.go index 2ea0b3e114..b580ff8da1 100644 --- a/pkg/scripts_test/check.go +++ b/pkg/scripts_test/check.go @@ -6,10 +6,7 @@ import ( "io/fs" "os" "os/exec" - "os/user" "path/filepath" - "strconv" - "syscall" "testing" "github.com/stretchr/testify/require" @@ -140,37 +137,6 @@ func checkAbortedDueToDifferentToken(c check) { require.Contains(c.test, c.output[len(c.output)-1], "You are trying to install with different token than in your configuration file!") } -func checkAbortedDueToNoToken(c check) { - require.Greater(c.test, len(c.output), 1) - require.Contains(c.test, c.output[len(c.output)-2], "Installation token has not been provided. Please set the 'SUMOLOGIC_INSTALLATION_TOKEN' environment variable.") - require.Contains(c.test, c.output[len(c.output)-1], "You can ignore this requirement by adding '--skip-installation-token argument.") -} - -func preActionMockConfig(c check) { - err := os.MkdirAll(etcPath, fs.FileMode(etcPathPermissions)) - require.NoError(c.test, err) - - f, err := os.Create(configPath) - require.NoError(c.test, err) - - err = f.Chmod(fs.FileMode(configPathFilePermissions)) - require.NoError(c.test, err) -} - -func preActionMockUserConfig(c check) { - err := os.MkdirAll(etcPath, fs.FileMode(etcPathPermissions)) - require.NoError(c.test, err) - - err = os.MkdirAll(confDPath, fs.FileMode(configPathDirPermissions)) - require.NoError(c.test, err) - - f, err := os.Create(userConfigPath) - require.NoError(c.test, err) - - err = f.Chmod(fs.FileMode(commonConfigPathFilePermissions)) - require.NoError(c.test, err) -} - func preActionWriteAPIBaseURLToUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) @@ -242,25 +208,6 @@ func PathHasPermissions(t *testing.T, path string, perms uint32) { require.Equal(t, expected, got, "%s should have %o permissions but has %o", path, expected, got) } -func PathHasOwner(t *testing.T, path string, ownerName string, groupName string) { - info, err := os.Stat(path) - require.NoError(t, err) - - // get the owning user and group - stat := info.Sys().(*syscall.Stat_t) - uid := strconv.FormatUint(uint64(stat.Uid), 10) - gid := strconv.FormatUint(uint64(stat.Gid), 10) - - usr, err := user.LookupId(uid) - require.NoError(t, err) - - group, err := user.LookupGroupId(gid) - require.NoError(t, err) - - require.Equal(t, ownerName, usr.Username, "%s should be owned by user '%s'", path, ownerName) - require.Equal(t, groupName, group.Name, "%s should be owned by group '%s'", path, groupName) -} - func PathHasUserACL(t *testing.T, path string, ownerName string, perms string) { cmd := exec.Command("/usr/bin/getfacl", path) diff --git a/pkg/scripts_test/check_unix.go b/pkg/scripts_test/check_unix.go new file mode 100644 index 0000000000..71e3fad7cd --- /dev/null +++ b/pkg/scripts_test/check_unix.go @@ -0,0 +1,64 @@ +//go:build linux || darwin + +package sumologic_scripts_tests + +import ( + "io/fs" + "os" + "os/user" + "strconv" + "syscall" + "testing" + + "github.com/stretchr/testify/require" +) + +func checkAbortedDueToNoToken(c check) { + require.Greater(c.test, len(c.output), 1) + require.Contains(c.test, c.output[len(c.output)-2], "Installation token has not been provided. Please set the 'SUMOLOGIC_INSTALLATION_TOKEN' environment variable.") + require.Contains(c.test, c.output[len(c.output)-1], "You can ignore this requirement by adding '--skip-installation-token argument.") +} + +func preActionMockConfig(c check) { + err := os.MkdirAll(etcPath, fs.FileMode(etcPathPermissions)) + require.NoError(c.test, err) + + f, err := os.Create(configPath) + require.NoError(c.test, err) + + err = f.Chmod(fs.FileMode(configPathFilePermissions)) + require.NoError(c.test, err) +} + +func preActionMockUserConfig(c check) { + err := os.MkdirAll(etcPath, fs.FileMode(etcPathPermissions)) + require.NoError(c.test, err) + + err = os.MkdirAll(confDPath, fs.FileMode(configPathDirPermissions)) + require.NoError(c.test, err) + + f, err := os.Create(userConfigPath) + require.NoError(c.test, err) + + err = f.Chmod(fs.FileMode(commonConfigPathFilePermissions)) + require.NoError(c.test, err) +} + +func PathHasOwner(t *testing.T, path string, ownerName string, groupName string) { + info, err := os.Stat(path) + require.NoError(t, err) + + // get the owning user and group + stat := info.Sys().(*syscall.Stat_t) + uid := strconv.FormatUint(uint64(stat.Uid), 10) + gid := strconv.FormatUint(uint64(stat.Gid), 10) + + usr, err := user.LookupId(uid) + require.NoError(t, err) + + group, err := user.LookupGroupId(gid) + require.NoError(t, err) + + require.Equal(t, ownerName, usr.Username, "%s should be owned by user '%s'", path, ownerName) + require.Equal(t, groupName, group.Name, "%s should be owned by group '%s'", path, groupName) +} diff --git a/pkg/scripts_test/command.go b/pkg/scripts_test/command_unix.go similarity index 100% rename from pkg/scripts_test/command.go rename to pkg/scripts_test/command_unix.go diff --git a/pkg/scripts_test/common.go b/pkg/scripts_test/common.go index cd76525014..e805ffd719 100644 --- a/pkg/scripts_test/common.go +++ b/pkg/scripts_test/common.go @@ -2,17 +2,6 @@ package sumologic_scripts_tests -import ( - "context" - "io" - "net" - "net/http" - "os" - "testing" - - "github.com/stretchr/testify/require" -) - type testSpec struct { name string options installOptions @@ -22,80 +11,3 @@ type testSpec struct { conditionalChecks []condCheckFunc installCode int } - -// These checks always have to be true after a script execution -var commonPostChecks = []checkFunc{checkNoBakFilesPresent} - -func cleanCache(t *testing.T) { - err := os.RemoveAll(cacheDirectory) - require.NoError(t, err) -} - -func runTest(t *testing.T, spec *testSpec) { - ch := check{ - test: t, - installOptions: spec.options, - expectedInstallCode: spec.installCode, - } - - t.Log("Running conditional checks") - for _, a := range spec.conditionalChecks { - if !a(ch) { - t.SkipNow() - } - } - - defer tearDown(t) - - t.Log("Starting HTTP server") - mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - _, err := io.WriteString(w, "200 OK\n") - require.NoError(t, err) - }) - - listener, err := net.Listen("tcp", ":3333") - require.NoError(t, err) - - httpServer := &http.Server{ - Handler: mux, - } - go func() { - err := httpServer.Serve(listener) - if err != nil && err != http.ErrServerClosed { - require.NoError(t, err) - } - }() - defer func() { - require.NoError(t, httpServer.Shutdown(context.Background())) - }() - - t.Log("Running pre actions") - for _, a := range spec.preActions { - a(ch) - } - - t.Log("Running pre checks") - for _, c := range spec.preChecks { - c(ch) - } - - ch.code, ch.output, ch.errorOutput, ch.err = runScript(ch) - - // Remove cache in case of curl issue - if ch.code == curlTimeoutErrorCode { - cleanCache(t) - } - - checkRun(ch) - - t.Log("Running common post checks") - for _, c := range commonPostChecks { - c(ch) - } - - t.Log("Running post checks") - for _, c := range spec.postChecks { - c(ch) - } -} diff --git a/pkg/scripts_test/common_linux.go b/pkg/scripts_test/common_linux.go index 92e54a9c10..60e3e101ef 100644 --- a/pkg/scripts_test/common_linux.go +++ b/pkg/scripts_test/common_linux.go @@ -1,3 +1,5 @@ +// go: + package sumologic_scripts_tests import ( diff --git a/pkg/scripts_test/common_unix.go b/pkg/scripts_test/common_unix.go new file mode 100644 index 0000000000..513aaf0cdc --- /dev/null +++ b/pkg/scripts_test/common_unix.go @@ -0,0 +1,91 @@ +//go:build linux || darwin + +package sumologic_scripts_tests + +import ( + "context" + "io" + "net" + "net/http" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +// These checks always have to be true after a script execution +var commonPostChecks = []checkFunc{checkNoBakFilesPresent} + +func cleanCache(t *testing.T) { + err := os.RemoveAll(cacheDirectory) + require.NoError(t, err) +} + +func runTest(t *testing.T, spec *testSpec) { + ch := check{ + test: t, + installOptions: spec.options, + expectedInstallCode: spec.installCode, + } + + t.Log("Running conditional checks") + for _, a := range spec.conditionalChecks { + if !a(ch) { + t.SkipNow() + } + } + + defer tearDown(t) + + t.Log("Starting HTTP server") + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, err := io.WriteString(w, "200 OK\n") + require.NoError(t, err) + }) + + listener, err := net.Listen("tcp", ":3333") + require.NoError(t, err) + + httpServer := &http.Server{ + Handler: mux, + } + go func() { + err := httpServer.Serve(listener) + if err != nil && err != http.ErrServerClosed { + require.NoError(t, err) + } + }() + defer func() { + require.NoError(t, httpServer.Shutdown(context.Background())) + }() + + t.Log("Running pre actions") + for _, a := range spec.preActions { + a(ch) + } + + t.Log("Running pre checks") + for _, c := range spec.preChecks { + c(ch) + } + + ch.code, ch.output, ch.errorOutput, ch.err = runScript(ch) + + // Remove cache in case of curl issue + if ch.code == curlTimeoutErrorCode { + cleanCache(t) + } + + checkRun(ch) + + t.Log("Running common post checks") + for _, c := range commonPostChecks { + c(ch) + } + + t.Log("Running post checks") + for _, c := range spec.postChecks { + c(ch) + } +} diff --git a/pkg/scripts_test/consts.go b/pkg/scripts_test/consts_unix.go similarity index 97% rename from pkg/scripts_test/consts.go rename to pkg/scripts_test/consts_unix.go index cabbf9ce23..55c0f88eb2 100644 --- a/pkg/scripts_test/consts.go +++ b/pkg/scripts_test/consts_unix.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package sumologic_scripts_tests const (