tests_fuzz/utils/
config.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::path::PathBuf;
16
17use common_telemetry::tracing::info;
18use serde::Serialize;
19use snafu::ResultExt;
20use tinytemplate::TinyTemplate;
21use tokio::fs::File;
22use tokio::io::AsyncWriteExt;
23
24use crate::error;
25use crate::error::Result;
26
27/// Get the path of config dir `tests-fuzz/conf`.
28pub fn get_conf_path() -> PathBuf {
29    let mut root_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
30    root_path.push("conf");
31    root_path
32}
33
34/// Returns rendered config file.
35pub fn render_config_file<C: Serialize>(template_path: &str, context: &C) -> String {
36    let mut tt = TinyTemplate::new();
37    let template = std::fs::read_to_string(template_path).unwrap();
38    tt.add_template(template_path, &template).unwrap();
39    tt.render(template_path, context).unwrap()
40}
41
42// Writes config file to `output_path`.
43pub async fn write_config_file<C: Serialize>(
44    template_path: &str,
45    context: &C,
46    output_path: &str,
47) -> Result<()> {
48    info!("template_path: {template_path}, output_path: {output_path}");
49    let content = render_config_file(template_path, context);
50    let mut config_file = File::create(output_path)
51        .await
52        .context(error::CreateFileSnafu { path: output_path })?;
53    config_file
54        .write_all(content.as_bytes())
55        .await
56        .context(error::WriteFileSnafu { path: output_path })?;
57    Ok(())
58}