Skip to content

Commit

Permalink
feat: linux package support
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Kolberg <[email protected]>
  • Loading branch information
amdprophet committed Aug 30, 2023
1 parent b6938f5 commit 214564f
Show file tree
Hide file tree
Showing 13 changed files with 2,206 additions and 803 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ jobs:
name: Test Install Script
runs-on: ${{ matrix.runs_on }}
strategy:
fail-fast: false
matrix:
include:
- arch_os: linux_amd64
Expand Down
161 changes: 103 additions & 58 deletions pkg/scripts_test/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type check struct {
test *testing.T
installOptions installOptions
installType installType
code int
err error
expectedInstallCode int
Expand Down Expand Up @@ -55,6 +56,51 @@ func checkRun(c check) {
require.Equal(c.test, c.expectedInstallCode, c.code, "unexpected installation script error code")
}

func checkConfigFilesOwnershipAndPermissions(c check) {
ownerName := getConfigFilesOwner(c)
groupName := getConfigFilesGroup(c)
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 {
switch path {
case configPath:
// /etc/otelcol-sumo/sumologic.yaml
permissions = configPathFilePermissions
case userConfigPath:
// /etc/otelcol-sumo/conf.d/common.yaml
permissions = commonConfigPathFilePermissions
case tokenEnvFilePath:
// /etc/otelcol-sumo/env/token.env
permissions = commonConfigPathFilePermissions
default:
// /etc/otelcol-sumo/conf.d/**/
permissions = confDPathFilePermissions
}
}
PathHasPermissions(c.test, path, permissions)
PathHasOwner(c.test, configPath, ownerName, groupName)
}
}
PathHasPermissions(c.test, configPath, configPathFilePermissions)
}

func checkConfigDirectoryOwnershipAndPermissions(c check) {
ownerName := getConfigFilesOwner(c)
groupName := getConfigFilesGroup(c)
PathHasOwner(c.test, etcPath, ownerName, groupName)
PathHasPermissions(c.test, etcPath, etcPathPermissions)
}

func checkConfigCreated(c check) {
require.FileExists(c.test, configPath, "configuration has not been created properly")
}
Expand All @@ -77,6 +123,13 @@ func checkConfigOverrided(c check) {
}, "invalid value for installation token")
}

func checkHostmetricsOwnershipAndPermissions(c check) {
ownerName := getConfigFilesOwner(c)
groupName := getConfigFilesGroup(c)
PathHasOwner(c.test, hostmetricsConfigPath, ownerName, groupName)
PathHasPermissions(c.test, hostmetricsConfigPath, confDPathFilePermissions)
}

func checkUserConfigCreated(c check) {
require.FileExists(c.test, userConfigPath, "user configuration has not been created properly")
}
Expand Down Expand Up @@ -138,75 +191,67 @@ func checkAbortedDueToNoToken(c check) {
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) {
err := os.MkdirAll(etcPath, 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(etcPath, fs.FileMode(etcPathPermissions))
require.NoError(c.test, err)

err = os.MkdirAll(confDPath, fs.FileMode(configPathDirPermissions))
require.NoError(c.test, err)

f, err := os.Create(userConfigPath)
require.NoError(c.test, err)

err = f.Chmod(fs.FileMode(commonConfigPathFilePermissions))
require.NoError(c.test, err)
func getConfigFilesOwner(c check) string {
if c.installOptions.uninstall {
if c.installType&PACKAGE_INSTALL != 0 {
return systemUser
}
return rootUser
}
if c.installOptions.skipSystemd || c.installOptions.skipInstallToken {
return rootUser
}
return systemUser
}

func preActionWriteAPIBaseURLToUserConfig(c check) {
conf, err := getConfig(userConfigPath)
require.NoError(c.test, err)

conf.Extensions.Sumologic.APIBaseURL = c.installOptions.apiBaseURL
err = saveConfig(userConfigPath, conf)
require.NoError(c.test, err)
func getConfigFilesGroup(c check) string {
if c.installOptions.uninstall {
if c.installType&PACKAGE_INSTALL != 0 {
return systemGroup
}
return rootGroup
}
if c.installOptions.skipSystemd || c.installOptions.skipInstallToken {
return rootGroup
}
return systemGroup
}

func preActionWriteDifferentAPIBaseURLToUserConfig(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 preActionInstallPackage(c check) {
c.installOptions.installToken = installToken
c.installOptions.uninstall = false
c.installOptions.purge = false
c.installOptions.apiBaseURL = mockAPIBaseURL
c.code, c.output, c.errorOutput, c.err = runScript(c)
}

func preActionWriteDifferentTagsToUserConfig(c check) {
conf, err := getConfig(userConfigPath)
require.NoError(c.test, err)

conf.Extensions.Sumologic.Tags = map[string]string{
"some": "tag",
func preActionInstallPackageVersion(version string) checkFunc {
return func(c check) {
c.installOptions.installToken = installToken
c.installOptions.uninstall = false
c.installOptions.purge = false
c.installOptions.version = version
c.installOptions.apiBaseURL = mockAPIBaseURL
c.code, c.output, c.errorOutput, c.err = runScript(c)
}
err = saveConfig(userConfigPath, conf)
require.NoError(c.test, err)
}

func preActionWriteEmptyUserConfig(c check) {
conf, err := getConfig(userConfigPath)
require.NoError(c.test, err)

err = saveConfig(userConfigPath, conf)
require.NoError(c.test, err)
func preActionInstallPreviousVersion(c check) {
if c.installType&PACKAGE_INSTALL != 0 {
preActionInstallPackageVersion(previousPackageVersion)(c)
}
preActionInstallVersion(previousBinaryVersion)(c)
}

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 preActionInstallVersion(version string) checkFunc {
return func(c check) {
c.installOptions.installToken = installToken
c.installOptions.uninstall = false
c.installOptions.purge = false
c.installOptions.version = version
c.installOptions.apiBaseURL = mockAPIBaseURL
c.code, c.output, c.errorOutput, c.err = runScript(c)
}
}

func checkAbortedDueToDifferentAPIBaseURL(c check) {
Expand Down
56 changes: 1 addition & 55 deletions pkg/scripts_test/check_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,12 @@ 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) {
PathHasPermissions(c.test, etcPath, etcPathPermissions)
PathHasOwner(c.test, etcPath, ownerName, ownerGroup)

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")

Expand All @@ -71,13 +28,6 @@ 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 All @@ -87,7 +37,7 @@ func checkLaunchdConfigNotCreated(c check) {
}

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

matchedLine := ""
Expand Down Expand Up @@ -121,10 +71,6 @@ func checkUserNotExists(c check) {
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)
Expand Down
Loading

0 comments on commit 214564f

Please sign in to comment.