Skip to content

Commit

Permalink
chore(pkg/scripts_test): clean up
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Kolberg <[email protected]>
  • Loading branch information
amdprophet committed Jun 19, 2023
1 parent 849841e commit 0443bd1
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 330 deletions.
108 changes: 3 additions & 105 deletions pkg/scripts_test/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"syscall"
"testing"

"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 16 additions & 8 deletions pkg/scripts_test/check_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
}

Expand Down Expand Up @@ -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)
Expand Down
90 changes: 89 additions & 1 deletion pkg/scripts_test/check_linux.go
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
}
Loading

0 comments on commit 0443bd1

Please sign in to comment.