173779f3dSLoGin use std::path::PathBuf;
273779f3dSLoGin use test_base::test_context::{self as test_context, test_context};
373779f3dSLoGin
473779f3dSLoGin use crate::{
573779f3dSLoGin context::{
673779f3dSLoGin DadkExecuteContextTestBuildRiscV64V1, DadkExecuteContextTestBuildX86_64V1, TestContextExt,
773779f3dSLoGin },
873779f3dSLoGin executor::Executor,
973779f3dSLoGin parser::Parser,
1073779f3dSLoGin scheduler::{SchedEntities, Scheduler},
1173779f3dSLoGin };
1273779f3dSLoGin
1373779f3dSLoGin use super::create_global_env_list;
1473779f3dSLoGin
setup_executor<T: TestContextExt>(config_file: PathBuf, ctx: &T) -> Executor1573779f3dSLoGin fn setup_executor<T: TestContextExt>(config_file: PathBuf, ctx: &T) -> Executor {
16*d2ade6efSJomo let task = Parser::new(ctx.base_context().config_v2_dir()).parse_config_file(&config_file);
1773779f3dSLoGin assert!(task.is_ok(), "parse error: {:?}", task);
1873779f3dSLoGin let scheduler = Scheduler::new(
1973779f3dSLoGin ctx.execute_context().self_ref().unwrap(),
2073779f3dSLoGin ctx.base_context().fake_dragonos_sysroot(),
2173779f3dSLoGin *ctx.execute_context().action(),
2273779f3dSLoGin vec![],
2373779f3dSLoGin );
2473779f3dSLoGin
2573779f3dSLoGin assert!(scheduler.is_ok(), "Create scheduler error: {:?}", scheduler);
2673779f3dSLoGin
2773779f3dSLoGin let mut scheduler = scheduler.unwrap();
2873779f3dSLoGin
2973779f3dSLoGin let entity = scheduler.add_task(config_file, task.unwrap());
3073779f3dSLoGin
3173779f3dSLoGin assert!(entity.is_ok(), "Add task error: {:?}", entity);
3273779f3dSLoGin let entity = entity.unwrap();
3373779f3dSLoGin let executor = Executor::new(
3473779f3dSLoGin entity.clone(),
3573779f3dSLoGin *ctx.execute_context().action(),
3673779f3dSLoGin ctx.base_context().fake_dragonos_sysroot(),
3773779f3dSLoGin );
3873779f3dSLoGin
3973779f3dSLoGin assert!(executor.is_ok(), "Create executor error: {:?}", executor);
4073779f3dSLoGin
4173779f3dSLoGin let executor = executor.unwrap();
4273779f3dSLoGin return executor;
4373779f3dSLoGin }
4473779f3dSLoGin
4573779f3dSLoGin /// 测试能否正确设置本地环境变量
4673779f3dSLoGin #[test_context(DadkExecuteContextTestBuildX86_64V1)]
4773779f3dSLoGin #[test]
set_local_env(ctx: &DadkExecuteContextTestBuildX86_64V1)4873779f3dSLoGin fn set_local_env(ctx: &DadkExecuteContextTestBuildX86_64V1) {
4973779f3dSLoGin let config_file_path = ctx
5073779f3dSLoGin .base_context()
515129c63bSJomo .config_v2_dir()
525129c63bSJomo .join("app_normal_with_env_0_2_0.toml");
5373779f3dSLoGin let mut executor = setup_executor(config_file_path, ctx);
5473779f3dSLoGin
5573779f3dSLoGin let r = executor.prepare_local_env();
5673779f3dSLoGin assert!(r.is_ok(), "Prepare local env error: {:?}", r);
5773779f3dSLoGin assert_ne!(executor.local_envs.envs.len(), 0);
5873779f3dSLoGin
5973779f3dSLoGin assert!(executor.local_envs.get("DADK_CURRENT_BUILD_DIR").is_some());
6073779f3dSLoGin assert!(executor.local_envs.get("CC").is_some());
6173779f3dSLoGin assert_eq!(executor.local_envs.get("CC").unwrap().value, "abc-gcc");
6273779f3dSLoGin
6373779f3dSLoGin let x = executor.execute();
6473779f3dSLoGin assert!(x.is_ok(), "Execute error: {:?}", x);
6573779f3dSLoGin }
6673779f3dSLoGin
6773779f3dSLoGin /// 测试执行错误时,能否感知到错误
6873779f3dSLoGin #[test_context(DadkExecuteContextTestBuildX86_64V1)]
6973779f3dSLoGin #[test]
execute_should_capture_error(ctx: &DadkExecuteContextTestBuildX86_64V1)7073779f3dSLoGin fn execute_should_capture_error(ctx: &DadkExecuteContextTestBuildX86_64V1) {
7173779f3dSLoGin let config_file_path = ctx
7273779f3dSLoGin .base_context()
735129c63bSJomo .config_v2_dir()
745129c63bSJomo .join("app_normal_with_env_fail_0_2_0.toml");
7573779f3dSLoGin let mut executor = setup_executor(config_file_path, ctx);
7673779f3dSLoGin
7773779f3dSLoGin let r = executor.prepare_local_env();
7873779f3dSLoGin assert!(r.is_ok(), "Prepare local env error: {:?}", r);
7973779f3dSLoGin assert_ne!(executor.local_envs.envs.len(), 0);
8073779f3dSLoGin
8173779f3dSLoGin assert!(executor.local_envs.get("DADK_CURRENT_BUILD_DIR").is_some());
8273779f3dSLoGin assert!(executor.local_envs.get("CC").is_some());
8373779f3dSLoGin assert_eq!(executor.local_envs.get("CC").unwrap().value, "abc-gcc1");
8473779f3dSLoGin
8573779f3dSLoGin let x = executor.execute();
8673779f3dSLoGin assert!(x.is_err(), "Executor cannot catch error when build error");
8773779f3dSLoGin }
8873779f3dSLoGin
8973779f3dSLoGin /// 测试能否正确设置ARCH全局环境变量为x86_64
9073779f3dSLoGin #[test_context(DadkExecuteContextTestBuildX86_64V1)]
9173779f3dSLoGin #[test]
check_arch_env_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1)9273779f3dSLoGin fn check_arch_env_x86_64(ctx: &DadkExecuteContextTestBuildX86_64V1) {
9373779f3dSLoGin let entities = SchedEntities::new();
9473779f3dSLoGin let env_list = create_global_env_list(&entities, &ctx.execute_context().self_ref().unwrap());
9573779f3dSLoGin assert!(
9673779f3dSLoGin env_list.is_ok(),
9773779f3dSLoGin "Create global env list error: {:?}",
9873779f3dSLoGin env_list
9973779f3dSLoGin );
10073779f3dSLoGin let env_list = env_list.unwrap();
10173779f3dSLoGin assert!(env_list.get("ARCH").is_some());
10273779f3dSLoGin assert_eq!(env_list.get("ARCH").unwrap().value, "x86_64");
10373779f3dSLoGin }
10473779f3dSLoGin
10573779f3dSLoGin /// 测试能否正确设置ARCH全局环境变量为riscv64
10673779f3dSLoGin #[test_context(DadkExecuteContextTestBuildRiscV64V1)]
10773779f3dSLoGin #[test]
check_arch_env_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1)10873779f3dSLoGin fn check_arch_env_riscv64(ctx: &DadkExecuteContextTestBuildRiscV64V1) {
10973779f3dSLoGin let entities = SchedEntities::new();
11073779f3dSLoGin let env_list = create_global_env_list(&entities, &ctx.execute_context().self_ref().unwrap());
11173779f3dSLoGin assert!(
11273779f3dSLoGin env_list.is_ok(),
11373779f3dSLoGin "Create global env list error: {:?}",
11473779f3dSLoGin env_list
11573779f3dSLoGin );
11673779f3dSLoGin let env_list = env_list.unwrap();
11773779f3dSLoGin assert!(env_list.get("ARCH").is_some());
11873779f3dSLoGin assert_eq!(env_list.get("ARCH").unwrap().value, "riscv64");
11973779f3dSLoGin }
120