summaryrefslogtreecommitdiff
path: root/internal/session/spec_test.go
blob: 182c517020e7e54dac57865cc5eb1aceaa8c0020 (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
package session

import (
	"encoding/base64"
	"encoding/json"
	"reflect"
	"strings"
	"testing"

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

func TestSpecStartCommandEncodesPayload(t *testing.T) {
	t.Parallel()

	spec := Spec{
		Mode:    omode.TailClient,
		Files:   []string{"/var/log/app.log"},
		Options: "plain=true",
		Regex:   "ERROR",
		Timeout: 15,
	}

	command, err := spec.StartCommand()
	if err != nil {
		t.Fatalf("StartCommand() error = %v", err)
	}
	if !strings.HasPrefix(command, "SESSION START ") {
		t.Fatalf("unexpected start command prefix: %q", command)
	}

	var decoded Spec
	if err := decodeSpecPayload(strings.TrimPrefix(command, "SESSION START "), &decoded); err != nil {
		t.Fatalf("decode start payload: %v", err)
	}
	if !reflect.DeepEqual(decoded, spec) {
		t.Fatalf("unexpected decoded spec: got %#v want %#v", decoded, spec)
	}
}

func TestSpecUpdateCommandIncludesGeneration(t *testing.T) {
	t.Parallel()

	spec := Spec{
		Mode:  omode.MapClient,
		Files: []string{"/var/log/app.log"},
		Query: "from STATS select count(*)",
	}

	command, err := spec.UpdateCommand(7)
	if err != nil {
		t.Fatalf("UpdateCommand() error = %v", err)
	}
	if !strings.HasPrefix(command, "SESSION UPDATE 7 ") {
		t.Fatalf("unexpected update command prefix: %q", command)
	}

	var decoded Spec
	if err := decodeSpecPayload(strings.TrimPrefix(command, "SESSION UPDATE 7 "), &decoded); err != nil {
		t.Fatalf("decode update payload: %v", err)
	}
	if !reflect.DeepEqual(decoded, spec) {
		t.Fatalf("unexpected decoded spec: got %#v want %#v", decoded, spec)
	}
}

func decodeSpecPayload(payload string, out *Spec) error {
	raw, err := base64.StdEncoding.DecodeString(payload)
	if err != nil {
		return err
	}
	return json.Unmarshal(raw, out)
}