|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashMap; |
| 19 | +use std::fs::read_to_string; |
| 20 | +use std::path::{Path, PathBuf}; |
| 21 | + |
| 22 | +use anyhow::{Context, anyhow}; |
| 23 | +use serde::{Deserialize, Serialize}; |
| 24 | +use toml::{Table as TomlTable, Value}; |
| 25 | +use tracing::info; |
| 26 | + |
| 27 | +use crate::engine::Engine; |
| 28 | + |
| 29 | +pub struct Schedule { |
| 30 | + /// Engine names to engine instances |
| 31 | + engines: HashMap<String, Engine>, |
| 32 | + /// List of test steps to run |
| 33 | + steps: Vec<Step>, |
| 34 | + /// Path of the schedule file |
| 35 | + schedule_file: String, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 39 | +pub struct Step { |
| 40 | + /// Engine name |
| 41 | + engine: String, |
| 42 | + /// Stl file path |
| 43 | + slt: String, |
| 44 | +} |
| 45 | + |
| 46 | +impl Schedule { |
| 47 | + pub fn new(engines: HashMap<String, Engine>, steps: Vec<Step>, schedule_file: String) -> Self { |
| 48 | + Self { |
| 49 | + engines, |
| 50 | + steps, |
| 51 | + schedule_file, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub async fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> { |
| 56 | + let path_str = path.as_ref().to_string_lossy().to_string(); |
| 57 | + let content = read_to_string(path)?; |
| 58 | + let toml_value = content.parse::<Value>()?; |
| 59 | + let toml_table = toml_value |
| 60 | + .as_table() |
| 61 | + .ok_or_else(|| anyhow!("Schedule file must be a TOML table"))?; |
| 62 | + |
| 63 | + let engines = Schedule::parse_engines(toml_table).await?; |
| 64 | + let steps = Schedule::parse_steps(toml_table)?; |
| 65 | + |
| 66 | + Ok(Self::new(engines, steps, path_str)) |
| 67 | + } |
| 68 | + |
| 69 | + pub async fn run(mut self) -> anyhow::Result<()> { |
| 70 | + info!("Starting test run with schedule: {}", self.schedule_file); |
| 71 | + |
| 72 | + for (idx, step) in self.steps.iter().enumerate() { |
| 73 | + info!( |
| 74 | + "Running step {}/{}, using engine {}, slt file path: {}", |
| 75 | + idx + 1, |
| 76 | + self.steps.len(), |
| 77 | + &step.engine, |
| 78 | + &step.slt |
| 79 | + ); |
| 80 | + |
| 81 | + let engine = self |
| 82 | + .engines |
| 83 | + .get_mut(&step.engine) |
| 84 | + .ok_or_else(|| anyhow!("Engine {} not found", step.engine))?; |
| 85 | + |
| 86 | + let step_sql_path = PathBuf::from(format!( |
| 87 | + "{}/testdata/slts/{}", |
| 88 | + env!("CARGO_MANIFEST_DIR"), |
| 89 | + &step.slt |
| 90 | + )); |
| 91 | + |
| 92 | + engine.run_slt_file(&step_sql_path).await?; |
| 93 | + |
| 94 | + info!( |
| 95 | + "Completed step {}/{}, engine {}, slt file path: {}", |
| 96 | + idx + 1, |
| 97 | + self.steps.len(), |
| 98 | + &step.engine, |
| 99 | + &step.slt |
| 100 | + ); |
| 101 | + } |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + |
| 105 | + async fn parse_engines(table: &TomlTable) -> anyhow::Result<HashMap<String, Engine>> { |
| 106 | + let engines_tbl = table |
| 107 | + .get("engines") |
| 108 | + .with_context(|| "Schedule file must have an 'engines' table")? |
| 109 | + .as_table() |
| 110 | + .ok_or_else(|| anyhow!("'engines' must be a table"))?; |
| 111 | + |
| 112 | + let mut engines = HashMap::new(); |
| 113 | + |
| 114 | + for (name, engine_val) in engines_tbl { |
| 115 | + let cfg_tbl = engine_val |
| 116 | + .as_table() |
| 117 | + .ok_or_else(|| anyhow!("Config of engine '{name}' is not a table"))? |
| 118 | + .clone(); |
| 119 | + |
| 120 | + let engine = Engine::new(cfg_tbl) |
| 121 | + .await |
| 122 | + .with_context(|| format!("Failed to construct engine '{name}'"))?; |
| 123 | + |
| 124 | + if engines.insert(name.clone(), engine).is_some() { |
| 125 | + return Err(anyhow!("Duplicate engine '{name}'")); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + Ok(engines) |
| 130 | + } |
| 131 | + |
| 132 | + fn parse_steps(table: &TomlTable) -> anyhow::Result<Vec<Step>> { |
| 133 | + let steps_val = table |
| 134 | + .get("steps") |
| 135 | + .with_context(|| "Schedule file must have a 'steps' array")?; |
| 136 | + |
| 137 | + let steps: Vec<Step> = steps_val |
| 138 | + .clone() |
| 139 | + .try_into() |
| 140 | + .with_context(|| "Failed to deserialize steps")?; |
| 141 | + |
| 142 | + Ok(steps) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(test)] |
| 147 | +mod tests { |
| 148 | + use toml::Table as TomlTable; |
| 149 | + |
| 150 | + use crate::schedule::Schedule; |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_parse_steps() { |
| 154 | + let input = r#" |
| 155 | + [[steps]] |
| 156 | + engine = "datafusion" |
| 157 | + slt = "test.slt" |
| 158 | +
|
| 159 | + [[steps]] |
| 160 | + engine = "spark" |
| 161 | + slt = "test2.slt" |
| 162 | + "#; |
| 163 | + |
| 164 | + let tbl: TomlTable = toml::from_str(input).unwrap(); |
| 165 | + let steps = Schedule::parse_steps(&tbl).unwrap(); |
| 166 | + |
| 167 | + assert_eq!(steps.len(), 2); |
| 168 | + assert_eq!(steps[0].engine, "datafusion"); |
| 169 | + assert_eq!(steps[0].slt, "test.slt"); |
| 170 | + assert_eq!(steps[1].engine, "spark"); |
| 171 | + assert_eq!(steps[1].slt, "test2.slt"); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_parse_steps_empty() { |
| 176 | + let input = r#" |
| 177 | + [[steps]] |
| 178 | + "#; |
| 179 | + |
| 180 | + let tbl: TomlTable = toml::from_str(input).unwrap(); |
| 181 | + let steps = Schedule::parse_steps(&tbl); |
| 182 | + |
| 183 | + assert!(steps.is_err()); |
| 184 | + } |
| 185 | + |
| 186 | + #[tokio::test] |
| 187 | + async fn test_parse_engines_invalid_table() { |
| 188 | + let toml_content = r#" |
| 189 | + engines = "not_a_table" |
| 190 | + "#; |
| 191 | + |
| 192 | + let table: TomlTable = toml::from_str(toml_content).unwrap(); |
| 193 | + let result = Schedule::parse_engines(&table).await; |
| 194 | + |
| 195 | + assert!(result.is_err()); |
| 196 | + } |
| 197 | +} |
0 commit comments