forked from open-policy-agent/opa
/
mock.go
260 lines (219 loc) · 7.01 KB
/
mock.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2019 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package mock defines a fake storage implementation for use in testing.
package mock
import (
"context"
"fmt"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
)
// Transaction is a mock storage.Transaction implementation for use in testing.
// It uses an internal storage.Transaction pointer with some added functionality.
type Transaction struct {
txn storage.Transaction
Committed int
Aborted int
}
// ID returns the underlying transaction ID
func (t *Transaction) ID() uint64 {
return t.txn.ID()
}
// Validate returns an error if the transaction is in an invalid state
func (t *Transaction) Validate() error {
if t.Committed > 1 {
return fmt.Errorf("transaction %d has too many commits (%d)", t.ID(), t.Committed)
}
if t.Aborted > 1 {
return fmt.Errorf("transaction %d has too many aborts (%d)", t.ID(), t.Committed)
}
return nil
}
func (t *Transaction) safeToUse() bool {
return t.Committed == 0 && t.Aborted == 0
}
// Store is a mock storage.Store implementation for use in testing.
type Store struct {
inmem storage.Store
baseData map[string]interface{}
Transactions []*Transaction
Reads []*ReadCall
Writes []*WriteCall
}
// ReadCall captures the parameters for a Read call
type ReadCall struct {
Transaction *Transaction
Path storage.Path
Error error
Safe bool
}
// WriteCall captures the parameters for a write call
type WriteCall struct {
Transaction *Transaction
Op storage.PatchOp
Path storage.Path
Error error
Safe bool
}
// New creates a new mock Store
func New() *Store {
s := &Store{}
s.Reset()
return s
}
// NewWithData creates a store with some initial data
func NewWithData(data map[string]interface{}) *Store {
s := &Store{
baseData: data,
}
s.Reset()
return s
}
// Reset the store
func (s *Store) Reset() {
s.Transactions = []*Transaction{}
s.Reads = []*ReadCall{}
s.Writes = []*WriteCall{}
if s.baseData != nil {
s.inmem = inmem.NewFromObject(s.baseData)
} else {
s.inmem = inmem.New()
}
}
// GetTransaction will a transaction with a specific ID
// that was associated with this Store.
func (s *Store) GetTransaction(id uint64) *Transaction {
for _, txn := range s.Transactions {
if txn.ID() == id {
return txn
}
}
return nil
}
// Errors returns a list of errors for each invalid state found.
// If any Transactions are invalid or reads/writes were
// unsafe an error will be returned for each problem.
func (s *Store) Errors() []error {
var errs []error
for _, txn := range s.Transactions {
err := txn.Validate()
if err != nil {
errs = append(errs, err)
}
}
for _, read := range s.Reads {
if !read.Safe {
errs = append(errs, fmt.Errorf("unsafe Read call %+v", *read))
}
}
for _, write := range s.Writes {
if !write.Safe {
errs = append(errs, fmt.Errorf("unsafe Write call %+v", *write))
}
}
return errs
}
// AssertValid will raise an error with the provided testing.T if
// there are any errors on the store.
func (s *Store) AssertValid(t *testing.T) {
t.Helper()
for _, err := range s.Errors() {
t.Errorf("Error detected on store: %s", err)
}
}
// storage.Store interface implementation
// Register just shims the call to the underlying inmem store
func (s *Store) Register(ctx context.Context, txn storage.Transaction, config storage.TriggerConfig) (storage.TriggerHandle, error) {
return s.inmem.Register(ctx, getRealTxn(txn), config)
}
// ListPolicies just shims the call to the underlying inmem store
func (s *Store) ListPolicies(ctx context.Context, txn storage.Transaction) ([]string, error) {
return s.inmem.ListPolicies(ctx, getRealTxn(txn))
}
// GetPolicy just shims the call to the underlying inmem store
func (s *Store) GetPolicy(ctx context.Context, txn storage.Transaction, name string) ([]byte, error) {
return s.inmem.GetPolicy(ctx, getRealTxn(txn), name)
}
// UpsertPolicy just shims the call to the underlying inmem store
func (s *Store) UpsertPolicy(ctx context.Context, txn storage.Transaction, name string, policy []byte) error {
return s.inmem.UpsertPolicy(ctx, getRealTxn(txn), name, policy)
}
// DeletePolicy just shims the call to the underlying inmem store
func (s *Store) DeletePolicy(ctx context.Context, txn storage.Transaction, name string) error {
return s.inmem.DeletePolicy(ctx, getRealTxn(txn), name)
}
// Build just shims the call to the underlying inmem store
func (s *Store) Build(ctx context.Context, txn storage.Transaction, ref ast.Ref) (storage.Index, error) {
return s.inmem.Build(ctx, getRealTxn(txn), ref)
}
// NewTransaction will create a new transaction on the underlying inmem store
// but wraps it with a mock Transaction. These are then tracked on the store.
func (s *Store) NewTransaction(ctx context.Context, params ...storage.TransactionParams) (storage.Transaction, error) {
realTxn, err := s.inmem.NewTransaction(ctx, params...)
if err != nil {
return nil, err
}
txn := &Transaction{
txn: realTxn,
Committed: 0,
Aborted: 0,
}
s.Transactions = append(s.Transactions, txn)
return txn, nil
}
// Read will make a read from the underlying inmem store and
// add a new entry to the mock store Reads list. If there
// is an error are the read is unsafe it will be noted in
// the ReadCall.
func (s *Store) Read(ctx context.Context, txn storage.Transaction, path storage.Path) (interface{}, error) {
mockTxn := txn.(*Transaction)
data, err := s.inmem.Read(ctx, mockTxn.txn, path)
s.Reads = append(s.Reads, &ReadCall{
Transaction: mockTxn,
Path: path,
Error: err,
Safe: mockTxn.safeToUse(),
})
return data, err
}
// Write will make a read from the underlying inmem store and
// add a new entry to the mock store Writes list. If there
// is an error are the write is unsafe it will be noted in
// the WriteCall.
func (s *Store) Write(ctx context.Context, txn storage.Transaction, op storage.PatchOp, path storage.Path, value interface{}) error {
mockTxn := txn.(*Transaction)
err := s.inmem.Write(ctx, mockTxn.txn, op, path, value)
s.Writes = append(s.Writes, &WriteCall{
Transaction: mockTxn,
Op: op,
Path: path,
Error: err,
Safe: mockTxn.safeToUse(),
})
return nil
}
// Commit will commit the underlying transaction while
// also updating the mock Transaction
func (s *Store) Commit(ctx context.Context, txn storage.Transaction) error {
mockTxn := txn.(*Transaction)
err := s.inmem.Commit(ctx, mockTxn.txn)
if err != nil {
return err
}
mockTxn.Committed++
return nil
}
// Abort will abort the underlying transaction while
// also updating the mock Transaction
func (s *Store) Abort(ctx context.Context, txn storage.Transaction) {
mockTxn := txn.(*Transaction)
s.inmem.Abort(ctx, mockTxn.txn)
mockTxn.Aborted++
return
}
func getRealTxn(txn storage.Transaction) storage.Transaction {
return txn.(*Transaction).txn
}