cloud-sdk-testkit 0.30.4

Provider-neutral mock transport and fixture boundary for cloud-sdk.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use core::future::Future;
use core::task::{Context, Poll, Waker};

use cloud_sdk::authentication::{AuthenticationScopePolicy, ScopeRequirement};
use cloud_sdk::operation::{
    BodyReplayability, ContentTypePolicy, CostIntent, OperationImpact, OperationMetadata,
    PreparedExecutionError, PreparedRequest, ProviderService, RequestIdPolicy, RequestSemantics,
    ResponseBodyPolicy, ResponsePolicy, ResponsePolicyError, RetryEligibility,
};
use cloud_sdk::transport::{
    ContentType, EndpointIdentity, EndpointIdentityError, EndpointPolicy, EndpointScheme,
    HeaderName, MediaType, RawResponsePolicy, RequestHeader, RequestHeaders, RequestTarget,
    ResponseMediaPolicy, StatusCode, TransportRequest,
};
use cloud_sdk::{
    Method, ProviderId, ProviderMarker, ServiceId, ServiceMarker, provider_id, service_id,
};

use crate::{
    ExpectedRequest, FixtureBody, LocalMockTransport, MockError, MockExchange, MockTransport,
    PreparedRequestRecord, ResponseFixture,
};

static OK_STATUS: [StatusCode; 1] = [StatusCode::OK];
static JSON_MEDIA: [MediaType<'static>; 1] = [MediaType::JSON];
static JSON_REQUEST_HEADERS: [RequestHeader<'static>; 1] =
    [RequestHeader::content_type(ContentType::JSON)];

enum ExampleProvider {}

impl ProviderMarker for ExampleProvider {
    const ID: ProviderId = provider_id!("example");
}

enum ComputeService {}

impl ServiceMarker for ComputeService {
    type Provider = ExampleProvider;
    const ID: ServiceId = service_id!("compute");
}

#[test]
fn prepared_records_capture_policy_and_redact_request_values() {
    let prepared = mutation_prepared_request(16);
    assert!(prepared.is_ok());
    let Ok(prepared) = prepared else {
        unreachable!("testkit security fixture construction failed");
    };
    let record = PreparedRequestRecord::capture(prepared);
    assert_eq!(record.method(), Method::Post);
    assert_eq!(record.target_len(), 8);
    assert_eq!(record.body_len(), 2);
    assert!(record.has_request_content_type());
    assert_eq!(record.header_count(), 1);
    assert_eq!(record.sensitive_header_count(), 0);
    assert_eq!(record.service().provider_id(), ExampleProvider::ID);
    assert_eq!(record.service().service_id(), ComputeService::ID);
    assert_eq!(record.metadata().impact(), OperationImpact::Mutation);
    assert_eq!(
        record.metadata().retry_eligibility(),
        RetryEligibility::ExplicitPolicy
    );
    assert_eq!(record.response_policy().max_body_bytes(), 16);
    assert_eq!(
        record.authentication_policy().provider_requirement(),
        ScopeRequirement::Required(ExampleProvider::ID)
    );
    assert_eq!(
        record.authentication_policy().service_requirement(),
        ScopeRequirement::Required(ComputeService::ID)
    );
    assert!(matches!(
        record.authentication_policy().endpoint_requirement(),
        ScopeRequirement::Required(_)
    ));
    assert_eq!(
        record.authentication_policy().audience_requirement(),
        ScopeRequirement::Forbidden
    );
    assert_eq!(record.raw_response_policy().max_body_bytes(), 16);
    assert_eq!(
        record.body_replayability(),
        BodyReplayability::NotReplayable
    );
    assert!(record.raw_response_policy().admits_header("content-type"));
    assert!(record.raw_response_policy().admits_header("x-request-id"));

    let debug = alloc::format!("{record:?}");
    assert!(debug.contains("[redacted]"));
    assert!(!debug.contains("/servers"));
    assert!(!debug.contains("{}"));
}

#[test]
fn bound_mock_executes_prepared_requests_for_blocking_and_async_contracts() {
    let prepared = prepared_request(16);
    let first_exchange = successful_exchange();
    let second_exchange = successful_exchange();
    let endpoint = official_endpoint();
    assert!(
        prepared.is_ok() && first_exchange.is_ok() && second_exchange.is_ok() && endpoint.is_ok()
    );
    let (Ok(prepared), Ok(first_exchange), Ok(second_exchange), Ok(endpoint)) =
        (prepared, first_exchange, second_exchange, endpoint)
    else {
        unreachable!("testkit security fixture construction failed");
    };
    let exchanges = [first_exchange, second_exchange];
    let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);

    let mut blocking_output = [0_u8; 32];
    let mut blocking_headers = [0_u8; 8192];
    let blocking = prepared.execute_blocking(&mock, &mut blocking_output, &mut blocking_headers);
    assert!(
        blocking
            .is_ok_and(|response| { response.with_borrowed(|checked| checked.body() == b"{}") })
    );

    let mut async_output = [0_u8; 32];
    let mut async_headers = [0_u8; 8192];
    let future = prepared.execute_async(&mock, &mut async_output, &mut async_headers);
    let mut future = core::pin::pin!(future);
    let waker = Waker::noop();
    let mut context = Context::from_waker(waker);
    let asynchronous = Future::poll(future.as_mut(), &mut context);
    assert!(matches!(asynchronous, Poll::Ready(Ok(_))));
    assert!(mock.is_complete());
}

#[test]
fn local_async_mock_executes_a_checked_prepared_request() {
    let prepared = prepared_request(16);
    let exchange = successful_exchange();
    let endpoint = official_endpoint();
    assert!(prepared.is_ok() && exchange.is_ok() && endpoint.is_ok());
    let (Ok(prepared), Ok(exchange), Ok(endpoint)) = (prepared, exchange, endpoint) else {
        unreachable!("testkit security fixture construction failed");
    };
    let exchanges = [exchange];
    let mock = LocalMockTransport::new(&exchanges).with_endpoint(endpoint);
    let mut output = [0_u8; 32];
    let mut headers = [0_u8; 8192];
    let future = prepared.execute_local_async(&mock, &mut output, &mut headers);
    let mut future = core::pin::pin!(future);
    let mut context = Context::from_waker(Waker::noop());
    assert!(matches!(
        Future::poll(future.as_mut(), &mut context),
        Poll::Ready(Ok(_))
    ));
    assert!(mock.is_complete());
}

#[test]
fn mock_models_endpoint_status_content_type_and_empty_body_failures() {
    let prepared = prepared_request(16);
    let expected = expected_request();
    let endpoint = official_endpoint();
    let other = other_endpoint();
    assert!(prepared.is_ok() && expected.is_ok() && endpoint.is_ok() && other.is_ok());
    let (Ok(prepared), Ok(expected), Ok(endpoint), Ok(other)) =
        (prepared, expected, endpoint, other)
    else {
        unreachable!("testkit security fixture construction failed");
    };
    let Ok(json_body) = FixtureBody::new(b"{}") else {
        unreachable!("testkit security fixture construction failed");
    };
    let Ok(empty_body) = FixtureBody::new(b"") else {
        unreachable!("testkit security fixture construction failed");
    };

    let success = ResponseFixture::success(json_body).with_content_type("application/json");
    let exchanges = [MockExchange::new(expected, success)];
    let wrong_endpoint = MockTransport::new(&exchanges).with_endpoint(other);
    let mut output = [0_u8; 16];
    let mut response_headers = [0_u8; 8192];
    assert!(matches!(
        prepared.execute_blocking(&wrong_endpoint, &mut output, &mut response_headers),
        Err(PreparedExecutionError::EndpointMismatch)
    ));
    assert_eq!(wrong_endpoint.remaining(), 1);

    let error = ResponseFixture::error(StatusCode::TOO_MANY_REQUESTS, json_body);
    assert!(error.is_ok());
    if let Ok(error) = error {
        let exchanges = [MockExchange::new(
            expected,
            error.with_content_type("application/json"),
        )];
        let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);
        assert!(matches!(
            prepared.execute_blocking(&mock, &mut output, &mut response_headers),
            Err(PreparedExecutionError::ResponsePolicy(
                ResponsePolicyError::UnexpectedStatus
            ))
        ));
    } else {
        unreachable!("provider-error fixture construction failed");
    }

    for (fixture, expected_error) in [
        (
            ResponseFixture::success(json_body),
            ResponsePolicyError::MissingContentType,
        ),
        (
            ResponseFixture::success(json_body).with_content_type("text/plain"),
            ResponsePolicyError::UnexpectedContentType,
        ),
        (
            ResponseFixture::success(empty_body).with_content_type("application/json"),
            ResponsePolicyError::MissingBody,
        ),
    ] {
        let exchanges = [MockExchange::new(expected, fixture)];
        let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);
        assert!(matches!(
            prepared.execute_blocking(&mock, &mut output, &mut response_headers),
            Err(PreparedExecutionError::ResponsePolicy(error))
                if error == expected_error
        ));
    }
}

