Skip to content

Commit a42f0ab

Browse files
committed
genpolicy: Refactor tests to add different request types in testcases json
Cherry-pick upstream changes in PR kata-containers#11074 to add test data for multiple request type in a single testcases.json file. This allows for stateful testing, for ex: enable testing ExecProcessRequest using policy state set after testing a CreateContainerRequest. Signed-off-by: Sumedh Sharma <sumsharma@microsoft.com>
1 parent cf1e7c2 commit a42f0ab

File tree

8 files changed

+66
-32
lines changed

8 files changed

+66
-32
lines changed

src/tools/genpolicy/tests/main.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,51 @@
66
#[cfg(test)]
77
mod tests {
88
use base64::prelude::*;
9-
use std::any;
9+
use std::fmt::{self, Display};
1010
use std::fs::{self, File};
1111
use std::path;
1212
use std::str;
1313

1414
use protocols::agent::{
1515
CreateContainerRequest, CreateSandboxRequest, UpdateInterfaceRequest, UpdateRoutesRequest,
1616
};
17-
use serde::de::DeserializeOwned;
1817
use serde::{Deserialize, Serialize};
1918

2019
use kata_agent_policy::policy::{
2120
AgentPolicy, PolicyCopyFileRequest, PolicyCreateContainerRequest,
2221
};
2322

24-
#[derive(Clone, Debug, Deserialize, Serialize)]
25-
struct TestCase<T> {
23+
// each test case in testcase.json will translate
24+
// to one request type
25+
#[derive(Deserialize, Serialize)]
26+
#[serde(tag = "type")]
27+
enum TestRequest {
28+
LegacyCreateContainer(CreateContainerRequest),
29+
CopyFile(PolicyCopyFileRequest),
30+
CreateContainer(PolicyCreateContainerRequest),
31+
CreateSandbox(CreateSandboxRequest),
32+
UpdateInterface(UpdateInterfaceRequest),
33+
UpdateRoutes(UpdateRoutesRequest),
34+
}
35+
36+
impl Display for TestRequest {
37+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38+
match self {
39+
TestRequest::LegacyCreateContainer(_) => write!(f, "CreateContainerRequest"),
40+
TestRequest::CopyFile(_) => write!(f, "CopyFileRequest"),
41+
TestRequest::CreateContainer(_) => write!(f, "CreateContainerRequest"),
42+
TestRequest::CreateSandbox(_) => write!(f, "CreateSandboxRequest"),
43+
TestRequest::UpdateInterface(_) => write!(f, "UpdateInterfaceRequest"),
44+
TestRequest::UpdateRoutes(_) => write!(f, "UpdateRoutesRequest"),
45+
}
46+
}
47+
}
48+
49+
#[derive(Deserialize, Serialize)]
50+
struct TestCase {
2651
description: String,
2752
allowed: bool,
28-
request: T,
53+
request: TestRequest,
2954
}
3055

3156
/// Run tests from the given directory.
@@ -34,10 +59,7 @@ mod tests {
3459
/// The resources must produce a policy when fed into genpolicy, so there
3560
/// should be exactly one entry with a PodSpec. The test case file must contain
3661
/// a JSON list of [TestCase] instances appropriate for `T`.
37-
async fn runtests<T>(test_case_dir: &str)
38-
where
39-
T: DeserializeOwned + Serialize,
40-
{
62+
async fn runtests(test_case_dir: &str) {
4163
// Prepare temp dir for running genpolicy.
4264
let workdir = path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(test_case_dir);
4365
fs::create_dir_all(&workdir)
@@ -105,18 +127,19 @@ mod tests {
105127

106128
let case_file =
107129
File::open(testdata_dir.join("testcases.json")).expect("test case file should open");
108-
let test_cases: Vec<TestCase<T>> =
130+
let test_cases: Vec<TestCase> =
109131
serde_json::from_reader(case_file).expect("test case file should parse");
110132

111133
for test_case in test_cases {
112134
println!("\n== case: {} ==\n", test_case.description);
113135

114136
let v = serde_json::to_value(&test_case.request).unwrap();
115137

116-
let request_type = map_request(any::type_name::<T>().split("::").last().unwrap());
117-
118138
let results = pol
119-
.allow_request(request_type, &serde_json::to_string(&v).unwrap())
139+
.allow_request(
140+
&test_case.request.to_string(),
141+
&serde_json::to_string(&v).unwrap(),
142+
)
120143
.await;
121144

122145
let logs = fs::read_to_string(workdir.join("policy.log")).unwrap();
@@ -130,45 +153,38 @@ mod tests {
130153
}
131154
}
132155

133-
fn map_request(request: &str) -> &str {
134-
match request {
135-
"PolicyCopyFileRequest" => "CopyFileRequest",
136-
"PolicyCreateContainerRequest" => "CreateContainerRequest",
137-
_ => request,
138-
}
139-
}
140-
141156
#[tokio::test]
142157
async fn test_copyfile() {
143-
runtests::<PolicyCopyFileRequest>("copyfile").await;
158+
runtests("copyfile").await;
144159
}
145160

146161
#[tokio::test]
147162
async fn test_create_sandbox() {
148-
runtests::<CreateSandboxRequest>("createsandbox").await;
163+
runtests("createsandbox").await;
149164
}
150165

151166
#[tokio::test]
152167
async fn test_update_routes() {
153-
runtests::<UpdateRoutesRequest>("updateroutes").await;
168+
runtests("updateroutes").await;
154169
}
155170

156171
#[tokio::test]
157172
async fn test_update_interface() {
158-
runtests::<UpdateInterfaceRequest>("updateinterface").await;
173+
runtests("updateinterface").await;
159174
}
175+
160176
#[tokio::test]
161177
async fn test_legacy_basic_create_container() {
162-
runtests::<CreateContainerRequest>("createContainer/legacy").await;
178+
runtests("createContainer/legacy").await;
163179
}
164180

165181
#[tokio::test]
166182
async fn test_basic_create_container() {
167-
runtests::<PolicyCreateContainerRequest>("createContainer/basic").await;
183+
runtests("createContainer/basic").await;
168184
}
169185

170186
#[tokio::test]
171187
async fn test_create_container_generate_name() {
172-
runtests::<PolicyCreateContainerRequest>("createcontainer/generate_name").await;
188+
runtests("createcontainer/generate_name").await;
173189
}
174190
}

src/tools/genpolicy/tests/testdata/copyfile/testcases.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"description": "copy initiated by k8s mount",
44
"allowed": true,
55
"request": {
6+
"type": "CopyFile",
67
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-resolv.conf"
78
}
89
},
910
{
1011
"description": "attempt to copy outside of container root",
1112
"allowed": false,
1213
"request": {
14+
"type": "CopyFile",
1315
"path": "/etc/ssl/cert.pem"
1416
}
1517
}
16-
]
18+
]

src/tools/genpolicy/tests/testdata/createContainer/basic/testcases.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "basic request for pause container",
44
"allowed": true,
55
"request": {
6+
"type": "CreateContainer",
67
"base": {
78
"OCI": {
89
"Annotations": {
@@ -286,4 +287,4 @@
286287
}
287288
}
288289
}
289-
]
290+
]

