Skip to content

Commit 21b46f3

Browse files
test: Add DefaultPolicy tests. (#40)
Signed-off-by: Philip Conrad <[email protected]>
1 parent 9d019a4 commit 21b46f3

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

test/SmokeTest.Tests/OPAContainerFixture.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ public async Task InitializeAsync()
1515
// Read in the test data files.
1616
var policy = System.IO.File.ReadAllBytes(Path.Combine("testdata", "policy.rego"));
1717
var secondPolicy = System.IO.File.ReadAllBytes(Path.Combine("testdata", "weird_name.rego"));
18+
var systemPolicy = System.IO.File.ReadAllBytes(Path.Combine("testdata", "simple", "system.rego"));
1819
var data = System.IO.File.ReadAllBytes(Path.Combine("testdata", "data.json"));
1920

21+
2022
// Create a new instance of a container.
2123
IContainer container = new ContainerBuilder()
2224
.WithImage("openpolicyagent/opa:latest")
2325
// Bind port 8181 of the container to a random port on the host.
2426
.WithPortBinding(8181, true)
25-
.WithCommand("run", "--server", "policy.rego", "weird_name.rego", "data.json")
27+
.WithCommand("run", "--server", "policy.rego", "weird_name.rego", "system.rego", "data.json")
2628
// Map our policy and data files into the container instance.
2729
.WithResourceMapping(policy, "policy.rego")
2830
.WithResourceMapping(secondPolicy, "weird_name.rego")
31+
.WithResourceMapping(systemPolicy, "system.rego")
2932
.WithResourceMapping(data, "data.json")
3033
// Wait until the HTTP endpoint of the container is available.
3134
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(8181).ForPath("/health")))

test/SmokeTest.Tests/OpenApiTest.cs

+35
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,39 @@ public async Task OpenApiClientEncodedPathTest()
6767

6868
Assert.Equal(true, resultMap?.GetValueOrDefault("allow", false));
6969
}
70+
71+
[Fact]
72+
public async Task OpenApiClientEvaluateDefaultTest()
73+
{
74+
// Construct the request URI by specifying the scheme, hostname, assigned random host port, and the endpoint "uuid".
75+
var requestUri = new UriBuilder(Uri.UriSchemeHttp, _container.Hostname, _container.GetMappedPublicPort(8181)).Uri;
76+
77+
// Send an HTTP GET request to the specified URI and retrieve the response as a string.
78+
var client = new OpaApiClient(serverIndex: 0, serverUrl: requestUri.ToString());
79+
80+
// Exercise the low-level OPA C# SDK.
81+
ExecuteDefaultPolicyWithInputRequest req = new ExecuteDefaultPolicyWithInputRequest()
82+
{
83+
Input = Input.CreateMapOfany(
84+
new Dictionary<string, object>() {
85+
{ "user", "alice" },
86+
{ "action", "read" },
87+
{ "object", "id123" },
88+
{ "type", "dog" },
89+
}),
90+
};
91+
92+
// Note(philip): To to how the API is generated, we have to fire off
93+
// requests directly-- there's no building of requests in advance for later
94+
// launching.
95+
var res = await client.ExecuteDefaultPolicyWithInputAsync(Input.CreateMapOfany(
96+
new Dictionary<string, object>() {
97+
{ "hello", "world" },
98+
}));
99+
var resultMap = res.Result?.MapOfany;
100+
101+
// Ensure we got back the expected fields from the eval.
102+
Assert.Equal("this is the default path", resultMap?.GetValueOrDefault("msg", ""));
103+
Assert.Equal(new Dictionary<string, object>() { { "hello", "world" } }, resultMap?.GetValueOrDefault("echo", ""));
104+
}
70105
}

test/SmokeTest.Tests/SmokeTest.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<ItemGroup>
3434
<None Update="testdata\data.json" CopyToOutputDirectory="PreserveNewest" />
3535
<None Update="testdata\policy.rego" CopyToOutputDirectory="PreserveNewest" />
36+
<None Update="testdata\simple\system.rego" CopyToOutputDirectory="PreserveNewest" />
3637
<None Update="testdata\weird_name.rego" CopyToOutputDirectory="PreserveNewest" />
3738
</ItemGroup>
3839

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package system
2+
3+
# This is used to exercise the default query functionality.
4+
5+
msg := "this is the default path"
6+
7+
main := x {
8+
x := {"msg": msg, "echo": input}
9+
} else {
10+
x := {"msg": msg}
11+
}

0 commit comments

Comments
 (0)