#[test]
fn mock_models_oversized_responses_and_retry_classification_mistakes() {
    let prepared = prepared_request(2);
    let expected = expected_request();
    let endpoint = official_endpoint();
    assert!(prepared.is_ok() && expected.is_ok() && endpoint.is_ok());
    let (Ok(prepared), Ok(expected), Ok(endpoint)) = (prepared, expected, endpoint) else {
        unreachable!("testkit security fixture construction failed");
    };
    let Ok(oversized_body) = FixtureBody::new(b"123") else {
        unreachable!("testkit security fixture construction failed");
    };
    let fixture = ResponseFixture::success(oversized_body).with_content_type("application/json");
    let exchanges = [MockExchange::new(expected, fixture)];
    let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);
    let mut output = [0_u8; 64];
    let mut response_headers = [0_u8; 8192];
    assert!(matches!(
        prepared.execute_blocking(&mock, &mut output, &mut response_headers),
        Err(PreparedExecutionError::Transport(
            MockError::ResponseBufferTooSmall
        ))
    ));
    assert_eq!(mock.remaining(), 1);

    let Some(mutation) = mutation_prepared_request(2).ok() else {
        unreachable!("testkit security fixture construction failed");
    };
    let record = PreparedRequestRecord::capture(mutation);
    assert_ne!(record.metadata().impact(), OperationImpact::ReadOnly);
    assert_ne!(record.metadata().semantics(), RequestSemantics::Safe);
    assert_eq!(
        record.metadata().retry_eligibility(),
        RetryEligibility::ExplicitPolicy
    );
}

