xref: /DADK/dadk-user/src/scheduler/tests.rs (revision c9f71754563759e653d59a110f15298891564be9)
1 use dadk_config::common::target_arch::TargetArch;
2 use test_base::{
3     global::BaseGlobalTestContext,
4     test_context::{self as test_context, test_context},
5 };
6 
7 use crate::{
8     context::{
9         DadkExecuteContextTestBuildRiscV64V1, DadkExecuteContextTestBuildX86_64V1, TestContextExt,
10     },
11     parser::Parser,
12 };
13 
14 use super::*;
15 
16 /// 不应在x86_64上运行仅限riscv64的任务
17 #[test_context(DadkExecuteContextTestBuildX86_64V1)]
18 #[test]
19 fn should_not_run_task_only_riscv64_on_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1) {
20     let config_file = ctx
21         .base_context()
22         .config_v2_dir()
23         .join("app_target_arch_riscv64_only_0_2_0.toml");
24     let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
25     assert!(task.is_ok(), "parse error: {:?}", task);
26     let task = task.unwrap();
27     assert!(
28         task.target_arch.len() == 1,
29         "target_arch length error: {:?}",
30         task
31     );
32     assert!(
33         task.target_arch[0] == TargetArch::RiscV64,
34         "target_arch should be riscv64: {:?}",
35         task
36     );
37 
38     let scheduler = Scheduler::new(
39         ctx.execute_context().self_ref().unwrap(),
40         ctx.base_context().fake_dragonos_sysroot(),
41         *ctx.execute_context().action(),
42         vec![],
43     );
44 
45     assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
46 
47     let mut scheduler = scheduler.unwrap();
48 
49     let entity = scheduler.add_task(config_file, task);
50     assert!(
51         entity.is_err(),
52         "Add task should return error: {:?}",
53         entity
54     );
55 }
56 
57 /// 不应在riscv64上运行仅限x86_64的任务
58 #[test_context(DadkExecuteContextTestBuildRiscV64V1)]
59 #[test]
60 fn should_not_run_task_only_x86_64_on_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1) {
61     let config_file = ctx
62         .base_context()
63         .config_v2_dir()
64         .join("app_target_arch_x86_64_only_0_2_0.toml");
65     let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
66     assert!(task.is_ok(), "parse error: {:?}", task);
67     let task = task.unwrap();
68     assert!(
69         task.target_arch.len() == 1,
70         "target_arch length error: {:?}",
71         task
72     );
73     assert!(
74         task.target_arch[0] == TargetArch::X86_64,
75         "target_arch should be x86_64: {:?}",
76         task
77     );
78 
79     let scheduler = Scheduler::new(
80         ctx.execute_context().self_ref().unwrap(),
81         ctx.base_context().fake_dragonos_sysroot(),
82         *ctx.execute_context().action(),
83         vec![],
84     );
85 
86     assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
87 
88     let mut scheduler = scheduler.unwrap();
89 
90     let entity = scheduler.add_task(config_file, task);
91     assert!(
92         entity.is_err(),
93         "Add task should return error: {:?}",
94         entity
95     );
96 }
97 
98 /// 应在x86_64上运行包含x86_64的任务
99 #[test_context(DadkExecuteContextTestBuildX86_64V1)]
100 #[test]
101 fn should_run_task_include_x86_64_on_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1) {
102     let config_file = ctx
103         .base_context()
104         .config_v2_dir()
105         .join("app_all_target_arch_0_2_0.toml");
106     let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
107     assert!(task.is_ok(), "parse error: {:?}", task);
108     let task = task.unwrap();
109 
110     assert!(
111         task.target_arch.contains(&TargetArch::X86_64),
112         "Cannot find target_arch x86_64: {:?}",
113         task
114     );
115 
116     let scheduler = Scheduler::new(
117         ctx.execute_context().self_ref().unwrap(),
118         ctx.base_context().fake_dragonos_sysroot(),
119         *ctx.execute_context().action(),
120         vec![],
121     );
122 
123     assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
124 
125     let mut scheduler = scheduler.unwrap();
126 
127     let entity = scheduler.add_task(config_file, task);
128     assert!(entity.is_ok(), "Add task should return ok: {:?}", entity);
129 }
130 
131 /// 应在riscv64上运行包含riscv64的任务
132 #[test_context(DadkExecuteContextTestBuildRiscV64V1)]
133 #[test]
134 fn should_run_task_include_riscv64_on_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1) {
135     let config_file = ctx
136         .base_context()
137         .config_v2_dir()
138         .join("app_all_target_arch_0_2_0.toml");
139     let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
140     assert!(task.is_ok(), "parse error: {:?}", task);
141     let task = task.unwrap();
142 
143     assert!(
144         task.target_arch.contains(&TargetArch::RiscV64),
145         "Cannot find target_arch riscv64: {:?}",
146         task
147     );
148 
149     let scheduler = Scheduler::new(
150         ctx.execute_context().self_ref().unwrap(),
151         ctx.base_context().fake_dragonos_sysroot(),
152         *ctx.execute_context().action(),
153         vec![],
154     );
155 
156     assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
157 
158     let mut scheduler = scheduler.unwrap();
159 
160     let entity = scheduler.add_task(config_file, task);
161     assert!(entity.is_ok(), "Add task should return ok: {:?}", entity);
162 }
163 
164 /// 确保文件 app_all_target_arch_0_2_0.toml 包含了所有的目标架构
165 #[test_context(BaseGlobalTestContext)]
166 #[test]
167 fn ensure_all_target_arch_testcase_v1(ctx: &BaseGlobalTestContext) {
168     let config_file = ctx.config_v2_dir().join("app_all_target_arch_0_2_0.toml");
169     let task = Parser::new(ctx.config_v2_dir()).parse_config_file(&config_file);
170     assert!(task.is_ok(), "parse error: {:?}", task);
171     let task = task.unwrap();
172 
173     for a in TargetArch::EXPECTED.iter() {
174         let target_arch = TargetArch::try_from(*a).unwrap();
175         assert!(
176             task.target_arch.contains(&target_arch),
177             "Cannot find target_arch '{:?}' in task: {:?}",
178             a,
179             task
180         );
181     }
182 }
183