forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters_test.go
78 lines (63 loc) · 2.6 KB
/
filters_test.go
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
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Authors: Guillaume Lefranc <guillaume@signal18.io>
// Stephane Varoqui <stephane@mariadb.com>
// This source code is licensed under the GNU General Public License, version 3.
package haproxy
import (
"encoding/json"
"io/ioutil"
"testing"
)
const (
FILTERS_CORRECT_JSON = "../test/test_filters_correct.json"
FILTERS_WRONG_JSON = "../test/test_filters_wrong.json"
)
func TestFilters_ParseFilter(t *testing.T) {
fakeRoute := "my_route"
j, _ := ioutil.ReadFile(FILTERS_CORRECT_JSON)
var filtersCorrect, filtersWrong []*Filter
_ = json.Unmarshal(j, &filtersCorrect)
i, _ := ioutil.ReadFile(FILTERS_WRONG_JSON)
_ = json.Unmarshal(i, &filtersWrong)
for _, filter := range filtersCorrect {
if _, err := parseFilter(fakeRoute, filter); err != nil {
t.Errorf("Failed to correctly parse a filter %s", err.Error())
}
}
for _, filter := range filtersWrong {
if _, err := parseFilter(fakeRoute, filter); err == nil {
t.Errorf("Filter parsing should fail with incorrect filters")
}
}
}
func TestFilters_ParseFilterCondition(t *testing.T) {
/*
these two notations should be equivalent. The full Haproxy condition
should pass through untouched
*/
tests := []struct {
Input string
ExpectedString string
ExpectedNegate bool
}{
{"hdr_sub(user-agent) Android", "hdr_sub(user-agent) Android", false},
{"user-agent=Android", "hdr_sub(user-agent) Android", false},
{"user-agent!=Android", "hdr_sub(user-agent) Android", true},
{"User-Agent=Android", "hdr_sub(user-agent) Android", false},
{"user-agent = Android", "hdr_sub(user-agent) Android", false},
{"user-agent = Android", "user-agent = Android", false},
{"user.agent = Ios", "hdr_sub(user-agent) Ios", false},
{"host = www.google.com", "hdr_str(host) www.google.com", false},
{"host != www.google.com", "hdr_str(host) www.google.com", true},
{"cookie MYCUSTOMER contains Value=good", "cook_sub(MYCUSTOMER) Value=good", false},
{"has cookie JSESSIONID", "cook(JSESSIONID) -m found", false},
{"misses cookie JSESSIONID", "cook_cnt(JSESSIONID) eq 0", false},
{"has header X-SPECIAL", "hdr_cnt(X-SPECIAL) gt 0", false},
{"misses header X-SPECIAL", "hdr_cnt(X-SPECIAL) eq 0", false},
}
for i, condition := range tests {
if result, negate := parseFilterCondition(condition.Input); result != condition.ExpectedString || negate != condition.ExpectedNegate {
t.Errorf("Failed to correctly parse filter condition %d. Got %s but expected %s with negate %s", (i + 1), result, condition.ExpectedString, condition.ExpectedNegate)
}
}
}