#[test]
fn mock_rejects_unbound_endpoints_request_media_mismatch_and_invalid_fixture_media() {
    let prepared = prepared_request(16);
    let exchange = successful_exchange();
    assert!(prepared.is_ok() && exchange.is_ok());
    let (Ok(prepared), Ok(exchange)) = (prepared, exchange) else {
        unreachable!("testkit security fixture construction failed");
    };
    let exchanges = [exchange];
    let unbound = MockTransport::new(&exchanges);
    let mut output = [0xA5_u8; 16];
    let mut response_headers = [0xA5_u8; 8192];
    assert!(matches!(
        prepared.execute_blocking(&unbound, &mut output, &mut response_headers),
        Err(PreparedExecutionError::EndpointIdentity(
            EndpointIdentityError::UnboundTransport
        ))
    ));
    assert_eq!(unbound.remaining(), 1);
    assert_eq!(output, [0_u8; 16]);

    let endpoint = official_endpoint();
    let target = RequestTarget::new("/servers");
    let body = FixtureBody::new(b"{}");
    assert!(endpoint.is_ok() && target.is_ok() && body.is_ok());
    let (Ok(endpoint), Ok(target), Ok(body)) = (endpoint, target, body) else {
        unreachable!("testkit security fixture construction failed");
    };
    let no_media_expectation = ExpectedRequest::new(Method::Get, target).with_body(b"{}");
    let exchanges = [MockExchange::new(
        no_media_expectation,
        ResponseFixture::success(body).with_content_type("application/json"),
    )];
    let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);
    assert!(matches!(
        prepared.execute_blocking(&mock, &mut output, &mut response_headers),
        Err(PreparedExecutionError::Transport(
            MockError::HeadersMismatch
        ))
    ));
    assert_eq!(mock.remaining(), 1);

    let expected = expected_request();
    assert!(expected.is_ok());
    if let Ok(expected) = expected {
        let exchanges = [MockExchange::new(
            expected,
            ResponseFixture::success(body).with_content_type("application/json; charset"),
        )];
        let mock = MockTransport::new(&exchanges).with_endpoint(endpoint);
        output.fill(0xA5);
        assert!(matches!(
            prepared.execute_blocking(&mock, &mut output, &mut response_headers),
            Err(PreparedExecutionError::Transport(
                MockError::InvalidFixtureMetadata
            ))
        ));
        assert_eq!(output, [0_u8; 16]);
        assert_eq!(mock.remaining(), 1);
    } else {
        unreachable!("expected-request fixture construction failed");
    }
}

