From 131e444e9464bbe6a0c4284128fe7ba7e7e45105 Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Mon, 19 Jun 2023 14:20:59 -0700 Subject: [PATCH] chore(pkg/scripts_test): additional clean up Signed-off-by: Justin Kolberg --- pkg/scripts_test/check.go | 177 ++-------------- pkg/scripts_test/check_darwin.go | 4 - pkg/scripts_test/check_linux.go | 142 +++++++++++++ pkg/scripts_test/common_darwin.go | 6 - pkg/scripts_test/consts_darwin.go | 5 +- pkg/scripts_test/consts_linux.go | 5 +- pkg/scripts_test/install_darwin_test.go | 271 ++++++++++++++++++++++-- pkg/scripts_test/install_unix_test.go | 14 +- 8 files changed, 433 insertions(+), 191 deletions(-) diff --git a/pkg/scripts_test/check.go b/pkg/scripts_test/check.go index d364f893c0..f28ca97966 100644 --- a/pkg/scripts_test/check.go +++ b/pkg/scripts_test/check.go @@ -3,7 +3,6 @@ package sumologic_scripts_tests import ( - "fmt" "io/fs" "os" "os/exec" @@ -11,7 +10,6 @@ import ( "path/filepath" "runtime" "strconv" - "strings" "syscall" "testing" @@ -59,7 +57,7 @@ func checkRun(c check) { } func checkConfigDirectoryOwnershipAndPermissions(c check) { - PathHasOwner(c.test, etcPath, "root", rootGroup) + PathHasOwner(c.test, etcPath, rootUser, rootGroup) // TODO: fix mismatch between package permissions & expected permissions if runtime.GOOS == "darwin" { PathHasPermissions(c.test, etcPath, 0755) @@ -159,31 +157,6 @@ func checkNoBakFilesPresent(c check) { } } -func checkTokenInConfig(c check) { - require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") - - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err, "error while reading configuration") - - require.Equal(c.test, c.installOptions.installToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") -} - -func checkDeprecatedTokenInConfig(c check) { - require.NotEmpty(c.test, c.installOptions.deprecatedInstallToken, "installation token has not been provided") - - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err, "error while reading configuration") - - require.Equal(c.test, c.installOptions.deprecatedInstallToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") -} - -func checkDifferentTokenInConfig(c check) { - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err, "error while reading configuration") - - require.Equal(c.test, "different"+c.installOptions.installToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") -} - func checkHostmetricsConfigCreated(c check) { require.FileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has not been created properly") } @@ -215,19 +188,15 @@ func checkDifferentTags(c check) { require.Equal(c.test, "tag", conf.Extensions.Sumologic.Tags["some"], "installation token is different than expected") } -func preActionMockStructure(c check) { - preActionMockConfigs(c) - - err := os.MkdirAll(fileStoragePath, os.ModePerm) - require.NoError(c.test, err) - - _, err = os.Create(binaryPath) - require.NoError(c.test, err) +func checkAbortedDueToDifferentToken(c check) { + require.Greater(c.test, len(c.output), 0) + require.Contains(c.test, c.output[len(c.output)-1], "You are trying to install with different token than in your configuration file!") } -func preActionMockConfigs(c check) { - preActionMockConfig(c) - preActionMockUserConfig(c) +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) { @@ -241,17 +210,6 @@ func preActionMockConfig(c check) { require.NoError(c.test, err) } -func preActionMockEnvFiles(c check) { - err := os.MkdirAll(envDirectoryPath, 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(confDPath, fs.FileMode(configPathDirPermissions)) require.NoError(c.test, err) @@ -263,93 +221,48 @@ func preActionMockUserConfig(c check) { require.NoError(c.test, err) } -func preActionCreateHomeDirectory(c check) { - err := os.MkdirAll(libPath, fs.FileMode(etcPathPermissions)) - require.NoError(c.test, err) -} - -func preActionWriteTokenToUserConfig(c check) { +func preActionWriteAPIBaseURLToUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) - conf.Extensions.Sumologic.InstallToken = c.installOptions.installToken + conf.Extensions.Sumologic.APIBaseURL = c.installOptions.apiBaseURL err = saveConfig(userConfigPath, conf) require.NoError(c.test, err) } -func preActionWriteDifferentTokenToUserConfig(c check) { +func preActionWriteDifferentAPIBaseURLToUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) - conf.Extensions.Sumologic.InstallToken = "different" + c.installOptions.installToken + conf.Extensions.Sumologic.APIBaseURL = "different" + c.installOptions.apiBaseURL err = saveConfig(userConfigPath, conf) require.NoError(c.test, err) } -func preActionWriteDifferentTokenToEnvFile(c check) { - preActionMockEnvFiles(c) - - content := fmt.Sprintf("SUMOLOGIC_INSTALLATION_TOKEN=different%s", c.installOptions.installToken) - err := os.WriteFile(tokenEnvFilePath, []byte(content), fs.FileMode(etcPathPermissions)) - require.NoError(c.test, err) -} - -func preActionWriteDifferentDeprecatedTokenToEnvFile(c check) { - preActionMockEnvFiles(c) - - content := fmt.Sprintf("SUMOLOGIC_INSTALL_TOKEN=different%s", c.installOptions.installToken) - err := os.WriteFile(tokenEnvFilePath, []byte(content), fs.FileMode(etcPathPermissions)) - require.NoError(c.test, err) -} - -func checkAbortedDueToDifferentToken(c check) { - require.Greater(c.test, len(c.output), 0) - 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 checkOutputUserAddWarnings(c check) { - output := strings.Join(c.output, "\n") - require.NotContains(c.test, output, "useradd", "unexpected useradd output") - - errOutput := strings.Join(c.errorOutput, "\n") - require.NotContains(c.test, errOutput, "useradd", "unexpected useradd output") -} - -func checkDownloadTimeout(c check) { - output := strings.Join(c.errorOutput, "\n") - count := strings.Count(output, "Operation timed out after") - require.Equal(c.test, 6, count) -} - -func preActionWriteAPIBaseURLToUserConfig(c check) { +func preActionWriteDifferentTagsToUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) - conf.Extensions.Sumologic.APIBaseURL = c.installOptions.apiBaseURL + conf.Extensions.Sumologic.Tags = map[string]string{ + "some": "tag", + } err = saveConfig(userConfigPath, conf) require.NoError(c.test, err) } -func preActionWriteDifferentAPIBaseURLToUserConfig(c check) { +func preActionWriteEmptyUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) - conf.Extensions.Sumologic.APIBaseURL = "different" + c.installOptions.apiBaseURL err = saveConfig(userConfigPath, conf) require.NoError(c.test, err) } -func preActionWriteDefaultAPIBaseURLToUserConfig(c check) { +func preActionWriteTagsToUserConfig(c check) { conf, err := getConfig(userConfigPath) require.NoError(c.test, err) - conf.Extensions.Sumologic.APIBaseURL = apiBaseURL + conf.Extensions.Sumologic.Tags = c.installOptions.tags err = saveConfig(userConfigPath, conf) require.NoError(c.test, err) } @@ -366,63 +279,11 @@ func checkAPIBaseURLInConfig(c check) { require.Equal(c.test, c.installOptions.apiBaseURL, conf.Extensions.Sumologic.APIBaseURL, "api base url is different than expected") } -func preActionWriteEmptyUserConfig(c check) { - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err) - - err = saveConfig(userConfigPath, conf) - require.NoError(c.test, err) -} - -func preActionWriteTagsToUserConfig(c check) { - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err) - - conf.Extensions.Sumologic.Tags = c.installOptions.tags - err = saveConfig(userConfigPath, conf) - require.NoError(c.test, err) -} - -func preActionWriteDifferentTagsToUserConfig(c check) { - conf, err := getConfig(userConfigPath) - require.NoError(c.test, err) - - conf.Extensions.Sumologic.Tags = map[string]string{ - "some": "tag", - } - err = saveConfig(userConfigPath, conf) - require.NoError(c.test, err) -} - func checkAbortedDueToDifferentTags(c check) { require.Greater(c.test, len(c.output), 0) require.Contains(c.test, c.output[len(c.output)-1], "You are trying to install with different tags than in your configuration file!") } -// preActionCreateUser creates the system user and then set it as owner of configPath -func preActionCreateUser(c check) { - preActionMockUserConfig(c) - - 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(systemUser) - require.NoError(c.test, err) - - uid, err := strconv.Atoi(user.Uid) - require.NoError(c.test, err) - - gid, err := strconv.Atoi(user.Gid) - require.NoError(c.test, err) - - err = f.Chown(uid, gid) - require.NoError(c.test, err) -} - 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 7d94ad087a..d1769898f2 100644 --- a/pkg/scripts_test/check_darwin.go +++ b/pkg/scripts_test/check_darwin.go @@ -9,10 +9,6 @@ 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") diff --git a/pkg/scripts_test/check_linux.go b/pkg/scripts_test/check_linux.go index 718eb9dca7..e65d363fd7 100644 --- a/pkg/scripts_test/check_linux.go +++ b/pkg/scripts_test/check_linux.go @@ -1,8 +1,13 @@ package sumologic_scripts_tests import ( + "fmt" + "io/fs" "os" + "os/exec" "os/user" + "strconv" + "strings" "testing" "github.com/joho/godotenv" @@ -14,6 +19,22 @@ func checkACLAvailability(c check) bool { return assert.FileExists(&testing.T{}, "/usr/bin/getfacl", "File ACLS is not supported") } +func checkDeprecatedTokenInConfig(c check) { + require.NotEmpty(c.test, c.installOptions.deprecatedInstallToken, "installation token has not been provided") + + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err, "error while reading configuration") + + require.Equal(c.test, c.installOptions.deprecatedInstallToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") +} + +func checkDifferentTokenInConfig(c check) { + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err, "error while reading configuration") + + require.Equal(c.test, "different"+c.installOptions.installToken, conf.Extensions.Sumologic.InstallToken, "installation token is different than expected") +} + func checkDifferentTokenInEnvFile(c check) { require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") @@ -27,6 +48,20 @@ func checkDifferentTokenInEnvFile(c check) { } } +func checkDownloadTimeout(c check) { + output := strings.Join(c.errorOutput, "\n") + count := strings.Count(output, "Operation timed out after") + require.Equal(c.test, 6, count) +} + +func checkOutputUserAddWarnings(c check) { + output := strings.Join(c.output, "\n") + require.NotContains(c.test, output, "useradd", "unexpected useradd output") + + errOutput := strings.Join(c.errorOutput, "\n") + require.NotContains(c.test, errOutput, "useradd", "unexpected useradd output") +} + func checkSystemdAvailability(c check) bool { return assert.DirExists(&testing.T{}, systemdDirectoryPath, "systemd is not supported") } @@ -55,6 +90,15 @@ func checkTokenEnvFileNotCreated(c check) { require.NoFileExists(c.test, tokenEnvFilePath, "env token file not been created") } +func checkTokenInConfig(c check) { + require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided") + + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err, "error while reading configuration") + + require.Equal(c.test, 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") @@ -91,9 +135,107 @@ func checkVarLogACL(c check) { PathHasUserACL(c.test, "/var/log", systemUser, "r-x") } +func preActionCreateHomeDirectory(c check) { + err := os.MkdirAll(libPath, fs.FileMode(etcPathPermissions)) + require.NoError(c.test, err) +} + +// preActionCreateUser creates the system user and then set it as owner of configPath +func preActionCreateUser(c check) { + preActionMockUserConfig(c) + + 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(systemUser) + require.NoError(c.test, err) + + uid, err := strconv.Atoi(user.Uid) + require.NoError(c.test, err) + + gid, err := strconv.Atoi(user.Gid) + require.NoError(c.test, err) + + err = f.Chown(uid, gid) + require.NoError(c.test, err) +} + +func preActionMockConfigs(c check) { + preActionMockConfig(c) + preActionMockUserConfig(c) +} + +func preActionMockEnvFiles(c check) { + err := os.MkdirAll(envDirectoryPath, 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 preActionMockStructure(c check) { + preActionMockConfigs(c) + + err := os.MkdirAll(fileStoragePath, os.ModePerm) + require.NoError(c.test, err) + + _, err = os.Create(binaryPath) + require.NoError(c.test, err) +} + func preActionMockSystemdStructure(c check) { preActionMockStructure(c) _, err := os.Create(systemdPath) require.NoError(c.test, err) } + +func preActionWriteDefaultAPIBaseURLToUserConfig(c check) { + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err) + + conf.Extensions.Sumologic.APIBaseURL = apiBaseURL + err = saveConfig(userConfigPath, conf) + require.NoError(c.test, err) +} + +func preActionWriteDifferentDeprecatedTokenToEnvFile(c check) { + preActionMockEnvFiles(c) + + content := fmt.Sprintf("SUMOLOGIC_INSTALL_TOKEN=different%s", c.installOptions.installToken) + err := os.WriteFile(tokenEnvFilePath, []byte(content), fs.FileMode(etcPathPermissions)) + require.NoError(c.test, err) +} + +func preActionWriteDifferentTokenToEnvFile(c check) { + preActionMockEnvFiles(c) + + content := fmt.Sprintf("SUMOLOGIC_INSTALLATION_TOKEN=different%s", c.installOptions.installToken) + err := os.WriteFile(tokenEnvFilePath, []byte(content), fs.FileMode(etcPathPermissions)) + require.NoError(c.test, err) +} + +func preActionWriteDifferentTokenToUserConfig(c check) { + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err) + + conf.Extensions.Sumologic.InstallToken = "different" + c.installOptions.installToken + err = saveConfig(userConfigPath, conf) + require.NoError(c.test, err) +} + +func preActionWriteTokenToUserConfig(c check) { + conf, err := getConfig(userConfigPath) + require.NoError(c.test, err) + + conf.Extensions.Sumologic.InstallToken = c.installOptions.installToken + err = saveConfig(userConfigPath, conf) + require.NoError(c.test, err) +} diff --git a/pkg/scripts_test/common_darwin.go b/pkg/scripts_test/common_darwin.go index 5226a54a08..fe244dff65 100644 --- a/pkg/scripts_test/common_darwin.go +++ b/pkg/scripts_test/common_darwin.go @@ -6,7 +6,6 @@ import ( "io" "os" "os/exec" - "path" "testing" "github.com/stretchr/testify/require" @@ -57,11 +56,6 @@ func dsclKeyExistsForPath(t *testing.T, path, key string) bool { return false } -func getPackagePath() string { - tmpDir := os.Getenv("TMPDIR") - return path.Join(tmpDir, packageName) -} - func removeFileIfExists(t *testing.T, path string) { if _, err := os.Stat(path); err != nil { return diff --git a/pkg/scripts_test/consts_darwin.go b/pkg/scripts_test/consts_darwin.go index ffd84ccb85..02460cfa41 100644 --- a/pkg/scripts_test/consts_darwin.go +++ b/pkg/scripts_test/consts_darwin.go @@ -5,9 +5,10 @@ const ( 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" + rootGroup string = "wheel" + rootUser string = "root" systemGroup string = "otelcol-sumo" + systemUser string = "_otelcol-sumo" ) diff --git a/pkg/scripts_test/consts_linux.go b/pkg/scripts_test/consts_linux.go index 64304191e1..a44a86f899 100644 --- a/pkg/scripts_test/consts_linux.go +++ b/pkg/scripts_test/consts_linux.go @@ -1,10 +1,11 @@ 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" + rootGroup string = "root" + rootUser string = "root" systemGroup string = "otelcol-sumo" + systemUser string = "otelcol-sumo" ) diff --git a/pkg/scripts_test/install_darwin_test.go b/pkg/scripts_test/install_darwin_test.go index c83fc8ed2d..478652d5b9 100644 --- a/pkg/scripts_test/install_darwin_test.go +++ b/pkg/scripts_test/install_darwin_test.go @@ -79,6 +79,7 @@ func TestInstallScriptDarwin(t *testing.T) { checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), checkUserConfigCreated, checkLaunchdConfigCreated, + checkHomeDirectoryCreated, }, }, { @@ -98,6 +99,29 @@ func TestInstallScriptDarwin(t *testing.T) { checkUserExists, checkGroupExists, checkHostmetricsConfigNotCreated, + checkHomeDirectoryCreated, + }, + }, + { + name: "override default config", + options: installOptions{ + skipInstallToken: true, + autoconfirm: true, + }, + preActions: []checkFunc{preActionMockConfig}, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigCreated, + checkUserConfigNotCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkConfigOverrided, + checkUserConfigNotCreated, + checkLaunchdConfigCreated, }, }, { @@ -118,7 +142,8 @@ func TestInstallScriptDarwin(t *testing.T) { checkUserExists, checkGroupExists, checkHostmetricsConfigCreated, - checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup), + checkHostmetricsOwnershipAndPermissions(rootUser, rootGroup), + checkHomeDirectoryCreated, }, }, { @@ -140,40 +165,262 @@ func TestInstallScriptDarwin(t *testing.T) { checkTokenInLaunchdConfig, checkUserExists, checkGroupExists, + checkHomeDirectoryCreated, }, }, { - name: "same installation token", + name: "same installation token in launchd config", options: installOptions{ installToken: installToken, }, preActions: []checkFunc{preActionMockLaunchdConfig}, preChecks: []checkFunc{ - checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, - checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigNotCreated, + checkUserNotExists, + checkGroupNotExists, + checkLaunchdConfigCreated, }, postChecks: []checkFunc{ - checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkUserConfigCreated, checkLaunchdConfigCreated, checkTokenInLaunchdConfig, + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkUserConfigCreated, + checkLaunchdConfigCreated, + checkTokenInLaunchdConfig, + checkHomeDirectoryCreated, }, }, { - name: "different installation token", + name: "different installation token in launchd config", options: installOptions{ installToken: installToken, }, - preActions: []checkFunc{preActionMockLaunchdConfig, preActionWriteDifferentTokenToLaunchdConfig}, + preActions: []checkFunc{ + preActionMockLaunchdConfig, + preActionWriteDifferentTokenToLaunchdConfig, + }, preChecks: []checkFunc{ - checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, - checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigNotCreated, + checkUserNotExists, + checkGroupNotExists, + checkLaunchdConfigCreated, }, postChecks: []checkFunc{ - checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, - checkUserNotExists, checkGroupNotExists, checkLaunchdConfigCreated, + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigNotCreated, + checkUserNotExists, + checkGroupNotExists, + checkLaunchdConfigCreated, checkAbortedDueToDifferentToken, + checkDifferentTokenInLaunchdConfig, }, installCode: 1, }, + { + name: "same api base url", + options: installOptions{ + apiBaseURL: apiBaseURL, + skipInstallToken: true, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteAPIBaseURLToUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkUserConfigCreated, + checkAPIBaseURLInConfig, + }, + }, + { + name: "different api base url", + options: installOptions{ + apiBaseURL: apiBaseURL, + skipInstallToken: true, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteDifferentAPIBaseURLToUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkLaunchdConfigNotCreated, + checkAbortedDueToDifferentAPIBaseURL, + }, + installCode: 1, + }, + { + name: "adding api base url", + options: installOptions{ + apiBaseURL: apiBaseURL, + skipInstallToken: true, + }, + preActions: []checkFunc{preActionMockUserConfig}, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkConfigCreated, + checkUserConfigCreated, + checkAPIBaseURLInConfig, + }, + }, + { + name: "editing api base url", + options: installOptions{ + apiBaseURL: apiBaseURL, + skipInstallToken: true, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteEmptyUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkConfigCreated, + checkUserConfigCreated, + checkAPIBaseURLInConfig, + }, + }, + { + name: "configuration with tags", + options: installOptions{ + skipInstallToken: true, + tags: map[string]string{ + "lorem": "ipsum", + "foo": "bar", + }, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigNotCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), + checkTags, + checkLaunchdConfigCreated, + }, + }, + { + name: "same tags", + options: installOptions{ + skipInstallToken: true, + tags: map[string]string{ + "lorem": "ipsum", + "foo": "bar", + }, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteTagsToUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkUserConfigCreated, + checkTags, + checkLaunchdConfigCreated, + }, + }, + { + name: "different tags", + options: installOptions{ + skipInstallToken: true, + tags: map[string]string{ + "lorem": "ipsum", + "foo": "bar", + }, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteDifferentTagsToUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkDifferentTags, + checkLaunchdConfigNotCreated, + checkAbortedDueToDifferentTags, + }, + installCode: 1, + }, + { + name: "editing tags", + options: installOptions{ + skipInstallToken: true, + tags: map[string]string{ + "lorem": "ipsum", + "foo": "bar", + }, + }, + preActions: []checkFunc{ + preActionMockUserConfig, + preActionWriteEmptyUserConfig, + }, + preChecks: []checkFunc{ + checkBinaryNotCreated, + checkConfigNotCreated, + checkUserConfigCreated, + checkUserNotExists, + }, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkTags, + checkLaunchdConfigCreated, + }, + }, } { t.Run(spec.name, func(t *testing.T) { runTest(t, &spec) diff --git a/pkg/scripts_test/install_unix_test.go b/pkg/scripts_test/install_unix_test.go index 084f7cedd2..e62696c82b 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", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkUserConfigNotCreated, checkSystemdConfigNotCreated, }, @@ -83,7 +83,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, @@ -103,7 +103,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkUserConfigCreated, checkDeprecatedTokenInConfig, checkSystemdConfigNotCreated, @@ -123,13 +123,13 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, checkUserNotExists, checkHostmetricsConfigCreated, - checkHostmetricsOwnershipAndPermissions("root", rootGroup), + checkHostmetricsOwnershipAndPermissions(rootUser, rootGroup), }, }, { @@ -146,7 +146,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkUserConfigCreated, checkTokenInConfig, checkSystemdConfigNotCreated, @@ -266,7 +266,7 @@ func TestInstallScript(t *testing.T) { checkBinaryCreated, checkBinaryIsRunning, checkConfigCreated, - checkConfigFilesOwnershipAndPermissions("root", rootGroup), + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), checkTags, checkSystemdConfigNotCreated, },