src/tools/genpolicy/tests/testdata/createContainer/legacy/testcases.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "legacy request for pause container",
44
"allowed": true,
55
"request": {
6+
"type": "LegacyCreateContainer",
67
"OCI": {
78
"Annotations": {
89
"io.katacontainers.pkg.oci.bundle_path": "/run/containerd/io.containerd.runtime.v2.task/k8s.io/4bbf2a6b6b510a279cd17b2bfc8b64d39c11ebb55f855ba78a0034c4fe394246",
@@ -281,4 +282,4 @@
281282
"string_user": null
282283
}
283284
}
284-
]
285+
]

src/tools/genpolicy/tests/testdata/createcontainer/generate_name/testcases.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "generated name with valid prefix (dummyxyz)",
44
"allowed": true,
55
"request": {
6+
"type": "CreateContainer",
67
"base": {
78
"OCI": {
89
"Annotations": {
@@ -290,6 +291,7 @@
290291
"description": "generated name with invalid prefix (xyzdummy)",
291292
"allowed": false,
292293
"request": {
294+
"type": "CreateContainer",
293295
"base": {
294296
"OCI": {
295297
"Annotations": {
@@ -573,4 +575,4 @@
573575
}
574576
}
575577
}
576-
]
578+
]

src/tools/genpolicy/tests/testdata/createsandbox/testcases.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "no pidns",
44
"allowed": true,
55
"request": {
6+
"type": "CreateSandbox",
67
"sandbox_pidns": false
78
}
89
}

src/tools/genpolicy/tests/testdata/updateinterface/testcases.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "no flags",
44
"allowed": true,
55
"request": {
6+
"type": "UpdateInterface",
67
"interface": {
78
"device": "eth0",
89
"name": "eth0",
@@ -22,6 +23,7 @@
2223
"description": "allowed arp flag",
2324
"allowed": true,
2425
"request": {
26+
"type": "UpdateInterface",
2527
"interface": {
2628
"device": "eth0",
2729
"name": "eth0",
@@ -41,6 +43,7 @@
4143
"description": "forbidden flag",
4244
"allowed": false,
4345
"request": {
46+
"type": "UpdateInterface",
4447
"interface": {
4548
"device": "eth0",
4649
"name": "eth0",
@@ -60,6 +63,7 @@
6063
"description": "forbidden name",
6164
"allowed": false,
6265
"request": {
66+
"type": "UpdateInterface",
6367
"interface": {
6468
"device": "eth0",
6569
"name": "lo",
@@ -79,6 +83,7 @@
7983
"description": "forbidden hwAddr",
8084
"allowed": false,
8185
"request": {
86+
"type": "UpdateInterface",
8287
"interface": {
8388
"device": "eth0",
8489
"name": "eth0",

src/tools/genpolicy/tests/testdata/updateroutes/testcases.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "compliant routes",
44
"allowed": true,
55
"request": {
6+
"type": "UpdateRoutes",
67
"routes": {
78
"Routes": [
89
{
@@ -21,6 +22,7 @@
2122
"description": "forbidden device",
2223
"allowed": false,
2324
"request": {
25+
"type": "UpdateRoutes",
2426
"routes": {
2527
"Routes": [
2628
{
@@ -39,6 +41,7 @@
3941
"description": "one compliant route, one noncompliant",
4042
"allowed": false,
4143
"request": {
44+
"type": "UpdateRoutes",
4245
"routes": {
4346
"Routes": [
4447
{
@@ -65,6 +68,7 @@
6568
"description": "noncompliant routes",
6669
"allowed": false,
6770
"request": {
71+
"type": "UpdateRoutes",
6872
"routes": {
6973
"Routes": [
7074
{
@@ -83,6 +87,7 @@
8387
"description": "noncompliant routes ipv6 1",
8488
"allowed": false,
8589
"request": {
90+
"type": "UpdateRoutes",
8691
"routes": {
8792
"Routes": [
8893
{
@@ -101,6 +106,7 @@
101106
"description": "noncompliant routes ipv6 2",
102107
"allowed": false,
103108
"request": {
109+
"type": "UpdateRoutes",
104110
"routes": {
105111
"Routes": [
106112
{

0 commit comments

Comments
 (0)