dropshot 0.16.2

expose REST APIs from a Rust program
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
// Copyright 2023 Oxide Computer Company
//! Automated testing facilities.  These are intended for use both by this crate
//! and dependents of this crate.

use camino::Utf8PathBuf;
use chrono::DateTime;
use chrono::Utc;
use http::method::Method;
use http_body_util::BodyExt as _;
use hyper::Request;
use hyper::Response;
use hyper::StatusCode;
use hyper::Uri;
use hyper_util::client::legacy::{connect::HttpConnector, Client};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;
use slog::Logger;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::fs;
use std::iter::Iterator;
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;

use crate::api_description::ApiDescription;
use crate::body::Body;
use crate::config::ConfigDropshot;
use crate::error::HttpErrorResponseBody;
use crate::http_util::CONTENT_TYPE_URL_ENCODED;
use crate::logging::ConfigLogging;
use crate::pagination::ResultsPage;
use crate::server::{HttpServer, ServerBuilder, ServerContext};

enum AllowedValue<'a> {
    Any,
    OneOf(&'a [&'a str]),
}

struct AllowedHeader<'a> {
    name: &'a str,
    value: AllowedValue<'a>,
}

impl<'a> AllowedHeader<'a> {
    const fn new(name: &'a str) -> Self {
        Self { name, value: AllowedValue::Any }
    }
}

pub const TEST_HEADER_1: &str = "x-dropshot-test-header-1";
pub const TEST_HEADER_2: &str = "x-dropshot-test-header-2";

// List of allowed HTTP headers in responses.
// Used to make sure we don't leak headers unexpectedly.
const ALLOWED_HEADERS: [AllowedHeader<'static>; 8] = [
    AllowedHeader::new("content-length"),
    AllowedHeader::new("content-type"),
    AllowedHeader::new("date"),
    AllowedHeader::new("location"),
    AllowedHeader::new("x-request-id"),
    AllowedHeader {
        name: "transfer-encoding",
        value: AllowedValue::OneOf(&["chunked"]),
    },
    AllowedHeader::new(TEST_HEADER_1),
    AllowedHeader::new(TEST_HEADER_2),
];

/// ClientTestContext encapsulates several facilities associated with using an
/// HTTP client for testing.
#[derive(Clone)]
pub struct ClientTestContext {
    /// actual bind address of the HTTP server under test
    pub bind_address: SocketAddr,
    /// HTTP client, used for making requests against the test server
    pub client: Client<HttpConnector, crate::Body>,
    /// logger for the test suite HTTP client
    pub client_log: Logger,
}

// Macro to generate methods on `ClientTestContext` and
// `TypedErrorClientTestContext` that have identical implementations but
// differing error types. The type for which this macro is invoked must have a
// `async fn make_request_with_request` method, as the generated methods call
// that.
macro_rules! impl_client_test_context {
    { type Error = $Error:ty; } => {
        /// Execute an HTTP request against the test server and perform basic
        /// validation of the result, including:
        ///
        /// - the expected status code
        /// - the expected Date header (within reason)
        /// - for error responses: the expected body content
        /// - header names are in allowed list
        /// - any other semantics that can be verified in general
        ///
        /// The body will be JSON encoded.
        pub async fn make_request<RequestBodyType: Serialize + Debug>(
            &self,
            method: Method,
            path: &str,
            request_body: Option<RequestBodyType>,
            expected_status: StatusCode,
        ) -> Result<Response<Body>, $Error> {
            let body = match request_body {
                None => Body::empty(),
                Some(input) => serde_json::to_string(&input).unwrap().into(),
            };

            self.make_request_with_body(method, path, body, expected_status).await
        }

        /// Execute an HTTP request against the test server and perform basic
        /// validation of the result like [`ClientTestContext::make_request`],
        /// but with a content type of "application/x-www-form-urlencoded".
        pub async fn make_request_url_encoded<
            RequestBodyType: Serialize + Debug,
        >(
            &self,
            method: Method,
            path: &str,
            request_body: Option<RequestBodyType>,
            expected_status: StatusCode,
        ) -> Result<Response<Body>, $Error> {
            let body: Body = match request_body {
                None => Body::empty(),
                Some(input) => serde_urlencoded::to_string(&input).unwrap().into(),
            };

            self.make_request_with_body_url_encoded(
                method,
                path,
                body,
                expected_status,
            )
            .await
        }

        pub async fn make_request_no_body(
            &self,
            method: Method,
            path: &str,
            expected_status: StatusCode,
        ) -> Result<Response<Body>, $Error> {
            self.make_request_with_body(
                method,
                path,
                Body::empty(),
                expected_status,
            )
            .await
        }

        /// Fetches a resource for which we expect to get an error response.
        pub async fn make_request_error(
            &self,
            method: Method,
            path: &str,
            expected_status: StatusCode,
        ) -> $Error {
            self.make_request_with_body(method, path, "".into(), expected_status)
                .await
                .unwrap_err()
        }

        /// Fetches a resource for which we expect to get an error response.
        /// TODO-cleanup the make_request_error* interfaces are slightly
        /// different than the non-error ones (and probably a bit more
        /// ergonomic).
        pub async fn make_request_error_body<T: Serialize + Debug>(
            &self,
            method: Method,
            path: &str,
            body: T,
            expected_status: StatusCode,
        ) -> $Error {
            self.make_request(method, path, Some(body), expected_status)
                .await
                .unwrap_err()
        }

        pub async fn make_request_with_body(
            &self,
            method: Method,
            path: &str,
            body: Body,
            expected_status: StatusCode,
        ) -> Result<Response<Body>, $Error> {
            let uri = self.url(path);
            let request = Request::builder()
                .method(method)
                .uri(uri)
                .body(body)
                .expect("attempted to construct invalid request");
            self.make_request_with_request(request, expected_status).await
        }

        pub async fn make_request_with_body_url_encoded(
            &self,
            method: Method,
            path: &str,
            body: Body,
            expected_status: StatusCode,
        ) -> Result<Response<Body>, $Error> {
            let uri = self.url(path);
            let request = Request::builder()
                .method(method)
                .header(http::header::CONTENT_TYPE, CONTENT_TYPE_URL_ENCODED)
                .uri(uri)
                .body(body)
                .expect("attempted to construct invalid request");
            self.make_request_with_request(request, expected_status).await
        }
    }
}

