xref: /DADK/dadk-user/src/executor/tests.rs (revision d2ade6ef037f43d0342021730a0d8b3af7bc36c1)
1 use std::path::PathBuf;
2 use test_base::test_context::{self as test_context, test_context};
3 
4 use crate::{
5     context::{
6         DadkExecuteContextTestBuildRiscV64V1, DadkExecuteContextTestBuildX86_64V1, TestContextExt,
7     },
8     executor::Executor,
9     parser::Parser,
10     scheduler::{SchedEntities, Scheduler},
11 };
12 
13 use super::create_global_env_list;
14 
setup_executor<T: TestContextExt>(config_file: PathBuf, ctx: &T) -> Executor15 fn setup_executor<T: TestContextExt>(config_file: PathBuf, ctx: &T) -> Executor {
16     let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
17     assert!(task.is_ok(), "parse error: {:?}", task);
18     let scheduler = Scheduler::new(
19         ctx.execute_context().self_ref().unwrap(),
20         ctx.base_context().fake_dragonos_sysroot(),
21         *ctx.execute_context().action(),
22         vec![],
23     );
24 
25     assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
26 
27     let mut scheduler = scheduler.unwrap();
28 
29     let entity = scheduler.add_task(config_file, task.unwrap());
30 
31     assert!(entity.is_ok(), "Add task error: {:?}", entity);
32     let entity = entity.unwrap();
33     let executor = Executor::new(
34         entity.clone(),
35         *ctx.execute_context().action(),
36         ctx.base_context().fake_dragonos_sysroot(),
37     );
38 
39     assert!(executor.is_ok(), "Create executor error: {:?}", executor);
40 
41     let executor = executor.unwrap();
42     return executor;
43 }
44 
45 /// 测试能否正确设置本地环境变量
46 #[test_context(DadkExecuteContextTestBuildX86_64V1)]
47 #[test]
set_local_env(ctx: &DadkExecuteContextTestBuildX86_64V1)48 fn set_local_env(ctx: &DadkExecuteContextTestBuildX86_64V1) {
49     let config_file_path = ctx
50         .base_context()
51         .config_v2_dir()
52         .join("app_normal_with_env_0_2_0.toml");
53     let mut executor = setup_executor(config_file_path, ctx);
54 
55     let r = executor.prepare_local_env();
56     assert!(r.is_ok(), "Prepare local env error: {:?}", r);
57     assert_ne!(executor.local_envs.envs.len(), 0);
58 
59     assert!(executor.local_envs.get("DADK_CURRENT_BUILD_DIR").is_some());
60     assert!(executor.local_envs.get("CC").is_some());
61     assert_eq!(executor.local_envs.get("CC").unwrap().value, "abc-gcc");
62 
63     let x = executor.execute();
64     assert!(x.is_ok(), "Execute error: {:?}", x);
65 }
66 
67 /// 测试执行错误时,能否感知到错误
68 #[test_context(DadkExecuteContextTestBuildX86_64V1)]
69 #[test]
execute_should_capture_error(ctx: &DadkExecuteContextTestBuildX86_64V1)70 fn execute_should_capture_error(ctx: &DadkExecuteContextTestBuildX86_64V1) {
71     let config_file_path = ctx
72         .base_context()
73         .config_v2_dir()
74         .join("app_normal_with_env_fail_0_2_0.toml");
75     let mut executor = setup_executor(config_file_path, ctx);
76 
77     let r = executor.prepare_local_env();
78     assert!(r.is_ok(), "Prepare local env error: {:?}", r);
79     assert_ne!(executor.local_envs.envs.len(), 0);
80 
81     assert!(executor.local_envs.get("DADK_CURRENT_BUILD_DIR").is_some());
82     assert!(executor.local_envs.get("CC").is_some());
83     assert_eq!(executor.local_envs.get("CC").unwrap().value, "abc-gcc1");
84 
85     let x = executor.execute();
86     assert!(x.is_err(), "Executor cannot catch error when build error");
87 }
88 
89 /// 测试能否正确设置ARCH全局环境变量为x86_64
90 #[test_context(DadkExecuteContextTestBuildX86_64V1)]
91 #[test]
check_arch_env_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1)92 fn check_arch_env_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1) {
93     let entities = SchedEntities::new();
94     let env_list = create_global_env_list(&entities, &ctx.execute_context().self_ref().unwrap());
95     assert!(
96         env_list.is_ok(),
97         "Create global env list error: {:?}",
98         env_list
99     );
100     let env_list = env_list.unwrap();
101     assert!(env_list.get("ARCH").is_some());
102     assert_eq!(env_list.get("ARCH").unwrap().value, "x86_64");
103 }
104 
105 /// 测试能否正确设置ARCH全局环境变量为riscv64
106 #[test_context(DadkExecuteContextTestBuildRiscV64V1)]
107 #[test]
check_arch_env_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1)108 fn check_arch_env_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1) {
109     let entities = SchedEntities::new();
110     let env_list = create_global_env_list(&entities, &ctx.execute_context().self_ref().unwrap());
111     assert!(
112         env_list.is_ok(),
113         "Create global env list error: {:?}",
114         env_list
115     );
116     let env_list = env_list.unwrap();
117     assert!(env_list.get("ARCH").is_some());
118     assert_eq!(env_list.get("ARCH").unwrap().value, "riscv64");
119 }
120