Skip to content

Commit 790e195

Browse files
committed
policy: Refactor tests to allow different request types in a testcase.
This commit introduces changes to add different request types in a single testcases.json file. This allows for testing request types, ex: ExecProcessRequest which depends on state from evaluation of CreateContainerRequest. The changes include: - refactor tests/main.rs to allow different types in testcases.json - modify existing test cases data - add test for ExecProcessRequest Signed-off-by: Sumedh Sharma <[email protected]>
1 parent 9db7002 commit 790e195

File tree

10 files changed

+480
-31
lines changed

10 files changed

+480
-31
lines changed

src/tools/genpolicy/tests/main.rs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,53 @@
66
#[cfg(test)]
77
mod tests {
88
use base64::prelude::*;
9-
use std::any;
109
use std::fs::{self, File};
1110
use std::path;
1211
use std::str;
1312

1413
use protocols::agent::{
1514
CreateContainerRequest, CreateSandboxRequest, UpdateInterfaceRequest, UpdateRoutesRequest,
15+
ExecProcessRequest,
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+
ExecProcess(ExecProcessRequest),
33+
UpdateInterface(UpdateInterfaceRequest),
34+
UpdateRoutes(UpdateRoutesRequest),
35+
}
36+
37+
impl ToString for TestRequest {
38+
fn to_string(&self) -> String {
39+
match self {
40+
TestRequest::LegacyCreateContainer(_) => String::from("CreateContainerRequest"),
41+
TestRequest::CopyFile(_) => String::from("CopyFileRequest"),
42+
TestRequest::CreateContainer(_) => String::from("CreateContainerRequest"),
43+
TestRequest::CreateSandbox(_) => String::from("CreateSandboxRequest"),
44+
TestRequest::ExecProcess(_) => String::from("ExecProcessRequest"),
45+
TestRequest::UpdateInterface(_) => String::from("UpdateInterfaceRequest"),
46+
TestRequest::UpdateRoutes(_) => String::from("UpdateRoutesRequest"),
47+
}
48+
}
49+
}
50+
51+
#[derive(Deserialize, Serialize)]
52+
struct TestCase {
2653
description: String,
2754
allowed: bool,
28-
request: T,
55+
request: TestRequest,
2956
}
3057

3158
/// Run tests from the given directory.
@@ -34,9 +61,7 @@ mod tests {
3461
/// The resources must produce a policy when fed into genpolicy, so there
3562
/// should be exactly one entry with a PodSpec. The test case file must contain
3663
/// a JSON list of [TestCase] instances appropriate for `T`.
37-
async fn runtests<T>(test_case_dir: &str)
38-
where
39-
T: DeserializeOwned + Serialize,
64+
async fn runtests(test_case_dir: &str)
4065
{
4166
// Prepare temp dir for running genpolicy.
4267
let workdir = path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(test_case_dir);
@@ -105,18 +130,16 @@ mod tests {
105130

106131
let case_file =
107132
File::open(testdata_dir.join("testcases.json")).expect("test case file should open");
108-
let test_cases: Vec<TestCase<T>> =
133+
let test_cases: Vec<TestCase> =
109134
serde_json::from_reader(case_file).expect("test case file should parse");
110135

111136
for test_case in test_cases {
112137
println!("\n== case: {} ==\n", test_case.description);
113138

114139
let v = serde_json::to_value(&test_case.request).unwrap();
115140

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

122145
let logs = fs::read_to_string(workdir.join("policy.log")).unwrap();
@@ -130,45 +153,43 @@ 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;
189+
}
190+
191+
#[tokio::test]
192+
async fn test_exec_process() {
193+
runtests("execprocess").await;
173194
}
174195
}

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
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: busybox
5+
spec:
6+
runtimeClassName: kata-cc
7+
containers:
8+
- name: first-test-container
9+
image: "quay.io/prometheus/busybox:latest"
10+
env:
11+
- name: CONTAINER_NAME
12+
value: first-test-container
13+
command:
14+
- sleep
15+
- "3600"
16+
livenessProbe:
17+
exec:
18+
command:
19+
- echo
20+
- test

0 commit comments

Comments
 (0)