forked from istio/istio
/
client.go
148 lines (121 loc) · 3.51 KB
/
client.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policybackend
import (
"fmt"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"istio.io/istio/pkg/test/fakes/policy"
"istio.io/istio/pkg/test/util/retry"
)
type client struct {
controller *policy.Controller
}
// DenyCheck implementation
func (c *client) DenyCheck(t testing.TB, deny bool) {
t.Helper()
if err := c.controller.DenyCheck(deny); err != nil {
t.Fatalf("Error setting DenyCheck: %v", err)
}
}
// ExpectReport implementation
func (c *client) ExpectReport(t testing.TB, expected ...proto.Message) {
t.Helper()
_, err := retry.Do(func() (interface{}, bool, error) {
reports, err := c.controller.GetReports()
if err != nil {
return nil, false, err
}
if !contains(protoArrayToInterfaceArray(reports), protoArrayToInterfaceArray(expected)) {
return nil, false, fmt.Errorf("expected reports not found.\nExpected:\n%v\nActual:\n%v",
spew.Sdump(expected), spew.Sdump(reports))
}
return nil, true, nil
})
if err != nil {
t.Fatalf("ExpectReport failed: %v", err)
}
}
// ExpectReportJSON checks that the backend has received the given report request.
func (c *client) ExpectReportJSON(t testing.TB, expected ...string) {
t.Helper()
_, err := retry.Do(func() (interface{}, bool, error) {
reports, err := c.controller.GetReports()
if err != nil {
return nil, false, err
}
m := jsonpb.Marshaler{
Indent: " ",
}
var actual []string
for _, r := range reports {
as, err := m.MarshalToString(r)
if err != nil {
t.Fatalf("Failed marshalling to string: %v", err)
}
actual = append(actual, as)
}
exMaps := jsonStringsToMaps(t, expected)
acMaps := jsonStringsToMaps(t, actual)
if !contains(mapArrayToInterfaceArray(acMaps), mapArrayToInterfaceArray(exMaps)) {
return nil, false, fmt.Errorf("expected reports not found.\nExpected:\n%v\nActual:\n%v", expected, actual)
}
return nil, true, nil
})
if err != nil {
t.Fatalf("ExpectReportJSON failed: %v", err)
}
}
func (c *client) GetReports(t testing.TB) []proto.Message {
t.Helper()
reports, err := c.controller.GetReports()
if err != nil {
t.Fatalf("PolicyBackend.GetReports: %v", err)
}
return reports
}
// contains checks whether items contains all entries in expected.
func contains(items, expected []interface{}) bool {
mainloop:
for _, e := range expected {
for _, i := range items {
if reflect.DeepEqual(e, i) {
continue mainloop
}
}
return false
}
return true
}
func protoArrayToInterfaceArray(arr []proto.Message) []interface{} {
result := make([]interface{}, len(arr))
for i, p := range arr {
result[i] = p
}
return result
}
func mapArrayToInterfaceArray(arr []map[string]interface{}) []interface{} {
result := make([]interface{}, len(arr))
for i, p := range arr {
result[i] = p
}
return result
}
// Reset implements internal.Resettable.
func (c *client) Reset() error {
return c.controller.Reset()
}