Skip to content

Commit

Permalink
test(install-script): refactor tests in prep for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm-sumo committed Mar 6, 2024
1 parent 683a389 commit 0216434
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 141 deletions.
1 change: 1 addition & 0 deletions pkg/scripts_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 0 additions & 53 deletions pkg/scripts_test/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"syscall"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
64 changes: 64 additions & 0 deletions pkg/scripts_test/check_unix.go
Original file line number Diff line number Diff line change
@@ -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)
}
File renamed without changes.
88 changes: 0 additions & 88 deletions pkg/scripts_test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
2 changes: 2 additions & 0 deletions pkg/scripts_test/common_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// go:

package sumologic_scripts_tests

import (
Expand Down
91 changes: 91 additions & 0 deletions pkg/scripts_test/common_unix.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux || darwin

package sumologic_scripts_tests

const (
Expand Down

0 comments on commit 0216434

Please sign in to comment.