summaryrefslogtreecommitdiff
path: root/integrationtests/dtailhealth_test.go
blob: 8c0918fb37fb79920036bca830e50ed36a358ed3 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package integrationtests

import (
	"context"
	"fmt"
	"os"
	"testing"

	"github.com/mimecast/dtail/internal/config"
)

func TestDTailHealthCheck(t *testing.T) {
	if !config.Env("DTAIL_RUN_INTEGRATION_TESTS") {
		t.Log("Skipping")
		return
	}
	stdoutFile := "dtailhealth.stdout.tmp"
	expectedStdoutFile := "dtailhealth.expected"

	t.Log("Serverless check, is supposed to exit with warning state.")
	exitCode, err := runCommand(context.TODO(), t, stdoutFile, "../dtailhealth")
	if exitCode != 1 {
		t.Error(fmt.Sprintf("Expected exit code '1' but got '%d': %v", exitCode, err))
		return
	}

	if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil {
		t.Error(err)
		return
	}
	os.Remove(stdoutFile)
}

func TestDTailHealthCheck2(t *testing.T) {
	if !config.Env("DTAIL_RUN_INTEGRATION_TESTS") {
		t.Log("Skipping")
		return
	}
	stdoutFile := "dtailhealth2.stdout.tmp"
	expectedStdoutFile := "dtailhealth2.expected"

	t.Log("Negative test, is supposed to exit with a critical state.")
	exitCode, err := runCommand(context.TODO(), t, stdoutFile,
		"../dtailhealth", "--server", "example:1")

	if exitCode != 2 {
		t.Error(fmt.Sprintf("Expected exit code '2' but got '%d': %v", exitCode, err))
		return
	}

	if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil {
		t.Error(err)
		return
	}

	os.Remove(stdoutFile)
}

func TestDTailHealthCheck3(t *testing.T) {
	if !config.Env("DTAIL_RUN_INTEGRATION_TESTS") {
		t.Log("Skipping")
		return
	}
	stdoutFile := "dtailhealth3.stdout.tmp"
	expectedStdoutFile := "dtailhealth3.expected"

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	_, _, _, err := startCommand(ctx, t,
		"../dserver",
		"--logger", "stdout",
		"--logLevel", "trace",
		"--bindAddress", "localhost",
		"--port", "4242",
	)
	if err != nil {
		t.Error(err)
		return
	}

	_, err = runCommandRetry(ctx, t, 10, stdoutFile,
		"../dtailhealth", "--server", "localhost:4242")
	if err != nil {
		t.Error(err)
		return
	}

	if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil {
		t.Error(err)
		return
	}

	os.Remove(stdoutFile)
}