impl ClientTestContext {
    /// Set up a `ClientTestContext` for running tests against an API server.
    pub fn new(server_addr: SocketAddr, log: Logger) -> ClientTestContext {
        ClientTestContext {
            bind_address: server_addr,
            client: Client::builder(hyper_util::rt::TokioExecutor::new())
                .build(HttpConnector::new()),
            client_log: log,
        }
    }

    /// Given the path for an API endpoint (e.g., "/projects"), return a Uri that
    /// we can use to invoke this endpoint from the client.  This essentially
    /// appends the path to a base URL constructed from the server's IP address
    /// and port.
    pub fn url(&self, path: &str) -> Uri {
        Uri::builder()
            .scheme("http")
            .authority(format!("{}", self.bind_address).as_str())
            .path_and_query(path)
            .build()
            .expect("attempted to construct invalid URI")
    }

    /// Temporarily configures the client to expect `E`-typed error responses,
    /// rather than [`dropshot::HttpError`][crate::HttpError] error responses.
    ///
    /// `ClientTestContext` expects that all error responses are
    /// `dropshot::HttpError`. For testing APIs that return other error types, this
    /// method borrows the `ClientTestContext` and returns a
    /// [`TypedErrorClientTestContext`]`<E>`, which expects `E`-typed error
    /// responses from  the API server, instead.
    ///
    /// Because the `TypedClientErrorTestContext` is a borrowed wrapper around
    /// the underlying `ClientTestContext`, the same client may be used to
    /// expect multiple error types from different endpoints of the same API.
    pub fn with_error_type<E>(&self) -> TypedErrorClientTestContext<'_, E>
    where
        E: DeserializeOwned + std::fmt::Debug,
    {
        TypedErrorClientTestContext { client: self, _error: PhantomData }
    }

    impl_client_test_context! {
        type Error = HttpErrorResponseBody;
    }

    pub async fn make_request_with_request(
        &self,
        request: Request<Body>,
        expected_status: StatusCode,
    ) -> Result<Response<Body>, HttpErrorResponseBody> {
        self.make_request_inner::<HttpErrorResponseBody>(
            request,
            expected_status,
        )
        .await
        .map_err(|(request_id_header, error_body)| {
            assert_eq!(error_body.request_id, request_id_header);
            error_body
        })
    }

