Skip to content

Commit

Permalink
fix(pkg/scripts_test): darwin tests
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Kolberg <[email protected]>
  • Loading branch information
amdprophet committed Jun 22, 2023
1 parent 6a14271 commit a5e3486
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 44 deletions.
1 change: 0 additions & 1 deletion pkg/scripts_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ GOTESTBINARY=sumologic_scripts_tests.test
test:
$(GOTEST) -c
$(GOTESTPREFIX) ./$(GOTESTBINARY)
#-test.run TestInstallScriptDarwin/configuration_with_tags
42 changes: 0 additions & 42 deletions pkg/scripts_test/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,6 @@ func checkConfigCreated(c check) {
checkConfigDirectoryOwnershipAndPermissions(c)
}

func checkConfigFilesOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
etcPathGlob := filepath.Join(etcPath, "*")
etcPathNestedGlob := filepath.Join(etcPath, "*", "*")

for _, glob := range []string{etcPathGlob, etcPathNestedGlob} {
paths, err := filepath.Glob(glob)
require.NoError(c.test, err)
for _, path := range paths {
var permissions uint32
info, err := os.Stat(path)
require.NoError(c.test, err)
if info.IsDir() {
switch path {
case etcPath:
permissions = etcPathPermissions
default:
permissions = configPathDirPermissions
}
} else {
switch path {
case configPath, userConfigPath:
permissions = configPathFilePermissions
default:
permissions = confDPathFilePermissions
}
}
PathHasPermissions(c.test, path, permissions)
PathHasOwner(c.test, configPath, ownerName, ownerGroup)
}
}
PathHasPermissions(c.test, configPath, configPathFilePermissions)
}
}

func checkConfigNotCreated(c check) {
require.NoFileExists(c.test, configPath, "configuration has been created")
}
Expand Down Expand Up @@ -148,13 +113,6 @@ func checkHostmetricsConfigCreated(c check) {
require.FileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has not been created properly")
}

func checkHostmetricsOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
PathHasOwner(c.test, hostmetricsConfigPath, ownerName, ownerGroup)
PathHasPermissions(c.test, hostmetricsConfigPath, confDPathFilePermissions)
}
}

func checkHostmetricsConfigNotCreated(c check) {
require.NoFileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has been created")
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/scripts_test/check_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,46 @@ package sumologic_scripts_tests
import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/stretchr/testify/require"
)

func checkConfigFilesOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
etcPathGlob := filepath.Join(etcPath, "*")
etcPathNestedGlob := filepath.Join(etcPath, "*", "*")

for _, glob := range []string{etcPathGlob, etcPathNestedGlob} {
paths, err := filepath.Glob(glob)
require.NoError(c.test, err)
for _, path := range paths {
var permissions uint32
info, err := os.Stat(path)
require.NoError(c.test, err)
if info.IsDir() {
if path == etcPath {
permissions = etcPathPermissions
} else {
permissions = configPathDirPermissions
}
} else {
if filepath.Dir(path) != confDPath || path == userConfigPath {
permissions = configPathFilePermissions
} else {
permissions = confDPathFilePermissions
}
}
PathHasPermissions(c.test, path, permissions)
PathHasOwner(c.test, configPath, ownerName, ownerGroup)
}
}
PathHasPermissions(c.test, configPath, configPathFilePermissions)
}
}

func checkDifferentTokenInLaunchdConfig(c check) {
require.NotEmpty(c.test, c.installOptions.installToken, "installation token has not been provided")

Expand All @@ -28,6 +62,13 @@ func checkGroupNotExists(c check) {
require.False(c.test, exists, "group has been created")
}

func checkHostmetricsOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
PathHasOwner(c.test, hostmetricsConfigPath, ownerName, ownerGroup)
PathHasPermissions(c.test, hostmetricsConfigPath, confDPathFilePermissions)
}
}

func checkLaunchdConfigCreated(c check) {
require.FileExists(c.test, launchdPath, "launchd configuration has not been created properly")
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/scripts_test/check_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"testing"
Expand All @@ -19,6 +20,31 @@ func checkACLAvailability(c check) bool {
return assert.FileExists(&testing.T{}, "/usr/bin/getfacl", "File ACLS is not supported")
}

func checkConfigFilesOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
etcPathGlob := filepath.Join(etcPath, "*")
etcPathNestedGlob := filepath.Join(etcPath, "*", "*")

for _, glob := range []string{etcPathGlob, etcPathNestedGlob} {
paths, err := filepath.Glob(glob)
require.NoError(c.test, err)
for _, path := range paths {
var permissions uint32
info, err := os.Stat(path)
require.NoError(c.test, err)
if info.IsDir() {
permissions = configPathDirPermissions
} else {
permissions = configPathFilePermissions
}
PathHasPermissions(c.test, path, permissions)
PathHasOwner(c.test, configPath, ownerName, ownerGroup)
}
}
PathHasPermissions(c.test, configPath, configPathFilePermissions)
}
}

func checkDeprecatedTokenInConfig(c check) {
require.NotEmpty(c.test, c.installOptions.deprecatedInstallToken, "installation token has not been provided")

Expand Down Expand Up @@ -54,6 +80,13 @@ func checkDownloadTimeout(c check) {
require.Equal(c.test, 6, count)
}

func checkHostmetricsOwnershipAndPermissions(ownerName string, ownerGroup string) func(c check) {
return func(c check) {
PathHasOwner(c.test, hostmetricsConfigPath, ownerName, ownerGroup)
PathHasPermissions(c.test, hostmetricsConfigPath, configPathFilePermissions)
}
}

func checkOutputUserAddWarnings(c check) {
output := strings.Join(c.output, "\n")
require.NotContains(c.test, output, "useradd", "unexpected useradd output")
Expand Down
2 changes: 1 addition & 1 deletion pkg/scripts_test/install_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestInstallScriptDarwin(t *testing.T) {
checkBinaryIsRunning,
checkConfigCreated,
checkConfigOverrided,
checkUserConfigNotCreated,
checkUserConfigCreated,
checkLaunchdConfigCreated,
},
},
Expand Down

0 comments on commit a5e3486

Please sign in to comment.