Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scripts): update install.sh to use packages on macOS #1127

Merged
merged 14 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 29 additions & 290 deletions pkg/scripts_test/check.go

Large diffs are not rendered by default.

166 changes: 166 additions & 0 deletions pkg/scripts_test/check_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
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 {
switch path {
case configPath:
// /etc/otelcol-sumo/sumologic.yaml
permissions = configPathFilePermissions
case userConfigPath:
// /etc/otelcol-sumo/conf.d/common.yaml
permissions = commonConfigPathFilePermissions
default:
// /etc/otelcol-sumo/conf.d/*
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")

conf, err := getLaunchdConfig(launchdPath)
require.NoError(c.test, err)

require.Equal(c.test, "different"+c.installOptions.installToken, conf.EnvironmentVariables.InstallationToken, "installation token is different than expected")
}

func checkGroupExists(c check) {
exists := dsclKeyExistsForPath(c.test, "/Groups", systemGroup)
require.True(c.test, exists, "group has not been created")
}

func checkGroupNotExists(c check) {
exists := dsclKeyExistsForPath(c.test, "/Groups", systemGroup)
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")
}

func checkLaunchdConfigNotCreated(c check) {
require.NoFileExists(c.test, launchdPath, "launchd configuration has been created")
}

func checkPackageCreated(c check) {
re, err := regexp.Compile("Package downloaded to: .*/otelcol-sumo.pkg")
require.NoError(c.test, err)

matchedLine := ""
for _, line := range c.output {
if re.MatchString(line) {
matchedLine = line
}
}
require.NotEmpty(c.test, matchedLine, "package path not in output")

packagePath := strings.TrimPrefix(matchedLine, "Package downloaded to: ")
require.FileExists(c.test, packagePath, "package has not been created")
}

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

conf, err := getLaunchdConfig(launchdPath)
require.NoError(c.test, err)

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 preActionInstallPackage(c check) {
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionInstallPackageWithDifferentAPIBaseURL(c check) {
c.installOptions.apiBaseURL = "different" + c.installOptions.apiBaseURL
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionInstallPackageWithDifferentTags(c check) {
c.installOptions.tags = map[string]string{
"some": "tag",
}
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionInstallPackageWithNoAPIBaseURL(c check) {
c.installOptions.apiBaseURL = ""
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionInstallPackageWithNoTags(c check) {
c.installOptions.tags = nil
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionMockLaunchdConfig(c check) {
f, err := os.Create(launchdPath)
require.NoError(c.test, err)

err = f.Chmod(fs.FileMode(launchdPathFilePermissions))
require.NoError(c.test, err)

conf := NewLaunchdConfig()
err = saveLaunchdConfig(launchdPath, conf)
require.NoError(c.test, err)
}

func preActionWriteDifferentTokenToLaunchdConfig(c check) {
conf, err := getLaunchdConfig(launchdPath)
require.NoError(c.test, err)

conf.EnvironmentVariables.InstallationToken = "different" + c.installOptions.installToken
err = saveLaunchdConfig(launchdPath, conf)
require.NoError(c.test, err)
}
Loading