    /// Internal implementation detail of `make_request_with_request` and
    /// `make_request_with_error` that's generic over the error type, and
    /// returns both the parsed error and the request ID header in the error
    /// case.
    async fn make_request_inner<E>(
        &self,
        request: Request<Body>,
        expected_status: StatusCode,
    ) -> Result<Response<Body>, (String, E)>
    where
        E: DeserializeOwned + std::fmt::Debug,
    {
        let time_before = chrono::offset::Utc::now().timestamp();
        info!(self.client_log, "client request";
            "method" => %request.method(),
            "uri" => %request.uri(),
            "body" => ?&request.body(),
        );

        let mut response = self
            .client
            .request(request)
            .await
            .expect("failed to make request to server");

        // Check that we got the expected response code.
        let status = response.status();
        info!(self.client_log, "client received response"; "status" => ?status);
        assert_eq!(expected_status, status);

        // Check that we didn't have any unexpected headers.  This could be more
        // efficient by putting the allowed headers into a BTree or Hash, but
        // right now the structure is tiny and it's convenient to have it
        // statically-defined above.
        let headers = response.headers();
        for (header_name, header_value) in headers {
            let mut okay = false;
            for allowed_header in ALLOWED_HEADERS.iter() {
                if header_name == allowed_header.name {
                    match allowed_header.value {
                        AllowedValue::Any => {
                            okay = true;
                        }
                        AllowedValue::OneOf(allowed_values) => {
                            let header = header_value
                                .to_str()
                                .expect("Cannot turn header value to string");
                            okay = allowed_values.contains(&header);
                        }
                    }
                    break;
                }
            }

            if !okay {
                panic!("header name not in allowed list: \"{}\"", header_name);
            }
        }

        // Sanity check the Date header in the response.  Note that this
        // assertion will fail spuriously in the unlikely event that the system
        // clock is adjusted backwards in between when we sent the request and
        // when we received the response, but we consider that case unlikely
        // enough to be worth doing this check anyway.  (We'll try to check for
        // the clock reset condition, too, but we cannot catch all cases that
        // would cause the Date header check to be incorrect.)
        //
        // Note that the Date header typically only has precision down to one
        // second, so we don't want to try to do a more precise comparison.
        let time_after = chrono::offset::Utc::now().timestamp();
        let date_header = headers
            .get(http::header::DATE)
            .expect("missing Date header")
            .to_str()
            .expect("non-ASCII characters in Date header");
        let time_request = chrono::DateTime::parse_from_rfc2822(date_header)
            .expect("unable to parse server's Date header");
        assert!(
            time_before <= time_after,
            "time obviously went backwards during the test"
        );
        assert!(time_request.timestamp() >= time_before - 1);
        assert!(time_request.timestamp() <= time_after + 1);

        // Validate that we have a request id header.
        // TODO-coverage check that it's unique among requests we've issued
        let request_id_header = headers
            .get(crate::HEADER_REQUEST_ID)
            .expect("missing request id header")
            .to_str()
            .expect("non-ASCII characters in request id")
            .to_string();

        // For "204 No Content" responses, validate that we got no content in
        // the body.
        if status == StatusCode::NO_CONTENT {
            let body_bytes = read_bytes(&mut response).await;
            assert_eq!(0, body_bytes.len());
        }

        let mut response = response.map(Body::wrap);
        // If this was a successful response, there's nothing else to check
        // here.  Return the response so the caller can validate the content if
        // they want.
        if !status.is_client_error() && !status.is_server_error() {
            return Ok(response);
        }

        // We got an error.  Parse the response body to make sure it's valid and
        // then return that.
        let error_body: E = read_json(&mut response).await;
        info!(self.client_log, "client error"; "error_body" => ?error_body);
        Err((request_id_header, error_body))
    }
}

/// A `ClientTestContext` wrapper which expects that the API server under test
/// will return `E`-typed error responses.
///
/// `ClientTestContext` expects that all error responses are
/// `dropshot::HttpError`. For testing APIs that return other error types, the
/// [`ClientTestContext::with_error_type`]`<E>` method allows constructing a
/// `TypedErrorClientTestContext`, which expects `E`-typed error responses from
/// the API server, instead.
///
/// In order to make API requests with a `TypedErrorTestContext`, `E` must
/// implement [`DeserializeOwned`] and [`std::fmt::Debug`].
pub struct TypedErrorClientTestContext<'client, E> {
    pub client: &'client ClientTestContext,
    _error: PhantomData<fn(E)>,
}

// A manual implementation of `Clone` is required to avoid requiring that `E:
// Clone`, as this type does not actually contain an `E`. Unfortunately,
// `#[derive(Clone)]` is not aware of `PhantomData`, and will always require
// that all of a generic type's type parameters are `Clone`, even if the type
// does not actually contain them.
impl<E> Clone for TypedErrorClientTestContext<'_, E> {
    fn clone(&self) -> Self {
        Self { client: self.client, _error: PhantomData }
    }
}

impl<E> TypedErrorClientTestContext<'_, E>
where
    E: DeserializeOwned + std::fmt::Debug,
{
    /// Given the path for an API endpoint (e.g., "/projects"), return a Uri that
    /// we can use to invoke this endpoint from the client.  This essentially
    /// appends the path to a base URL constructed from the server's IP address
    /// and port.
    pub fn url(&self, path: &str) -> Uri {
        self.client.url(path)
    }

    // Generate all the methods with identical implementations to
    // `ClientTestContext`.
    impl_client_test_context! {
        type Error = E;
    }

    pub async fn make_request_with_request(
        &self,
        request: Request<Body>,
        expected_status: StatusCode,
    ) -> Result<Response<Body>, E> {
        self.client
            .make_request_inner::<E>(request, expected_status)
            .await
            .map_err(|(_, error_body)| error_body)
    }
}

