Skip to content

Commit

Permalink
osx: add pkg installation/uninstall to integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Mar 24, 2024
1 parent b5086d2 commit a7eaa24
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 22 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,25 @@ jobs:
integration-test-osx:
strategy:
matrix:
go-os: [darwin]
go-arch: [x86_64]
runs-on: macos-latest
needs: [pkg-osx]
needs: [get-version,pkg-osx]
env:
BIN: "snclient-${{needs.get-version.outputs.version}}-osx-${{ matrix.go-arch }}"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GOVERSION }}
- uses: actions/download-artifact@v4
with:
name: "${{ env.BIN }}.pkg"
- name: "OSX Integration Tests"
run: |
mv ${{ env.BIN }}.pkg t/snclient.pkg
cd t
go test -v
Expand Down
21 changes: 2 additions & 19 deletions t/40_build_msi_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,7 @@ func TestMSIinstaller(t *testing.T) {
runCmd(t, &cmd{Cmd: "net", Args: []string{"start", "snclient"}})

// wait a couple of seconds till daemon answers
waitUntilResponse := func() {
waitStart := time.Now()
waitUntil := time.Now().Add(10 * time.Second)
for time.Now().Before(waitUntil) {
res := runCmd(t, &cmd{
Cmd: bin,
Args: []string{"run", "check_nsc_web", "-k", "-p", "test", "-u", "https://localhost:8443", "check_snclient_version"},
Exit: -1,
})
if res.ExitCode == 0 {
t.Logf("daemon responded after %s", time.Since(waitStart))

break
}
time.Sleep(100 * time.Millisecond)
}
}
waitUntilResponse()
waitUntilResponse(t, bin)

// verify response
runCmd(t, &cmd{
Expand Down Expand Up @@ -136,7 +119,7 @@ func TestMSIinstaller(t *testing.T) {
require.FileExistsf(t, `C:\Program Files\snclient\`+file, file+" still exists")
}

waitUntilResponse()
waitUntilResponse(t, bin)

// verify response
runCmd(t, &cmd{
Expand Down
90 changes: 90 additions & 0 deletions t/50_pkg_osx_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"os"
"testing"

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

const (
localOSXINIPath = `/etc/snclient/snclient_local.ini`
)

var localOSXINI = `
[/modules]
CheckBuiltinPlugins = enabled
CheckExternalScripts = enabled
[/settings/default]
password = test
`

// this test requires:
// - snclient.pkg
// it tries a installation and removes it afterwards
func TestOSXinstaller(t *testing.T) {
bin := getBinary()
require.FileExistsf(t, "snclient.pkg", "snclient.pkg binary must exist")

// install pkg file
runCmd(t, &cmd{
Cmd: "installer",
Args: []string{"-pkg", "snclient.pkg", "-target", "/"},
})

requiredFiles := []string{
"/usr/local/bin/snclient",
"/usr/local/bin/snclient_uninstall.sh",
"/usr/local/bin/node_exporter",
"/etc/snclient/snclient.ini",
"/etc/snclient/cacert.pem",
"/etc/snclient/server.key",
"/etc/snclient/server.crt",
"/Library/LaunchDaemons/com.snclient.snclient.plist",
}
for _, file := range requiredFiles {
require.FileExistsf(t, file, file+" has been installed")
}

// add custom .ini
writeFile(t, localOSXINIPath, localOSXINI)

waitUntilResponse(t, bin)

// verify response
runCmd(t, &cmd{
Cmd: bin,
Args: []string{"run", "check_nsc_web", "-k", "-p", "test", "-u", "https://localhost:8443", "check_snclient_version"},
Like: []string{`^SNClient\+ v`},
})

runCmd(t, &cmd{
Cmd: bin,
Args: []string{"run", "check_nsc_web", "-k", "-p", "test", "-u", "https://localhost:8443", "check_uptime", "crit=uptime<2s", "warn=uptime<1s"},
Like: []string{"OK - uptime"},
})

runCmd(t, &cmd{
Cmd: bin,
Args: []string{"run", "check_nsc_web", "-k", "-p", "test", "-u", "https://localhost:8443", "check_cpu", "crit=load>101", "warn=load>101"},
Like: []string{"OK - CPU load is ok."},
})

// cleanup
os.Remove(localOSXINIPath)

// uninstall pkg file
runCmd(t, &cmd{
Cmd: `/usr/local/bin/snclient_uninstall.sh`,
Args: []string{},
})

for _, file := range requiredFiles {
assert.NoFileExistsf(t, file, file+" has been removed")
}

// remove remaining files
os.Remove("snclient.ini")
}
24 changes: 22 additions & 2 deletions t/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"os"
"os/exec"
"path/filepath"
"pkg/utils"
"runtime"
"syscall"
"testing"
"time"

"pkg/utils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -199,6 +198,27 @@ func writeFile(t *testing.T, path, content string) {
require.NoErrorf(t, err, fmt.Sprintf("writing file %s succeeded", path))
}

// wait a couple of seconds till daemon answers
func waitUntilResponse(t *testing.T, bin string) {
t.Helper()

waitStart := time.Now()
waitUntil := time.Now().Add(10 * time.Second)
for time.Now().Before(waitUntil) {
res := runCmd(t, &cmd{
Cmd: bin,
Args: []string{"run", "check_nsc_web", "-k", "-p", "test", "-u", "https://localhost:8443", "check_snclient_version"},
Exit: -1,
})
if res.ExitCode == 0 {
t.Logf("daemon responded after %s", time.Since(waitStart))

break
}
time.Sleep(100 * time.Millisecond)
}
}

var (
daemonPid = 0
daemonPidFile = "snclient.lock"
Expand Down

0 comments on commit a7eaa24

Please sign in to comment.