Skip to content

Commit 2247c0c

Browse files
committed
feat: Introduce macros for skipping tests
- add `catalog show XXX` command to check catalog configuration for the service - add `skip_without_service` and `skip_without_extension` macros to allow tests requiring certain cloud features. Since there is no way to simply skip test in rust return OK.
1 parent b2ec5a8 commit 2247c0c

File tree

13 files changed

+298
-103
lines changed

13 files changed

+298
-103
lines changed

openstack_cli/src/catalog/list.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
//! Catalog list command
16+
17+
use clap::Parser;
18+
19+
use serde::{Deserialize, Serialize};
20+
use serde_json::Value;
21+
22+
use std::fmt;
23+
use tracing::info;
24+
25+
use openstack_sdk::AsyncOpenStack;
26+
27+
use crate::Cli;
28+
use crate::OpenStackCliError;
29+
use crate::output::OutputProcessor;
30+
use structable::StructTable;
31+
use structable::StructTableOptions;
32+
33+
/// Shows current catalog information
34+
#[derive(Parser)]
35+
pub struct ListCommand {}
36+
37+
/// Catalog entries
38+
#[derive(Deserialize, Serialize)]
39+
pub struct VecCatalogEndpoints(pub Vec<CatalogEndpoint>);
40+
41+
/// Catalog
42+
#[derive(Deserialize, Serialize, StructTable)]
43+
pub struct Catalog {
44+
/// Service type
45+
#[structable(title = "service_type")]
46+
#[serde(rename = "type")]
47+
service_type: String,
48+
49+
/// Service name
50+
#[structable(title = "service_name")]
51+
name: String,
52+
53+
/// Service endpoints
54+
endpoints: VecCatalogEndpoints,
55+
}
56+
57+
/// Catalog entry representation
58+
#[derive(Deserialize, Serialize, StructTable)]
59+
pub struct CatalogEndpoint {
60+
/// id
61+
id: String,
62+
/// Interface
63+
interface: String,
64+
///Region
65+
region: String,
66+
/// URL
67+
url: String,
68+
}
69+
70+
impl fmt::Display for CatalogEndpoint {
71+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72+
write!(
73+
f,
74+
"interface: {}, region: {}, url: {}",
75+
self.interface, self.region, self.url
76+
)
77+
}
78+
}
79+
80+
impl fmt::Display for VecCatalogEndpoints {
81+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82+
write!(
83+
f,
84+
"{}",
85+
self.0
86+
.iter()
87+
.map(|v| v.to_string())
88+
.collect::<Vec<String>>()
89+
.join("\n")
90+
)
91+
}
92+
}
93+
94+
impl ListCommand {
95+
/// Perform command action
96+
pub async fn take_action(
97+
&self,
98+
parsed_args: &Cli,
99+
client: &mut AsyncOpenStack,
100+
) -> Result<(), OpenStackCliError> {
101+
info!("Show Catalog");
102+
103+
let op = OutputProcessor::from_args(parsed_args);
104+
op.validate_args(parsed_args)?;
105+
106+
let data: Vec<Value> = client
107+
.get_token_catalog()
108+
.unwrap_or_default()
109+
.into_iter()
110+
.map(|x| serde_json::to_value(x).unwrap())
111+
.collect();
112+
113+
op.output_list::<Catalog>(data).unwrap();
114+
Ok(())
115+
}
116+
}

openstack_cli/src/catalog/mod.rs

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@
1616
1717
use clap::{Parser, Subcommand};
1818

19-
use serde::{Deserialize, Serialize};
20-
use serde_json::Value;
21-
22-
use std::fmt;
23-
use tracing::info;
24-
2519
use openstack_sdk::AsyncOpenStack;
2620

2721
use crate::Cli;
2822
use crate::OpenStackCliError;
29-
use crate::output::OutputProcessor;
30-
use structable::StructTable;
31-
use structable::StructTableOptions;
23+
24+
mod list;
25+
mod show;
3226

