|
| 1 | +// Copyright 2022 Datafuse Labs. |
| 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 | + |
| 15 | +use std::collections::BTreeMap; |
| 16 | + |
| 17 | +use databend_common_base::runtime; |
| 18 | +use databend_common_cloud_control::billing_client::BillingClient; |
| 19 | +use databend_common_cloud_control::pb::BillingUsageDailyRow; |
| 20 | +use databend_common_cloud_control::pb::GetBillingUsageDailyRequest; |
| 21 | +use databend_common_cloud_control::pb::GetBillingUsageDailyResponse; |
| 22 | +use databend_common_cloud_control::pb::billing_service_server::BillingService; |
| 23 | +use databend_common_cloud_control::pb::billing_service_server::BillingServiceServer; |
| 24 | +use hyper_util::rt::TokioIo; |
| 25 | +use tonic::Request; |
| 26 | +use tonic::Response; |
| 27 | +use tonic::Status; |
| 28 | +use tonic::codegen::tokio_stream; |
| 29 | +use tonic::transport::Endpoint; |
| 30 | +use tonic::transport::Server; |
| 31 | +use tonic::transport::Uri; |
| 32 | +use tower::service_fn; |
| 33 | + |
| 34 | +#[derive(Default)] |
| 35 | +pub struct MockBillingService {} |
| 36 | + |
| 37 | +#[tonic::async_trait] |
| 38 | +impl BillingService for MockBillingService { |
| 39 | + async fn get_billing_usage_daily( |
| 40 | + &self, |
| 41 | + request: Request<GetBillingUsageDailyRequest>, |
| 42 | + ) -> std::result::Result<Response<GetBillingUsageDailyResponse>, Status> { |
| 43 | + Ok(Response::new(GetBillingUsageDailyResponse { |
| 44 | + rows: vec![BillingUsageDailyRow { |
| 45 | + usage_date: request.into_inner().billing_month, |
| 46 | + usage_type: "compute".to_string(), |
| 47 | + service_type: "WAREHOUSE_METERING".to_string(), |
| 48 | + resource_name: "default".to_string(), |
| 49 | + usage: "2653".to_string(), |
| 50 | + usage_unit: "second".to_string(), |
| 51 | + rate: "".to_string(), |
| 52 | + rate_unit: "second".to_string(), |
| 53 | + usage_in_currency: "0.737".to_string(), |
| 54 | + currency: "USD".to_string(), |
| 55 | + tags: BTreeMap::from([("env".to_string(), "test".to_string())]), |
| 56 | + details: "{\"cluster_name\":\"cl-00000\"}".to_string(), |
| 57 | + }], |
| 58 | + error: None, |
| 59 | + })) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[tokio::test(flavor = "current_thread")] |
| 64 | +async fn test_billing_client_success_cases() -> anyhow::Result<()> { |
| 65 | + let (client, server) = tokio::io::duplex(1024); |
| 66 | + let client = TokioIo::new(client); |
| 67 | + |
| 68 | + runtime::spawn(async move { |
| 69 | + Server::builder() |
| 70 | + .add_service(BillingServiceServer::new(MockBillingService::default())) |
| 71 | + .serve_with_incoming(tokio_stream::iter(vec![Ok::<_, std::io::Error>(server)])) |
| 72 | + .await |
| 73 | + }); |
| 74 | + |
| 75 | + let mut client_io = Some(client); |
| 76 | + let channel = Endpoint::try_from("http://[::]:0") |
| 77 | + .unwrap() |
| 78 | + .connect_with_connector(service_fn(move |_: Uri| { |
| 79 | + let client = client_io.take(); |
| 80 | + |
| 81 | + async move { |
| 82 | + if let Some(client) = client { |
| 83 | + Ok(client) |
| 84 | + } else { |
| 85 | + Err(std::io::Error::other("Client already taken")) |
| 86 | + } |
| 87 | + } |
| 88 | + })) |
| 89 | + .await |
| 90 | + .unwrap(); |
| 91 | + |
| 92 | + let client = BillingClient::new(channel).await?; |
| 93 | + |
| 94 | + let resp = client |
| 95 | + .get_billing_usage_daily(Request::new(GetBillingUsageDailyRequest { |
| 96 | + tenant_id: "tenant".to_string(), |
| 97 | + billing_month: "2026-03".to_string(), |
| 98 | + sql_user: "root".to_string(), |
| 99 | + query_id: "query-1".to_string(), |
| 100 | + })) |
| 101 | + .await?; |
| 102 | + assert_eq!(resp.rows.len(), 1); |
| 103 | + assert_eq!(resp.rows[0].usage_date, "2026-03"); |
| 104 | + assert_eq!(resp.rows[0].usage_type, "compute"); |
| 105 | + assert_eq!(resp.rows[0].resource_name, "default"); |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
0 commit comments