From 0443bd16012a66e18d606ca39b264f59c365b643 Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Mon, 19 Jun 2023 11:33:08 -0700 Subject: [PATCH] chore(pkg/scripts_test): clean up Signed-off-by: Justin Kolberg --- pkg/scripts_test/check.go | 108 +-------------- pkg/scripts_test/check_darwin.go | 24 ++-- pkg/scripts_test/check_linux.go | 90 ++++++++++++- pkg/scripts_test/common.go | 171 ------------------------ pkg/scripts_test/common_darwin.go | 116 +++++++++++++++- pkg/scripts_test/common_linux.go | 21 +++ pkg/scripts_test/consts.go | 41 +++--- pkg/scripts_test/consts_darwin.go | 11 +- pkg/scripts_test/consts_linux.go | 10 ++ pkg/scripts_test/install_darwin_test.go | 36 +++-- pkg/scripts_test/install_unix_test.go | 14 +- 11 files changed, 312 insertions(+), 330 deletions(-) create mode 100644 pkg/scripts_test/common_linux.go create mode 100644 pkg/scripts_test/consts_linux.go diff --git a/pkg/scripts_test/check.go b/pkg/scripts_test/check.go index 8a2fc30d9d..d364f893c0 100644 --- a/pkg/scripts_test/check.go +++ b/pkg/scripts_test/check.go @@ -15,8 +15,6 @@ import ( "syscall" "testing" - "github.com/joho/godotenv" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -36,14 +34,6 @@ func checkSkipTest(c check) bool { return false } -func checkSystemdAvailability(c check) bool { - return assert.DirExists(&testing.T{}, systemdDirectoryPath, "systemd is not supported") -} - -func checkACLAvailability(c check) bool { - return assert.FileExists(&testing.T{}, "/usr/bin/getfacl", "File ACLS is not supported") -} - type checkFunc func(check) func checkBinaryCreated(c check) { @@ -69,7 +59,7 @@ func checkRun(c check) { } func checkConfigDirectoryOwnershipAndPermissions(c check) { - PathHasOwner(c.test, etcPath, "root", getRootGroupName()) + PathHasOwner(c.test, etcPath, "root", rootGroup) // TODO: fix mismatch between package permissions & expected permissions if runtime.GOOS == "darwin" { PathHasPermissions(c.test, etcPath, 0755) @@ -125,10 +115,6 @@ func checkConfigFilesOwnershipAndPermissions(ownerName string, ownerGroup string } } -func checkConfigPathOwnership(c check) { - PathHasOwner(c.test, configPath, getSystemUser(), getSystemUser()) -} - func checkConfigNotCreated(c check) { require.NoFileExists(c.test, configPath, "configuration has been created") } @@ -198,32 +184,6 @@ func checkDifferentTokenInConfig(c check) { require.Equal(c.test, "different"+c.installOptions.installToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") } -func checkTokenInEnvFile(c check) { - require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") - - envs, err := godotenv.Read(tokenEnvFilePath) - - require.NoError(c.test, err) - if _, ok := envs["SUMOLOGIC_INSTALL_TOKEN"]; ok { - require.Equal(c.test, c.installOptions.installToken, envs["SUMOLOGIC_INSTALL_TOKEN"], "installation token is different than expected") - } else { - require.Equal(c.test, c.installOptions.installToken, envs["SUMOLOGIC_INSTALLATION_TOKEN"], "installation token is different than expected") - } -} - -func checkDifferentTokenInEnvFile(c check) { - require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") - - envs, err := godotenv.Read(tokenEnvFilePath) - - require.NoError(c.test, err) - if _, ok := envs["SUMOLOGIC_INSTALL_TOKEN"]; ok { - require.Equal(c.test, "different"+c.installOptions.installToken, envs["SUMOLOGIC_INSTALL_TOKEN"], "installation token is different than expected") - } else { - require.Equal(c.test, "different"+c.installOptions.installToken, envs["SUMOLOGIC_INSTALLATION_TOKEN"], "installation token is different than expected") - } -} - func checkHostmetricsConfigCreated(c check) { require.FileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has not been created properly") } @@ -239,22 +199,6 @@ func checkHostmetricsConfigNotCreated(c check) { require.NoFileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has been created") } -func checkSystemdConfigCreated(c check) { - require.FileExists(c.test, systemdPath, "systemd configuration has not been created properly") -} - -func checkSystemdConfigNotCreated(c check) { - require.NoFileExists(c.test, systemdPath, "systemd configuration has been created") -} - -func checkSystemdEnvDirExists(c check) { - require.DirExists(c.test, etcPath+"/env", "systemd env directory does not exist") -} - -func checkSystemdEnvDirPermissions(c check) { - PathHasPermissions(c.test, etcPath+"/env", configPathDirPermissions) -} - func checkTags(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err, "error while reading configuration") @@ -319,13 +263,6 @@ func preActionMockUserConfig(c check) { require.NoError(c.test, err) } -func preActionMockSystemdStructure(c check) { - preActionMockStructure(c) - - _, err := os.Create(systemdPath) - require.NoError(c.test, err) -} - func preActionCreateHomeDirectory(c check) { err := os.MkdirAll(libPath, fs.FileMode(etcPathPermissions)) require.NoError(c.test, err) @@ -466,14 +403,14 @@ func checkAbortedDueToDifferentTags(c check) { func preActionCreateUser(c check) { preActionMockUserConfig(c) - cmd := exec.Command("useradd", getSystemUser()) + cmd := exec.Command("useradd", systemUser) _, err := cmd.CombinedOutput() require.NoError(c.test, err) f, err := os.Open(configPath) require.NoError(c.test, err) - user, err := user.Lookup(getSystemUser()) + user, err := user.Lookup(systemUser) require.NoError(c.test, err) uid, err := strconv.Atoi(user.Uid) @@ -486,45 +423,6 @@ func preActionCreateUser(c check) { require.NoError(c.test, err) } -func checkUserExists(c check) { - username := getSystemUser() - - if runtime.GOOS == "darwin" { - exists := dsclKeyExistsForPath(c.test, "/Users", username) - require.True(c.test, exists, "user has not been created") - } else { - _, err := user.Lookup(username) - require.NoError(c.test, err, "user has not been created") - } - - checkConfigPathOwnership(c) -} - -func checkUserNotExists(c check) { - username := getSystemUser() - - if runtime.GOOS == "darwin" { - exists := dsclKeyExistsForPath(c.test, "/Users", username) - require.False(c.test, exists, "user has been created") - } else { - _, err := user.Lookup(username) - require.Error(c.test, err, "user has been created") - } -} - -func checkVarLogACL(c check) { - if !checkACLAvailability(c) { - return - } - - PathHasUserACL(c.test, "/var/log", getSystemUser(), "r-x") -} - -func checkUninstallationOutput(c check) { - require.Greater(c.test, len(c.output), 1) - require.Contains(c.test, c.output[len(c.output)-1], "Uninstallation completed") -} - func PathHasPermissions(t *testing.T, path string, perms uint32) { info, err := os.Stat(path) require.NoError(t, err) diff --git a/pkg/scripts_test/check_darwin.go b/pkg/scripts_test/check_darwin.go index 26d25cb081..7d94ad087a 100644 --- a/pkg/scripts_test/check_darwin.go +++ b/pkg/scripts_test/check_darwin.go @@ -9,6 +9,10 @@ import ( "github.com/stretchr/testify/require" ) +func checkConfigPathOwnership(c check) { + PathHasOwner(c.test, configPath, systemUser, systemUser) +} + func checkDifferentTokenInLaunchdConfig(c check) { require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") @@ -19,18 +23,12 @@ func checkDifferentTokenInLaunchdConfig(c check) { } func checkGroupExists(c check) { - group := getSystemGroup() - - exists := dsclKeyExistsForPath(c.test, "/Groups", group) + exists := dsclKeyExistsForPath(c.test, "/Groups", systemGroup) require.True(c.test, exists, "group has not been created") - - checkConfigPathOwnership(c) } func checkGroupNotExists(c check) { - group := getSystemGroup() - - exists := dsclKeyExistsForPath(c.test, "/Groups", group) + exists := dsclKeyExistsForPath(c.test, "/Groups", systemGroup) require.False(c.test, exists, "group has been created") } @@ -67,6 +65,16 @@ func checkTokenInLaunchdConfig(c check) { require.Equal(c.test, c.installOptions.installToken, conf.EnvironmentVariables.InstallationToken, "installation token is different than expected") } +func checkUserExists(c check) { + exists := dsclKeyExistsForPath(c.test, "/Users", systemUser) + require.True(c.test, exists, "user has not been created") +} + +func checkUserNotExists(c check) { + exists := dsclKeyExistsForPath(c.test, "/Users", systemUser) + require.False(c.test, exists, "user has been created") +} + func preActionMockLaunchdConfig(c check) { f, err := os.Create(launchdPath) require.NoError(c.test, err) diff --git a/pkg/scripts_test/check_linux.go b/pkg/scripts_test/check_linux.go index c20a1bc7c8..718eb9dca7 100644 --- a/pkg/scripts_test/check_linux.go +++ b/pkg/scripts_test/check_linux.go @@ -1,6 +1,51 @@ package sumologic_scripts_tests -import "github.com/stretchr/testify/require" +import ( + "os" + "os/user" + "testing" + + "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func checkACLAvailability(c check) bool { + return assert.FileExists(&testing.T{}, "/usr/bin/getfacl", "File ACLS is not supported") +} + +func checkDifferentTokenInEnvFile(c check) { + require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") + + envs, err := godotenv.Read(tokenEnvFilePath) + + require.NoError(c.test, err) + if _, ok := envs["SUMOLOGIC_INSTALL_TOKEN"]; ok { + require.Equal(c.test, "different"+c.installOptions.installToken, envs["SUMOLOGIC_INSTALL_TOKEN"], "installation token is different than expected") + } else { + require.Equal(c.test, "different"+c.installOptions.installToken, envs["SUMOLOGIC_INSTALLATION_TOKEN"], "installation token is different than expected") + } +} + +func checkSystemdAvailability(c check) bool { + return assert.DirExists(&testing.T{}, systemdDirectoryPath, "systemd is not supported") +} + +func checkSystemdConfigCreated(c check) { + require.FileExists(c.test, systemdPath, "systemd configuration has not been created properly") +} + +func checkSystemdConfigNotCreated(c check) { + require.NoFileExists(c.test, systemdPath, "systemd configuration has been created") +} + +func checkSystemdEnvDirExists(c check) { + require.DirExists(c.test, etcPath+"/env", "systemd env directory does not exist") +} + +func checkSystemdEnvDirPermissions(c check) { + PathHasPermissions(c.test, etcPath+"/env", configPathDirPermissions) +} func checkTokenEnvFileCreated(c check) { require.FileExists(c.test, tokenEnvFilePath, "env token file has not been created") @@ -9,3 +54,46 @@ func checkTokenEnvFileCreated(c check) { func checkTokenEnvFileNotCreated(c check) { require.NoFileExists(c.test, tokenEnvFilePath, "env token file not been created") } + +func checkTokenInEnvFile(c check) { + require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") + + envs, err := godotenv.Read(tokenEnvFilePath) + + require.NoError(c.test, err) + if _, ok := envs["SUMOLOGIC_INSTALL_TOKEN"]; ok { + require.Equal(c.test, c.installOptions.installToken, envs["SUMOLOGIC_INSTALL_TOKEN"], "installation token is different than expected") + } else { + require.Equal(c.test, c.installOptions.installToken, envs["SUMOLOGIC_INSTALLATION_TOKEN"], "installation token is different than expected") + } +} + +func checkUninstallationOutput(c check) { + require.Greater(c.test, len(c.output), 1) + require.Contains(c.test, c.output[len(c.output)-1], "Uninstallation completed") +} + +func checkUserExists(c check) { + _, err := user.Lookup(systemUser) + require.NoError(c.test, err, "user has not been created") +} + +func checkUserNotExists(c check) { + _, err := user.Lookup(systemUser) + require.Error(c.test, err, "user has been created") +} + +func checkVarLogACL(c check) { + if !checkACLAvailability(c) { + return + } + + PathHasUserACL(c.test, "/var/log", systemUser, "r-x") +} + +func preActionMockSystemdStructure(c check) { + preActionMockStructure(c) + + _, err := os.Create(systemdPath) + require.NoError(c.test, err) +} diff --git a/pkg/scripts_test/common.go b/pkg/scripts_test/common.go index a331549e4a..b086ed05f6 100644 --- a/pkg/scripts_test/common.go +++ b/pkg/scripts_test/common.go @@ -3,12 +3,7 @@ package sumologic_scripts_tests import ( - "bufio" - "fmt" - "io" "os" - "os/exec" - "runtime" "testing" "github.com/stretchr/testify/require" @@ -27,95 +22,6 @@ type testSpec struct { // These checks always have to be true after a script execution var commonPostChecks = []checkFunc{checkNoBakFilesPresent} -func tearDown(t *testing.T) { - t.Log("Cleaning up") - - switch runtime.GOOS { - case "darwin": - tearDownDarwin(t) - default: - tearDownOther(t) - } -} - -func unloadLaunchdService(t *testing.T) { - info, err := os.Stat(launchdPath) - if err != nil { - return - } - - require.Falsef(t, info.IsDir(), "launchd config is not a file: %s", launchdPath) - - output, err := exec.Command("launchctl", "unload", "-w", "otelcol-sumo").Output() - require.NoErrorf(t, err, "error stopping service: %s", string(output)) - -} - -func removeFileIfExists(t *testing.T, path string) { - if _, err := os.Stat(path); err != nil { - return - } - - require.NoErrorf(t, os.Remove(path), "error removing file: %s", path) -} - -func removeDirectoryIfExists(t *testing.T, path string) { - info, err := os.Stat(path) - if err != nil { - return - } - - require.Truef(t, info.IsDir(), "path is not a directory: %s", path) - require.NoErrorf(t, os.RemoveAll(path), "error removing directory: %s", path) -} - -func tearDownDarwin(t *testing.T) { - // Stop service - unloadLaunchdService(t) - - // Remove files - removeFileIfExists(t, binaryPath) - removeFileIfExists(t, launchdPath) - - // Remove configuration & data - removeDirectoryIfExists(t, etcPath) - removeDirectoryIfExists(t, fileStoragePath) - removeDirectoryIfExists(t, logDirPath) - removeDirectoryIfExists(t, appSupportDirPath) - - // Remove user & group - systemUser := getSystemUser() - if dsclKeyExistsForPath(t, "/Users", systemUser) { - dsclDeletePath(t, fmt.Sprintf("/Users/%s", systemUser)) - } - - systemGroup := getSystemGroup() - if dsclKeyExistsForPath(t, "/Groups", systemGroup) { - dsclDeletePath(t, fmt.Sprintf("/Groups/%s", systemGroup)) - } - - if dsclKeyExistsForPath(t, "/Users", systemUser) { - panic(fmt.Sprintf("user exists after deletion: %s", systemUser)) - } - if dsclKeyExistsForPath(t, "/Groups", systemGroup) { - panic(fmt.Sprintf("group exists after deletion: %s", systemGroup)) - } -} - -func tearDownOther(t *testing.T) { - ch := check{ - test: t, - installOptions: installOptions{ - uninstall: true, - purge: true, - autoconfirm: true, - }, - } - - _, _, _, err := runScript(ch) - require.NoError(t, err) -} - func cleanCache(t *testing.T) { err := os.RemoveAll(cacheDirectory) require.NoError(t, err) @@ -166,80 +72,3 @@ func runTest(t *testing.T, spec *testSpec) { c(ch) } } - -func getSystemUser() string { - switch runtime.GOOS { - case "darwin": - return fmt.Sprintf("_%s", systemUser) - case "linux": - return systemUser - } - - panic(fmt.Sprintf("Encountered unsupported OS: %s", runtime.GOOS)) -} - -func getSystemGroup() string { - switch runtime.GOOS { - case "darwin": - return fmt.Sprintf("_%s", systemGroup) - case "linux": - return systemGroup - } - - panic(fmt.Sprintf("Encountered unsupported OS: %s", runtime.GOOS)) -} - -func getRootGroupName() string { - if runtime.GOOS == "darwin" { - return "wheel" - } else if runtime.GOOS == "linux" { - return "root" - } - - panic(fmt.Sprintf("Encountered unsupported OS: %s", runtime.GOOS)) -} - -// The user.Lookup() and user.LookupGroup() functions do not appear to work -// correctly on Darwin. The functions will still return a user or group after it -// has been deleted. There are several GitHub issues in github.com/golang/go -// that describe similar or related behaviour. To work around this issue we use -// the dscl command to determine if a user or group exists. -func dsclKeyExistsForPath(t *testing.T, path, key string) bool { - cmd := exec.Command("dscl", ".", "-list", path) - out, err := cmd.StdoutPipe() - if err != nil { - require.NoError(t, err) - } - defer out.Close() - - bufOut := bufio.NewReader(out) - - if err := cmd.Start(); err != nil { - require.NoError(t, err) - } - - for { - line, _, err := bufOut.ReadLine() - - if string(line) == key { - return true - } - - // exit if script finished - if err == io.EOF { - break - } - - // otherwise ensure there is no error - require.NoError(t, err) - } - - return false -} - -func dsclDeletePath(t *testing.T, path string) { - cmd := exec.Command("dscl", ".", "-delete", path) - output, err := cmd.CombinedOutput() - require.NoErrorf(t, err, "error while using dscl to delete path: %s, path: %s", output, path) - require.Empty(t, string(output)) -} diff --git a/pkg/scripts_test/common_darwin.go b/pkg/scripts_test/common_darwin.go index 284918d74d..5226a54a08 100644 --- a/pkg/scripts_test/common_darwin.go +++ b/pkg/scripts_test/common_darwin.go @@ -1,11 +1,125 @@ package sumologic_scripts_tests import ( + "bufio" + "fmt" + "io" "os" + "os/exec" "path" + "testing" + + "github.com/stretchr/testify/require" ) +func dsclDeletePath(t *testing.T, path string) { + cmd := exec.Command("dscl", ".", "-delete", path) + output, err := cmd.CombinedOutput() + require.NoErrorf(t, err, "error while using dscl to delete path: %s, path: %s", output, path) + require.Empty(t, string(output)) +} + +// The user.Lookup() and user.LookupGroup() functions do not appear to work +// correctly on Darwin. The functions will still return a user or group after it +// has been deleted. There are several GitHub issues in github.com/golang/go +// that describe similar or related behaviour. To work around this issue we use +// the dscl command to determine if a user or group exists. +func dsclKeyExistsForPath(t *testing.T, path, key string) bool { + cmd := exec.Command("dscl", ".", "-list", path) + out, err := cmd.StdoutPipe() + if err != nil { + require.NoError(t, err) + } + defer out.Close() + + bufOut := bufio.NewReader(out) + + if err := cmd.Start(); err != nil { + require.NoError(t, err) + } + + for { + line, _, err := bufOut.ReadLine() + + if string(line) == key { + return true + } + + // exit if script finished + if err == io.EOF { + break + } + + // otherwise ensure there is no error + require.NoError(t, err) + } + + return false +} + func getPackagePath() string { tmpDir := os.Getenv("TMPDIR") - return path.Join(tmpDir, darwinPackageName) + return path.Join(tmpDir, packageName) +} + +func removeFileIfExists(t *testing.T, path string) { + if _, err := os.Stat(path); err != nil { + return + } + + require.NoErrorf(t, os.Remove(path), "error removing file: %s", path) +} + +func removeDirectoryIfExists(t *testing.T, path string) { + info, err := os.Stat(path) + if err != nil { + return + } + + require.Truef(t, info.IsDir(), "path is not a directory: %s", path) + require.NoErrorf(t, os.RemoveAll(path), "error removing directory: %s", path) +} + +func tearDown(t *testing.T) { + // Stop service + unloadLaunchdService(t) + + // Remove files + removeFileIfExists(t, binaryPath) + removeFileIfExists(t, launchdPath) + + // Remove configuration & data + removeDirectoryIfExists(t, etcPath) + removeDirectoryIfExists(t, fileStoragePath) + removeDirectoryIfExists(t, logDirPath) + removeDirectoryIfExists(t, appSupportDirPath) + + // Remove user & group + if dsclKeyExistsForPath(t, "/Users", systemUser) { + dsclDeletePath(t, fmt.Sprintf("/Users/%s", systemUser)) + } + + if dsclKeyExistsForPath(t, "/Groups", systemGroup) { + dsclDeletePath(t, fmt.Sprintf("/Groups/%s", systemGroup)) + } + + if dsclKeyExistsForPath(t, "/Users", systemUser) { + panic(fmt.Sprintf("user exists after deletion: %s", systemUser)) + } + if dsclKeyExistsForPath(t, "/Groups", systemGroup) { + panic(fmt.Sprintf("group exists after deletion: %s", systemGroup)) + } +} + +func unloadLaunchdService(t *testing.T) { + info, err := os.Stat(launchdPath) + if err != nil { + return + } + + require.Falsef(t, info.IsDir(), "launchd config is not a file: %s", launchdPath) + + output, err := exec.Command("launchctl", "unload", "-w", "otelcol-sumo").Output() + require.NoErrorf(t, err, "error stopping service: %s", string(output)) + } diff --git a/pkg/scripts_test/common_linux.go b/pkg/scripts_test/common_linux.go new file mode 100644 index 0000000000..92e54a9c10 --- /dev/null +++ b/pkg/scripts_test/common_linux.go @@ -0,0 +1,21 @@ +package sumologic_scripts_tests + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func tearDown(t *testing.T) { + ch := check{ + test: t, + installOptions: installOptions{ + uninstall: true, + purge: true, + autoconfirm: true, + }, + } + + _, _, _, err := runScript(ch) + require.NoError(t, err) +} diff --git a/pkg/scripts_test/consts.go b/pkg/scripts_test/consts.go index 0b3760f578..f52d8fc8be 100644 --- a/pkg/scripts_test/consts.go +++ b/pkg/scripts_test/consts.go @@ -1,36 +1,27 @@ package sumologic_scripts_tests const ( - binaryPath string = "/usr/local/bin/otelcol-sumo" - libPath string = "/var/lib/otelcol-sumo" - fileStoragePath string = libPath + "/file_storage" - etcPath string = "/etc/otelcol-sumo" - etcPathPermissions uint32 = 0551 - systemdPath string = "/etc/systemd/system/otelcol-sumo.service" - launchdPath string = "/Library/LaunchDaemons/com.sumologic.otelcol-sumo.plist" - launchdPathFilePermissions uint32 = 0640 - scriptPath string = "../../scripts/install.sh" - appSupportDirPath string = "/Library/Application Support/otelcol-sumo" - configPath string = etcPath + "/sumologic.yaml" - configPathFilePermissions uint32 = 0440 - configPathDirPermissions uint32 = 0550 - confDPath string = etcPath + "/conf.d" - userConfigPath string = confDPath + "/common.yaml" - hostmetricsConfigPath string = confDPath + "/hostmetrics.yaml" - envDirectoryPath string = etcPath + "/env" - tokenEnvFilePath string = envDirectoryPath + "/token.env" - cacheDirectory string = "/var/cache/otelcol-sumo/" - logDirPath string = "/var/log/otelcol-sumo" - - systemdDirectoryPath string = "/run/systemd/system" + binaryPath string = "/usr/local/bin/otelcol-sumo" + libPath string = "/var/lib/otelcol-sumo" + fileStoragePath string = libPath + "/file_storage" + etcPath string = "/etc/otelcol-sumo" + etcPathPermissions uint32 = 0551 + scriptPath string = "../../scripts/install.sh" + configPath string = etcPath + "/sumologic.yaml" + configPathFilePermissions uint32 = 0440 + configPathDirPermissions uint32 = 0550 + confDPath string = etcPath + "/conf.d" + userConfigPath string = confDPath + "/common.yaml" + hostmetricsConfigPath string = confDPath + "/hostmetrics.yaml" + envDirectoryPath string = etcPath + "/env" + tokenEnvFilePath string = envDirectoryPath + "/token.env" + cacheDirectory string = "/var/cache/otelcol-sumo/" + logDirPath string = "/var/log/otelcol-sumo" installToken string = "token" installTokenEnv string = "SUMOLOGIC_INSTALLATION_TOKEN" deprecatedInstallTokenEnv string = "SUMOLOGIC_INSTALL_TOKEN" apiBaseURL string = "https://open-collectors.sumologic.com" - systemUser string = "otelcol-sumo" - systemGroup string = "otelcol-sumo" - curlTimeoutErrorCode int = 28 ) diff --git a/pkg/scripts_test/consts_darwin.go b/pkg/scripts_test/consts_darwin.go index feaebdb64b..ffd84ccb85 100644 --- a/pkg/scripts_test/consts_darwin.go +++ b/pkg/scripts_test/consts_darwin.go @@ -1,6 +1,13 @@ package sumologic_scripts_tests const ( - darwinPackageName string = "otelcol-sumo.pkg" - darwinUninstallScriptPath string = appSupportDirPath + "/uninstall.sh" + appSupportDirPath string = "/Library/Application Support/otelcol-sumo" + packageName string = "otelcol-sumo.pkg" + launchdPath string = "/Library/LaunchDaemons/com.sumologic.otelcol-sumo.plist" + launchdPathFilePermissions uint32 = 0640 + rootGroup string = "wheel" + uninstallScriptPath string = appSupportDirPath + "/uninstall.sh" + + systemUser string = "_otelcol-sumo" + systemGroup string = "otelcol-sumo" ) diff --git a/pkg/scripts_test/consts_linux.go b/pkg/scripts_test/consts_linux.go new file mode 100644 index 0000000000..64304191e1 --- /dev/null +++ b/pkg/scripts_test/consts_linux.go @@ -0,0 +1,10 @@ +package sumologic_scripts_tests + +const ( + rootGroup string = "root" + systemdDirectoryPath string = "/run/systemd/system" + systemdPath string = "/etc/systemd/system/otelcol-sumo.service" + + systemUser string = "otelcol-sumo" + systemGroup string = "otelcol-sumo" +) diff --git a/pkg/scripts_test/install_darwin_test.go b/pkg/scripts_test/install_darwin_test.go index 34205b1748..c83fc8ed2d 100644 --- a/pkg/scripts_test/install_darwin_test.go +++ b/pkg/scripts_test/install_darwin_test.go @@ -76,7 +76,7 @@ func TestInstallScriptDarwin(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions(getSystemUser(), getSystemGroup()), + checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), checkUserConfigCreated, checkLaunchdConfigCreated, }, @@ -91,11 +91,12 @@ func TestInstallScriptDarwin(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions(getSystemUser(), getSystemGroup()), + checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig, checkUserExists, + checkGroupExists, checkHostmetricsConfigNotCreated, }, }, @@ -110,13 +111,14 @@ func TestInstallScriptDarwin(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions(getSystemUser(), getSystemGroup()), + checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig, checkUserExists, + checkGroupExists, checkHostmetricsConfigCreated, - checkConfigFilesOwnershipAndPermissions(getSystemUser(), getSystemGroup()), + checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), }, }, { @@ -132,11 +134,12 @@ func TestInstallScriptDarwin(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions(getSystemUser(), getSystemGroup()), + checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig, checkUserExists, + checkGroupExists, }, }, { @@ -145,17 +148,30 @@ func TestInstallScriptDarwin(t *testing.T) { installToken: installToken, }, preActions: []checkFunc{preActionMockLaunchdConfig}, - preChecks: []checkFunc{checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated}, - postChecks: []checkFunc{checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig}, + preChecks: []checkFunc{ + checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, + checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + }, + postChecks: []checkFunc{ + checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, + checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig, + }, }, { name: "different installation token", options: installOptions{ installToken: installToken, }, - preActions: []checkFunc{preActionMockLaunchdConfig, preActionWriteDifferentTokenToLaunchdConfig}, - preChecks: []checkFunc{checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated}, - postChecks: []checkFunc{checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, checkAbortedDueToDifferentToken}, + preActions: []checkFunc{preActionMockLaunchdConfig, preActionWriteDifferentTokenToLaunchdConfig}, + preChecks: []checkFunc{ + checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, + checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + }, + postChecks: []checkFunc{ + checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, + checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + checkAbortedDueToDifferentToken, + }, installCode: 1, }, } { diff --git a/pkg/scripts_test/install_unix_test.go b/pkg/scripts_test/install_unix_test.go index be86d1c369..084f7cedd2 100644 --- a/pkg/scripts_test/install_unix_test.go +++ b/pkg/scripts_test/install_unix_test.go @@ -56,7 +56,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkUserConfigNotCreated, checkSystemdConfigNotCreated, }, @@ -83,7 +83,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, @@ -103,7 +103,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkUserConfigCreated, checkDeprecatedTokenInConfig, checkSystemdConfigNotCreated, @@ -123,13 +123,13 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, checkUserNotExists, checkHostmetricsConfigCreated, - checkHostmetricsOwnershipAndPermissions("root", getRootGroupName()), + checkHostmetricsOwnershipAndPermissions("root", rootGroup), }, }, { @@ -146,7 +146,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, @@ -266,7 +266,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", getRootGroupName()), + checkConfigFilesOwnershipAndPermissions("root", rootGroup), checkTags, checkSystemdConfigNotCreated, },