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