/// Constructs a Logger for use by a test suite.  If a file-based logger is
/// requested, the file will be put in a temporary directory and the name will be
/// unique for a given test name and is likely to be unique across multiple runs
/// of this test.  The file will also be deleted if the test succeeds, indicated
/// by invoking [`LogContext::cleanup_successful`].  This way, you can debug a
/// test failure from the failed instance rather than hoping the failure is
/// reproducible.
///
/// ## Example
///
/// ```
/// # use dropshot::ConfigLoggingLevel;
/// #
/// # fn my_logging_config() -> ConfigLogging {
/// #     ConfigLogging::StderrTerminal {
/// #         level: ConfigLoggingLevel::Info,
/// #     }
/// # }
/// #
/// # fn some_invariant() -> bool {
/// #     true
/// # }
/// #
/// use dropshot::ConfigLogging;
/// use dropshot::test_util::LogContext;
///
/// #[macro_use]
/// extern crate slog; /* for the `info!` macro below */
///
/// # fn main() {
/// let log_config: ConfigLogging = my_logging_config();
/// let logctx = LogContext::new("my_test", &log_config);
/// let log = &logctx.log;
///
/// /* Run your test.  Use the log like you normally would. */
/// info!(log, "the test is going great");
/// assert!(some_invariant());
///
/// /* Upon successful completion, invoke `cleanup_successful()`. */
/// logctx.cleanup_successful();
/// # }
/// ```
///
/// If the test fails (e.g., the `some_invariant()` assertion fails), the log
/// file will be retained.  If the test gets as far as calling
/// `cleanup_successful()`, the log file will be removed.
///
/// Note that `cleanup_successful()` is not invoked automatically on `drop`
/// because that would remove the file even if the test failed, which isn't what
/// we want.  You have to explicitly call `cleanup_successful`.  Normally, you
/// just do this as one of the last steps in your test.  This pattern ensures
/// that the log file sticks around if the test fails, but is removed if the test
/// succeeds.
pub struct LogContext {
    /// general-purpose logger
    pub log: Logger,
    test_name: String,
    log_path: Option<Utf8PathBuf>,
}

impl LogContext {
    /// Sets up a LogContext.  If `initial_config_logging` specifies a file-based
    /// log (i.e., [`ConfigLogging::File`]), then the requested path _must_ be
    /// the string `"UNUSED"` and it will be replaced with a file name (in a
    /// temporary directory) containing `test_name` and other information to make
    /// the filename likely to be unique across multiple runs (e.g., process id).
    pub fn new(
        test_name: &str,
        initial_config_logging: &ConfigLogging,
    ) -> LogContext {
        // See above.  If the caller requested a file path, assert that the path
        // matches our sentinel (just to improve debuggability -- otherwise
        // people might be pretty confused about where the logs went).  Then
        // override the path with one uniquely generated for this test.
        // TODO-developer allow keeping the logs in successful cases with an
        // environment variable or other flag.
        let (log_path, log_config) = match initial_config_logging {
            ConfigLogging::File { level, path: dummy_path, if_exists } => {
                assert_eq!(
                    dummy_path, "UNUSED",
                    "for test suite logging configuration, when mode = \
                     \"file\" is used, the path MUST be the sentinel string \
                     \"UNUSED\".  It will be replaced with a unique path for \
                     each test."
                );
                let new_path = log_file_for_test(test_name);
                eprintln!("log file: {}", new_path);
                (
                    Some(new_path.clone()),
                    ConfigLogging::File {
                        level: level.clone(),
                        path: new_path,
                        if_exists: if_exists.clone(),
                    },
                )
            }
            other_config => (None, other_config.clone()),
        };

        let log = log_config.to_logger(test_name).unwrap();
        LogContext { log, test_name: test_name.to_owned(), log_path }
    }

    /// Returns the name of the test.
    #[inline]
    pub fn test_name(&self) -> &str {
        &self.test_name
    }

    /// Removes the log file, if this was a file-based logger.
    pub fn cleanup_successful(self) {
        if let Some(ref log_path) = self.log_path {
            fs::remove_file(log_path).unwrap();
        }
    }
}

/// TestContext is used to manage a matched server and client for the common
/// test-case pattern of setting up a logger, server, and client and tearing them
/// all down at the end.
pub struct TestContext<Context: ServerContext> {
    pub client_testctx: ClientTestContext,
    pub server: HttpServer<Context>,
    pub log: Logger,
    log_context: Option<LogContext>,
}

