-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(install-script): refactor tests in prep for Windows
- Loading branch information
Showing
8 changed files
with
160 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// go: | ||
|
||
package sumologic_scripts_tests | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux || darwin | ||
|
||
package sumologic_scripts_tests | ||
|
||
const ( | ||
|