github.com/fafucoder/cilium@v1.6.11/pkg/monitor/api/types_test.go (about) 1 // Copyright 2016-2018 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 api 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "sort" 23 "testing" 24 "time" 25 26 "github.com/cilium/cilium/pkg/checker" 27 "github.com/cilium/cilium/pkg/labels" 28 "github.com/cilium/cilium/pkg/policy/api" 29 30 . "gopkg.in/check.v1" 31 ) 32 33 // Hook up gocheck into the "go test" runner. 34 func Test(t *testing.T) { 35 TestingT(t) 36 } 37 38 type MonitorAPISuite struct{} 39 40 var _ = Suite(&MonitorAPISuite{}) 41 42 func testEqualityRules(got, expected string, c *C) { 43 gotStruct := &PolicyUpdateNotification{} 44 expectedStruct := &PolicyUpdateNotification{} 45 46 err := json.Unmarshal([]byte(got), gotStruct) 47 c.Assert(err, IsNil) 48 err = json.Unmarshal([]byte(expected), expectedStruct) 49 c.Assert(err, IsNil) 50 c.Assert(gotStruct, checker.DeepEquals, expectedStruct) 51 } 52 53 func testEqualityEndpoint(got, expected string, c *C) { 54 gotStruct := &EndpointRegenNotification{} 55 expectedStruct := &EndpointRegenNotification{} 56 57 err := json.Unmarshal([]byte(got), gotStruct) 58 c.Assert(err, IsNil) 59 err = json.Unmarshal([]byte(expected), expectedStruct) 60 c.Assert(err, IsNil) 61 62 sort.Strings(gotStruct.Labels) 63 sort.Strings(expectedStruct.Labels) 64 c.Assert(gotStruct, checker.DeepEquals, expectedStruct) 65 } 66 67 func (s *MonitorAPISuite) TestRulesRepr(c *C) { 68 rules := api.Rules{ 69 &api.Rule{ 70 Labels: labels.LabelArray{ 71 labels.NewLabel("key1", "value1", labels.LabelSourceUnspec), 72 }, 73 }, 74 &api.Rule{ 75 Labels: labels.LabelArray{ 76 labels.NewLabel("key2", "value2", labels.LabelSourceUnspec), 77 }, 78 }, 79 } 80 81 labels := make([]string, 0, len(rules)) 82 for _, r := range rules { 83 labels = append(labels, r.Labels.GetModel()...) 84 } 85 repr, err := PolicyUpdateRepr(len(rules), labels, 1) 86 87 c.Assert(err, IsNil) 88 testEqualityRules(repr, `{"labels":["unspec:key1=value1","unspec:key2=value2"],"revision":1,"rule_count":2}`, c) 89 } 90 91 func (s *MonitorAPISuite) TestRulesReprEmpty(c *C) { 92 repr, err := PolicyUpdateRepr(0, []string{}, 1) 93 94 c.Assert(err, IsNil) 95 testEqualityRules(repr, `{"revision":1,"rule_count":0}`, c) 96 } 97 98 func (s *MonitorAPISuite) TestPolicyDeleteRepr(c *C) { 99 lab := labels.LabelArray{ 100 labels.NewLabel("key1", "value1", labels.LabelSourceUnspec), 101 } 102 103 repr, err := PolicyDeleteRepr(1, lab.GetModel(), 2) 104 c.Assert(err, IsNil) 105 testEqualityRules(repr, `{"labels":["unspec:key1=value1"],"revision":2,"rule_count":1}`, c) 106 } 107 108 type RegenError struct{} 109 110 func (RegenError) Error() string { 111 return "RegenError" 112 } 113 114 type MockEndpoint struct{} 115 116 func (MockEndpoint) GetID() uint64 { 117 return 10 118 } 119 120 func (MockEndpoint) GetOpLabels() []string { 121 return labels.Labels{"label": labels.NewLabel("key1", "value1", labels.LabelSourceUnspec), 122 "label2": labels.NewLabel("key2", "value2", labels.LabelSourceUnspec), 123 }.GetModel() 124 } 125 126 func (MockEndpoint) GetK8sPodName() string { 127 return "" 128 } 129 130 func (MockEndpoint) GetK8sNamespace() string { 131 return "" 132 } 133 134 func (s *MonitorAPISuite) TestEndpointRegenRepr(c *C) { 135 e := MockEndpoint{} 136 rerr := RegenError{} 137 138 repr, err := EndpointRegenRepr(e, rerr) 139 c.Assert(err, IsNil) 140 testEqualityEndpoint(repr, `{"id":10,"labels":["unspec:key1=value1","unspec:key2=value2"],"error":"RegenError"}`, c) 141 142 repr, err = EndpointRegenRepr(e, nil) 143 c.Assert(err, IsNil) 144 testEqualityEndpoint(repr, `{"id":10,"labels":["unspec:key1=value1","unspec:key2=value2"]}`, c) 145 } 146 147 func (s *MonitorAPISuite) TestTimeRepr(c *C) { 148 t := time.Now() 149 150 repr, err := TimeRepr(t) 151 152 c.Assert(err, IsNil) 153 c.Assert(repr, Equals, fmt.Sprintf(`{"time":"%s"}`, t.String())) 154 }