impl<Context: ServerContext> TestContext<Context> {
    /// Instantiate a TestContext by creating a new Dropshot server with `api`,
    /// `private`, `config_dropshot`, and `log`, and then creating a
    /// `ClientTestContext` with whatever address the server wound up bound to.
    ///
    /// This interfaces requires that `config_dropshot.bind_address.port()` be
    /// `0` to allow the server to bind to any available port.  This is necessary
    /// in order for it to be used concurrently by many tests.
    pub fn new(
        api: ApiDescription<Context>,
        private: Context,
        config_dropshot: &ConfigDropshot,
        log_context: Option<LogContext>,
        log: Logger,
    ) -> TestContext<Context> {
        assert_eq!(
            0,
            config_dropshot.bind_address.port(),
            "test suite only supports binding on port 0 (any available port)"
        );

        // Set up the server itself.
        let server = ServerBuilder::new(api, private, log.clone())
            .config(config_dropshot.clone())
            .start()
            .unwrap();

        let server_addr = server.local_addr();
        let client_log = log.new(o!("http_client" => "dropshot test suite"));
        let client_testctx = ClientTestContext::new(server_addr, client_log);

        TestContext { client_testctx, server, log, log_context }
    }

    /// Requests a graceful shutdown of the server, waits for that to complete,
    /// and cleans up the associated log context (if any).
    // TODO-cleanup: is there an async analog to Drop?
    pub async fn teardown(self) {
        self.server.close().await.expect("server stopped with an error");
        if let Some(log_context) = self.log_context {
            log_context.cleanup_successful();
        }
    }
}

/// Given a Hyper Response whose body is expected to represent newline-separated
/// JSON, each line of which is expected to be parseable via Serde as type T,
/// asynchronously read the body of the response and parse it accordingly,
/// returning a vector of T.
pub async fn read_ndjson<T: DeserializeOwned>(
    response: &mut Response<Body>,
) -> Vec<T> {
    let headers = response.headers();
    assert_eq!(
        crate::CONTENT_TYPE_NDJSON,
        headers.get(http::header::CONTENT_TYPE).expect("missing content-type")
    );
    let body_string = read_string(response).await;

    // TODO-cleanup: Consider using serde_json::StreamDeserializer or maybe
    // implementing an NDJSON-based Serde type?
    // TODO-correctness: If we don't do that, this should split on (\r?\n)+ to
    // be NDJSON-compatible.
    body_string
        .split('\n')
        .filter(|line| !line.is_empty())
        .map(|line| {
            serde_json::from_str(line)
                .expect("failed to parse server body as expected type")
        })
        .collect::<Vec<T>>()
}

/// Given a Hyper response whose body is expected to be a JSON object that should
/// be parseable via Serde as type T, asynchronously read the body of the
/// response and parse it, returning an instance of T.
pub async fn read_json<T: DeserializeOwned>(
    response: &mut Response<Body>,
) -> T {
    let headers = response.headers();
    assert_eq!(
        crate::CONTENT_TYPE_JSON,
        headers.get(http::header::CONTENT_TYPE).expect("missing content-type")
    );
    let body_bytes = read_bytes(response).await;
    serde_json::from_slice(body_bytes.as_ref())
        .expect("failed to parse server body as expected type")
}

/// Given a Hyper Response whose body is expected to be a UTF-8-encoded string,
/// asynchronously read the body.
pub async fn read_string(response: &mut Response<Body>) -> String {
    let body_bytes = read_bytes(response).await;
    String::from_utf8(body_bytes.as_ref().into())
        .expect("response contained non-UTF-8 bytes")
}

async fn read_bytes<B>(response: &mut Response<B>) -> hyper::body::Bytes
where
    B: hyper::body::Body + Unpin,
    B::Error: std::fmt::Debug,
{
    response.body_mut().collect().await.expect("error reading body").to_bytes()
}

/// Given a Hyper Response, extract and parse the Content-Length header.
pub fn read_content_length(response: &Response<Body>) -> usize {
    response
        .headers()
        .get(http::header::CONTENT_LENGTH)
        .unwrap()
        .to_str()
        .unwrap()
        .parse()
        .unwrap()
}

/// Fetches a single resource from the API.
pub async fn object_get<T: DeserializeOwned>(
    client: &ClientTestContext,
    object_url: &str,
) -> T {
    let mut response = client
        .make_request_with_body(
            Method::GET,
            &object_url,
            "".into(),
            StatusCode::OK,
        )
        .await
        .unwrap();
    read_json::<T>(&mut response).await
}

/// Fetches a list of resources from the API.
pub async fn objects_list<T: DeserializeOwned>(
    client: &ClientTestContext,
    list_url: &str,
) -> Vec<T> {
    let mut response = client
        .make_request_with_body(
            Method::GET,
            &list_url,
            "".into(),
            StatusCode::OK,
        )
        .await
        .unwrap();
    read_ndjson::<T>(&mut response).await
}

/// Fetches a page of resources from the API.
pub async fn objects_list_page<ItemType>(
    client: &ClientTestContext,
    list_url: &str,
) -> ResultsPage<ItemType>
where
    ItemType: DeserializeOwned,
{
    let mut response = client
        .make_request_with_body(
            Method::GET,
            &list_url,
            "".into(),
            StatusCode::OK,
        )
        .await
        .unwrap();

    read_json::<ResultsPage<ItemType>>(&mut response).await
}

