Skip to content

Commit ccb21dc

Browse files
committed
chore: Initialize mock testing
1 parent 3b2c45a commit ccb21dc

File tree

22 files changed

+364
-3
lines changed

22 files changed

+364
-3
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,25 @@ jobs:
9090
9191
- name: Run tests
9292
run: cargo +nightly fuzz run fuzz_openstack_sdk_config --features=fuzzing -- -max_total_time=60
93+
94+
mock_openstack_types:
95+
runs-on: ubuntu-latest
96+
steps:
97+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
98+
99+
- name: Install Rust
100+
uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # stable
101+
with:
102+
toolchain: stable
103+
104+
- name: Rust Cache
105+
uses: swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8
106+
107+
- name: Start prism
108+
run: |
109+
docker run --init --rm -d -v "$GITHUB_WORKSPACE/openstack_types/data":"/data" -p 4010:4010 stoplight/prism:4 mock -h 0.0.0.0 /data/block-storage/v3.yaml -d
110+
111+
- name: Run tests
112+
env:
113+
OPENSTACK_BLOCK_STORAGE_ENDPOINT: http://localhost:4010/v3
114+
run: cargo test -p openstack_types --test mocked

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openstack_sdk/src/auth/authtoken.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ pub struct AuthToken {
159159
pub(crate) auth_info: Option<AuthResponse>,
160160
}
161161

162+
impl From<&str> for AuthToken {
163+
fn from(value: &str) -> Self {
164+
Self {
165+
token: value.into(),
166+
..Default::default()
167+
}
168+
}
169+
}
170+
162171
impl Debug for AuthToken {
163172
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164173
f.debug_struct("Auth")

openstack_sdk/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ pub use crate::openstack::OpenStack;
3636
pub use crate::openstack_async::AsyncOpenStack;
3737

3838
#[allow(dead_code)]
39-
mod test;
39+
pub mod test;

openstack_sdk/src/test/client.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::api::AsyncClient;
3535
#[cfg(feature = "sync")]
3636
use crate::api::Client;
3737
use crate::api::{ApiError, RestClient};
38+
use crate::auth::Auth;
3839

3940
use crate::catalog::{CatalogError, ServiceEndpoint};
4041
use crate::types::identity::v3::Project;
@@ -96,6 +97,9 @@ use crate::RestError;
9697
pub struct FakeOpenStackClient {
9798
/// Known endpoints used by the client
9899
endpoints: HashMap<String, ServiceEndpoint>,
100+
101+
/// Optional auth information to be used
102+
auth: Option<Auth>,
99103
}
100104

101105
impl FakeOpenStackClient {
@@ -104,6 +108,7 @@ impl FakeOpenStackClient {
104108
pub fn new<S: AsRef<str>>(url: S) -> Self {
105109
let mut slf = Self {
106110
endpoints: HashMap::new(),
111+
auth: None,
107112
};
108113
slf.add_endpoint("default", Url::parse(url.as_ref()).unwrap());
109114
slf
@@ -118,6 +123,12 @@ impl FakeOpenStackClient {
118123
);
119124
self
120125
}
126+
127+
/// Set auth information
128+
pub fn set_auth(&mut self, auth: Option<Auth>) -> &mut Self {
129+
self.auth = auth;
130+
self
131+
}
121132
}
122133

123134
impl RestClient for FakeOpenStackClient {
@@ -145,10 +156,13 @@ impl RestClient for FakeOpenStackClient {
145156
impl Client for FakeOpenStackClient {
146157
fn rest(
147158
&self,
148-
request: RequestBuilder,
159+
mut request: RequestBuilder,
149160
body: Vec<u8>,
150161
) -> Result<Response<Bytes>, ApiError<Self::Error>> {
151162
let call = || -> Result<_, Self::Error> {
163+
if let Some(auth) = &self.auth {
164+
auth.set_header(request.headers_mut().unwrap())?;
165+
}
152166
let http_request = request.body(body.clone())?;
153167
let request = http_request.try_into()?;
154168

@@ -175,11 +189,14 @@ impl Client for FakeOpenStackClient {
175189
impl AsyncClient for FakeOpenStackClient {
176190
async fn rest_async(
177191
&self,
178-
request: http::request::Builder,
192+
mut request: http::request::Builder,
179193
body: Vec<u8>,
180194
) -> Result<HttpResponse<Bytes>, ApiError<Self::Error>> {
181195
use futures_util::TryFutureExt;
182196
let call = || async {
197+
if let Some(auth) = &self.auth {
198+
auth.set_header(request.headers_mut().unwrap())?;
199+
}
183200
let http_request = request.body(body)?;
184201
let request = http_request.try_into()?;
185202

openstack_types/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@ chrono = { workspace= true }
4242
serde = { workspace = true }
4343
serde_json = { workspace = true }
4444
structable = { workspace = true }
45+
46+
[dev-dependencies]
47+
openstack_sdk = { path = "../openstack_sdk" }
48+
reqwest.workspace = true
49+
tokio.workspace = true
50+
url.workspace = true
51+
52+
[[test]]
53+
name = "mocked"
54+
path = "tests/mocked/main.rs"
55+
test = false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
mod v3;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
mod response;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
use openstack_sdk::api::block_storage::v3::attachment::list::Request;
16+
use openstack_sdk::api::{paged, Pagination, QueryAsync};
17+
use openstack_types::block_storage::v3::attachment::response::list::AttachmentResponse;
18+
19+
use crate::get_client;
20+
21+
#[tokio::test]
22+
async fn deserialize() -> Result<(), Box<dyn std::error::Error>> {
23+
let client = get_client("block-storage");
24+
25+
let _res: Vec<AttachmentResponse> = paged(Request::builder().build()?, Pagination::Limit(10))
26+
.query_async(&client)
27+
.await?;
28+
29+
Ok(())
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
use openstack_sdk::api::block_storage::v3::attachment::list_detailed::Request;
16+
use openstack_sdk::api::{paged, Pagination, QueryAsync};
17+
use openstack_types::block_storage::v3::attachment::response::list_detailed::AttachmentResponse;
18+
19+
use crate::get_client;
20+
21+
#[tokio::test]
22+
async fn deserialize() -> Result<(), Box<dyn std::error::Error>> {
23+
let client = get_client("block-storage");
24+
25+
let _res: Vec<AttachmentResponse> = paged(Request::builder().build()?, Pagination::Limit(10))
26+
.query_async(&client)
27+
.await?;
28+
29+
Ok(())
30+
}

0 commit comments

Comments
 (0)