Skip to content

Commit f08b3e4

Browse files
wip
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent 572358d commit f08b3e4

File tree

7 files changed

+469
-0
lines changed

7 files changed

+469
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- [Rego: basics](https://www.openpolicyagent.org/docs/latest/policy-language/)
99
- [Opa: Awesome collection](https://github.com/StyraInc/awesome-opa)
1010
- [Opa: 101](https://www.permit.io/blog/load-external-data-into-opa)
11+
- [Rego Cool blog](https://www.styra.com/blog/how-to-express-or-in-rego/)
12+
- [Rego Style guide](https://docs.styra.com/opa/rego-style-guide)
1113

1214
## Example Repositories
1315

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"input": {
3+
"name": "other",
4+
"asdfffdas": null,
5+
"asdfffdsdf": null,
6+
"dependencies": null,
7+
"asdf": {
8+
"created_at": "asdf",
9+
"updated_at": "asdf",
10+
"uuid": "asdf",
11+
"file_count": 0,
12+
"version": "aasdf-41141",
13+
"external_uri": "asdf",
14+
"properties": null,
15+
"asdfasffdafdfdf": null,
16+
"asdfasdf": null,
17+
"asdfasdfasdfasdf": {
18+
"asdfffdafdfdfasdfasdf": "asdf",
19+
"timestamp": "asdf",
20+
"input_path": "8d9cd9624a321b",
21+
"total_issues": 2,
22+
"summary": {
23+
"asdfasdf": {
24+
"asdf": 0,
25+
"asdfasdf": 10,
26+
"asdfasdfasdf": 0,
27+
"asdfasdfasdfasdf": 0
28+
}
29+
},
30+
"asdfasdfdfdfdfdfdfdfddf": {
31+
"asdf": [],
32+
"asdfasdf": [],
33+
"asdfasdfasdf": [],
34+
"asdfasdfasdfasdf": [
35+
{
36+
"description": "asdf",
37+
"operator": "asdf",
38+
"module": "__builtin__",
39+
"source": "asdf",
40+
"scanner": "asdf"
41+
},
42+
{
43+
"description": "asdf",
44+
"operator": "asdf",
45+
"module": "asdf",
46+
"source": "asdf",
47+
"scanner": "asdf"
48+
}
49+
]
50+
},
51+
"errors": [],
52+
"skipped": {
53+
"total_skipped": 4,
54+
"skipped_files": [
55+
"asdf",
56+
"asdfasdf"
57+
]
58+
}
59+
},
60+
"model": {
61+
"created_at": "asdf",
62+
"updated_at": "asdf",
63+
"uuid": "asdf",
64+
"name": "a-asdf",
65+
"description": "asdf"
66+
},
67+
"bom_available": false,
68+
"direction": null,
69+
"files": [
70+
{
71+
"uri": "sasdf",
72+
"storage_type": "S3",
73+
"resource_name": "asdf",
74+
"size": 1,
75+
"version": "asdf",
76+
"version_strategy": "S3"
77+
}
78+
]
79+
},
80+
"fasdfasdf": [],
81+
"asdfffdfdfdfdfdfdfd": "asdf",
82+
"dfdfdfdfdfdfdsafasdfasdf": "asdf",
83+
"asdfffdfdfdfdfdfaaaaaa": []
84+
}
85+
}

playground/ex18/example2.rego

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package example2
2+
3+
import rego.v1
4+
5+
6+
default allow := false
7+
8+
9+
deny contains "not not-ray" if {
10+
input.name != "not-ray"
11+
}
12+
13+
14+
allow := true if {
15+
count(deny) == 0
16+
}

playground/ex18/example3.rego

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
package example3
3+
4+
import rego.v1
5+
6+
7+
default allow := false
8+
9+
deny contains "not ray" if {
10+
input.name != "ray"
11+
}
12+
13+
allow := true if {
14+
count(deny) == 0
15+
}

playground/ex19/assert.rego

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# https://github.com/anderseknert/rego-test-assertions/blob/main/test/assert/assert.rego
2+
3+
# METADATA
4+
# description: Utility functions for working with Rego unit tests
5+
# authors:
6+
# - Anders Eknert <[email protected]>
7+
#
8+
package test.assert
9+
10+
import rego.v1
11+
12+
# METADATA
13+
# description: Assert expected is equal to result
14+
equals(expected, result) if {
15+
expected == result
16+
} else := false if {
17+
print("expected equals:", _quote_str(expected), "got:", result)
18+
}
19+
20+
# METADATA
21+
# description: Assert expected is not equal to result
22+
not_equals(expected, result) if {
23+
expected != result
24+
} else := false if {
25+
print("expected not equals:", _quote_str(expected), "got:", result)
26+
}
27+
28+
# METADATA
29+
# description: Assert all items in coll are equal to value
30+
all_equals(coll, value) if {
31+
every item in coll {
32+
item == value
33+
}
34+
} else := false if {
35+
exceptions := [item | some item in coll; item != value]
36+
print("expected all items to have value", _append_comma(value), "failed for", exceptions)
37+
}
38+
39+
# METADATA
40+
# description: Assert no items in coll are equal to value
41+
none_equals(coll, value) if {
42+
every item in coll {
43+
item != value
44+
}
45+
} else := false if {
46+
exceptions := [item | some item in coll; item == value]
47+
print("expected no items to have value", _append_comma(value), "failed for", exceptions)
48+
}
49+
50+
# METADATA
51+
# description: Assert item is in coll
52+
has(item, coll) if {
53+
item in coll
54+
} else := false if {
55+
print("expected", type_name(item), _quote_str(item), "in", type_name(coll), "got:", coll)
56+
}
57+
58+
# METADATA
59+
# description: Assert item is not in coll
60+
not_has(item, coll) if {
61+
not item in coll
62+
} else := false if {
63+
print("expected", type_name(item), _quote_str(item), "not in", type_name(coll), "got:", coll)
64+
}
65+
66+
# METADATA
67+
# description: Assert key is in obj
68+
has_key(key, obj) if {
69+
some keys, _ in obj
70+
key == keys
71+
} else := false if {
72+
print("expected", type_name(key), _quote_str(key), "in", type_name(obj), "got:", obj)
73+
}
74+
75+
# METADATA
76+
# description: Assert key is not in obj
77+
not_has_key(key, obj) := false if {
78+
some keys, _ in obj
79+
key == keys
80+
} else if {
81+
print("expected", type_name(key), _quote_str(key), "not in", type_name(obj), "got:", obj)
82+
}
83+
84+
# METADATA
85+
# description: Assert value is in obj
86+
has_value(value, obj) if {
87+
some _, values in obj
88+
value == values
89+
} else := false if {
90+
print("expected", type_name(value), _quote_str(value), "in", type_name(obj), "got:", obj)
91+
}
92+
93+
# METADATA
94+
# description: Assert value is not in obj
95+
not_has_value(value, obj) := false if {
96+
some _, values in obj
97+
value == values
98+
} else if {
99+
print("expected", type_name(value), _quote_str(value), "not in", type_name(obj), "got:", obj)
100+
}
101+
102+
# METADATA
103+
# description: Assert provided collection is empty
104+
empty(coll) if {
105+
count(coll) == 0
106+
} else := false if {
107+
print("expected empty", type_name(coll), "got:", coll)
108+
}
109+
110+
# METADATA
111+
# description: Assert provided collection is not empty
112+
not_empty(coll) if {
113+
count(coll) != 0
114+
} else := false if {
115+
print("expected not empty", type_name(coll))
116+
}
117+
118+
# METADATA
119+
# description: Assert string starts with search
120+
starts_with(str, search) if {
121+
startswith(str, search)
122+
} else := false if {
123+
print("expected", _quote_str(str), "to start with", _quote_str(search))
124+
}
125+
126+
# METADATA
127+
# description: Assert string ends with search
128+
ends_with(str, search) if {
129+
endswith(str, search)
130+
} else := false if {
131+
print("expected", _quote_str(str), "to end with", _quote_str(search))
132+
}
133+
134+
# METADATA
135+
# description: Assert all strings in coll starts with search
136+
all_starts_with(coll, search) if {
137+
every str in coll {
138+
startswith(str, search)
139+
}
140+
} else := false if {
141+
exceptions := [str | some str in coll; not startswith(str, search)]
142+
print("expected all strings to start with", _append_comma(search), "failed for", exceptions)
143+
}
144+
145+
# METADATA
146+
# description: Assert all strings in coll ends with search
147+
all_ends_with(coll, search) if {
148+
every str in coll {
149+
endswith(str, search)
150+
}
151+
} else := false if {
152+
exceptions := [str | some str in coll; not endswith(str, search)]
153+
print("expected all strings to end with", _append_comma(search), "failed for", exceptions)
154+
}
155+
156+
# METADATA
157+
# description: Assert no strings in coll starts with search
158+
none_starts_with(coll, search) if {
159+
every str in coll {
160+
not startswith(str, search)
161+
}
162+
} else := false if {
163+
exceptions := [str | some str in coll; startswith(str, search)]
164+
print("expected no strings to start with", _append_comma(search), "failed for", exceptions)
165+
}
166+
167+
# METADATA
168+
# description: Assert no strings in coll ends with search
169+
none_ends_with(coll, search) if {
170+
every str in coll {
171+
not endswith(str, search)
172+
}
173+
} else := false if {
174+
exceptions := [str | some str in coll; endswith(str, search)]
175+
print("expected no strings to end with", _append_comma(search), "failed for", exceptions)
176+
}
177+
178+
# METADATA
179+
# description: Fail with provided message
180+
fail(msg) := [][0] if print(msg)
181+
182+
_quote_str(x) := concat("", [`"`, x, `"`]) if is_string(x)
183+
184+
_quote_str(x) := x if not is_string(x)
185+
186+
_append_comma(str) := sprintf("%v,", [_quote_str(str)])

0 commit comments

Comments
 (0)