/// Issues an HTTP POST to the specified collection URL to create an object.
pub async fn objects_post<S: Serialize + Debug, T: DeserializeOwned>(
    client: &ClientTestContext,
    collection_url: &str,
    input: S,
) -> T {
    let mut response = client
        .make_request(
            Method::POST,
            &collection_url,
            Some(input),
            StatusCode::CREATED,
        )
        .await
        .unwrap();
    read_json::<T>(&mut response).await
}

/// Issues an HTTP PUT to the specified collection URL to update an object.
pub async fn object_put<S: Serialize + Debug, T: DeserializeOwned>(
    client: &ClientTestContext,
    object_url: &str,
    input: S,
    status: StatusCode,
) {
    client
        .make_request(Method::PUT, &object_url, Some(input), status)
        .await
        .unwrap();
}

/// Issues an HTTP DELETE to the specified object URL to delete an object.
pub async fn object_delete(client: &ClientTestContext, object_url: &str) {
    client
        .make_request_no_body(
            Method::DELETE,
            &object_url,
            StatusCode::NO_CONTENT,
        )
        .await
        .unwrap();
}

/// Iterate a paginated collection.
pub async fn iter_collection<T: Clone + DeserializeOwned>(
    client: &ClientTestContext,
    collection_url: &str,
    initial_params: &str,
    limit: usize,
) -> (Vec<T>, usize) {
    let mut page = objects_list_page::<T>(
        &client,
        &format!("{}?limit={}&{}", collection_url, limit, initial_params),
    )
    .await;
    assert!(page.items.len() <= limit);

    let mut rv = page.items.clone();
    let mut npages = 1;

    while let Some(token) = page.next_page {
        page = objects_list_page::<T>(
            &client,
            &format!("{}?limit={}&page_token={}", collection_url, limit, token),
        )
        .await;
        assert!(page.items.len() <= limit);
        rv.extend_from_slice(&page.items);
        npages += 1
    }

    (rv, npages)
}

static TEST_SUITE_LOGGER_ID: AtomicU32 = AtomicU32::new(0);

/// Returns a unique prefix for log files generated by other processes.
///
/// The return value is a combination of:
///
/// - a directory path within which the logs should go
/// - a unique string prefix that identifies this test and process ID, along
///   with a counter that is atomically bumped each time this function is
///   called.
pub fn log_prefix_for_test(test_name: &str) -> (Utf8PathBuf, String) {
    let arg0 = {
        let arg0path = Utf8PathBuf::from(std::env::args().next().unwrap());
        arg0path.file_name().unwrap().to_owned()
    };

    // We currently write all log files to the temporary directory.
    let dir = Utf8PathBuf::try_from(std::env::temp_dir())
        .expect("temp dir is valid UTF-8");
    let id = TEST_SUITE_LOGGER_ID.fetch_add(1, Ordering::SeqCst);
    let pid = std::process::id();
    (dir, format!("{arg0}-{test_name}.{pid}.{id}"))
}

/// Returns a unique path name in a temporary directory that includes the given
/// `test_name`.
pub fn log_file_for_test(test_name: &str) -> Utf8PathBuf {
    let (mut dir, prefix) = log_prefix_for_test(test_name);
    dir.push(format!("{prefix}.log"));
    dir
}

/// Load an object of type `T` (usually a hunk of configuration) from the string
/// `contents`.  `label` is used as an identifying string in a log message.  It
/// should be unique for each test.
pub fn read_config<T: DeserializeOwned + Debug>(
    label: &str,
    contents: &str,
) -> Result<T, toml::de::Error> {
    let result = toml::from_str(contents);
    eprintln!("config \"{}\": {:?}", label, result);
    result
}

// Bunyan testing facilities

/// Represents a Bunyan log record.  This form does not support any non-standard
/// fields.  "level" is not yet supported because we don't (yet) need it.
#[derive(Deserialize)]
pub struct BunyanLogRecord {
    pub time: DateTime<Utc>,
    pub name: String,
    pub hostname: String,
    pub pid: u32,
    pub msg: String,
    pub v: usize,
}

/// Read a file containing a Bunyan-format log, returning an array of records.
pub fn read_bunyan_log(logpath: &Path) -> Vec<BunyanLogRecord> {
    let log_contents = fs::read_to_string(logpath).unwrap();
    log_contents
        .split('\n')
        .filter(|line| !line.is_empty())
        .map(|line| serde_json::from_str::<BunyanLogRecord>(line).unwrap())
        .collect::<Vec<BunyanLogRecord>>()
}

