|
| 1 | +package eventfilter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/cloudfoundry-community/splunk-firehose-nozzle/eventfilter" |
| 7 | + "github.com/cloudfoundry/sonde-go/events" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/ginkgo/extensions/table" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("Rule parsing", func() { |
| 15 | + testError := func(filterConf string, errorMsg string) { |
| 16 | + filters, err := eventfilter.New(filterConf) |
| 17 | + Expect(filters).To(BeNil()) |
| 18 | + Expect(err).To(MatchError(ContainSubstring(errorMsg))) |
| 19 | + } |
| 20 | + DescribeTable("throws error", testError, |
| 21 | + Entry("not enough fields", ":", "format must be"), |
| 22 | + Entry("too many fields", "xxx:yyy:zzz:rrrr", "format must be"), |
| 23 | + Entry("invalid value", "xxx::", "filter value must not be empty"), |
| 24 | + Entry("invalid filter", "xxx:yyy:zzz", "needs to be either"), |
| 25 | + Entry("invalid field", "must:notValid:zzz", "invalid filter key \"notValid\""), |
| 26 | + ) |
| 27 | + |
| 28 | + testOk := func(filterConf string, length int) { |
| 29 | + filters, err := eventfilter.New(filterConf) |
| 30 | + Expect(err).NotTo(HaveOccurred()) |
| 31 | + Expect(filters).NotTo(BeNil(), "filters have not been initialized") |
| 32 | + Expect(filters.Length()).To(Equal(length), "Expected %d filter rules", length) |
| 33 | + } |
| 34 | + DescribeTable("parses ok", testOk, |
| 35 | + Entry("no filters at all", "", 0), |
| 36 | + Entry("multiple empty rules", ";;;;", 0), |
| 37 | + Entry("filtering on deployment", "must:deployment:some deployment", 1), |
| 38 | + Entry("accepts whitespace between rules", " must:deployment:something ; must:origin:someOrigin ", 2), |
| 39 | + Entry("accepts whitespace in filter", " must :deployment:something", 1), |
| 40 | + |
| 41 | + Entry("inclusion filter on deployment", "must:Deployment:something", 1), |
| 42 | + Entry("inclusion filter on origin", "must:origin:something", 1), |
| 43 | + Entry("inclusion filter on job", "must:job:something", 1), |
| 44 | + |
| 45 | + Entry("exclusion filter on deployment", "mustNot:Deployment:something", 1), |
| 46 | + Entry("exclusion filter on origin", "mustNot:origin:something", 1), |
| 47 | + Entry("exclusion filter on job", "mustNot:job:something", 1), |
| 48 | + ) |
| 49 | +}) |
| 50 | + |
| 51 | +var _ = Describe("Filtering", func() { |
| 52 | + msg := &events.Envelope{ |
| 53 | + Deployment: p("p-healthwatch2-123123123"), |
| 54 | + Origin: p("some origin"), |
| 55 | + Job: p("some job"), |
| 56 | + } |
| 57 | + |
| 58 | + test := func(filterConf string, expected bool) { |
| 59 | + filters, err := eventfilter.New(filterConf) |
| 60 | + Expect(err).NotTo(HaveOccurred()) |
| 61 | + Expect(filters.Accepts(msg)). |
| 62 | + To(Equal(expected), "Expected event {%v} to be %s", msg, tern(expected, "accepted", "discarded")) |
| 63 | + Expect(filters).NotTo(BeNil(), "filters have not been initialized") |
| 64 | + } |
| 65 | + |
| 66 | + DescribeTable("on", test, |
| 67 | + Entry("empty filter conf should accept", "", true), |
| 68 | + Entry("matching inclusion filter should accept", "must:deployment:healthwatch2", true), |
| 69 | + Entry("non-matching inclusion filter should discard", "must:deployment:something", false), |
| 70 | + Entry("matching exclusion filter should discard", "mustNot:deployment:healthwatch2", false), |
| 71 | + Entry("2nd exclusion filter should discard", "must:deployment:health ; mustNot:deployment:watch", false), |
| 72 | + Entry("3rd exclusion filter should discard", |
| 73 | + "must:deployment:health ; mustNot:job:other job ; mustNot:deployment:watch", |
| 74 | + false, |
| 75 | + ), |
| 76 | + Entry("many matching inclusion filters should accept", |
| 77 | + "must:deployment:h ; must:deployment:e ; must:deployment:a ; must:deployment:l ; must:deployment:t ; must:deployment:h", |
| 78 | + true, |
| 79 | + ), |
| 80 | + Entry("many non-matching exclusion filters should accept", |
| 81 | + "mustNot:deployment:x ; mustNot:deployment:y ; mustNot:deployment:z ; mustNot:deployment:u ; mustNot:deployment:b ; mustNot:deployment:r", |
| 82 | + true, |
| 83 | + ), |
| 84 | + ) |
| 85 | +}) |
| 86 | + |
| 87 | +var _ = Describe("filter keys", func() { |
| 88 | + entries := []TableEntry{} |
| 89 | + for _, key := range eventfilter.SupportedFilterKeys { |
| 90 | + entries = append(entries, Entry(fmt.Sprintf("key %q", key), key)) |
| 91 | + } |
| 92 | + |
| 93 | + DescribeTable("support", func(key string) { |
| 94 | + _, err := eventfilter.New("must:" + key + ":someValue") |
| 95 | + Expect(err).NotTo(HaveOccurred()) |
| 96 | + }, entries...) |
| 97 | +}) |
| 98 | + |
| 99 | +func p(s string) *string { return &s } |
| 100 | + |
| 101 | +func tern(b bool, t string, f string) string { |
| 102 | + if b { |
| 103 | + return t |
| 104 | + } |
| 105 | + |
| 106 | + return f |
| 107 | +} |
0 commit comments