-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathoptions_test.cc
More file actions
1300 lines (1144 loc) · 66.3 KB
/
options_test.cc
File metadata and controls
1300 lines (1144 loc) · 66.3 KB
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
#include "external/envoy/test/test_common/utility.h"
#include "source/client/options_impl.h"
#include "test/client/utility.h"
#include "test/test_common/environment.h"
#include "test/test_common/proto_matchers.h"
#include "test/user_defined_output/fake_plugin/fake_user_defined_output.pb.h"
#include "gtest/gtest.h"
using namespace std::chrono_literals;
using namespace testing;
using ::Envoy::Protobuf::TextFormat;
namespace Nighthawk {
namespace Client {
class OptionsImplTest : public Test {
public:
OptionsImplTest()
: client_name_("nighthawk_client"), good_test_uri_("http://127.0.0.1/"),
no_arg_match_("Couldn't find match for argument") {}
void verifyHeaderOptionParse(absl::string_view header_option, absl::string_view expected_key,
absl::string_view expected_value) {
std::string s_header_option(header_option);
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(std::vector<const char*>{
client_name_.c_str(), "--request-header", s_header_option.c_str(), good_test_uri_.c_str()});
EXPECT_EQ(std::vector<std::string>{s_header_option}, options->requestHeaders());
auto optionsPtr = options->toCommandLineOptions();
const auto& headers = optionsPtr->request_options().request_headers();
EXPECT_EQ(1, headers.size());
EXPECT_EQ(expected_key, headers[0].header().key());
EXPECT_EQ(expected_value, headers[0].header().value());
}
std::string client_name_;
std::string good_test_uri_;
std::string no_arg_match_;
};
class OptionsImplIntTest : public OptionsImplTest, public WithParamInterface<const char*> {};
class OptionsImplIntTestNonZeroable : public OptionsImplTest,
public WithParamInterface<const char*> {};
TEST_F(OptionsImplTest, BogusInput) {
// When just passing the non-existing argument --foo it would be interpreted as a
// hostname. However, hostnames shouldn't start with '-', and hence this test should
// not pass.
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --foo", client_name_)),
MalformedArgvException, "Invalid target URI: ''");
}
TEST_F(OptionsImplTest, BogusRequestSource) {
// Request source that looks like an accidental --arg
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --request-source --foo http://foo", client_name_)),
MalformedArgvException, "Invalid replay source URI");
// Request source that specifies a bad scheme
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --request-source http://bar http://foo", client_name_)),
MalformedArgvException, "Invalid replay source URI");
}
TEST_F(OptionsImplTest, NoDurationAndDurationAreMutuallyExclusive) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --duration 5 --no-duration http://foo", client_name_)),
MalformedArgvException, "mutually exclusive");
}
TEST_F(OptionsImplTest, DurationAndNoDurationSanity) {
std::unique_ptr<OptionsImpl> options =
TestUtility::createOptionsImpl(fmt::format("{} http://foo", client_name_));
EXPECT_FALSE(options->noDuration());
EXPECT_EQ(5s, options->duration());
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_FALSE(cmd->has_no_duration());
ASSERT_TRUE(cmd->has_duration());
EXPECT_EQ(5, cmd->duration().seconds());
options =
TestUtility::createOptionsImpl(fmt::format("{} --no-duration http://foo", client_name_));
EXPECT_TRUE(options->noDuration());
cmd = options->toCommandLineOptions();
ASSERT_TRUE(cmd->has_no_duration());
EXPECT_TRUE(cmd->no_duration().value());
}
TEST_F(OptionsImplTest, StatsFlushIntervalAndStatsFlushIntervalDurationAreMutuallyExclusive) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(
fmt::format("{} --stats-flush-interval 5 --stats-flush-interval-duration 1s http://foo",
client_name_)),
MalformedArgvException, "mutually exclusive");
}
TEST_F(OptionsImplTest, StatsSinksMustBeSetWhenStatsFlushIntervalSet) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --stats-flush-interval 10", client_name_)),
MalformedArgvException,
"if --stats-flush-interval or --stats-flush-interval-duration is set, then --stats-sinks "
"must also be set");
}
TEST_F(OptionsImplTest, StatsSinksMustBeSetWhenStatsFlushIntervalDurationSet) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --stats-flush-interval-duration 1.000000001s", client_name_)),
MalformedArgvException,
"if --stats-flush-interval or --stats-flush-interval-duration is set, "
"then --stats-sinks must also be set");
}
TEST_F(OptionsImplTest, InvalidStatsFlushIntervalDuration) {
const std::string sink_json =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\"}}";
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format(
"{} --stats-flush-interval-duration invalid-duration --stats-sinks {} http://foo",
client_name_, sink_json)),
MalformedArgvException, "Invalid value for --stats-flush-interval-duration");
}
TEST_F(OptionsImplTest, SecondsOutOfRangeStatsFlushIntervalDuration) {
const std::string sink_json =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\"}}";
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --stats-flush-interval-duration -1s --stats-sinks {} http://foo",
client_name_, sink_json)),
MalformedArgvException,
"--stats-flush-interval-duration is out of range");
}
TEST_F(OptionsImplTest, NanosOutOfRangeStatsFlushIntervalDuration) {
const std::string sink_json =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\"}}";
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format(
"{} --stats-flush-interval-duration -0.000000001s --stats-sinks {} http://foo",
client_name_, sink_json)),
MalformedArgvException, "--stats-flush-interval-duration is out of range");
}
TEST_F(OptionsImplTest,
SanityCheckDefaultWhenBothStatsFlushIntervalAndStatsFlushIntervalDurationNotSet) {
std::unique_ptr<OptionsImpl> options =
TestUtility::createOptionsImpl(fmt::format("{} http://foo", client_name_));
EXPECT_EQ(5, options->statsFlushInterval());
EXPECT_EQ(0, options->statsFlushIntervalDuration().seconds());
EXPECT_EQ(0, options->statsFlushIntervalDuration().nanos());
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_FALSE(cmd->has_stats_flush_interval_duration());
ASSERT_TRUE(cmd->has_stats_flush_interval());
EXPECT_EQ(5, cmd->stats_flush_interval().value());
}
TEST_F(OptionsImplTest, SanityCheckWhenStatsFlushIntervalDurationIsSet) {
const std::string sink_json =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\"}}";
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --stats-flush-interval-duration 1.000000001s --stats-sinks {} http://foo",
client_name_, sink_json));
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_FALSE(cmd->has_stats_flush_interval());
ASSERT_TRUE(cmd->has_stats_flush_interval_duration());
EXPECT_EQ(1, cmd->stats_flush_interval_duration().seconds());
EXPECT_EQ(1, cmd->stats_flush_interval_duration().nanos());
}
// This test should cover every option we offer, except some mutually exclusive ones that
// have separate tests.
TEST_F(OptionsImplTest, AlmostAll) {
Envoy::MessageUtil util;
const std::string sink_json_1 =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\"}}";
const std::string sink_json_2 =
"{name:\"envoy.stat_sinks.statsd\",typed_config:{\"@type\":\"type."
"googleapis.com/"
"envoy.config.metrics.v3.StatsdSink\",tcp_cluster_name:\"statsd\",prefix:"
"\"nighthawk\"}}";
const std::string user_defined_output_plugin =
"{name:\"nighthawk.fake_user_defined_output\",typed_config:"
"{\"@type\":\"type.googleapis.com/nighthawk.FakeUserDefinedOutputConfig\"}}";
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(fmt::format(
"{} --rps 4 --connections 5 --duration 6 --timeout 7 --h2 "
"--concurrency 8 --verbosity error --output-format yaml --prefetch-connections "
"--burst-size 13 --address-family v6 --request-method POST --request-body-size 1234 "
"--upstream-bind-config {} "
"--transport-socket {} "
"--request-header f1:b1 --request-header f2:b2 --request-header f3:b3:b4 "
"--max-pending-requests 10 "
"--max-active-requests 11 --max-requests-per-connection 12 --sequencer-idle-strategy sleep "
"--termination-predicate t1:1 --termination-predicate t2:2 --failure-predicate f1:1 "
"--failure-predicate f2:2 --no-default-failure-predicates --jitter-uniform .00001s "
"--max-concurrent-streams 42 "
"--experimental-h1-connection-reuse-strategy lru --label label1 --label label2 {} "
"--simple-warmup --stats-sinks {} --stats-sinks {} --stats-flush-interval 10 "
"--latency-response-header-name zz --user-defined-plugin-config {}",
client_name_, "{source_address:{address:\"127.0.0.1\",port_value:0}}",
"{name:\"envoy.transport_sockets.tls\","
"typed_config:{\"@type\":\"type.googleapis.com/"
"envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext\","
"common_tls_context:{tls_params:{"
"cipher_suites:[\"-ALL:ECDHE-RSA-AES256-GCM-SHA384\"]}}}}",
good_test_uri_, sink_json_1, sink_json_2, user_defined_output_plugin));
EXPECT_EQ(4, options->requestsPerSecond());
EXPECT_EQ(5, options->connections());
EXPECT_EQ(6s, options->duration());
EXPECT_EQ(7s, options->timeout());
EXPECT_EQ(Envoy::Http::Protocol::Http2, options->protocol());
EXPECT_EQ("8", options->concurrency());
EXPECT_EQ(nighthawk::client::Verbosity::ERROR, options->verbosity());
EXPECT_EQ(nighthawk::client::OutputFormat::YAML, options->outputFormat());
EXPECT_EQ(true, options->prefetchConnections());
EXPECT_EQ(13, options->burstSize());
EXPECT_EQ(nighthawk::client::AddressFamily::V6, options->addressFamily());
EXPECT_EQ(good_test_uri_, options->uri());
EXPECT_EQ(envoy::config::core::v3::RequestMethod::POST, options->requestMethod());
const std::vector<std::string> expected_headers = {"f1:b1", "f2:b2", "f3:b3:b4"};
EXPECT_EQ(expected_headers, options->requestHeaders());
EXPECT_EQ(1234, options->requestBodySize());
envoy::config::core::v3::TransportSocket expected_transport_socket;
TextFormat::ParseFromString(
R"pb(name: "envoy.transport_sockets.tls"
typed_config {
[type.googleapis.com/
envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext] {
common_tls_context {
tls_params {
cipher_suites: "-ALL:ECDHE-RSA-AES256-GCM-SHA384"
}
}
}
})pb",
&expected_transport_socket);
EXPECT_THAT(options->transportSocket().value(), EqualsProto(expected_transport_socket));
EXPECT_EQ(10, options->maxPendingRequests());
EXPECT_EQ(11, options->maxActiveRequests());
EXPECT_EQ(12, options->maxRequestsPerConnection());
EXPECT_EQ(nighthawk::client::SequencerIdleStrategy::SLEEP, options->sequencerIdleStrategy());
ASSERT_EQ(2, options->terminationPredicates().size());
EXPECT_EQ(1, options->terminationPredicates()["t1"]);
EXPECT_EQ(2, options->terminationPredicates()["t2"]);
ASSERT_EQ(2, options->failurePredicates().size());
EXPECT_EQ(1, options->failurePredicates()["f1"]);
EXPECT_EQ(2, options->failurePredicates()["f2"]);
EXPECT_TRUE(options->noDefaultFailurePredicates());
EXPECT_EQ(10us, options->jitterUniform());
EXPECT_EQ(42, options->maxConcurrentStreams());
EXPECT_EQ(nighthawk::client::H1ConnectionReuseStrategy::LRU,
options->h1ConnectionReuseStrategy());
const std::vector<std::string> expected_labels{"label1", "label2"};
EXPECT_EQ(expected_labels, options->labels());
EXPECT_TRUE(options->simpleWarmup());
EXPECT_EQ(10, options->statsFlushInterval());
ASSERT_EQ(2, options->statsSinks().size());
envoy::config::metrics::v3::StatsSink expected_stats_sink1;
TextFormat::ParseFromString(
R"pb(name: "envoy.stat_sinks.statsd"
typed_config {
[type.googleapis.com/envoy.config.metrics.v3.StatsdSink] {
tcp_cluster_name: "statsd"
}
})pb",
&expected_stats_sink1);
EXPECT_THAT(options->statsSinks()[0], EqualsProto(expected_stats_sink1));
envoy::config::metrics::v3::StatsSink expected_stats_sink2;
TextFormat::ParseFromString(
R"pb(name: "envoy.stat_sinks.statsd"
typed_config {
[type.googleapis.com/envoy.config.metrics.v3.StatsdSink] {
tcp_cluster_name: "statsd"
prefix: "nighthawk"
}
})pb",
&expected_stats_sink2);
EXPECT_THAT(options->statsSinks()[1], EqualsProto(expected_stats_sink2));
EXPECT_EQ("zz", options->responseHeaderWithLatencyInput());
nighthawk::FakeUserDefinedOutputConfig expected_user_defined_output_config;
nighthawk::FakeUserDefinedOutputConfig actual_user_defined_output_config;
EXPECT_EQ(options->userDefinedOutputPluginConfigs().size(), 1);
envoy::config::core::v3::TypedExtensionConfig extension_config =
options->userDefinedOutputPluginConfigs()[0];
EXPECT_TRUE(Envoy::MessageUtil::unpackTo(extension_config.typed_config(),
actual_user_defined_output_config)
.ok());
EXPECT_THAT(actual_user_defined_output_config, EqualsProto(expected_user_defined_output_config));
// Check that our conversion to CommandLineOptionsPtr makes sense.
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_EQ(cmd->requests_per_second().value(), options->requestsPerSecond());
EXPECT_EQ(cmd->connections().value(), options->connections());
EXPECT_EQ(cmd->duration().seconds(), options->duration().count());
EXPECT_EQ(cmd->timeout().seconds(), options->timeout().count());
EXPECT_TRUE(cmd->h2().value());
EXPECT_EQ(cmd->concurrency().value(), options->concurrency());
EXPECT_EQ(cmd->verbosity().value(), options->verbosity());
EXPECT_EQ(cmd->output_format().value(), options->outputFormat());
EXPECT_EQ(cmd->prefetch_connections().value(), options->prefetchConnections());
EXPECT_EQ(cmd->burst_size().value(), options->burstSize());
EXPECT_EQ(cmd->address_family().value(), options->addressFamily());
EXPECT_EQ(cmd->uri().value(), options->uri());
EXPECT_EQ(cmd->request_options().request_method(), cmd->request_options().request_method());
EXPECT_EQ(expected_headers.size(), cmd->request_options().request_headers_size());
int i = 0;
for (const auto& header : cmd->request_options().request_headers()) {
EXPECT_EQ(expected_headers[i++], header.header().key() + ":" + header.header().value());
}
EXPECT_EQ(cmd->request_options().request_body_size().value(), options->requestBodySize());
EXPECT_TRUE(util(cmd->transport_socket(), options->transportSocket().value()));
EXPECT_EQ(cmd->max_pending_requests().value(), options->maxPendingRequests());
EXPECT_EQ(cmd->max_active_requests().value(), options->maxActiveRequests());
EXPECT_EQ(cmd->max_requests_per_connection().value(), options->maxRequestsPerConnection());
EXPECT_EQ(cmd->sequencer_idle_strategy().value(), options->sequencerIdleStrategy());
ASSERT_EQ(2, cmd->termination_predicates_size());
EXPECT_EQ(cmd->termination_predicates().at("t1"), 1);
EXPECT_EQ(cmd->termination_predicates().at("t2"), 2);
ASSERT_EQ(2, cmd->failure_predicates_size());
EXPECT_EQ(cmd->failure_predicates().at("f1"), 1);
EXPECT_EQ(cmd->failure_predicates().at("f2"), 2);
EXPECT_TRUE(cmd->no_default_failure_predicates().value());
// Now we construct a new options from the proto we created above. This should result in an
// OptionsImpl instance equivalent to options. We test that by converting both to yaml strings,
// expecting them to be equal. This should provide helpful output when the test fails by showing
// the unexpected (yaml) diff.
// The predicates are defined as proto maps, and these seem to re-serialize into a different
// order. Hence we trim the maps to contain a single entry so they don't thwart our textual
// comparison below.
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("f1"));
EXPECT_EQ(1, cmd->mutable_termination_predicates()->erase("t1"));
EXPECT_EQ(cmd->jitter_uniform().nanos(), options->jitterUniform().count());
EXPECT_EQ(cmd->max_concurrent_streams().value(), options->maxConcurrentStreams());
EXPECT_EQ(cmd->experimental_h1_connection_reuse_strategy().value(),
options->h1ConnectionReuseStrategy());
EXPECT_THAT(cmd->labels(), ElementsAreArray(expected_labels));
EXPECT_EQ(cmd->simple_warmup().value(), options->simpleWarmup());
EXPECT_EQ(10, cmd->stats_flush_interval().value());
ASSERT_EQ(cmd->stats_sinks_size(), options->statsSinks().size());
EXPECT_TRUE(util(cmd->stats_sinks(0), options->statsSinks()[0]));
EXPECT_TRUE(util(cmd->stats_sinks(1), options->statsSinks()[1]));
EXPECT_EQ(cmd->latency_response_header_name().value(), options->responseHeaderWithLatencyInput());
EXPECT_EQ(cmd->user_defined_plugin_configs_size(),
options->userDefinedOutputPluginConfigs().size());
EXPECT_TRUE(
util(cmd->user_defined_plugin_configs(0), options->userDefinedOutputPluginConfigs()[0]));
// TODO(#433) Here and below, replace comparisons once we choose a proto diff.
OptionsImpl options_from_proto(*cmd);
std::string s1 = Envoy::MessageUtil::getYamlStringFromMessage(
*(options_from_proto.toCommandLineOptions()), true, true);
std::string s2 = Envoy::MessageUtil::getYamlStringFromMessage(*cmd, true, true);
EXPECT_EQ(s1, s2);
// For good measure, also directly test for proto equivalence, though this should be
// superfluous.
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *cmd));
}
// We test RequestSource here and not in All above because it is exclusive to some of the other
// options.
TEST_F(OptionsImplTest, RequestSource) {
Envoy::MessageUtil util;
const std::string request_source = "127.9.9.4:32323";
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --request-source {} {}", client_name_, request_source, good_test_uri_));
EXPECT_EQ(options->requestSource(), request_source);
// Check that our conversion to CommandLineOptionsPtr makes sense.
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_EQ(cmd->request_source().uri(), request_source);
// TODO(#433)
OptionsImpl options_from_proto(*cmd);
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *cmd));
}
class RequestSourcePluginTestFixture : public OptionsImplTest,
public WithParamInterface<std::string> {};
TEST_P(RequestSourcePluginTestFixture, CreatesOptionsImplWithRequestSourceConfig) {
Envoy::MessageUtil util;
const std::string request_source_config = GetParam();
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --request-source-plugin-config {} {}", client_name_, request_source_config,
good_test_uri_));
CommandLineOptionsPtr command = options->toCommandLineOptions();
EXPECT_TRUE(
util(command->request_source_plugin_config(), options->requestSourcePluginConfig().value()));
// The predicates are defined as proto maps, and these seem to re-serialize into a different
// order. Hence we trim the maps to contain a single entry so they don't thwart our textual
// comparison below.
EXPECT_EQ(1, command->mutable_failure_predicates()->erase("benchmark.http_4xx"));
EXPECT_EQ(1, command->mutable_failure_predicates()->erase("benchmark.http_5xx"));
EXPECT_EQ(1, command->mutable_failure_predicates()->erase("benchmark.stream_resets"));
EXPECT_EQ(1, command->mutable_failure_predicates()->erase("requestsource.upstream_rq_5xx"));
// TODO(#433)
// Now we construct a new options from the proto we created above. This should result in an
// OptionsImpl instance equivalent to options. We test that by converting both to yaml strings,
// expecting them to be equal. This should provide helpful output when the test fails by showing
// the unexpected (yaml) diff.
OptionsImpl options_from_proto(*command);
std::string yaml_for_options_proto = Envoy::MessageUtil::getYamlStringFromMessage(
*(options_from_proto.toCommandLineOptions()), true, true);
std::string yaml_for_command = Envoy::MessageUtil::getYamlStringFromMessage(*command, true, true);
EXPECT_EQ(yaml_for_options_proto, yaml_for_command);
// Additional comparison to avoid edge cases missed.
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *command));
}
std::vector<std::string> RequestSourcePluginJsons() {
std::string file_request_source_plugin_json =
"{"
R"(name:"nighthawk.file-based-request-source-plugin",)"
"typed_config:{"
R"("@type":"type.googleapis.com/)"
R"(nighthawk.request_source.FileBasedOptionsListRequestSourceConfig",)"
R"(file_path:")" +
TestEnvironment::runfilesPath("test/request_source/test_data/test-config-ab.yaml") +
"\","
"}"
"}";
std::string in_line_request_source_plugin_json =
"{"
R"(name:"nighthawk.in-line-options-list-request-source-plugin",)"
"typed_config:{"
R"("@type":"type.googleapis.com/)"
R"(nighthawk.request_source.InLineOptionsListRequestSourceConfig",)"
"options_list:{"
R"(options:[{request_method:"1",request_headers:[{header:{key:"key",value:"value"}}]}])"
"},"
"}"
"}";
std::string stub_request_source_plugin_json =
"{"
R"(name:"nighthawk.stub-request-source-plugin",)"
"typed_config:{"
R"("@type":"type.googleapis.com/nighthawk.request_source.StubPluginConfig",)"
R"(test_value:"3",)"
"}"
"}";
return std::vector<std::string>{
file_request_source_plugin_json,
in_line_request_source_plugin_json,
stub_request_source_plugin_json,
};
}
INSTANTIATE_TEST_SUITE_P(HappyPathRequestSourceConfigJsonSuccessfullyTranslatesIntoOptions,
RequestSourcePluginTestFixture,
testing::ValuesIn(RequestSourcePluginJsons()));
// This test covers --RequestSourcePlugin, which can't be tested at the same time as --RequestSource
// and some other options. This is the test for the inlineoptionslistplugin.
TEST_F(OptionsImplTest, InLineOptionsListRequestSourcePluginIsMutuallyExclusiveWithRequestSource) {
const std::string request_source = "127.9.9.4:32323";
const std::string request_source_config =
"{"
"name:\"nighthawk.in-line-options-list-request-source-plugin\","
"typed_config:{"
"\"@type\":\"type.googleapis.com/"
"nighthawk.request_source.InLineOptionsListRequestSourceConfig\","
"options_list:{"
"options:[{request_method:\"1\",request_headers:[{header:{key:\"key\",value:\"value\"}}]}]"
"},"
"}"
"}";
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(
fmt::format("{} --request-source-plugin-config {} --request-source {} {}", client_name_,
request_source_config, request_source, good_test_uri_)),
MalformedArgvException,
"--request-source and --request_source_plugin_config cannot both be set.");
}
TEST_F(OptionsImplTest, BadRequestSourcePluginSpecification) {
// Bad JSON
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --request-source-plugin-config {} {}",
client_name_, "{broken_json:", good_test_uri_)),
MalformedArgvException, "Unable to parse JSON as proto");
// Correct JSON, but contents not according to spec.
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --request-source-plugin-config {} {}", client_name_,
"{misspelled_field:{}}", good_test_uri_)),
MalformedArgvException,
"envoy.config.core.v3.TypedExtensionConfig reason INVALID_ARGUMENT");
}
// We test --no-duration here and not in All above because it is exclusive to --duration.
TEST_F(OptionsImplTest, NoDuration) {
Envoy::MessageUtil util;
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --no-duration {}", client_name_, good_test_uri_));
EXPECT_TRUE(options->noDuration());
// Check that our conversion to CommandLineOptionsPtr makes sense.
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
// TODO(#433)
OptionsImpl options_from_proto(*cmd);
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *cmd));
}
// This test covers --tls-context, which can't be tested at the same time as --transport-socket.
// We test --tls-context here and not in AlmostAll above because it is mutually
// exclusive with --transport-socket.
TEST_F(OptionsImplTest, TlsContext) {
Envoy::MessageUtil util;
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --tls-context {} {}", client_name_,
"{common_tls_context:{tls_params:{"
"cipher_suites:[\"-ALL:ECDHE-RSA-AES256-GCM-SHA384\"]}}}",
good_test_uri_));
envoy::extensions::transport_sockets::tls::v3::UpstreamTlsContext expected_tls_context;
TextFormat::ParseFromString(
R"pb(common_tls_context {
tls_params { cipher_suites: "-ALL:ECDHE-RSA-AES256-GCM-SHA384" }
})pb",
&expected_tls_context);
EXPECT_THAT(options->tlsContext(), EqualsProto(expected_tls_context));
// Check that our conversion to CommandLineOptionsPtr makes sense.
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_TRUE(util(cmd->tls_context(), options->tlsContext()));
// Now we construct a new options from the proto we created above. This should result in an
// OptionsImpl instance equivalent to options. We test that by converting both to yaml strings,
// expecting them to be equal. This should provide helpful output when the test fails by showing
// the unexpected (yaml) diff.
// The predicates are defined as proto maps, and these seem to re-serialize into a different
// order. Hence we trim the maps to contain a single entry so they don't thwart our textual
// comparison below.
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.http_4xx"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.http_5xx"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.stream_resets"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("requestsource.upstream_rq_5xx"));
// TODO(#433)
OptionsImpl options_from_proto(*cmd);
std::string s1 = Envoy::MessageUtil::getYamlStringFromMessage(
*(options_from_proto.toCommandLineOptions()), true, true);
std::string s2 = Envoy::MessageUtil::getYamlStringFromMessage(*cmd, true, true);
EXPECT_EQ(s1, s2);
// For good measure, also directly test for proto equivalence, though this should be
// superfluous.
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *cmd));
}
// We test --multi-target-* options here and not in AlmostAll above because they are mutually
// exclusive with the URI arg.
TEST_F(OptionsImplTest, MultiTarget) {
Envoy::MessageUtil util;
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --multi-target-endpoint 1.1.1.1:3 "
"--multi-target-endpoint 2.2.2.2:4 "
"--multi-target-endpoint [::1]:5 "
"--multi-target-endpoint www.example.com:6 "
"--multi-target-path /x/y/z --multi-target-use-https",
client_name_));
EXPECT_EQ("/x/y/z", options->multiTargetPath());
EXPECT_EQ(true, options->multiTargetUseHttps());
ASSERT_EQ(4, options->multiTargetEndpoints().size());
EXPECT_EQ("1.1.1.1", options->multiTargetEndpoints()[0].address().value());
EXPECT_EQ(3, options->multiTargetEndpoints()[0].port().value());
EXPECT_EQ("2.2.2.2", options->multiTargetEndpoints()[1].address().value());
EXPECT_EQ(4, options->multiTargetEndpoints()[1].port().value());
EXPECT_EQ("[::1]", options->multiTargetEndpoints()[2].address().value());
EXPECT_EQ(5, options->multiTargetEndpoints()[2].port().value());
EXPECT_EQ("www.example.com", options->multiTargetEndpoints()[3].address().value());
EXPECT_EQ(6, options->multiTargetEndpoints()[3].port().value());
CommandLineOptionsPtr cmd = options->toCommandLineOptions();
EXPECT_EQ(cmd->multi_target().use_https().value(), options->multiTargetUseHttps());
EXPECT_EQ(cmd->multi_target().path().value(), options->multiTargetPath());
ASSERT_EQ(4, cmd->multi_target().endpoints_size());
EXPECT_EQ(cmd->multi_target().endpoints(0).address().value(), "1.1.1.1");
EXPECT_EQ(cmd->multi_target().endpoints(0).port().value(), 3);
EXPECT_EQ(cmd->multi_target().endpoints(1).address().value(), "2.2.2.2");
EXPECT_EQ(cmd->multi_target().endpoints(1).port().value(), 4);
EXPECT_EQ(cmd->multi_target().endpoints(2).address().value(), "[::1]");
EXPECT_EQ(cmd->multi_target().endpoints(2).port().value(), 5);
EXPECT_EQ(cmd->multi_target().endpoints(3).address().value(), "www.example.com");
EXPECT_EQ(cmd->multi_target().endpoints(3).port().value(), 6);
// Now we construct a new options from the proto we created above. This should result in an
// OptionsImpl instance equivalent to options. We test that by converting both to yaml strings,
// expecting them to be equal. This should provide helpful output when the test fails by showing
// the unexpected (yaml) diff.
// The predicates are defined as proto maps, and these seem to re-serialize into a different
// order. Hence we trim the maps to contain a single entry so they don't thwart our
// textual comparison below.
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.http_4xx"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.http_5xx"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("benchmark.stream_resets"));
EXPECT_EQ(1, cmd->mutable_failure_predicates()->erase("requestsource.upstream_rq_5xx"));
// TODO(#433)
OptionsImpl options_from_proto(*cmd);
std::string s1 = Envoy::MessageUtil::getYamlStringFromMessage(
*(options_from_proto.toCommandLineOptions()), true, true);
std::string s2 = Envoy::MessageUtil::getYamlStringFromMessage(*cmd, true, true);
EXPECT_EQ(s1, s2);
// For good measure, also directly test for proto equivalence, though this should be
// superfluous.
EXPECT_TRUE(util(*(options_from_proto.toCommandLineOptions()), *cmd));
}
// Test that TCLAP's way of handling --help behaves as expected.
TEST_F(OptionsImplTest, Help) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --help", client_name_)),
NoServingException, "NoServingException");
}
// Test that TCLAP's way of handling --version behaves as expected.
TEST_F(OptionsImplTest, Version) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --version", client_name_)),
NoServingException, "NoServingException");
}
// We should fail when no arguments are passed.
TEST_F(OptionsImplTest, NoArguments) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{}", client_name_)),
MalformedArgvException,
"A URI or --multi-target-\\* options must be specified.");
}
TEST_P(OptionsImplIntTestNonZeroable, NonZeroableOptions) {
const char* option_name = GetParam();
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --{} 0 --stats-sinks {} {}", client_name_, option_name,
"{name:\"envoy.stat_sinks.statsd\"}", good_test_uri_)),
std::exception, "Proto constraint validation failed");
}
INSTANTIATE_TEST_SUITE_P(NonZeroableIntOptionTests, OptionsImplIntTestNonZeroable,
Values("rps", "connections", "max-active-requests",
"max-requests-per-connection", "stats-flush-interval"));
// Check standard expectations for any integer values options we offer.
TEST_P(OptionsImplIntTest, IntOptionsBadValuesTest) {
const char* option_name = GetParam();
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --{} -1 {}", client_name_,
option_name, good_test_uri_)),
MalformedArgvException,
fmt::format("Invalid value for --{}", option_name));
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --{} {}", client_name_, option_name, good_test_uri_)),
MalformedArgvException, "Couldn't read argument value from string");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --{} foo {}", client_name_,
option_name, good_test_uri_)),
MalformedArgvException, "Couldn't read argument value");
}
INSTANTIATE_TEST_SUITE_P(IntOptionTests, OptionsImplIntTest,
Values("rps", "connections", "duration", "timeout", "request-body-size",
"burst-size", "max-pending-requests", "max-active-requests",
"max-requests-per-connection"));
TEST_F(OptionsImplTest, MaxConcurrentStreamsHasDefaultValue) {
const std::unique_ptr<OptionsImpl> option =
TestUtility::createOptionsImpl(fmt::format("{} {}", client_name_, good_test_uri_));
EXPECT_EQ(OptionsImpl::largest_acceptable_concurrent_streams_value,
option->maxConcurrentStreams());
// Verify the default remains when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(OptionsImpl::largest_acceptable_concurrent_streams_value,
converted_option->maxConcurrentStreams());
}
TEST_F(OptionsImplTest, UsesHttp11ByDefault) {
const std::unique_ptr<OptionsImpl> option =
TestUtility::createOptionsImpl(fmt::format("{} {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http11, option->protocol());
// Verify the default remains HTTP/1.1 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http11, converted_option->protocol());
}
TEST_F(OptionsImplTest, UsesHttp1WhenProtocolHttp1IsSet) {
const std::unique_ptr<OptionsImpl> option = TestUtility::createOptionsImpl(
fmt::format("{} --protocol http1 {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http11, option->protocol());
// Verify the default remains HTTP/1.1 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http11, converted_option->protocol());
}
TEST_F(OptionsImplTest, UsesHttp2WhenH2FlagIsSet) {
const std::unique_ptr<OptionsImpl> option =
TestUtility::createOptionsImpl(fmt::format("{} --h2 {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http2, option->protocol());
// Verify the default remains HTTP/2 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http2, converted_option->protocol());
}
TEST_F(OptionsImplTest, UsesHttp2WhenProtocolHttp2IsSet) {
const std::unique_ptr<OptionsImpl> option = TestUtility::createOptionsImpl(
fmt::format("{} --protocol http2 {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http2, option->protocol());
// Verify the default remains HTTP/2 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http2, converted_option->protocol());
}
TEST_F(OptionsImplTest, FailsForInvalidH2FlagValues) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --h2 0 {}", client_name_, good_test_uri_)),
MalformedArgvException, "Couldn't find match for argument");
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --h2 true {}", client_name_, good_test_uri_)),
MalformedArgvException, "Couldn't find match for argument");
}
TEST_F(OptionsImplTest, UsesHttp3WhenProtocolHttp3IsSet) {
const std::unique_ptr<OptionsImpl> option = TestUtility::createOptionsImpl(
fmt::format("{} --protocol http3 {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http3, option->protocol());
// Verify the default remains HTTP/3 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http3, converted_option->protocol());
}
TEST_F(OptionsImplTest, UsesHttp3WhenProtocolShortFormFlagIsSet) {
const std::unique_ptr<OptionsImpl> option =
TestUtility::createOptionsImpl(fmt::format("{} -p http3 {}", client_name_, good_test_uri_));
EXPECT_EQ(Envoy::Http::Protocol::Http3, option->protocol());
// Verify the default remains HTTP/3 when converting back from proto.
CommandLineOptionsPtr proto = option->toCommandLineOptions();
const auto converted_option = std::make_unique<OptionsImpl>(*proto);
EXPECT_EQ(Envoy::Http::Protocol::Http3, converted_option->protocol());
}
TEST_F(OptionsImplTest, FailsForInvalidProtocolFlagValues) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --protocol 0 {}", client_name_, good_test_uri_)),
MalformedArgvException, "does not meet constraint");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --protocol true {}", client_name_, good_test_uri_)),
MalformedArgvException, "does not meet constraint");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --protocol http0 {}", client_name_, good_test_uri_)),
MalformedArgvException, "does not meet constraint");
}
TEST_F(OptionsImplTest, FailsWhenBothH2AndProtocolAreSet) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --h2 --protocol http1 http://foo", client_name_)),
MalformedArgvException, "mutually exclusive");
}
TEST_F(OptionsImplTest, BadHttp3ProtocolOptionsSpecification) {
// Bad JSON.
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --protocol http3 --http3-protocol-options {} http://foo/",
client_name_, "{broken_json:")),
MalformedArgvException, "Unable to parse JSON as proto");
// Correct JSON, but contents not according to spec.
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --protocol http3 --http3-protocol-options {} http://foo/",
client_name_, "{invalid_http3_protocol_options:{}}")),
MalformedArgvException, "INVALID_ARGUMENT");
}
TEST_F(OptionsImplTest, FailsWhenHttp3ProtocolOptionsSpecifiedForNonHttp3) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --protocol http2 --http3-protocol-options {} http://foo/",
client_name_, "{quic_protocol_options:{max_concurrent_streams:1}}")),
MalformedArgvException,
"--http3-protocol-options can only be used with --protocol http3");
}
TEST_F(OptionsImplTest, FailsWhenBothHttp3ProtocolOptionsAndMaxConcurrentStreamsAreSet) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format(
"{} --protocol http3 --http3-protocol-options {} --max-concurrent-streams 2 http://foo/",
client_name_, "{max_concurrent_streams:1}")),
MalformedArgvException, "");
}
TEST_F(OptionsImplTest, FailsWhenDeprecatedExperimentalH2UseMultipleConnectionsIsSetOnCommandLine) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(
fmt::format("{} --experimental-h2-use-multiple-connections http://foo", client_name_)),
MalformedArgvException, "experimental-h2-use-multiple-connections");
}
TEST_F(OptionsImplTest, FailsWhenDeprecatedExperimentalH2UseMultipleConnectionsIsSetViaProto) {
const std::unique_ptr<OptionsImpl> option =
TestUtility::createOptionsImpl(fmt::format("{} http://127.0.0.1/", client_name_));
CommandLineOptionsPtr proto = option->toCommandLineOptions();
proto->mutable_experimental_h2_use_multiple_connections()->set_value(true);
EXPECT_THROW_WITH_REGEX((void)std::make_unique<OptionsImpl>(*proto), MalformedArgvException,
"experimental_h2_use_multiple_connections");
}
TEST_F(OptionsImplTest, PrefetchConnectionsFlag) {
EXPECT_FALSE(TestUtility::createOptionsImpl(fmt::format("{} {}", client_name_, good_test_uri_))
->prefetchConnections());
EXPECT_TRUE(TestUtility::createOptionsImpl(
fmt::format("{} --prefetch-connections {}", client_name_, good_test_uri_))
->prefetchConnections());
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --prefetch-connections 0 {}", client_name_, good_test_uri_)),
MalformedArgvException, "Couldn't find match for argument");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --prefetch-connections true {}", client_name_, good_test_uri_)),
MalformedArgvException, "Couldn't find match for argument");
}
// Test --concurrency, which is a bit special. It's an int option, which also accepts 'auto' as
// a value. We need to implement some stuff ourselves to get this to work, hence we don't run it
// through the OptionsImplIntTest.
TEST_F(OptionsImplTest, BadConcurrencyValuesThrow) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --concurrency 0 {}", client_name_, good_test_uri_)),
MalformedArgvException,
"Value for --concurrency should be greater then 0.");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --concurrency -1 {}", client_name_, good_test_uri_)),
MalformedArgvException,
"Value for --concurrency should be greater then 0.");
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} --concurrency foo {}", client_name_, good_test_uri_)),
MalformedArgvException, "Invalid value for --concurrency");
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(
fmt::format("{} --concurrency 999999999999999999999 {}", client_name_, good_test_uri_)),
MalformedArgvException, "Value out of range: --concurrency");
}
TEST_F(OptionsImplTest, JitterValueRangeTest) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --jitter-uniform a {}",
client_name_, good_test_uri_)),
MalformedArgvException, "Invalid value for --jitter-uniform");
// Should end with 's'.
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --jitter-uniform .1 {}",
client_name_, good_test_uri_)),
MalformedArgvException, "Invalid value for --jitter-uniform");
// No negative durations accepted
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --jitter-uniform -1s {}",
client_name_, good_test_uri_)),
MalformedArgvException, "--jitter-uniform is out of range");
// Durations >= 0s are accepted
EXPECT_NO_THROW(TestUtility::createOptionsImpl(
fmt::format("{} --jitter-uniform 0s {}", client_name_, good_test_uri_)));
EXPECT_NO_THROW(TestUtility::createOptionsImpl(
fmt::format("{} --jitter-uniform 0.1s {}", client_name_, good_test_uri_)));
EXPECT_NO_THROW(TestUtility::createOptionsImpl(
fmt::format("{} --jitter-uniform 1s {}", client_name_, good_test_uri_)));
EXPECT_NO_THROW(TestUtility::createOptionsImpl(
fmt::format("{} --jitter-uniform 100s {}", client_name_, good_test_uri_)));
}
// Test a relatively large uint value to see if we can get reasonable range
// when we specced a uint32_t
// See https://github.com/envoyproxy/nighthawk/pull/88/files#r299572672
TEST_F(OptionsImplTest, ParserIntRangeTest) {
const uint32_t test_value = OptionsImpl::largest_acceptable_uint32_option_value;
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(fmt::format(
"{} --max-requests-per-connection {} {} ", client_name_, test_value, good_test_uri_));
EXPECT_EQ(test_value, options->maxRequestsPerConnection());
}
// Test we accept --concurrency auto
TEST_F(OptionsImplTest, AutoConcurrencyValueParsedOK) {
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --concurrency auto {} ", client_name_, good_test_uri_));
EXPECT_EQ("auto", options->concurrency());
}
TEST_F(OptionsImplTest, NoDefaultFailurePredicates) {
std::unique_ptr<OptionsImpl> options = TestUtility::createOptionsImpl(
fmt::format("{} --no-default-failure-predicates {}", client_name_, good_test_uri_));
EXPECT_EQ(0, options->failurePredicates().size());
}
class OptionsImplVerbosityTest : public OptionsImplTest, public WithParamInterface<const char*> {};
// Test we accept all possible --verbosity values.
TEST_P(OptionsImplVerbosityTest, VerbosityValues) {
TestUtility::createOptionsImpl(
fmt::format("{} --verbosity {} {}", client_name_, GetParam(), good_test_uri_));
}
INSTANTIATE_TEST_SUITE_P(VerbosityOptionTests, OptionsImplVerbosityTest,
Values("trace", "debug", "info", "warn", "error", "critical"));
// Test we don't accept any bad --verbosity values.
TEST_F(OptionsImplTest, VerbosityValuesAreConstrained) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(
fmt::format("{} {} --verbosity foo", client_name_, good_test_uri_)),
MalformedArgvException, "Value 'foo' does not meet constraint");
}
/// ---
class OptionsImplRequestMethodTest : public OptionsImplTest,
public WithParamInterface<const char*> {};
// Test we accept all possible --request-method values.
TEST_P(OptionsImplRequestMethodTest, RequestMethodValues) {
TestUtility::createOptionsImpl(
fmt::format("{} --request-method {} {}", client_name_, GetParam(), good_test_uri_));
}
INSTANTIATE_TEST_SUITE_P(RequestMethodOptionTests, OptionsImplRequestMethodTest,
Values("GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS",
"TRACE"));
// Test we don't accept any bad --request-method values.
TEST_F(OptionsImplTest, RequestMethodValuesAreConstrained) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} {} --request-method foo",
client_name_, good_test_uri_)),
MalformedArgvException, "Value 'foo' does not meet constraint");
}
class OptionsImplAddressFamilyTest : public OptionsImplTest,
public WithParamInterface<const char*> {};
// Test we accept all possible --address-family values.
TEST_P(OptionsImplAddressFamilyTest, AddressFamilyValues) {
TestUtility::createOptionsImpl(
fmt::format("{} --address-family {} {}", client_name_, GetParam(), good_test_uri_));
}
INSTANTIATE_TEST_SUITE_P(AddressFamilyOptionTests, OptionsImplAddressFamilyTest,
Values("v4", "v6", "auto"));
// Test we don't accept any bad --address-family values.
TEST_F(OptionsImplTest, AddressFamilyValuesAreConstrained) {
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format("{} --address-family foo {}",
client_name_, good_test_uri_)),
MalformedArgvException, "Value 'foo' does not meet constraint");
}
// TODO(oschaaf): URI parsing/validation is weaker then it should be at the moment.
TEST_F(OptionsImplTest, InacceptibleUri) {
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} bad://127.0.0.1/", client_name_)),
MalformedArgvException, "Invalid target URI: ''");
}
TEST_F(OptionsImplTest, ProtoConstructorValidation) {
const auto option =
TestUtility::createOptionsImpl(fmt::format("{} http://127.0.0.1/", client_name_));
auto proto = option->toCommandLineOptions();
proto->mutable_requests_per_second()->set_value(0);
EXPECT_THROW_WITH_REGEX((void)std::make_unique<OptionsImpl>(*proto), MalformedArgvException,
"CommandLineOptionsValidationError.RequestsPerSecond");
}
TEST_F(OptionsImplTest, BadTlsContextSpecification) {
// Bad JSON
EXPECT_THROW_WITH_REGEX(TestUtility::createOptionsImpl(fmt::format(
"{} --tls-context {} http://foo/", client_name_, "{broken_json:")),
MalformedArgvException, "Unable to parse JSON as proto");
// Correct JSON, but contents not according to spec.
EXPECT_THROW_WITH_REGEX(
TestUtility::createOptionsImpl(fmt::format("{} --tls-context {} http://foo/", client_name_,
"{misspelled_tls_context:{}}")),
MalformedArgvException, "INVALID_ARGUMENT");
}