/// Analogous to a BunyanLogRecord, but where all fields are optional.
pub struct BunyanLogRecordSpec {
    pub name: Option<String>,
    pub hostname: Option<String>,
    pub pid: Option<u32>,
    pub v: Option<usize>,
}

/// Verify that the key fields of the log records emitted by `iter` match the
/// corresponding values in `expected`.  Fields that are `None` in `expected`
/// will not be checked.
pub fn verify_bunyan_records<'a, 'b, I>(
    iter: I,
    expected: &'a BunyanLogRecordSpec,
) where
    I: Iterator<Item = &'b BunyanLogRecord>,
{
    for record in iter {
        if let Some(ref expected_name) = expected.name {
            assert_eq!(expected_name, &record.name);
        }
        if let Some(ref expected_hostname) = expected.hostname {
            assert_eq!(expected_hostname, &record.hostname);
        }
        if let Some(expected_pid) = expected.pid {
            assert_eq!(expected_pid, record.pid);
        }
        if let Some(expected_v) = expected.v {
            assert_eq!(expected_v, record.v);
        }
    }
}

/// Verify that the Bunyan records emitted by `iter` are chronologically
/// sequential and after `maybe_time_before` and before `maybe_time_after`, if
/// those latter two parameters are specified.
pub fn verify_bunyan_records_sequential<'a, 'b, I>(
    iter: I,
    maybe_time_before: Option<&'a DateTime<Utc>>,
    maybe_time_after: Option<&'a DateTime<Utc>>,
) where
    I: Iterator<Item = &'a BunyanLogRecord>,
{
    let mut maybe_should_be_before = maybe_time_before;

    for record in iter {
        if let Some(should_be_before) = maybe_should_be_before {
            assert!(should_be_before.timestamp() <= record.time.timestamp());
        }
        maybe_should_be_before = Some(&record.time);
    }

    if let Some(should_be_before) = maybe_should_be_before {
        if let Some(time_after) = maybe_time_after {
            assert!(should_be_before.timestamp() <= time_after.timestamp());
        }
    }
}

#[cfg(test)]
mod test {
    const T1_STR: &str = "2020-03-24T00:00:00Z";
    const T2_STR: &str = "2020-03-25T00:00:00Z";

    use super::verify_bunyan_records;
    use super::verify_bunyan_records_sequential;
    use super::BunyanLogRecord;
    use super::BunyanLogRecordSpec;
    use chrono::DateTime;
    use chrono::Utc;

    fn make_dummy_record() -> BunyanLogRecord {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        BunyanLogRecord {
            time: t1,
            name: "n1".to_string(),
            hostname: "h1".to_string(),
            pid: 1,
            msg: "msg1".to_string(),
            v: 0,
        }
    }

    // Tests various cases where verify_bunyan_records() should not panic.
    #[test]
    fn test_bunyan_easy_cases() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let r1 = make_dummy_record();
        let r2 = BunyanLogRecord {
            time: t1,
            name: "n1".to_string(),
            hostname: "h2".to_string(),
            pid: 1,
            msg: "msg2".to_string(),
            v: 1,
        };

