forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testgen.go
207 lines (168 loc) · 4.19 KB
/
testgen.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
// Copyright 2018 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 main
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type params struct {
Output string
InputDir string
TestFilePattern string
}
type testCaseSet struct {
Cases []testCase `json:"cases"`
}
type testCase struct {
Note string `json:"note"`
Query string `json:"query"`
Modules []string `json:"modules"`
Input *interface{} `json:"input"`
ReturnCode int `json:"return_code"`
WantError string `json:"want_error"`
}
type compiledTestCaseSet struct {
Cases []compiledTestCase `json:"cases"`
}
type compiledTestCase struct {
testCase
WASM []byte `json:"wasm"`
}
func compileTestCases(ctx context.Context, tests testCaseSet) (*compiledTestCaseSet, error) {
var result []compiledTestCase
for _, tc := range tests.Cases {
args := []func(*rego.Rego){
rego.Query(tc.Query),
}
for idx, module := range tc.Modules {
args = append(args, rego.Module(fmt.Sprintf("module%d.rego", idx), module))
}
cr, err := rego.New(args...).Compile(ctx)
if err != nil {
return nil, err
}
result = append(result, compiledTestCase{
testCase: tc,
WASM: cr.Bytes,
})
}
return &compiledTestCaseSet{Cases: result}, nil
}
func run(params params, args []string) error {
ctx := context.Background()
if err := os.MkdirAll(path.Dir(params.Output), 0755); err != nil {
return err
}
f, err := os.Create(params.Output)
if err != nil {
return err
}
defer f.Close()
gw := gzip.NewWriter(f)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
files, err := ioutil.ReadDir(params.InputDir)
if err != nil {
return err
}
for i := range files {
if ok, err := path.Match(params.TestFilePattern, files[i].Name()); ok {
err := func() error {
bs, err := ioutil.ReadFile(filepath.Join(params.InputDir, files[i].Name()))
if err != nil {
return err
}
var tcs testCaseSet
if err := util.Unmarshal(bs, &tcs); err != nil {
return err
}
ctcs, err := compileTestCases(ctx, tcs)
if err != nil {
return err
}
bs, err = json.Marshal(ctcs)
if err != nil {
return err
}
dst := strings.Replace(files[i].Name(), ".yaml", ".json", -1)
if err := writeFile(tw, dst, bs); err != nil {
return err
}
return nil
}()
if err != nil {
return errors.Wrap(err, files[i].Name())
}
} else if err != nil {
return errors.Wrap(err, files[i].Name())
}
}
return copyFile(tw, "test.js", filepath.Join(params.InputDir, "test.js"))
}
func writeFile(tw *tar.Writer, dst string, bs []byte) error {
hdr := &tar.Header{
Name: strings.TrimLeft(dst, "/"),
Mode: 0600,
Typeflag: tar.TypeReg,
Size: int64(len(bs)),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
_, err := tw.Write(bs)
return err
}
func copyFile(tw *tar.Writer, dst, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
info, err := os.Stat(src)
if err != nil {
return err
}
hdr := &tar.Header{
Name: strings.TrimLeft(dst, "/"),
Mode: 0600,
Typeflag: tar.TypeReg,
Size: info.Size(),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}
_, err = io.Copy(tw, in)
return err
}
func main() {
var params params
executable := path.Base(os.Args[0])
command := &cobra.Command{
Use: executable,
Short: executable,
RunE: func(_ *cobra.Command, args []string) error {
return run(params, args)
},
}
command.Flags().StringVarP(¶ms.Output, "output", "", "", "set path of output file")
command.Flags().StringVarP(¶ms.InputDir, "input-dir", "", "", "set path of input directory containing test files")
command.Flags().StringVarP(¶ms.TestFilePattern, "file-pattern", "", "*.yaml", "set filename pattern to match test files against")
if err := command.Execute(); err != nil {
os.Exit(1)
}
}