sqlness_runner/cmd/
kube.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::sync::Arc;
16
17use clap::Parser;
18use sqlness::interceptor::Registry;
19use sqlness::{ConfigBuilder, Runner};
20
21use crate::cmd::SqlnessConfig;
22use crate::env::kube::{Env, NaiveResourcesManager};
23use crate::{protocol_interceptor, util};
24
25#[derive(Debug, Parser)]
26/// Run sqlness tests in kube mode.
27pub struct KubeCommand {
28    #[clap(flatten)]
29    config: SqlnessConfig,
30
31    /// Whether to delete the namespace on stop.
32    #[clap(long, default_value = "false")]
33    delete_namespace_on_stop: bool,
34
35    /// Address of the grpc server.
36    #[clap(short, long)]
37    server_addr: String,
38
39    /// Address of the postgres server. Must be set if server_addr is set.
40    #[clap(short, long)]
41    pg_server_addr: String,
42
43    /// Address of the mysql server. Must be set if server_addr is set.
44    #[clap(short, long)]
45    mysql_server_addr: String,
46
47    /// The namespace of the GreptimeDB.
48    #[clap(short, long)]
49    namespace: String,
50}
51
52impl KubeCommand {
53    pub async fn run(self) {
54        let mut interceptor_registry: Registry = Default::default();
55        interceptor_registry.register(
56            protocol_interceptor::PREFIX,
57            Arc::new(protocol_interceptor::ProtocolInterceptorFactory),
58        );
59
60        if let Some(d) = &self.config.case_dir
61            && !d.is_dir()
62        {
63            panic!("{} is not a directory", d.display());
64        }
65
66        let config = ConfigBuilder::default()
67            .case_dir(util::get_case_dir(self.config.case_dir))
68            .fail_fast(self.config.fail_fast)
69            .test_filter(self.config.test_filter)
70            .follow_links(true)
71            .env_config_file(self.config.env_config_file)
72            .interceptor_registry(interceptor_registry)
73            .build()
74            .unwrap();
75
76        let runner = Runner::new(
77            config,
78            Env {
79                delete_namespace_on_stop: self.delete_namespace_on_stop,
80                server_addr: self.server_addr,
81                pg_server_addr: self.pg_server_addr,
82                mysql_server_addr: self.mysql_server_addr,
83                database_manager: Arc::new(()),
84                resources_manager: Arc::new(NaiveResourcesManager::new(self.namespace)),
85            },
86        );
87        match runner.run().await {
88            Ok(_) => println!("\x1b[32mAll sqlness tests passed!\x1b[0m"),
89            Err(e) => {
90                println!("\x1b[31mTest failed: {}\x1b[0m", e);
91                std::process::exit(1);
92            }
93        }
94    }
95}