        // Test case: nothing to check.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: None,
                pid: None,
                v: None,
            },
        );

        // Test case: check name, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: Some("n1".to_string()),
                hostname: None,
                pid: None,
                v: None,
            },
        );

        // Test case: check hostname, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: Some("h1".to_string()),
                pid: None,
                v: None,
            },
        );

        // Test case: check pid, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: None,
                pid: Some(1),
                v: None,
            },
        );

        // Test case: check hostname, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: None,
                pid: None,
                v: Some(0),
            },
        );

        // Test case: check all, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: Some("n1".to_string()),
                hostname: Some("h1".to_string()),
                pid: Some(1),
                v: Some(0),
            },
        );

        // Test case: check multiple records, no problem.
        let records: Vec<&BunyanLogRecord> = vec![&r1, &r2];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: Some("n1".to_string()),
                hostname: None,
                pid: Some(1),
                v: None,
            },
        );
    }

    // Test cases exercising violations of each of the fields.

    #[test]
    #[should_panic(expected = "assertion `left == right` failed")]
    fn test_bunyan_bad_name() {
        let r1 = make_dummy_record();
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: Some("n2".to_string()),
                hostname: None,
                pid: None,
                v: None,
            },
        );
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed")]
    fn test_bunyan_bad_hostname() {
        let r1 = make_dummy_record();
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: Some("h2".to_string()),
                pid: None,
                v: None,
            },
        );
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed")]
    fn test_bunyan_bad_pid() {
        let r1 = make_dummy_record();
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: None,
                pid: Some(2),
                v: None,
            },
        );
    }

    #[test]
    #[should_panic(expected = "assertion `left == right` failed")]
    fn test_bunyan_bad_v() {
        let r1 = make_dummy_record();
        let records: Vec<&BunyanLogRecord> = vec![&r1];
        let iter = records.iter().map(|x| *x);
        verify_bunyan_records(
            iter,
            &BunyanLogRecordSpec {
                name: None,
                hostname: None,
                pid: None,
                v: Some(1),
            },
        );
    }

    // These cases exercise 0, 1, and 2 records with every valid combination
    // of lower and upper bounds.
    #[test]
    fn test_bunyan_seq_easy_cases() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let t2: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T2_STR).unwrap().into();
        let v0: Vec<BunyanLogRecord> = vec![];
        let v1: Vec<BunyanLogRecord> = vec![BunyanLogRecord {
            time: t1,
            name: "dummy_name".to_string(),
            hostname: "dummy_hostname".to_string(),
            pid: 123,
            msg: "dummy_msg".to_string(),
            v: 0,
        }];
        let v2: Vec<BunyanLogRecord> = vec![
            BunyanLogRecord {
                time: t1,
                name: "dummy_name".to_string(),
                hostname: "dummy_hostname".to_string(),
                pid: 123,
                msg: "dummy_msg".to_string(),
                v: 0,
            },
            BunyanLogRecord {
                time: t2,
                name: "dummy_name".to_string(),
                hostname: "dummy_hostname".to_string(),
                pid: 123,
                msg: "dummy_msg".to_string(),
                v: 0,
            },
        ];

        verify_bunyan_records_sequential(v0.iter(), None, None);
        verify_bunyan_records_sequential(v0.iter(), Some(&t1), None);
        verify_bunyan_records_sequential(v0.iter(), None, Some(&t1));
        verify_bunyan_records_sequential(v0.iter(), Some(&t1), Some(&t2));
        verify_bunyan_records_sequential(v1.iter(), None, None);
        verify_bunyan_records_sequential(v1.iter(), Some(&t1), None);
        verify_bunyan_records_sequential(v1.iter(), None, Some(&t2));
        verify_bunyan_records_sequential(v1.iter(), Some(&t1), Some(&t2));
        verify_bunyan_records_sequential(v2.iter(), None, None);
        verify_bunyan_records_sequential(v2.iter(), Some(&t1), None);
        verify_bunyan_records_sequential(v2.iter(), None, Some(&t2));
        verify_bunyan_records_sequential(v2.iter(), Some(&t1), Some(&t2));
    }

    // Test case: no records, but the bounds themselves violate the constraint.
    #[test]
    #[should_panic(expected = "assertion failed: should_be_before")]
    fn test_bunyan_seq_bounds_bad() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let t2: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T2_STR).unwrap().into();
        let v0: Vec<BunyanLogRecord> = vec![];
        verify_bunyan_records_sequential(v0.iter(), Some(&t2), Some(&t1));
    }

    // Test case: sole record appears before early bound.
    #[test]
    #[should_panic(expected = "assertion failed: should_be_before")]
    fn test_bunyan_seq_lower_violated() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let t2: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T2_STR).unwrap().into();
        let v1: Vec<BunyanLogRecord> = vec![BunyanLogRecord {
            time: t1,
            name: "dummy_name".to_string(),
            hostname: "dummy_hostname".to_string(),
            pid: 123,
            msg: "dummy_msg".to_string(),
            v: 0,
        }];
        verify_bunyan_records_sequential(v1.iter(), Some(&t2), None);
    }

    // Test case: sole record appears after late bound.
    #[test]
    #[should_panic(expected = "assertion failed: should_be_before")]
    fn test_bunyan_seq_upper_violated() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let t2: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T2_STR).unwrap().into();
        let v1: Vec<BunyanLogRecord> = vec![BunyanLogRecord {
            time: t2,
            name: "dummy_name".to_string(),
            hostname: "dummy_hostname".to_string(),
            pid: 123,
            msg: "dummy_msg".to_string(),
            v: 0,
        }];
        verify_bunyan_records_sequential(v1.iter(), None, Some(&t1));
    }

    // Test case: two records out of order.
    #[test]
    #[should_panic(expected = "assertion failed: should_be_before")]
    fn test_bunyan_seq_bad_order() {
        let t1: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T1_STR).unwrap().into();
        let t2: DateTime<Utc> =
            DateTime::parse_from_rfc3339(T2_STR).unwrap().into();
        let v2: Vec<BunyanLogRecord> = vec![
            BunyanLogRecord {
                time: t2,
                name: "dummy_name".to_string(),
                hostname: "dummy_hostname".to_string(),
                pid: 123,
                msg: "dummy_msg".to_string(),
                v: 0,
            },
            BunyanLogRecord {
                time: t1,
                name: "dummy_name".to_string(),
                hostname: "dummy_hostname".to_string(),
                pid: 123,
                msg: "dummy_msg".to_string(),
                v: 0,
            },
        ];
        verify_bunyan_records_sequential(v2.iter(), None, None);
    }
}