@@ -1048,6 +1048,7 @@ func TestBuildBundleModeWithManifestRegoVersion(t *testing.T) {
1048
1048
expErrs []string
1049
1049
v0Compatible bool
1050
1050
v1Compatible bool
1051
+ capabilities * ast.Capabilities
1051
1052
}{
1052
1053
{
1053
1054
note : "v0 bundle rego-version" ,
@@ -1181,8 +1182,47 @@ p[4] {
1181
1182
input.x == 1
1182
1183
}` ,
1183
1184
},
1185
+ expManifest : `{"revision":"","roots":["test1","test2"],"rego_version":0,"file_rego_versions":{"%ROOT%/bundle1/test2.rego":1,"%ROOT%/bundle2/test3.rego":1}}` ,
1186
+ },
1187
+ {
1188
+ note : "multiple bundles with different rego-versions, v0-compatible, no rego_v1 capabilities feature" ,
1189
+ v0Compatible : true ,
1190
+ roots : []string {"bundle1" , "bundle2" },
1191
+ files : map [string ]string {
1192
+ "bundle1/.manifest" : `{
1193
+ "roots": ["test1"],
1194
+ "rego_version": 0,
1195
+ "file_rego_versions": {
1196
+ "*/test2.rego": 1
1197
+ }
1198
+ }` ,
1199
+ "bundle1/test1.rego" : `package test1
1200
+ p[1] {
1201
+ input.x == 1
1202
+ }` ,
1203
+ "bundle1/test2.rego" : `package test1
1204
+ p contains 2 if {
1205
+ input.x == 1
1206
+ }` ,
1207
+ "bundle2/.manifest" : `{
1208
+ "roots": ["test2"],
1209
+ "rego_version": 1,
1210
+ "file_rego_versions": {
1211
+ "*/test4.rego": 0
1212
+ }
1213
+ }` ,
1214
+ "bundle2/test3.rego" : `package test2
1215
+ p contains 3 if {
1216
+ input.x == 1
1217
+ }` ,
1218
+ "bundle2/test4.rego" : `package test2
1219
+ p[4] {
1220
+ input.x == 1
1221
+ }` ,
1222
+ },
1223
+ capabilities : capsWithoutFeat (ast .RegoV0 , ast .FeatureRegoV1 ),
1184
1224
expErrs : []string {
1185
- // capabilities inferred from --v0-compatible doesn't include rego_v1 feature, which must be respected
1225
+ // capabilities doesn't include rego_v1 feature, which must be respected
1186
1226
"rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego" ,
1187
1227
},
1188
1228
},
@@ -1282,6 +1322,11 @@ p[4] {
1282
1322
params .v0Compatible = tc .v0Compatible
1283
1323
params .v1Compatible = tc .v1Compatible
1284
1324
1325
+ if tc .capabilities != nil {
1326
+ params .capabilities = newcapabilitiesFlag ()
1327
+ params .capabilities .C = tc .capabilities
1328
+ }
1329
+
1285
1330
if _ , ok := tc .files ["capabilities.json" ]; ok {
1286
1331
_ = params .capabilities .Set (path .Join (root , "capabilities.json" ))
1287
1332
}
@@ -1354,6 +1399,27 @@ p[4] {
1354
1399
}
1355
1400
}
1356
1401
1402
+ func capsWithoutFeat (regoVersion ast.RegoVersion , feat ... string ) * ast.Capabilities {
1403
+ caps := ast .CapabilitiesForThisVersion (ast .CapabilitiesRegoVersion (regoVersion ))
1404
+
1405
+ feats := make ([]string , 0 , len (caps .Features ))
1406
+ for _ , f := range caps .Features {
1407
+ skip := false
1408
+ for _ , skipF := range feat {
1409
+ if f == skipF {
1410
+ skip = true
1411
+ break
1412
+ }
1413
+ }
1414
+ if ! skip {
1415
+ feats = append (feats , f )
1416
+ }
1417
+ }
1418
+ caps .Features = feats
1419
+
1420
+ return caps
1421
+ }
1422
+
1357
1423
func TestBuildBundleFromOtherBundles (t * testing.T ) {
1358
1424
type bundleInfo map [string ]string
1359
1425
@@ -1623,37 +1689,6 @@ p {
1623
1689
"policy_1.rego" : `package test
1624
1690
q contains 1 if {
1625
1691
input.x == 1
1626
- }` ,
1627
- },
1628
- },
1629
- expErrs : []string {
1630
- // capabilities inferred from --v0-compatible doesn't include rego_v1 feature, which must be respected
1631
- "rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego" ,
1632
- },
1633
- },
1634
- {
1635
- note : "single v1 bundle, v0 per-file override, --v0-compatible, rego_v1 capabilities feature" ,
1636
- v0Compatible : true ,
1637
- capabilities : func () * ast.Capabilities {
1638
- caps := ast .CapabilitiesForThisVersion (ast .CapabilitiesRegoVersion (ast .RegoV0 ))
1639
- caps .Features = append (caps .Features , ast .FeatureRegoV1 )
1640
- return caps
1641
- }(),
1642
- bundles : map [string ]bundleInfo {
1643
- "bundle.tar.gz" : {
1644
- ".manifest" : `{
1645
- "rego_version": 1,
1646
- "file_rego_versions": {
1647
- "/policy_0.rego": 0
1648
- }
1649
- }` ,
1650
- "policy_0.rego" : `package test
1651
- p {
1652
- input.x == 1
1653
- }` ,
1654
- "policy_1.rego" : `package test
1655
- q contains 1 if {
1656
- input.x == 1
1657
1692
}` ,
1658
1693
},
1659
1694
},
@@ -1677,37 +1712,35 @@ q contains 1 if {
1677
1712
},
1678
1713
},
1679
1714
{
1680
- note : "v0 bundle + v1 bundle , --v0-compatible" ,
1715
+ note : "single v1 bundle, v0 per-file override , --v0-compatible, no rego_v1 capabilities feature " ,
1681
1716
v0Compatible : true ,
1717
+ capabilities : capsWithoutFeat (ast .RegoV0 , ast .FeatureRegoV1 ),
1682
1718
bundles : map [string ]bundleInfo {
1683
- "bundle_v0.tar.gz" : {
1684
- ".manifest" : `{"roots": ["test1"], "rego_version": 0}` ,
1685
- "policy.rego" : `package test1
1719
+ "bundle.tar.gz" : {
1720
+ ".manifest" : `{
1721
+ "rego_version": 1,
1722
+ "file_rego_versions": {
1723
+ "/policy_0.rego": 0
1724
+ }
1725
+ }` ,
1726
+ "policy_0.rego" : `package test
1686
1727
p {
1687
1728
input.x == 1
1688
1729
}` ,
1689
- },
1690
- "bundle_v1.tar.gz" : {
1691
- ".manifest" : `{"roots": ["test2"], "rego_version": 1}` ,
1692
- "policy.rego" : `package test2
1730
+ "policy_1.rego" : `package test
1693
1731
q contains 1 if {
1694
1732
input.x == 1
1695
1733
}` ,
1696
1734
},
1697
1735
},
1698
1736
expErrs : []string {
1699
- // capabilities inferred from --v0-compatible doesn't include rego_v1 feature, which must be respected
1737
+ // capabilities doesn't include rego_v1 feature, which must be respected
1700
1738
"rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego" ,
1701
1739
},
1702
1740
},
1703
1741
{
1704
- note : "v0 bundle + v1 bundle, --v0-compatible, rego_v1 capabilities feature " ,
1742
+ note : "v0 bundle + v1 bundle, --v0-compatible" ,
1705
1743
v0Compatible : true ,
1706
- capabilities : func () * ast.Capabilities {
1707
- caps := ast .CapabilitiesForThisVersion (ast .CapabilitiesRegoVersion (ast .RegoV0 ))
1708
- caps .Features = append (caps .Features , ast .FeatureRegoV1 )
1709
- return caps
1710
- }(),
1711
1744
bundles : map [string ]bundleInfo {
1712
1745
"bundle_v0.tar.gz" : {
1713
1746
".manifest" : `{"roots": ["test1"], "rego_version": 0}` ,
@@ -1743,6 +1776,31 @@ q contains 1 if {
1743
1776
` ,
1744
1777
},
1745
1778
},
1779
+ {
1780
+ note : "v0 bundle + v1 bundle, --v0-compatible, no rego_v1 capabilities feature" ,
1781
+ v0Compatible : true ,
1782
+ capabilities : capsWithoutFeat (ast .RegoV0 , ast .FeatureRegoV1 ),
1783
+ bundles : map [string ]bundleInfo {
1784
+ "bundle_v0.tar.gz" : {
1785
+ ".manifest" : `{"roots": ["test1"], "rego_version": 0}` ,
1786
+ "policy.rego" : `package test1
1787
+ p {
1788
+ input.x == 1
1789
+ }` ,
1790
+ },
1791
+ "bundle_v1.tar.gz" : {
1792
+ ".manifest" : `{"roots": ["test2"], "rego_version": 1}` ,
1793
+ "policy.rego" : `package test2
1794
+ q contains 1 if {
1795
+ input.x == 1
1796
+ }` ,
1797
+ },
1798
+ },
1799
+ expErrs : []string {
1800
+ // capabilities inferred from --v0-compatible doesn't include rego_v1 feature, which must be respected
1801
+ "rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego" ,
1802
+ },
1803
+ },
1746
1804
{
1747
1805
note : "v0 bundle + v1 bundle, --v1-compatible" ,
1748
1806
v1Compatible : true ,
@@ -2098,6 +2156,20 @@ p[x] {
2098
2156
x := 42
2099
2157
}` ,
2100
2158
},
2159
+ expErrs : []string {
2160
+ "test.rego:2: rego_parse_error: `if` keyword is required before rule body" ,
2161
+ "test.rego:2: rego_parse_error: `contains` keyword is required for partial set rules" ,
2162
+ },
2163
+ },
2164
+ {
2165
+ note : "v0 module, not v0-compatible, v0 capabilities without rego_v1 feature" ,
2166
+ capabilities : capsWithoutFeat (ast .RegoV0 , ast .FeatureRegoV1 ),
2167
+ files : map [string ]string {
2168
+ "test.rego" : `package test
2169
+ p[x] {
2170
+ x := 42
2171
+ }` ,
2172
+ },
2101
2173
expErrs : []string {
2102
2174
"rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego" ,
2103
2175
},
@@ -2188,6 +2260,17 @@ p contains x if {
2188
2260
files : map [string ]string {
2189
2261
"test.rego" : `package test
2190
2262
2263
+ p contains x if {
2264
+ x := 42
2265
+ }` ,
2266
+ },
2267
+ },
2268
+ {
2269
+ note : "v1 module, not v0-compatible, v0 capabilities without rego_v1 feature" ,
2270
+ capabilities : capsWithoutFeat (ast .RegoV0 , ast .FeatureRegoV1 ),
2271
+ files : map [string ]string {
2272
+ "test.rego" : `package test
2273
+
2191
2274
p contains x if {
2192
2275
x := 42
2193
2276
}` ,
0 commit comments