1use anyhow::Result;
4use async_trait::async_trait;
5use std::hash::{Hash, Hasher};
6use std::sync::Arc;
7use uuid::Uuid;
8
9use crate::runtime::{
10 Endpoint, GearInstance, GearManager, GrpcServiceNameConflict, ReportedState as RtReportedState,
11};
12
13fn openapi_spec_hash(spec: &str) -> String {
30 let mut hasher = std::collections::hash_map::DefaultHasher::new();
31 spec.hash(&mut hasher);
32 format!("{:016x}", hasher.finish())
33}
34
35pub use cf_system_sdks::directory::{
37 DirectoryClient, DirectoryInvalidArgument, DirectoryNotFound, DirectoryPermissionDenied,
38 DirectoryServiceNameConflict, GrpcServiceInfo, InstanceState, LabelSelector, RegisterAck,
39 RegisterInstanceInfo, ReportedState, ServiceEndpoint, ServiceInstanceInfo,
40};
41
42fn domain_reported_to_runtime(reported: ReportedState) -> RtReportedState {
45 match reported {
46 ReportedState::Starting => RtReportedState::Starting,
47 ReportedState::Serving => RtReportedState::Serving,
48 ReportedState::Draining => RtReportedState::Draining,
49 }
50}
51
52fn runtime_state_to_domain(state: crate::runtime::InstanceState) -> InstanceState {
56 use crate::runtime::InstanceState as Rt;
57 match state {
58 Rt::Registered => InstanceState::Registered,
59 Rt::Ready => InstanceState::Ready,
60 Rt::Healthy => InstanceState::Healthy,
61 Rt::Quarantined => InstanceState::Quarantined,
62 Rt::Draining => InstanceState::Draining,
63 }
64}
65
66fn project_instance(gear: &str, inst: &GearInstance) -> ServiceInstanceInfo {
74 let endpoint = inst
75 .grpc_services
76 .values()
77 .next()
78 .or(inst.rest_endpoint.as_ref())
79 .map(|ep| ServiceEndpoint::new(ep.uri.clone()));
80
81 ServiceInstanceInfo::new(gear, inst.instance_id.to_string())
82 .with_endpoint(endpoint)
83 .with_version(inst.version.clone())
84 .with_rest_endpoint(
85 inst.rest_endpoint
86 .as_ref()
87 .map(|ep| ServiceEndpoint::new(ep.uri.clone())),
88 )
89 .with_openapi_spec_hash(inst.openapi_spec.as_deref().map(openapi_spec_hash))
90 .with_grpc_services(
94 inst.grpc_services
95 .iter()
96 .map(|(name, e)| (name.clone(), ServiceEndpoint::new(e.uri.clone())))
97 .collect(),
98 )
99 .with_labels(inst.labels.clone())
100 .with_state(runtime_state_to_domain(inst.state()))
101}
102
103pub struct LocalDirectoryClient {
108 mgr: Arc<GearManager>,
109}
110
111impl LocalDirectoryClient {
112 #[must_use]
113 pub fn new(mgr: Arc<GearManager>) -> Self {
114 Self { mgr }
115 }
116}
117
118#[async_trait]
119impl DirectoryClient for LocalDirectoryClient {
120 async fn resolve_grpc_service(&self, service_name: &str) -> Result<ServiceEndpoint> {
128 if let Some((_gear, _inst, ep)) = self.mgr.pick_service_round_robin(service_name) {
129 return Ok(ServiceEndpoint::new(ep.uri));
130 }
131
132 Err(DirectoryNotFound::new(format!("service {service_name}")).into())
133 }
134
135 async fn resolve_rest_service(&self, gear_name: &str) -> Result<ServiceEndpoint> {
136 if let Some(ep) = self.mgr.pick_rest_endpoint_round_robin(gear_name) {
137 return Ok(ServiceEndpoint::new(ep.uri));
138 }
139
140 Err(DirectoryNotFound::new(format!("gear {gear_name}")).into())
141 }
142
143 async fn get_openapi_spec(&self, gear_name: &str) -> Result<String> {
144 self.mgr.openapi_spec_of(gear_name).ok_or_else(|| {
145 DirectoryNotFound::new(format!("openapi spec for gear {gear_name}")).into()
146 })
147 }
148
149 async fn list_instances(&self, gear: &str) -> Result<Vec<ServiceInstanceInfo>> {
150 Ok(self
153 .mgr
154 .instances_of(gear)
155 .iter()
156 .map(|inst| project_instance(gear, inst))
157 .collect())
158 }
159
160 async fn resolve_by_labels(
161 &self,
162 gear: &str,
163 selector: &LabelSelector,
164 ) -> Result<Vec<ServiceInstanceInfo>> {
165 Ok(self
168 .mgr
169 .instances_of(gear)
170 .iter()
171 .map(|inst| project_instance(gear, inst))
172 .filter(|i| selector.matches(&i.labels))
173 .collect())
174 }
175
176 async fn list_all_instances(&self) -> Result<Vec<ServiceInstanceInfo>> {
177 Ok(self
181 .mgr
182 .all_instances()
183 .iter()
184 .filter_map(|inst| {
185 let info = project_instance(&inst.gear, inst);
186 if info.endpoint.is_none() {
187 tracing::debug!(
188 gear = %inst.gear,
189 instance_id = %inst.instance_id,
190 "skipping instance with no gRPC or REST endpoint from cross-gear snapshot"
191 );
192 return None;
193 }
194 Some(info.without_labels())
195 })
196 .collect())
197 }
198
199 async fn register_instance(&self, info: RegisterInstanceInfo) -> Result<()> {
200 let instance_id = Uuid::parse_str(&info.instance_id).map_err(|e| {
202 anyhow::Error::from(DirectoryInvalidArgument::new(format!(
207 "invalid instance_id '{}': {e}",
208 info.instance_id
209 )))
210 })?;
211
212 cf_system_sdks::directory::validate_labels(&info.labels)
219 .map_err(|e| anyhow::Error::from(DirectoryInvalidArgument::new(e.to_string())))?;
220
221 let mut instance = GearInstance::new(info.gear.clone(), instance_id);
223
224 if let Some(version) = info.version {
226 instance = instance.with_version(version);
227 }
228
229 for (service_name, endpoint) in info.grpc_services {
231 instance = instance.with_grpc_service(service_name, Endpoint::from_uri(endpoint.uri));
232 }
233
234 if let Some(rest) = info.rest_endpoint {
236 instance = instance.with_rest_endpoint(Endpoint::from_uri(rest.uri));
237 }
238
239 if let Some(spec) = info.openapi_spec {
241 instance = instance.with_openapi_spec(spec);
242 }
243
244 if !info.labels.is_empty() {
246 instance = instance.with_labels(info.labels);
247 }
248
249 if let Some(reported) = info.reported_state {
252 instance = instance.with_reported_state(domain_reported_to_runtime(reported));
253 }
254
255 self.mgr.register_instance(Arc::new(instance)).map_err(
261 |GrpcServiceNameConflict {
262 service_name,
263 owner,
264 recoverable,
265 }| {
266 anyhow::Error::from(DirectoryServiceNameConflict {
267 service_name,
268 owner,
269 recoverable,
270 })
271 },
272 )?;
273
274 Ok(())
275 }
276
277 async fn register_with_lease(&self, info: RegisterInstanceInfo) -> Result<RegisterAck> {
278 self.register_instance(info).await?;
279 Ok(RegisterAck {
283 lease_ttl: Some(self.mgr.lease_ttl()),
284 })
285 }
286
287 async fn deregister_instance(&self, gear: &str, instance_id: &str) -> Result<()> {
288 let instance_id = Uuid::parse_str(instance_id)
289 .map_err(|e| anyhow::anyhow!("Invalid instance_id '{instance_id}': {e}"))?;
290 self.mgr.deregister(gear, instance_id);
291 Ok(())
292 }
293
294 async fn send_heartbeat(&self, gear: &str, instance_id: &str) -> Result<()> {
295 self.heartbeat(gear, instance_id, None).await
296 }
297
298 async fn heartbeat(
299 &self,
300 gear: &str,
301 instance_id: &str,
302 reported: Option<ReportedState>,
303 ) -> Result<()> {
304 let id = Uuid::parse_str(instance_id)
305 .map_err(|e| anyhow::anyhow!("Invalid instance_id '{instance_id}': {e}"))?;
306 let reported = reported.map(domain_reported_to_runtime);
312 if self.mgr.heartbeat(gear, id, reported) {
313 Ok(())
314 } else {
315 Err(DirectoryNotFound::new(format!("instance {gear}/{instance_id}")).into())
316 }
317 }
318}
319
320#[cfg(test)]
321#[cfg_attr(coverage_nightly, coverage(off))]
322mod tests {
323 use super::*;
324
325 #[tokio::test]
326 async fn test_resolve_grpc_service_not_found() {
327 let dir = Arc::new(GearManager::new());
328 let api = LocalDirectoryClient::new(dir);
329
330 let err = api
331 .resolve_grpc_service("nonexistent.Service")
332 .await
333 .unwrap_err();
334 assert!(
338 err.downcast_ref::<DirectoryNotFound>().is_some(),
339 "expected the typed not-found sentinel, got: {err:?}"
340 );
341 }
342
343 #[tokio::test]
344 async fn test_register_instance_via_api() {
345 let dir = Arc::new(GearManager::new());
346 let api = LocalDirectoryClient::new(dir.clone());
347
348 let instance_id = Uuid::new_v4();
349 let register_info = RegisterInstanceInfo::new("test_gear", instance_id.to_string())
351 .with_grpc_services(vec![(
352 "test.Service".to_owned(),
353 ServiceEndpoint::http("127.0.0.1", 8001),
354 )])
355 .with_version("1.0.0");
356
357 api.register_instance(register_info).await.unwrap();
358
359 let instances = dir.instances_of("test_gear");
361 assert_eq!(instances.len(), 1);
362 assert_eq!(instances[0].instance_id, instance_id);
363 assert_eq!(instances[0].version, Some("1.0.0".to_owned()));
364 assert!(instances[0].grpc_services.contains_key("test.Service"));
365 }
366
367 #[tokio::test]
371 async fn presence_and_hub_registrations_keep_each_others_half() {
372 for hub_first in [true, false] {
373 let api = LocalDirectoryClient::new(Arc::new(GearManager::new()));
375 let id = Uuid::new_v4().to_string();
376 let presence = RegisterInstanceInfo::new("worker", &id)
378 .with_rest_endpoint(ServiceEndpoint::http("127.0.0.1", 8080))
379 .with_openapi_spec(r#"{"openapi":"3.1.0"}"#);
380 let hub = RegisterInstanceInfo::new("worker", &id)
382 .with_grpc_services(vec![(
383 "worker.Svc".to_owned(),
384 ServiceEndpoint::http("127.0.0.1", 50051),
385 )])
386 .with_version("1.0.0");
387 let order = if hub_first {
388 [&hub, &presence, &presence, &hub]
389 } else {
390 [&presence, &hub, &hub, &presence]
391 };
392 for info in order {
393 api.register_instance(info.clone()).await.unwrap();
394 }
395 let instances = api.list_instances("worker").await.unwrap();
396 assert_eq!(instances.len(), 1);
397 assert_eq!(instances[0].grpc_services.len(), 1, "gRPC services kept");
398 assert!(instances[0].rest_endpoint.is_some(), "REST endpoint kept");
399 assert_eq!(
400 api.get_openapi_spec("worker").await.unwrap(),
401 r#"{"openapi":"3.1.0"}"#,
402 "OpenAPI spec kept"
403 );
404 }
405 }
406
407 #[tokio::test]
408 async fn register_rejects_invalid_labels_at_store_boundary() {
409 let dir = Arc::new(GearManager::new());
413 let api = LocalDirectoryClient::new(Arc::clone(&dir));
414
415 let err = api
416 .register_instance(
417 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
418 .with_rest_endpoint(ServiceEndpoint::new("http://worker:8080"))
419 .with_labels(labels(&[("bad key", "7")])),
420 )
421 .await
422 .expect_err("an invalid label must be rejected at the store boundary");
423 assert!(
424 err.downcast_ref::<DirectoryInvalidArgument>().is_some(),
425 "store-boundary label rejection must be typed InvalidArgument, got: {err}"
426 );
427 assert!(
428 dir.instances_of("worker").is_empty(),
429 "a rejected registration must not be stored"
430 );
431 }
432
433 #[tokio::test]
434 async fn test_register_and_resolve_rest_and_openapi() {
435 let dir = Arc::new(GearManager::new());
436 let api = LocalDirectoryClient::new(dir.clone());
437
438 let instance_id = Uuid::new_v4();
439 let register_info = RegisterInstanceInfo::new("billing", instance_id.to_string())
440 .with_version("1.0.0")
441 .with_rest_endpoint(ServiceEndpoint::http("billing", 8080))
442 .with_openapi_spec("{\"openapi\":\"3.1.0\"}");
443
444 api.register_instance(register_info).await.unwrap();
445
446 let resolved = api.resolve_rest_service("billing").await.unwrap();
448 assert_eq!(resolved.uri, concat!("http", "://billing:8080"));
449
450 let spec = api.get_openapi_spec("billing").await.unwrap();
452 assert!(spec.contains("openapi"));
453 }
454
455 #[tokio::test]
456 async fn test_resolve_rest_and_openapi_not_found() {
457 let dir = Arc::new(GearManager::new());
458 let api = LocalDirectoryClient::new(dir);
459
460 let rest_err = api.resolve_rest_service("missing").await.unwrap_err();
463 assert!(
464 rest_err.downcast_ref::<DirectoryNotFound>().is_some(),
465 "expected the typed not-found sentinel, got: {rest_err:?}"
466 );
467
468 let spec_err = api.get_openapi_spec("missing").await.unwrap_err();
469 assert!(
470 spec_err.downcast_ref::<DirectoryNotFound>().is_some(),
471 "expected the typed not-found sentinel, got: {spec_err:?}"
472 );
473 }
474
475 #[tokio::test]
476 async fn test_deregister_instance_via_api() {
477 let dir = Arc::new(GearManager::new());
478 let api = LocalDirectoryClient::new(dir.clone());
479
480 let instance_id = Uuid::new_v4();
481 let inst = Arc::new(GearInstance::new("test_gear", instance_id));
483 dir.register_instance(inst).unwrap();
484
485 assert_eq!(dir.instances_of("test_gear").len(), 1);
487
488 api.deregister_instance("test_gear", &instance_id.to_string())
490 .await
491 .unwrap();
492
493 assert_eq!(dir.instances_of("test_gear").len(), 0);
495 }
496
497 #[tokio::test]
498 async fn test_send_heartbeat_via_api() {
499 use crate::runtime::InstanceState;
500
501 let dir = Arc::new(GearManager::new());
502 let api = LocalDirectoryClient::new(dir.clone());
503
504 let instance_id = Uuid::new_v4();
505 let inst = Arc::new(GearInstance::new("test_gear", instance_id));
507 dir.register_instance(inst).unwrap();
508
509 let instances = dir.instances_of("test_gear");
511 assert_eq!(instances[0].state(), InstanceState::Registered);
512
513 api.send_heartbeat("test_gear", &instance_id.to_string())
515 .await
516 .unwrap();
517
518 let instances = dir.instances_of("test_gear");
520 assert_eq!(instances[0].state(), InstanceState::Healthy);
521 }
522
523 #[tokio::test]
524 async fn test_list_all_instances_across_gears() {
525 let dir = Arc::new(GearManager::new());
526 let api = LocalDirectoryClient::new(Arc::clone(&dir));
527
528 for (gear, port) in [("billing", 8080u16), ("catalog", 8081u16)] {
530 api.register_instance(
531 RegisterInstanceInfo::new(gear, Uuid::new_v4().to_string())
532 .with_version("1.0.0")
533 .with_rest_endpoint(ServiceEndpoint::http(gear, port))
534 .with_openapi_spec(format!("{{\"openapi\":\"3.1.0\",\"x\":\"{gear}\"}}")),
535 )
536 .await
537 .unwrap();
538 }
539
540 api.register_instance(
544 RegisterInstanceInfo::new("reporting", Uuid::new_v4().to_string())
545 .with_grpc_services(vec![(
546 "reporting.Service".to_owned(),
547 ServiceEndpoint::new("http://reporting:7000"),
548 )])
549 .with_version("1.0.0"),
550 )
551 .await
552 .unwrap();
553
554 let all = api.list_all_instances().await.unwrap();
555 assert_eq!(all.len(), 3);
556
557 let billing = all.iter().find(|i| i.gear == "billing").expect("billing");
558 assert_eq!(
559 billing.rest_endpoint.as_ref().map(|e| e.uri.as_str()),
560 Some("http://billing:8080")
561 );
562 assert!(billing.openapi_spec_hash.is_some());
566 assert!(
567 api.get_openapi_spec("billing")
568 .await
569 .expect("billing spec")
570 .contains("billing")
571 );
572
573 let reporting = all
576 .iter()
577 .find(|i| i.gear == "reporting")
578 .expect("reporting");
579 assert_eq!(
580 reporting.endpoint.as_ref().map(|e| e.uri.as_str()),
581 Some("http://reporting:7000")
582 );
583 assert!(reporting.rest_endpoint.is_none());
584 assert!(reporting.openapi_spec_hash.is_none());
585 }
586
587 fn labels(pairs: &[(&str, &str)]) -> std::collections::BTreeMap<String, String> {
588 pairs
589 .iter()
590 .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
591 .collect()
592 }
593
594 #[tokio::test]
595 async fn register_carries_labels_into_list_instances() {
596 let dir = Arc::new(GearManager::new());
597 let api = LocalDirectoryClient::new(Arc::clone(&dir));
598
599 api.register_instance(
600 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
601 .with_grpc_services(vec![(
602 "worker.Svc".to_owned(),
603 ServiceEndpoint::new("http://worker:7000"),
604 )])
605 .with_labels(labels(&[("shard", "7")])),
606 )
607 .await
608 .unwrap();
609
610 let listed = api.list_instances("worker").await.unwrap();
611 assert_eq!(listed.len(), 1);
612 assert_eq!(listed[0].labels.get("shard"), Some(&"7".to_owned()));
613 }
614
615 #[tokio::test]
616 async fn resolve_by_labels_selects_matching_instances() {
617 let dir = Arc::new(GearManager::new());
618 let api = LocalDirectoryClient::new(Arc::clone(&dir));
619
620 for (id, shard) in [("a", "7"), ("b", "8"), ("c", "7")] {
621 api.register_instance(
622 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
623 .with_grpc_services(vec![(
624 format!("worker.{id}"),
625 ServiceEndpoint::new(format!("http://worker-{id}:7000")),
626 )])
627 .with_labels(labels(&[("shard", shard)])),
628 )
629 .await
630 .unwrap();
631 }
632
633 let matched = api
634 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
635 .await
636 .unwrap();
637 assert_eq!(matched.len(), 2, "two instances carry shard=7");
638 assert!(
639 matched
640 .iter()
641 .all(|i| i.labels.get("shard") == Some(&"7".to_owned()))
642 );
643 }
644
645 #[tokio::test]
646 async fn labelless_reregister_preserves_labels_through_api() {
647 let dir = Arc::new(GearManager::new());
648 let api = LocalDirectoryClient::new(Arc::clone(&dir));
649
650 let instance_id = Uuid::new_v4().to_string();
651 api.register_instance(
652 RegisterInstanceInfo::new("worker", instance_id.clone())
653 .with_rest_endpoint(ServiceEndpoint::new("http://worker:8080"))
654 .with_labels(labels(&[("shard", "7")])),
655 )
656 .await
657 .unwrap();
658
659 api.register_instance(
661 RegisterInstanceInfo::new("worker", instance_id)
662 .with_rest_endpoint(ServiceEndpoint::new("http://worker:8080"))
663 .with_version("2.0.0"),
664 )
665 .await
666 .unwrap();
667
668 let listed = api.list_instances("worker").await.unwrap();
669 assert_eq!(listed.len(), 1);
670 assert_eq!(
671 listed[0].labels.get("shard"),
672 Some(&"7".to_owned()),
673 "label-less re-registration must not wipe stored labels"
674 );
675
676 let matched = api
678 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
679 .await
680 .unwrap();
681 assert_eq!(matched.len(), 1);
682 }
683
684 #[tokio::test]
685 async fn resolve_by_labels_carries_live_serving_state() {
686 let dir = Arc::new(GearManager::new());
687 let api = LocalDirectoryClient::new(Arc::clone(&dir));
688
689 let instance_id = Uuid::new_v4();
690 api.register_instance(
691 RegisterInstanceInfo::new("worker", instance_id.to_string())
692 .with_grpc_services(vec![(
693 "worker.Svc".to_owned(),
694 ServiceEndpoint::new("http://worker:7000"),
695 )])
696 .with_labels(labels(&[("shard", "7")])),
697 )
698 .await
699 .unwrap();
700
701 let matched = api
703 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
704 .await
705 .unwrap();
706 assert_eq!(matched.len(), 1);
707 assert_eq!(matched[0].state, InstanceState::Registered);
708 assert!(!matched[0].state.is_serving());
709
710 assert!(dir.heartbeat("worker", instance_id, None));
713 let matched = api
714 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
715 .await
716 .unwrap();
717 assert_eq!(matched[0].state, InstanceState::Healthy);
718 assert!(matched[0].state.is_serving());
719 }
720
721 #[tokio::test]
722 async fn resolve_by_labels_omits_openapi_spec() {
723 let dir = Arc::new(GearManager::new());
724 let api = LocalDirectoryClient::new(Arc::clone(&dir));
725
726 api.register_instance(
727 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
728 .with_rest_endpoint(ServiceEndpoint::new("http://worker:8080"))
729 .with_openapi_spec("{\"openapi\":\"3.1.0\"}")
730 .with_labels(labels(&[("shard", "7")])),
731 )
732 .await
733 .unwrap();
734
735 let matched = api
737 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
738 .await
739 .unwrap();
740 assert_eq!(matched.len(), 1);
741 assert!(
742 matched[0].openapi_spec_hash.is_some(),
743 "in-process resolve_by_labels carries only the spec hash, never the document"
744 );
745
746 let listed = api.list_instances("worker").await.unwrap();
749 assert!(
750 listed[0].openapi_spec_hash.is_some(),
751 "list_instances must carry the spec hash, never the document"
752 );
753 }
754
755 #[tokio::test]
756 async fn list_all_instances_skips_endpoint_less() {
757 let dir = Arc::new(GearManager::new());
758 let api = LocalDirectoryClient::new(Arc::clone(&dir));
759
760 api.register_instance(
763 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
764 .with_rest_endpoint(ServiceEndpoint::new("http://worker:8080")),
765 )
766 .await
767 .unwrap();
768 api.register_instance(RegisterInstanceInfo::new(
769 "placeholder",
770 Uuid::new_v4().to_string(),
771 ))
772 .await
773 .unwrap();
774
775 let all = api.list_all_instances().await.unwrap();
776 assert_eq!(all.len(), 1, "the endpoint-less instance is skipped");
777 assert_eq!(all[0].gear, "worker");
778 assert!(
779 all.iter()
780 .all(|i| i.endpoint.as_ref().is_some_and(|e| !e.uri.is_empty())),
781 "no instance may carry an absent or empty-URI endpoint"
782 );
783 }
784
785 #[tokio::test]
786 async fn label_path_returns_endpoint_less_match() {
787 let dir = Arc::new(GearManager::new());
788 let api = LocalDirectoryClient::new(Arc::clone(&dir));
789
790 api.register_instance(
795 RegisterInstanceInfo::new("worker", Uuid::new_v4().to_string())
796 .with_labels(labels(&[("shard", "7")])),
797 )
798 .await
799 .unwrap();
800
801 let matched = api
802 .resolve_by_labels("worker", &LabelSelector::new().with("shard", "7"))
803 .await
804 .unwrap();
805 assert_eq!(
806 matched.len(),
807 1,
808 "resolve_by_labels must return a matched instance even with no endpoint"
809 );
810 assert!(
811 matched[0].endpoint.is_none(),
812 "a no-endpoint match carries endpoint = None, not an empty-URI sentinel"
813 );
814 assert!(matched[0].rest_endpoint.is_none());
815 assert_eq!(matched[0].labels.get("shard"), Some(&"7".to_owned()));
816
817 let listed = api.list_instances("worker").await.unwrap();
819 assert_eq!(
820 listed.len(),
821 1,
822 "list_instances must not drop endpoint-less instances"
823 );
824 assert!(listed[0].endpoint.is_none());
825 }
826}