fn prepared_request(max_body_bytes: usize) -> Result<PreparedRequest<'static>, ()> {
    build_prepared_request(
        max_body_bytes,
        OperationImpact::ReadOnly,
        RequestSemantics::Safe,
        RetryEligibility::ExplicitPolicy,
        CostIntent::NoKnownCost,
    )
}

fn mutation_prepared_request(max_body_bytes: usize) -> Result<PreparedRequest<'static>, ()> {
    build_prepared_request(
        max_body_bytes,
        OperationImpact::Mutation,
        RequestSemantics::Idempotent,
        RetryEligibility::ExplicitPolicy,
        CostIntent::MayIncurCost,
    )
}

fn build_prepared_request(
    max_body_bytes: usize,
    impact: OperationImpact,
    semantics: RequestSemantics,
    retry: RetryEligibility,
    cost: CostIntent,
) -> Result<PreparedRequest<'static>, ()> {
    let target = RequestTarget::new("/servers").map_err(|_| ())?;
    let headers = RequestHeaders::new(&JSON_REQUEST_HEADERS).map_err(|_| ())?;
    let method = if matches!(impact, OperationImpact::ReadOnly) {
        Method::Get
    } else {
        Method::Post
    };
    let request = TransportRequest::new(method, target)
        .with_body(b"{}")
        .with_headers(headers);
    let metadata =
        OperationMetadata::new(impact, semantics, retry, cost, RequestIdPolicy::Protected)
            .map_err(|_| ())?;
    let response_policy = ResponsePolicy::new(
        &OK_STATUS,
        ContentTypePolicy::Required(&JSON_MEDIA),
        ResponseBodyPolicy::Required,
        max_body_bytes,
    )
    .map_err(|_| ())?;
    let endpoint = official_endpoint().map_err(|_| ())?;
    let authentication_policy = AuthenticationScopePolicy::new(
        ScopeRequirement::Required(ExampleProvider::ID),
        ScopeRequirement::Required(ComputeService::ID),
        ScopeRequirement::Required(endpoint),
        ScopeRequirement::Forbidden,
        ScopeRequirement::Forbidden,
        ScopeRequirement::Forbidden,
    );
    let content_type = HeaderName::new("content-type").map_err(|_| ())?;
    let request_id = HeaderName::new("x-request-id").map_err(|_| ())?;
    let raw_response_policy = RawResponsePolicy::new(
        max_body_bytes,
        max_body_bytes,
        ResponseMediaPolicy::Required(&JSON_MEDIA),
        ResponseMediaPolicy::Required(&JSON_MEDIA),
        &[content_type, request_id],
        8,
    )
    .map_err(|_| ())?;
    PreparedRequest::new(
        request,
        ProviderService::from_marker::<ComputeService>(EndpointPolicy::fixed(endpoint)),
        metadata,
        response_policy,
        authentication_policy,
        raw_response_policy,
        cloud_sdk::operation::RequestBodySensitivity::Public,
    )
    .map_err(|_| ())
}

fn expected_request() -> Result<ExpectedRequest<'static>, ()> {
    let target = RequestTarget::new("/servers").map_err(|_| ())?;
    let headers = RequestHeaders::new(&JSON_REQUEST_HEADERS).map_err(|_| ())?;
    Ok(ExpectedRequest::new(Method::Get, target)
        .with_body(b"{}")
        .with_headers(headers))
}

fn successful_exchange() -> Result<MockExchange<'static>, ()> {
    let body = FixtureBody::new(b"{}").map_err(|_| ())?;
    Ok(MockExchange::new(
        expected_request()?,
        ResponseFixture::success(body).with_content_type("application/json; charset=utf-8"),
    ))
}

fn official_endpoint() -> Result<EndpointIdentity<'static>, EndpointIdentityError> {
    EndpointIdentity::new(EndpointScheme::Https, "api.hetzner.cloud", 443, "/v1")
}

fn other_endpoint() -> Result<EndpointIdentity<'static>, EndpointIdentityError> {
    EndpointIdentity::new(EndpointScheme::Https, "example.invalid", 443, "/v1")
}