github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/kafka/policy_test.go (about) 1 // Copyright 2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package kafka 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/cilium/cilium/pkg/policy/api" 24 25 "github.com/optiopay/kafka/proto" 26 . "gopkg.in/check.v1" 27 ) 28 29 // Hook up gocheck into the "go test" runner. 30 func Test(t *testing.T) { 31 TestingT(t) 32 } 33 34 type kafkaTestSuite struct{} 35 36 var _ = Suite(&kafkaTestSuite{}) 37 38 var ( 39 messages = make([]*proto.Message, 100) 40 ) 41 42 func (k *kafkaTestSuite) SetUpTest(c *C) { 43 for i := range messages { 44 messages[i] = &proto.Message{ 45 Offset: int64(i), 46 Crc: uint32(i), 47 Key: nil, 48 Value: []byte(`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.`), 49 } 50 51 } 52 } 53 54 func (k *kafkaTestSuite) TestProduceRequest(c *C) { 55 req := &proto.ProduceReq{ 56 CorrelationID: 241, 57 ClientID: "test", 58 Compression: proto.CompressionNone, 59 RequiredAcks: proto.RequiredAcksAll, 60 Timeout: time.Second, 61 Topics: []proto.ProduceReqTopic{ 62 { 63 Name: "foo", 64 Partitions: []proto.ProduceReqPartition{ 65 { 66 ID: 0, 67 Messages: messages, 68 }, 69 }, 70 }, 71 { 72 Name: "bar", 73 Partitions: []proto.ProduceReqPartition{ 74 { 75 ID: 0, 76 Messages: messages, 77 }, 78 }, 79 }, 80 }, 81 } 82 83 reqMsg := RequestMessage{ 84 request: req, 85 } 86 87 // empty rules should match nothing 88 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{}), Equals, false) 89 90 // wildcard rule matches everything 91 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{{}}), Equals, true) 92 93 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 94 {Topic: "foo"}, 95 }), Equals, false) 96 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 97 {Topic: "foo"}, {Topic: "bar"}, 98 }), Equals, true) 99 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 100 {Topic: "foo"}, {Topic: "baz"}, 101 }), Equals, false) 102 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 103 {Topic: "baz"}, {Topic: "foo2"}, 104 }), Equals, false) 105 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 106 {Topic: "bar"}, {Topic: "foo"}, 107 }), Equals, true) 108 109 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{ 110 {Topic: "bar"}, {Topic: "foo"}, {Topic: "baz"}}), Equals, true) 111 112 } 113 114 func (k *kafkaTestSuite) TestUnknownRequest(c *C) { 115 reqMsg := RequestMessage{kind: 18} // ApiVersions request 116 117 // Empty rule should disallow 118 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{}), Equals, false) 119 120 // Whitelisting of unknown message 121 rule1 := api.PortRuleKafka{APIKey: "metadata"} 122 c.Assert(rule1.Sanitize(), IsNil) 123 rule2 := api.PortRuleKafka{APIKey: "apiversions"} 124 c.Assert(rule2.Sanitize(), IsNil) 125 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{rule1, rule2}), Equals, true) 126 127 reqMsg = RequestMessage{kind: 19} 128 c.Assert(reqMsg.MatchesRule([]api.PortRuleKafka{rule1, rule2}), Equals, false) 129 }