Skip to content

Commit

Permalink
tests: add pkg/deb 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 c933b5e
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 44 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,51 @@ jobs:


integration-test-linux:
strategy:
matrix:
go-os: [linux]
go-arch: [x86_64]
runs-on: ubuntu-latest
needs: [pkg-deb,pkg-rpm]
needs: [get-version,pkg-deb]
env:
BIN: "snclient-${{needs.get-version.outputs.version}}-${{ matrix.go-os }}-${{ 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 }}.deb"
- name: "Linux Integration Tests"
run: |
mv ${{ env.BIN }}.deb t/snclient.deb
cd t
go test -v
SNCLIENT_INSTALL_TEST=1 go test -v
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
SNCLIENT_INSTALL_TEST=1 go test -v
integration-test-windows:
Expand Down Expand Up @@ -447,6 +467,7 @@ jobs:
Move-Item -Path ${{ env.BIN }}.msi -Destination .\t\snclient.msi
Copy-Item .\windist\windows_exporter-amd64.exe .\windist\windows_exporter.exe
cd t
$env:SNCLIENT_INSTALL_TEST = 1
go test -v ; if ($LASTEXITCODE -ne 0) { exit 1 }
shell: powershell

Expand Down
109 changes: 109 additions & 0 deletions t/40_e2e_deb_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"os"
"testing"

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

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

// this test requires:
// - snclient.deb
// it tries a installation and removes it afterwards
func TestDEBinstaller(t *testing.T) {
// skip test unless requested, it will uninstall existing installations
if os.Getenv("SNCLIENT_INSTALL_TEST") != "1" {
t.Skipf("SKIPPED: pkg installer test requires env SNCLIENT_INSTALL_TEST=1")

return
}

bin := getBinary()
require.FileExistsf(t, "snclient.deb", "snclient.deb binary must exist")

// install deb file
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"apt-get", "install", "-y", "./snclient.deb"},
})

requiredFiles := []string{
"/usr/bin/snclient",
"/usr/lib/snclient/node_exporter",
"/etc/snclient/snclient.ini",
"/etc/snclient/cacert.pem",
"/etc/snclient/server.key",
"/etc/snclient/server.crt",
"/lib/systemd/system/snclient.service",
"/etc/logrotate.d/snclient",
}
for _, file := range requiredFiles {
require.FileExistsf(t, file, file+" has been installed")
}

runCmd(t, &cmd{
Cmd: "systemctl",
Args: []string{"status", "snclient"},
Like: []string{`/usr/bin/snclient`, `running`},
})

// add custom .ini
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"touch", localDEBINIPath},
})
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"chmod", "666", localDEBINIPath},
})
writeFile(t, localDEBINIPath, localTestINI)
writeFile(t, `snclient.ini`, localINI)

runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"systemctl", "restart", "snclient"},
Like: []string{`/usr/bin/snclient`, `running`},
})

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(localDEBINIPath)

// uninstall pkg file
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"apt-get", "remove", "-y", "--purge", "snclient"},
})

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

// remove remaining files
os.Remove("snclient.ini")
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,19 @@ var requiredFiles = []string{
"README.md",
}

var localWinINI = `
[/modules]
CheckBuiltinPlugins = enabled
CheckExternalScripts = enabled
[/settings/default]
password = test
[/settings/external scripts/scripts]
check_win_not_exist1 = C:\Program Files\test\test.exe
check_win_not_exist2 = C:\Program Files\te st\test.exe
check_win_snclient_test1 = C:\Program Files\snclient\snclient.exe run check_dummy 3 testpattern
check_win_snclient_test2 = 'C:\Program Files\snclient\snclient.exe' run check_dummy 3 testpattern
check_win_snclient_test3 = "C:\Program Files\snclient\snclient.exe" run check_dummy 3 testpattern
check_win_snclient_test4 = & 'C:\Program Files\snclient\snclient.exe' run check_dummy 3 testpattern
check_win_snclient_test5 = & Write-Host "testpattern"; exit 3
`

// this test requires the wix.exe (including .net 3.5) to be installed
// further requirements are:
// - snclient.msi
// - windist folder to build new msi (incl. snclient.exe and windows_exporter.exe)
// it builds the msi file, tries a installation and removes it afterwards
func TestMSIinstaller(t *testing.T) {
// skip test unless requested, it will uninstall existing installations
if os.Getenv("SNCLIENT_INSTALL_TEST") != "1" {
t.Skipf("SKIPPED: pkg installer test requires env SNCLIENT_INSTALL_TEST=1")

return
}

bin := getBinary()
require.FileExistsf(t, bin, "snclient binary must exist")
require.FileExistsf(t, "snclient.msi", "snclient.msi binary must exist")
Expand Down Expand Up @@ -76,32 +65,15 @@ func TestMSIinstaller(t *testing.T) {
})

// add custom .ini
writeFile(t, localINIPath, localWinINI)
writeFile(t, localINIPath, localTestINI)
writeFile(t, `snclient.ini`, localINI)

// restart with new config
runCmd(t, &cmd{Cmd: "net", Args: []string{"stop", "snclient"}})
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 +108,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
96 changes: 96 additions & 0 deletions t/42_e2e_pkg_osx_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"os"
"testing"

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

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

// this test requires:
// - snclient.pkg
// it tries a installation and removes it afterwards
func TestOSXinstaller(t *testing.T) {
// skip test unless requested, it will uninstall existing installations
if os.Getenv("SNCLIENT_INSTALL_TEST") != "1" {
t.Skipf("SKIPPED: pkg installer test requires env SNCLIENT_INSTALL_TEST=1")

return
}

bin := getBinary()
require.FileExistsf(t, "snclient.pkg", "snclient.pkg binary must exist")

// install pkg file
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"/usr/sbin/installer", "-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
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"touch", localOSXINIPath},
})
runCmd(t, &cmd{
Cmd: "sudo",
Args: []string{"chmod", "666", localOSXINIPath},
})
writeFile(t, localOSXINIPath, localTestINI)

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: "sudo",
Args: []string{"/usr/local/bin/snclient_uninstall.sh"},
})

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

// remove remaining files
os.Remove("snclient.ini")
}
19 changes: 19 additions & 0 deletions t/43_e2e_freebsd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"os"
"testing"
)

func TestFreeBSD(t *testing.T) {
// skip test unless requested, it will uninstall existing installations
if os.Getenv("SNCLIENT_INSTALL_TEST") != "1" {
t.Skipf("SKIPPED: pkg installer test requires env SNCLIENT_INSTALL_TEST=1")

return
}

t.Logf("not yet implemented...")
bin := getBinary()
waitUntilResponse(t, bin)
}
Loading

0 comments on commit c933b5e

Please sign in to comment.