aboutsummaryrefslogtreecommitdiff
path: root/pkg/zabbix/zabbix_test.go
blob: 49c0dbc9a8cecb6fe222a2c0c52155db4ce62954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package zabbix

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"testing"
)

func TestMain(t *testing.T) {
	os.RemoveAll("tmp")
	cwd, _ := os.Getwd()
	err := os.MkdirAll("tmp/ok-18.2", 0777)
	if err != nil {
		t.Skipf("skipping main tests because tmp directory cannot be created : %s", err)
	}
	wd, err := os.Getwd()
	if err != nil {
		t.Skipf("skipping main tests because cannot get working directory : %s", err)
	}
	os.MkdirAll("tmp/ok-17.2", 0777)
	os.MkdirAll("tmp/no_state_file", 0777)
	os.MkdirAll("tmp/bacula_auto_detect_failed/var/lib/bacula", 0777)
	os.MkdirAll("tmp/bareos_auto_detect_failed/var/lib/bareos", 0777)
	os.MkdirAll("tmp/error", 0777)
	os.Symlink("../../testdata/bareos-fd-17.2.state", "tmp/ok-17.2/state")
	os.Symlink("../../testdata/bareos-fd-18.2.state", "tmp/ok-18.2/state")
	os.Symlink("../../testdata/bareos-fd-18.2.state-with-error", "tmp/error/state")
	tests := []struct {
		name      string
		timestamp uint64
		rootDir   string
		args      []string
		want      string
	}{
		{"failed bacula_auto_detect", 0, "tmp/bacula_auto_detect_failed", []string{}, "INFO Failed to init programm : Could not autodetect a suitable state file. Has a job ever run? Does the user you are running the check as has read access to bacula or bareos' /var/lib directory? Alternatively use the -w and -f flags to specify the work directory and state file to use."},
		{"failed bareos_auto_detect", 0, "tmp/bareos_auto_detect_failed", []string{}, "INFO Failed to init programm : Could not autodetect a suitable state file. Has a job ever run? Does the user you are running the check as has read access to bacula or bareos' /var/lib directory? Alternatively use the -w and -f flags to specify the work directory and state file to use."},
		{"failed auto_detect", 0, "tmp/non_existent", []string{}, "INFO Failed to init programm : Could not autodetect a suitable work directory. Is bareos or bacula installed?"},
		{"no work directory", 0, "tmp", []string{"-w", "/non_existent"}, fmt.Sprintf("INFO Failed to init programm : Invalid work directory %s/tmp/non_existent : it does not exist or is not a directory", wd)},
		{"no state file auto_detect", 0, "tmp", []string{"-w", "/no_state_file"}, "INFO Failed to init programm : Could not autodetect a suitable state file. Has a job ever run? Does the user you are running the check as has read access to bacula or bareos' /var/lib directory? Alternatively use the -w and -f flags to specify the work directory and state file to use."},
		{"no state file", 0, "tmp", []string{"-w", "/no_state_file", "-f", "test"}, fmt.Sprintf("INFO Failed to init programm : Could not open state file %s/tmp/no_state_file/test", wd)},
		{"ok bareos 18.2", 1582579731, "tmp/ok-18.2", []string{"-w", "/", "-f", "state"}, "OK"},
		{"ok bareos 17.2", 1582579731, "tmp/ok-17.2", []string{"-w", "/", "-f", "state"}, "OK"},
		{"missing", 1582709331, "tmp/ok-18.2", []string{"-w", "/", "-f", "state"}, "AVERAGE:  missing: awhphpipam1_percona_xtrabackup, awhphpipam1_LinuxAll, awhphpipam1_www"},
		{"error", 1582579731, "tmp/error", []string{"-w", "/", "-f", "state"}, "AVERAGE: errors: awhphpipam1_percona_xtrabackup, awhphpipam1_www  Corrupted spool file: invalid argument"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			now = tt.timestamp
			root = filepath.Join(cwd, tt.rootDir)
			flag.CommandLine = flag.NewFlagSet("bareos-zabbix-check", flag.ExitOnError) //flags are now reset
			os.Args = append([]string{"bareos-zabbix-check"}, tt.args...)
			if got := Main(); got != tt.want {
				t.Log(workDir)
				t.Errorf("Main() = %v, want %v", got, tt.want)
			}
		})
	}
	os.RemoveAll("tmp")
}