-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathgroups.rs
More file actions
1469 lines (1292 loc) · 50.8 KB
/
Copy pathgroups.rs
File metadata and controls
1469 lines (1292 loc) · 50.8 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
//! Integration tests for multicast group APIs and IP pool operations.
//!
//! Core multicast functionality tests:
//!
//! - IP pool range validation and allocation
//! - Member operations: add, remove, list, lookup by IP
//! - Instance deletion cleanup (removes multicast memberships)
//! - Automatic pool selection and default pool behavior
//! - Pool exhaustion handling
//! - Pool deletion protection (cannot delete pool with active groups)
//! - DPD(-client) integration: verifies groups are programmed on switches
//! - SSM pool and group tests:
//! - SSM groups require sources (232/8 for IPv4, ff3x::/32 for IPv6)
//! - ASM groups have optional source filtering
//! - Multiple SSM groups can share the same pool
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use dpd_client::types as dpd_types;
use dropshot::HttpErrorResponseBody;
use dropshot::ResultsPage;
use http::{Method, StatusCode};
use crate::integration_tests::instances::{
instance_simulate, instance_wait_for_state,
};
use nexus_test_utils::dpd_client;
use nexus_test_utils::http_testing::{
AuthnMode, Collection, NexusRequest, RequestBuilder,
};
use nexus_test_utils::resource_helpers::{
create_default_ip_pools, create_instance, create_project, link_ip_pool,
object_create, object_create_error, object_delete, object_delete_error,
object_get, object_get_error, object_put_error,
};
use nexus_test_utils_macros::nexus_test;
use nexus_types::external_api::ip_pool::{
IpPool, IpPoolCreate, IpPoolRange, IpRange, IpVersion, Ipv4Range, Ipv6Range,
};
use nexus_types::external_api::multicast::{
InstanceMulticastGroupJoin, MulticastGroup, MulticastGroupMember,
};
use omicron_common::api::external::{
IdentityMetadataCreateParams, InstanceState,
};
use omicron_uuid_kinds::InstanceUuid;
use super::*;
/// Test that multicast IP pools reject invalid ranges at the pool level
#[nexus_test]
async fn test_multicast_ip_pool_range_validation(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
// Create IPv4 multicast pool
let pool_params = IpPoolCreate::new_multicast(
IdentityMetadataCreateParams {
name: "test-v4-pool".parse().unwrap(),
description: "IPv4 multicast pool for validation tests".to_string(),
},
IpVersion::V4,
);
object_create::<_, IpPool>(client, "/v1/system/ip-pools", &pool_params)
.await;
let range_url = "/v1/system/ip-pools/test-v4-pool/ranges/add";
// IPv4 non-multicast range should be rejected
let ipv4_unicast_range = IpRange::V4(
Ipv4Range::new(
Ipv4Addr::new(10, 0, 0, 1),
Ipv4Addr::new(10, 0, 0, 255),
)
.unwrap(),
);
object_create_error(
client,
range_url,
&ipv4_unicast_range,
StatusCode::BAD_REQUEST,
)
.await;
// IPv4 link-local multicast range should be rejected
let ipv4_link_local_range = IpRange::V4(
Ipv4Range::new(
Ipv4Addr::new(224, 0, 0, 1),
Ipv4Addr::new(224, 0, 0, 255),
)
.unwrap(),
);
object_create_error(
client,
range_url,
&ipv4_link_local_range,
StatusCode::BAD_REQUEST,
)
.await;
// Valid IPv4 multicast range should be accepted (using ASM range)
let valid_ipv4_range = IpRange::V4(
Ipv4Range::new(
Ipv4Addr::new(224, 1, 0, 1),
Ipv4Addr::new(224, 1, 0, 255),
)
.unwrap(),
);
object_create::<_, IpPoolRange>(client, range_url, &valid_ipv4_range).await;
// Create IPv6 multicast pool
let ipv6_pool_params = IpPoolCreate::new_multicast(
IdentityMetadataCreateParams {
name: "test-v6-pool".parse().unwrap(),
description: "IPv6 multicast pool for validation tests".to_string(),
},
IpVersion::V6,
);
object_create::<_, IpPool>(
client,
"/v1/system/ip-pools",
&ipv6_pool_params,
)
.await;
let v6_range_url = "/v1/system/ip-pools/test-v6-pool/ranges/add";
// IPv6 link-local multicast range (ff02::/16) should be rejected
let ipv6_link_local_range = IpRange::V6(
Ipv6Range::new(
Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 1),
Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 255),
)
.unwrap(),
);
object_create_error(
client,
v6_range_url,
&ipv6_link_local_range,
StatusCode::BAD_REQUEST,
)
.await;
// Valid IPv6 site-local multicast range (ff05::/16) should be accepted
let valid_ipv6_range = IpRange::V6(
Ipv6Range::new(
Ipv6Addr::new(0xff05, 0, 0, 0, 0, 0, 0, 1),
Ipv6Addr::new(0xff05, 0, 0, 0, 0, 0, 0, 255),
)
.unwrap(),
);
object_create::<_, IpPoolRange>(client, v6_range_url, &valid_ipv6_range)
.await;
}
#[nexus_test]
async fn test_multicast_group_member_operations(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "test-project";
let group_name = "test-group";
let instance_name = "test-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client), // For instance networking
create_multicast_ip_pool_with_range(
&client,
"mcast-pool",
(224, 4, 0, 10),
(224, 4, 0, 255),
),
)
.await;
let instance = create_instance(client, project_name, instance_name).await;
// Use instance-centric API: PUT /v1/instances/{instance}/multicast-groups/{group}
let join_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group_name}?project={project_name}"
);
let join_params =
InstanceMulticastGroupJoin { source_ips: None, ip_version: None };
let added_member: MulticastGroupMember =
put_upsert(client, &join_url, &join_params).await;
assert_eq!(
added_member.instance_id.to_string(),
instance.identity.id.to_string()
);
// Wait for member to become joined
// Member starts in "Joining" state and transitions to "Joined" via reconciler
// Member only transitions to "Joined" after successful DPD update
wait_for_member_state(
cptestctx,
group_name,
instance.identity.id,
nexus_db_model::MulticastGroupMemberState::Joined,
)
.await;
// Test listing members (should have 1 now in Joined state)
let members = list_multicast_group_members(&client, group_name).await;
assert_eq!(members.len(), 1, "Expected exactly 1 member");
assert_eq!(members[0].instance_id, added_member.instance_id);
assert_eq!(members[0].multicast_group_id, added_member.multicast_group_id);
// Test listing groups (should include our implicitly created group)
let groups = list_multicast_groups(&client).await;
assert!(
groups.iter().any(|g| g.identity.name == group_name),
"Expected group {group_name} to appear in group listing"
);
// DPD Validation: Verify groups exist in dataplane after member addition
let dpd_client = dpd_client(cptestctx);
// Get the multicast IP from the group (since member doesn't have the IP field)
let group_get_url = mcast_group_url(group_name);
let group: MulticastGroup = object_get(client, &group_get_url).await;
let external_multicast_ip = group.multicast_ip;
// List all groups in DPD to find both external and underlay groups
let dpd_groups = dpd_client
.multicast_groups_list(None, None)
.await
.expect("Should list DPD groups");
// Find the external IPv4 group (should exist but may not have members)
let expect_msg =
format!("External group {external_multicast_ip} should exist in DPD");
dpd_groups
.items
.iter()
.find(|g| {
let ip = match g {
dpd_types::MulticastGroupResponse::External {
group_ip,
..
} => *group_ip,
dpd_types::MulticastGroupResponse::Underlay {
group_ip,
..
} => IpAddr::V6(group_ip.0),
};
ip == external_multicast_ip
&& matches!(
g,
dpd_types::MulticastGroupResponse::External { .. }
)
})
.expect(&expect_msg);
// Directly get the underlay IPv6 group by finding the admin-local address
// First find the underlay group IP from the list to get the exact IPv6 address
let underlay_ip = dpd_groups
.items
.iter()
.find_map(|g| {
match g {
dpd_types::MulticastGroupResponse::Underlay {
group_ip,
..
} => {
// Check if it starts with ff04 (admin-local multicast)
if group_ip.0.segments()[0] == 0xff04 {
Some(group_ip.clone())
} else {
None
}
}
dpd_types::MulticastGroupResponse::External { .. } => None,
}
})
.expect("Should find underlay group IP in DPD response");
// Get the underlay group directly
let underlay_group = dpd_client
.multicast_group_get_underlay(&underlay_ip)
.await
.expect("Should get underlay group from DPD");
assert_eq!(
underlay_group.members.len(),
1,
"Underlay group should have exactly 1 member after member addition"
);
// Assert all underlay members use rear (backplane) ports with Underlay direction
for member in &underlay_group.members {
assert!(
matches!(member.port_id, dpd_client::types::PortId::Rear(_)),
"Underlay member should use rear (backplane) port, got: {:?}",
member.port_id
);
assert_eq!(
member.direction,
dpd_client::types::Direction::Underlay,
"Underlay member should have Underlay direction"
);
}
// Test removing instance from multicast group using instance-centric DELETE
let member_remove_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group_name}?project={project_name}"
);
NexusRequest::new(
RequestBuilder::new(client, http::Method::DELETE, &member_remove_url)
.expect_status(Some(StatusCode::NO_CONTENT)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("Should remove member from multicast group");
// Implicit deletion model: group is implicitly deleted when last member is removed
// Wait for both Nexus group and DPD group to be deleted
wait_for_group_deleted(cptestctx, group_name).await;
wait_for_group_deleted_from_dpd(cptestctx, external_multicast_ip).await;
}
#[nexus_test]
async fn test_instance_multicast_endpoints(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "test-project";
let group1_name = "mcast-group-1";
let group2_name = "mcast-group-2";
let instance_name = "test-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
&client,
"mcast-pool",
(224, 5, 0, 10),
(224, 5, 0, 255),
),
)
.await;
// Implicit deletion model: Groups will implicitly create when first instance joins
// Create an instance (starts automatically with create_instance helper)
let instance = create_instance(client, project_name, instance_name).await;
let instance_id = InstanceUuid::from_untyped_uuid(instance.identity.id);
// Simulate and wait for instance to be fully running with sled_id assigned
let nexus = &cptestctx.server.server_context().nexus;
instance_simulate(nexus, &instance_id).await;
instance_wait_for_state(client, instance_id, InstanceState::Running).await;
wait_for_instance_sled_assignment(cptestctx, &instance_id).await;
// Case: List instance multicast groups (should be empty initially)
let instance_groups_url = format!(
"/v1/instances/{instance_name}/multicast-groups?project={project_name}"
);
let instance_memberships: ResultsPage<MulticastGroupMember> =
object_get(client, &instance_groups_url).await;
assert_eq!(
instance_memberships.items.len(),
0,
"Instance should have no multicast memberships initially"
);
// Case: Join group1 using instance-centric endpoint (implicitly creates group1)
let instance_join_group1_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group1_name}?project={project_name}"
);
let join_params =
InstanceMulticastGroupJoin { source_ips: None, ip_version: None };
// Use PUT method and expect 201 Created (implicitly creating group1)
let member1: MulticastGroupMember =
put_upsert(client, &instance_join_group1_url, &join_params).await;
assert_eq!(member1.instance_id, instance.identity.id);
// Wait for group1 to become active after implicitly create
wait_for_group_active(client, group1_name).await;
// Wait for member to become joined
wait_for_member_state(
cptestctx,
group1_name,
instance.identity.id,
nexus_db_model::MulticastGroupMemberState::Joined,
)
.await;
// Case: Verify membership shows up in both endpoints
// Check group-centric view
let group1_members =
list_multicast_group_members(&client, group1_name).await;
assert_eq!(group1_members.len(), 1);
assert_eq!(group1_members[0].instance_id, instance.identity.id);
// Check instance-centric view (test the list endpoint thoroughly)
let instance_memberships: ResultsPage<MulticastGroupMember> =
object_get(client, &instance_groups_url).await;
assert_eq!(
instance_memberships.items.len(),
1,
"Instance should have exactly 1 membership"
);
assert_eq!(instance_memberships.items[0].instance_id, instance.identity.id);
assert_eq!(
instance_memberships.items[0].multicast_group_id,
member1.multicast_group_id
);
assert_eq!(instance_memberships.items[0].state, "Joined");
// Join group2 using instance-centric endpoint (implicitly creates group2)
let join_group2_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group2_name}?project={project_name}"
);
let join_params2 =
InstanceMulticastGroupJoin { source_ips: None, ip_version: None };
let member2: MulticastGroupMember =
put_upsert(client, &join_group2_url, &join_params2).await;
assert_eq!(member2.instance_id, instance.identity.id);
// Wait for group2 to become active after implicitly create
wait_for_group_active(client, group2_name).await;
// Wait for member to become joined
wait_for_member_state(
cptestctx,
group2_name,
instance.identity.id,
nexus_db_model::MulticastGroupMemberState::Joined,
)
.await;
// Verify instance now belongs to both groups (comprehensive list test)
let instance_memberships: ResultsPage<MulticastGroupMember> =
object_get(client, &instance_groups_url).await;
assert_eq!(
instance_memberships.items.len(),
2,
"Instance should belong to both groups"
);
// Verify the list endpoint returns the correct membership details
let membership_group_ids: Vec<_> = instance_memberships
.items
.iter()
.map(|m| m.multicast_group_id)
.collect();
assert!(
membership_group_ids.contains(&member1.multicast_group_id),
"List should include group1 membership"
);
assert!(
membership_group_ids.contains(&member2.multicast_group_id),
"List should include group2 membership"
);
// Verify all memberships show correct instance_id and state
for membership in &instance_memberships.items {
assert_eq!(membership.instance_id, instance.identity.id);
assert_eq!(membership.state, "Joined");
}
// Verify each group shows the instance as a member
let group1_members =
list_multicast_group_members(&client, group1_name).await;
let group2_members =
list_multicast_group_members(&client, group2_name).await;
assert_eq!(group1_members.len(), 1);
assert_eq!(group2_members.len(), 1);
assert_eq!(group1_members[0].instance_id, instance.identity.id);
assert_eq!(group2_members[0].instance_id, instance.identity.id);
// Leave group1 using instance-centric endpoint
let instance_leave_group1_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group1_name}?project={project_name}"
);
object_delete(client, &instance_leave_group1_url).await;
// Implicit deletion model: group1 should be deleted after last member leaves
wait_for_group_deleted(cptestctx, group1_name).await;
// Verify membership removed from both views
// Check instance-centric view - should only show active memberships (group2)
let instance_memberships: ResultsPage<MulticastGroupMember> =
object_get(client, &instance_groups_url).await;
assert_eq!(
instance_memberships.items.len(),
1,
"Instance should only show active membership (group2)"
);
assert_eq!(
instance_memberships.items[0].multicast_group_id,
member2.multicast_group_id,
"Remaining membership should be group2"
);
assert_eq!(
instance_memberships.items[0].state, "Joined",
"Group2 membership should be Joined"
);
// Check group2 still has the member (group1 is already deleted)
let group2_members =
list_multicast_group_members(&client, group2_name).await;
assert_eq!(group2_members.len(), 1, "Group2 should still have 1 member");
// Leave group2 using instance-centric endpoint
let member_remove_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group2_name}?project={project_name}"
);
NexusRequest::new(
RequestBuilder::new(client, http::Method::DELETE, &member_remove_url)
.expect_status(Some(StatusCode::NO_CONTENT)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("Should remove member from group2");
// Wait for reconciler to process the removal
wait_for_multicast_reconciler(&cptestctx.lockstep_client).await;
// Verify all memberships are gone
let instance_memberships: ResultsPage<MulticastGroupMember> =
object_get(client, &instance_groups_url).await;
assert_eq!(
instance_memberships.items.len(),
0,
"Instance should have no memberships"
);
// Implicit deletion model: Groups should be implicitly deleted after last member removed
ops::join2(
wait_for_group_deleted(cptestctx, group1_name),
wait_for_group_deleted(cptestctx, group2_name),
)
.await;
}
#[nexus_test]
async fn test_multicast_group_member_errors(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "test-project";
let group_name = "test-group";
let nonexistent_instance = "nonexistent-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
&client,
"mcast-pool",
(224, 6, 0, 10),
(224, 6, 0, 255),
),
)
.await;
// Implicitly create a multicast group by adding an instance as first member
let instance_name = "test-instance";
create_instance(client, project_name, instance_name).await;
// Use instance-centric API to join group (implicitly creates group)
multicast_group_attach(cptestctx, project_name, instance_name, group_name)
.await;
// Wait for group to become active before testing error cases
wait_for_group_active(&client, group_name).await;
// Test joining with nonexistent instance - should fail with NOT_FOUND
let bad_join_url = format!(
"/v1/instances/{nonexistent_instance}/multicast-groups/{group_name}?project={project_name}"
);
let join_params =
InstanceMulticastGroupJoin { source_ips: None, ip_version: None };
object_put_error(
client,
&bad_join_url,
&join_params,
StatusCode::NOT_FOUND,
)
.await;
cleanup_instances(cptestctx, client, project_name, &[instance_name]).await;
wait_for_group_deleted(cptestctx, group_name).await;
}
#[nexus_test]
async fn test_instance_deletion_removes_multicast_memberships(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "springfield-squidport"; // Use the same project name as instance helpers
let group_name = "instance-deletion-group";
let instance_name = "deletion-test-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
&client,
"mcast-pool",
(224, 9, 0, 10),
(224, 9, 0, 255),
),
)
.await;
// Implicitly create multicast group by adding instance as first member
let instance = create_instance(client, project_name, instance_name).await;
// Use instance-centric API to join group (implicit group creation)
multicast_group_attach(cptestctx, project_name, instance_name, group_name)
.await;
// Wait for group to become active after implicitly create
wait_for_group_active(&client, group_name).await;
// Get the group to find its auto-allocated IP address (needed for DPD check)
let created_group: MulticastGroup =
object_get(client, &mcast_group_url(group_name)).await;
let multicast_ip = created_group.multicast_ip;
// Wait for member to join
wait_for_member_state(
cptestctx,
group_name,
instance.identity.id,
nexus_db_model::MulticastGroupMemberState::Joined,
)
.await;
// Verify member was added
let members = list_multicast_group_members(&client, group_name).await;
assert_eq!(members.len(), 1, "Instance should be a member of the group");
assert_eq!(members[0].instance_id, instance.identity.id);
// Case: Instance deletion should clean up multicast memberships
cleanup_instances(cptestctx, client, project_name, &[instance_name]).await;
// Verify instance is gone
let instance_url =
format!("/v1/instances/{instance_name}?project={project_name}");
object_get_error(client, &instance_url, StatusCode::NOT_FOUND).await;
// Implicit model: group is implicitly deleted when last member (instance) is removed
wait_for_group_deleted(cptestctx, group_name).await;
// Wait for reconciler to clean up DPD state (activates reconciler repeatedly until DPD confirms deletion)
wait_for_group_deleted_from_dpd(cptestctx, multicast_ip).await;
}
/// Test that the multicast_ip field is correctly populated in MulticastGroupMember API responses.
/// This validates the denormalized multicast_ip field added for API ergonomics.
#[nexus_test]
async fn test_member_response_includes_multicast_ip(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "multicast-ip-test";
let group_name = "test-group";
let instance_name = "test-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
&client,
"test-pool",
(224, 30, 0, 1),
(224, 30, 0, 10),
),
)
.await;
// Create instance for implicit group creation
create_instance(client, project_name, instance_name).await;
// Implicitly create group via instance-centric API
let join_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group_name}?project={project_name}"
);
let join_params =
InstanceMulticastGroupJoin { source_ips: None, ip_version: None };
// Add member and verify multicast_ip field is present in response
let added_member: MulticastGroupMember =
put_upsert(client, &join_url, &join_params).await;
// Wait for group to become active
wait_for_group_active(client, group_name).await;
// Get the group to verify its multicast_ip
let group: MulticastGroup =
object_get(client, &mcast_group_url(group_name)).await;
// Verify multicast_ip field is present in member response
assert_eq!(
added_member.multicast_ip, group.multicast_ip,
"MulticastGroupMember API response should include multicast_ip field that matches the group's IP"
);
// Verify multicast_ip is in expected range from the pool
let member_ip_str = added_member.multicast_ip.to_string();
assert!(
member_ip_str.starts_with("224.30.0."),
"Member multicast_ip should be allocated from the pool range, got: {member_ip_str}"
);
// Case: List members and verify multicast_ip in all responses
let members_list_url = format!(
"{}?project={project_name}",
mcast_group_members_url(group_name)
);
let members: ResultsPage<MulticastGroupMember> =
object_get(client, &members_list_url).await;
assert_eq!(members.items.len(), 1, "Should have exactly one member");
assert_eq!(
members.items[0].multicast_ip, group.multicast_ip,
"Listed member should also include multicast_ip field"
);
// Case: Remove and re-add member (reactivation) - verify field preserved
let member_remove_url = format!(
"/v1/instances/{instance_name}/multicast-groups/{group_name}?project={project_name}"
);
NexusRequest::new(
RequestBuilder::new(client, http::Method::DELETE, &member_remove_url)
.expect_status(Some(StatusCode::NO_CONTENT)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("Should remove member");
wait_for_group_deleted(cptestctx, group_name).await;
// Re-create group by adding member again
let readded_member: MulticastGroupMember =
put_upsert(client, &join_url, &join_params).await;
wait_for_group_active(client, group_name).await;
let new_group: MulticastGroup =
object_get(client, &mcast_group_url(group_name)).await;
// Verify multicast_ip field is present in re-added member
assert_eq!(
readded_member.multicast_ip, new_group.multicast_ip,
"Re-added member should also have multicast_ip field"
);
NexusRequest::new(
RequestBuilder::new(client, http::Method::DELETE, &member_remove_url)
.expect_status(Some(StatusCode::NO_CONTENT)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("Should remove member for cleanup");
wait_for_group_deleted(cptestctx, group_name).await;
}
/// Test that we cannot delete a multicast IP pool when multicast groups are
/// linked to it (allocated IPs from it).
///
/// With implicit groups:
/// - Groups implicitly create when instances join
/// - Groups hold IPs from pools while they exist
/// - Pool should be protected while groups exist
/// - After groups are implicitly deleted (last member leaves), pool can be deleted
#[nexus_test]
async fn test_cannot_delete_multicast_pool_with_groups(
cptestctx: &ControlPlaneTestContext,
) {
let client = &cptestctx.external_client;
let project_name = "test-project";
let pool_name = "mcast-pool-delete-test";
let group_name = "mcast-group-blocks-delete";
let instance_name = "pool-test-instance";
// Create project and IP pools in parallel
ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
client,
pool_name,
(224, 10, 0, 1),
(224, 10, 0, 10),
),
)
.await;
let pool_url = format!("/v1/system/ip-pools/{pool_name}");
let range_url = format!("/v1/system/ip-pools/{pool_name}/ranges/remove");
let range = IpRange::V4(
Ipv4Range::new(
std::net::Ipv4Addr::new(224, 10, 0, 1),
std::net::Ipv4Addr::new(224, 10, 0, 10),
)
.unwrap(),
);
// Verify we can't delete the pool while it has ranges
let error: HttpErrorResponseBody =
object_delete_error(client, &pool_url, StatusCode::BAD_REQUEST).await;
assert_eq!(
error.message,
"IP Pool cannot be deleted while it contains IP ranges"
);
// Create instance and implicitly create group via instance-centric API
create_instance(client, project_name, instance_name).await;
multicast_group_attach(cptestctx, project_name, instance_name, group_name)
.await;
// Wait for group to become active
wait_for_group_active(client, group_name).await;
// Verify we can't delete the range while groups are allocated from it
let error: HttpErrorResponseBody = NexusRequest::new(
RequestBuilder::new(client, Method::POST, &range_url)
.body(Some(&range))
.expect_status(Some(StatusCode::BAD_REQUEST)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.unwrap()
.parsed_body()
.unwrap();
assert_eq!(
error.message,
"IP pool ranges cannot be deleted while multicast groups are allocated from them"
);
// Verify we still can't delete the pool (indirectly protected by ranges)
let error: HttpErrorResponseBody =
object_delete_error(client, &pool_url, StatusCode::BAD_REQUEST).await;
assert_eq!(
error.message,
"IP Pool cannot be deleted while it contains IP ranges"
);
// Verify we can't unlink the pool from the silo while groups are
// allocated from it.
let unlink_url = format!(
"/v1/system/ip-pools/{pool_name}/silos/{}",
DEFAULT_SILO.name().as_str()
);
object_delete_error(client, &unlink_url, StatusCode::BAD_REQUEST).await;
cleanup_instances(cptestctx, client, project_name, &[instance_name]).await;
wait_for_group_deleted(cptestctx, group_name).await;
// Now we should be able to delete the range
NexusRequest::new(
RequestBuilder::new(client, Method::POST, &range_url)
.body(Some(&range))
.expect_status(Some(StatusCode::NO_CONTENT)),
)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect(
"Should be able to delete range after groups are implicitly deleted",
);
// And we can unlink the pool from the silo
object_delete(client, &unlink_url).await;
// And now we should be able to delete the pool
NexusRequest::object_delete(client, &pool_url)
.authn_as(AuthnMode::PrivilegedUser)
.execute()
.await
.expect("Should be able to delete pool after ranges are deleted");
}
/// Consolidated test for SSM (Source-Specific Multicast) source IP behavior.
///
/// This test covers:
/// - Source IP union on group view (multiple members with different sources)
/// - IPv4/IPv6 source address family validation
/// - Multiple SSM groups from same pool with different sources
///
/// SSM groups (232.0.0.0/8 for IPv4, ff3x::/32 for IPv6) require source IPs
/// to be specified on join. The group's `source_ips` field shows the union
/// of all member sources.
#[nexus_test]
async fn test_ssm_source_ip_behavior(cptestctx: &ControlPlaneTestContext) {
let client = &cptestctx.external_client;
let project_name = "ssm-source-ip-test";
// Create project and IP pools in parallel
// SSM pool uses 232.x.x.x range with enough IPs for multiple groups
let (_, _, ssm_pool) = ops::join3(
create_project(&client, project_name),
create_default_ip_pools(&client),
create_multicast_ip_pool_with_range(
&client,
"ssm-test-pool",
(232, 1, 0, 1),
(232, 1, 0, 100),
),
)
.await;
// Also create IPv6 pool for address family validation tests
create_multicast_ip_pool_v6(&client, "ssm-test-pool-v6").await;
// Create instances for all test cases
let instance_names = [
"ssm-inst-1",
"ssm-inst-2",
"ssm-inst-3",
"ssm-inst-4",
"ssm-inst-5",
"ssm-inst-6",
];
for name in &instance_names {
create_instance(client, project_name, name).await;
}
// Case: Source IP union on group view
// Multiple members join with different sources, group view shows union
let ssm_union_ip = "232.1.0.10";
let source1 = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
let source2 = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 2));
let source3 = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 3));
// First instance joins with source1 - creates SSM group
let join_url1 = format!(
"/v1/instances/{}/multicast-groups/{ssm_union_ip}?project={project_name}",
instance_names[0]
);
let join_body1 = InstanceMulticastGroupJoin {
source_ips: Some(vec![source1]),
ip_version: None,
};
put_upsert::<_, MulticastGroupMember>(client, &join_url1, &join_body1)
.await;
// Verify group source_ips shows source1
let group: MulticastGroup =
object_get(client, &format!("/v1/multicast-groups/{ssm_union_ip}"))
.await;
assert_eq!(group.source_ips, vec![source1], "Group should show source1");
// Second instance joins with source1 and source2
let join_url2 = format!(
"/v1/instances/{}/multicast-groups/{ssm_union_ip}?project={project_name}",
instance_names[1]
);
let join_body2 = InstanceMulticastGroupJoin {
source_ips: Some(vec![source1, source2]),
ip_version: None,
};
put_upsert::<_, MulticastGroupMember>(client, &join_url2, &join_body2)
.await;
// Verify group source_ips is union of member sources (sorted for comparison)
let group: MulticastGroup =
object_get(client, &format!("/v1/multicast-groups/{ssm_union_ip}"))
.await;
let mut actual_sources = group.source_ips.clone();
actual_sources.sort();
let mut expected_sources = vec![source1, source2];
expected_sources.sort();
assert_eq!(
actual_sources, expected_sources,
"Group source_ips should be union of all member sources"
);
// Third instance joins with source3 - union should now include all three
let join_url3 = format!(
"/v1/instances/{}/multicast-groups/{ssm_union_ip}?project={project_name}",