3327
/// Catalog commands args
3428
#[derive(Parser)]
@@ -42,7 +36,8 @@ pub struct CatalogCommand {
4236
#[allow(missing_docs)]
4337
#[derive(Subcommand)]
4438
pub enum CatalogCommands {
45-
List(ListCommand),
39+
List(list::ListCommand),
40+
Show(show::ShowCommand),
4641
}
4742

4843
impl CatalogCommand {
@@ -54,91 +49,7 @@ impl CatalogCommand {
5449
) -> Result<(), OpenStackCliError> {
5550
match &self.command {
5651
CatalogCommands::List(cmd) => cmd.take_action(parsed_args, session).await,
52+
CatalogCommands::Show(cmd) => cmd.take_action(parsed_args, session).await,
5753
}
5854
}
5955
}
60-
61-
/// Shows current catalog information
62-
#[derive(Parser)]
63-
pub struct ListCommand {}
64-
65-
/// Catalog entries
66-
#[derive(Deserialize, Serialize)]
67-
pub struct VecCatalogEndpoints(pub Vec<CatalogEndpoint>);
68-
69-
/// Catalog
70-
#[derive(Deserialize, Serialize, StructTable)]
71-
pub struct Catalog {
72-
/// Service type
73-
#[structable(title = "service_type")]
74-
#[serde(rename = "type")]
75-
service_type: String,
76-
77-
/// Service name
78-
#[structable(title = "service_name")]
79-
name: String,
80-
81-
/// Service endpoints
82-
endpoints: VecCatalogEndpoints,
83-
}
84-
85-
/// Catalog entry representation
86-
#[derive(Deserialize, Serialize, StructTable)]
87-
pub struct CatalogEndpoint {
88-
/// id
89-
id: String,
90-
/// Interface
91-
interface: String,
92-
///Region
93-
region: String,
94-
/// URL
95-
url: String,
96-
}
97-
98-
impl fmt::Display for CatalogEndpoint {
99-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100-
write!(
101-
f,
102-
"interface: {}, region: {}, url: {}",
103-
self.interface, self.region, self.url
104-
)
105-
}
106-
}
107-
108-
impl fmt::Display for VecCatalogEndpoints {
109-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110-
write!(
111-
f,
112-
"{}",
113-
self.0
114-
.iter()
115-
.map(|v| v.to_string())
116-
.collect::<Vec<String>>()
117-
.join("\n")
118-
)
119-
}
120-
}
121-
122-
impl ListCommand {
123-
/// Perform command action
124-
pub async fn take_action(
125-
&self,
126-
parsed_args: &Cli,
127-
client: &mut AsyncOpenStack,
128-
) -> Result<(), OpenStackCliError> {
129-
info!("Show Catalog");
130-
131-
let op = OutputProcessor::from_args(parsed_args);
132-
op.validate_args(parsed_args)?;
133-
134-
let data: Vec<Value> = client
135-
.get_token_catalog()
136-
.unwrap_or_default()
137-
.into_iter()
138-
.map(|x| serde_json::to_value(x).unwrap())
139-
.collect();
140-
141-
op.output_list::<Catalog>(data).unwrap();
142-
Ok(())
143-
}
144-
}

openstack_cli/src/catalog/show.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
//! Catalog list command
16+
17+
use clap::Args;
18+
use serde::{Deserialize, Serialize};
19+
use serde_json::Value;
20+
use tracing::info;
21+
22+
use openstack_sdk::AsyncOpenStack;
23+
24+
use crate::Cli;
25+
use crate::OpenStackCliError;
26+
use crate::output::OutputProcessor;
27+
use structable::StructTable;
28+
use structable::StructTableOptions;
29+
30+
/// Shows current catalog information
31+
#[derive(Args)]
32+
pub struct ShowCommand {
33+
/// Service type
34+
#[arg(long, alias = "type")]
35+
service_type: String,
36+
}
37+
38+
/// Catalog entry representation
39+
#[derive(Deserialize, Serialize, StructTable)]
40+
pub struct CatalogEndpoint {
41+
/// Endpoint Id
42+
id: String,
43+
/// Interface
44+
interface: String,
45+
/// Region
46+
region: String,
47+
/// URL
48+
url: String,
49+
}
50+
51+
impl ShowCommand {
52+
/// Perform command action
53+
pub async fn take_action(
54+
&self,
55+
parsed_args: &Cli,
56+
client: &mut AsyncOpenStack,
57+
) -> Result<(), OpenStackCliError> {
58+
info!("Show service endpoint catalog configuration");
59+
60+
let op = OutputProcessor::from_args(parsed_args);
61+
op.validate_args(parsed_args)?;
62+
63+
let data: Vec<Value> = client
64+
.get_token_catalog()
65+
.unwrap_or_default()
66+
.into_iter()
67+
.filter(|x| x.service_type == self.service_type)
68+
.flat_map(|x| {
69+
let mut eps = x.endpoints;
70+
eps.sort_by_key(|x| format!("{}{}", x.region, x.interface));
71+
eps
72+
})
73+
.map(|x| serde_json::to_value(x).unwrap())
74+
.collect();
75+
76+
if data.is_empty() {
77+
return Err(openstack_sdk::catalog::CatalogError::ServiceNotConfigured(
78+
self.service_type.clone(),
79+
)
80+
.into());
81+
}
82+
83+
op.output_list::<CatalogEndpoint>(data).unwrap();
84+
Ok(())
85+
}
86+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
//
15+
// WARNING: This file is automatically generated from OpenAPI schema using
16+
// `openstack-codegenerator`.
17+
18+
use assert_cmd::prelude::*;
19+
use std::process::Command;
20+
21+
#[test]
22+
fn list() -> Result<(), Box<dyn std::error::Error>> {
23+
let mut cmd = Command::cargo_bin("osc")?;
24+
25+
cmd.args(["block-storage", "attachment", "list"]);
26+
cmd.assert().success();
27+
28+
Ok(())
29+
}

openstack_cli/tests/block_storage/v3/attachment/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
mod create_327_autogen;
1616
mod create_354_autogen;
1717
mod delete_autogen;
18+
mod list;
1819
mod list_autogen;
1920
mod os_complete_autogen;
2021
mod set_327_autogen;

openstack_cli/tests/image/v2/image/file/roundtrip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn image_upload_download_roundtrip() -> Result<(), Box<dyn std::error::Err
4747

4848
let img_name = format!(
4949
"test-rust-{}",
50-
Alphanumeric.sample_string(&mut rand::thread_rng(), 16)
50+
Alphanumeric.sample_string(&mut rand::rng(), 16)
5151
);
5252

5353
// Create image

0 commit comments

Comments
 (0)