1use crate::{GpuSpec, MachineProfile, Platform};
13use serde::{Deserialize, Serialize};
14
15pub fn parse_cpu(s: &str) -> Result<f64, String> {
23 let s = s.trim();
24 if s.is_empty() {
25 return Err("empty CPU string".to_string());
26 }
27
28 if let Some(millis) = s.strip_suffix('m') {
29 let v: f64 = millis
30 .parse()
31 .map_err(|_| format!("invalid CPU millicore value: '{s}'"))?;
32 Ok(v / 1000.0)
33 } else {
34 s.parse().map_err(|_| format!("invalid CPU value: '{s}'"))
35 }
36}
37
38pub fn parse_memory_bytes(s: &str) -> Result<u64, String> {
43 let s = s.trim();
44 if s.is_empty() {
45 return Err("empty memory/storage string".to_string());
46 }
47
48 for (suffix, multiplier) in [
49 ("Ti", 1024_f64.powi(4)),
50 ("Gi", 1024_f64.powi(3)),
51 ("Mi", 1024_f64.powi(2)),
52 ("Ki", 1024_f64),
53 ("T", 1_000_000_000_000_f64),
54 ("G", 1_000_000_000_f64),
55 ("M", 1_000_000_f64),
56 ("k", 1_000_f64),
57 ] {
58 if let Some(number) = s.strip_suffix(suffix) {
59 let value: f64 = number
60 .parse()
61 .map_err(|_| format!("invalid memory value: '{s}'"))?;
62 let bytes = value * multiplier;
63 if !bytes.is_finite() || bytes < 0.0 || bytes >= u64::MAX as f64 {
66 return Err(format!("memory value is out of range: '{s}'"));
67 }
68 return Ok(bytes as u64);
69 }
70 }
71 s.parse()
72 .map_err(|_| format!("invalid memory value: '{s}'"))
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
81#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
82#[serde(rename_all = "camelCase")]
83pub enum InstanceFamily {
84 Burstable,
85 GeneralPurpose,
86 ComputeOptimized,
87 MemoryOptimized,
88 StorageOptimized,
89 GpuCompute,
90}
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
95#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
96#[serde(rename_all = "snake_case")]
97pub enum Architecture {
98 Arm64,
99 X86_64,
100}
101
102pub fn default_architecture(platform: Platform) -> Option<Architecture> {
104 match platform {
105 Platform::Aws => Some(Architecture::Arm64),
106 Platform::Gcp | Platform::Azure => Some(Architecture::X86_64),
107 Platform::Kubernetes | Platform::Machines | Platform::Local | Platform::Test => None,
108 }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub struct CatalogGpu {
114 pub gpu_type: &'static str,
115 pub count: u32,
116}
117
118#[derive(Debug, Clone)]
122pub struct InstanceTypeSpec {
123 pub name: &'static str,
124 pub platform: Platform,
125 pub family: InstanceFamily,
127 pub series: &'static str,
129 pub label: &'static str,
131 pub architecture: Architecture,
132 pub vcpu: u32,
134 pub memory_bytes: u64,
136 pub ephemeral_storage_bytes: u64,
138 pub local_disk_bytes: u64,
140 pub machine_type: &'static str,
143 pub gpu: Option<CatalogGpu>,
145 pub nested_virt: bool,
148 pub bare_metal: bool,
150 pub auto_select: bool,
154}
155
156impl InstanceTypeSpec {
157 pub fn has_configurable_ephemeral_storage(&self) -> bool {
160 matches!(
161 self.family,
162 InstanceFamily::Burstable
163 | InstanceFamily::GeneralPurpose
164 | InstanceFamily::ComputeOptimized
165 | InstanceFamily::MemoryOptimized
166 )
167 }
168
169 pub fn is_nested_virt_capable(&self) -> bool {
174 self.nested_virt
175 }
176
177 pub fn to_machine_profile(&self) -> MachineProfile {
179 MachineProfile {
180 cpu: format!("{}.0", self.vcpu),
181 memory_bytes: self.memory_bytes,
182 ephemeral_storage_bytes: self.ephemeral_storage_bytes,
183 architecture: Some(self.architecture),
184 gpu: self.gpu.map(|g| GpuSpec {
185 gpu_type: g.gpu_type.to_string(),
186 count: g.count,
187 }),
188 data_disk: None,
189 }
190 }
191
192 pub fn to_machine_profile_for_storage(&self, requested_bytes: u64) -> MachineProfile {
196 let mut profile = self.to_machine_profile();
197 if self.has_configurable_ephemeral_storage() {
198 profile.ephemeral_storage_bytes = profile.ephemeral_storage_bytes.max(requested_bytes);
199 }
200 profile
201 }
202}
203
204const KI: u64 = 1024;
206const MI: u64 = KI * 1024;
207const GI: u64 = MI * 1024;
208const TI: u64 = GI * 1024;
209
210pub fn max_configurable_ephemeral_storage_bytes(platform: Platform) -> Option<u64> {
216 let disk_limit_gib = match platform {
217 Platform::Aws | Platform::Gcp => 64 * 1024,
219 Platform::Azure => 4_095,
221 Platform::Kubernetes | Platform::Machines | Platform::Local | Platform::Test => {
222 return None;
223 }
224 };
225
226 let max_requested_gib = (disk_limit_gib - 12) * 4 / 5;
231 Some(max_requested_gib * GI)
232}
233
234mod catalog;
237use catalog::CATALOG;
238
239pub fn catalog_for_platform(platform: Platform) -> Vec<&'static InstanceTypeSpec> {
245 CATALOG
246 .iter()
247 .filter(|spec| spec.platform == platform)
248 .collect()
249}
250
251pub fn find_instance_type(platform: Platform, name: &str) -> Option<&'static InstanceTypeSpec> {
253 CATALOG
254 .iter()
255 .find(|spec| spec.platform == platform && spec.name == name)
256}
257
258pub fn is_same_architecture_aws_machine(old: &str, new: &str) -> bool {
261 is_same_architecture_machine(Platform::Aws, old, new)
262}
263
264pub fn is_same_architecture_machine(platform: Platform, old: &str, new: &str) -> bool {
266 matches!(platform, Platform::Aws | Platform::Gcp | Platform::Azure)
267 && find_instance_type(platform, old)
268 .zip(find_instance_type(platform, new))
269 .is_some_and(|(old, new)| old.architecture == new.architecture)
270}
271
272#[derive(Debug, Clone)]
278pub struct WorkloadRequirements {
279 pub total_cpu_at_desired: f64,
281 pub total_memory_bytes_at_desired: u64,
283 pub total_cpu_at_max: f64,
285 pub total_memory_bytes_at_max: u64,
287 pub max_cpu_per_container: f64,
289 pub max_memory_per_container: u64,
291 pub max_ephemeral_storage_bytes: u64,
293 pub gpu: Option<GpuSpec>,
295 pub architecture: Option<Architecture>,
297 pub nested_virt: bool,
301}
302
303#[derive(Debug, Clone)]
305pub struct InstanceSelection {
306 pub instance_type: &'static str,
308 pub profile: MachineProfile,
310 pub min_machines: u32,
312 pub max_machines: u32,
314}
315
316const STORAGE_OPTIMIZED_THRESHOLD: u64 = 200 * GI;
318
319const MAX_MACHINES_PER_CLUSTER: u32 = 10;
321
322const MAX_STANDARD_VCPU: u32 = 8;
325
326const SYSTEM_RESERVE_CPU: f64 = 0.5;
328
329const WORKLOAD_HEADROOM_FACTOR: f64 = 1.15;
331
332pub fn select_instance_type(
343 platform: Platform,
344 requirements: &WorkloadRequirements,
345) -> Result<InstanceSelection, String> {
346 select_instance_type_where(platform, requirements, &|_| true)
347}
348
349type MachineFilter<'a> = &'a dyn Fn(&InstanceTypeSpec) -> bool;
351
352pub fn select_instance_type_with_room(
360 platform: Platform,
361 requirements: &WorkloadRequirements,
362 machines: u32,
363 has_room: &dyn Fn(&InstanceTypeSpec, u32) -> bool,
364) -> Result<InstanceSelection, String> {
365 let mut without_room: Vec<&'static str> = Vec::new();
366 loop {
367 let remaining = |spec: &InstanceTypeSpec| !without_room.contains(&spec.series);
368 let Ok(selection) = select_instance_type_where(platform, requirements, &remaining) else {
369 break;
370 };
371 let Some(spec) = find_instance_type(platform, selection.instance_type) else {
372 break;
373 };
374 if has_room(spec, machines) {
375 return Ok(selection);
376 }
377 without_room.push(spec.series);
378 }
379 select_instance_type(platform, requirements)
380}
381
382pub fn auto_select_series(platform: Platform) -> Vec<&'static str> {
384 let mut series: Vec<&'static str> = Vec::new();
385 for spec in CATALOG
386 .iter()
387 .filter(|spec| spec.auto_select && spec.platform == platform)
388 {
389 if !series.contains(&spec.series) {
390 series.push(spec.series);
391 }
392 }
393 series
394}
395
396fn select_instance_type_where(
397 platform: Platform,
398 requirements: &WorkloadRequirements,
399 allowed: MachineFilter,
400) -> Result<InstanceSelection, String> {
401 if requirements.architecture.is_none()
406 && (requirements.nested_virt || requirements.gpu.is_some())
407 {
408 if let Some(default) = default_architecture(platform) {
409 let other = match default {
410 Architecture::Arm64 => Architecture::X86_64,
411 Architecture::X86_64 => Architecture::Arm64,
412 };
413 let with_architecture = |architecture| WorkloadRequirements {
414 architecture: Some(architecture),
415 ..requirements.clone()
416 };
417 let selection = select_instance_type_for_architecture(
418 platform,
419 &with_architecture(default),
420 allowed,
421 );
422 if selection.is_ok() {
423 return selection;
424 }
425 return select_instance_type_for_architecture(
428 platform,
429 &with_architecture(other),
430 allowed,
431 )
432 .map_err(|other_error| {
433 let default_error = selection.err().unwrap_or_default();
434 format!("{default:?}: {default_error}; {other:?}: {other_error}")
435 });
436 }
437 }
438 select_instance_type_for_architecture(platform, requirements, allowed)
439}
440
441fn select_instance_type_for_architecture(
442 platform: Platform,
443 requirements: &WorkloadRequirements,
444 allowed: MachineFilter,
445) -> Result<InstanceSelection, String> {
446 let family = select_family(requirements);
447 let family = if requirements.nested_virt && family == InstanceFamily::Burstable {
448 InstanceFamily::GeneralPurpose
449 } else {
450 family
451 };
452 let selection = select_instance_type_in_family(platform, requirements, family, allowed);
453 if selection.is_err() && family == InstanceFamily::Burstable {
454 return select_instance_type_in_family(
455 platform,
456 requirements,
457 InstanceFamily::GeneralPurpose,
458 allowed,
459 );
460 }
461 selection
462}
463
464fn select_instance_type_in_family(
465 platform: Platform,
466 requirements: &WorkloadRequirements,
467 family: InstanceFamily,
468 allowed: MachineFilter,
469) -> Result<InstanceSelection, String> {
470 let architecture = requirements
471 .architecture
472 .or_else(|| default_architecture(platform))
473 .ok_or_else(|| format!("platform {platform} has no default compute architecture"))?;
474
475 let candidates: Vec<&InstanceTypeSpec> = CATALOG
476 .iter()
477 .filter(|spec| spec.auto_select && spec.platform == platform && spec.family == family)
478 .filter(|spec| allowed(spec))
479 .filter(|spec| {
480 if requirements.nested_virt {
481 spec.is_nested_virt_capable()
482 } else {
483 platform != Platform::Aws || !spec.is_nested_virt_capable()
484 }
485 })
486 .filter(|spec| spec.architecture == architecture)
487 .collect();
488
489 if candidates.is_empty() && family != InstanceFamily::StorageOptimized {
494 let family_has_other_architecture = CATALOG.iter().any(|spec| {
495 spec.auto_select
496 && spec.platform == platform
497 && spec.family == family
498 && if requirements.nested_virt {
499 spec.is_nested_virt_capable()
500 } else {
501 platform != Platform::Aws || !spec.is_nested_virt_capable()
502 }
503 });
504 if family_has_other_architecture {
505 return Err(format!(
506 "architecture {architecture:?} is unavailable for this workload on platform {platform}"
507 ));
508 }
509 return Err(if requirements.nested_virt {
510 format!(
511 "no nested-virt-capable {family:?} instance types in catalog for platform {platform}"
512 )
513 } else {
514 format!("no {family:?} instance types in catalog for platform {platform}")
515 });
516 }
517
518 let candidates = if let Some(ref gpu) = requirements.gpu {
520 let filtered: Vec<&InstanceTypeSpec> = candidates
521 .into_iter()
522 .filter(|spec| {
523 spec.gpu.as_ref().map_or(false, |g| {
524 g.gpu_type == gpu.gpu_type && g.count >= gpu.count
525 })
526 })
527 .collect();
528 if filtered.is_empty() {
529 return Err(format!(
530 "no instance type for GPU type '{}' x{} on platform {platform}",
531 gpu.gpu_type, gpu.count
532 ));
533 }
534 filtered
535 } else {
536 candidates
537 };
538
539 let candidates = if family == InstanceFamily::StorageOptimized {
541 let filtered: Vec<&InstanceTypeSpec> = candidates
542 .into_iter()
543 .filter(|spec| spec.ephemeral_storage_bytes >= requirements.max_ephemeral_storage_bytes)
544 .collect();
545 if filtered.is_empty() {
546 CATALOG
550 .iter()
551 .filter(|spec| {
552 spec.auto_select
553 && spec.platform == platform
554 && spec.family == InstanceFamily::GeneralPurpose
555 && allowed(spec)
556 && spec.has_configurable_ephemeral_storage()
557 && max_configurable_ephemeral_storage_bytes(platform)
558 .is_some_and(|max| requirements.max_ephemeral_storage_bytes <= max)
559 })
560 .filter(|spec| {
561 if requirements.nested_virt {
562 spec.is_nested_virt_capable()
563 } else {
564 platform != Platform::Aws || !spec.is_nested_virt_capable()
565 }
566 })
567 .filter(|spec| spec.architecture == architecture)
568 .collect()
569 } else {
570 filtered
571 }
572 } else {
573 candidates
574 };
575
576 if candidates.is_empty() {
577 return Err(format!(
578 "architecture {architecture:?} is unavailable for this workload on platform {platform}"
579 ));
580 }
581
582 let candidates: Vec<_> = candidates
585 .into_iter()
586 .filter(|spec| {
587 allocatable_cpu(spec) >= requirements.max_cpu_per_container * WORKLOAD_HEADROOM_FACTOR
588 && allocatable_memory_bytes(spec) as f64
589 >= requirements.max_memory_per_container as f64 * WORKLOAD_HEADROOM_FACTOR
590 })
591 .collect();
592 let smallest_replica_machine = candidates.iter().map(|spec| spec.vcpu).min()
593 .ok_or_else(|| format!("no {platform} machine can fit one replica with system reserve and workload headroom"))?;
594
595 let effective_family = candidates[0].family;
600 let vcpu_cap = if effective_family == InstanceFamily::GpuCompute
601 || effective_family == InstanceFamily::StorageOptimized
602 {
603 u32::MAX
604 } else {
605 MAX_STANDARD_VCPU.max(smallest_replica_machine)
606 };
607
608 let desired_target_machines = desired_target_machines(requirements);
609 let target_cpu = requirements
610 .max_cpu_per_container
611 .max(requirements.total_cpu_at_desired / desired_target_machines as f64)
612 * WORKLOAD_HEADROOM_FACTOR;
613 let target_memory = (requirements.max_memory_per_container as f64)
614 .max(requirements.total_memory_bytes_at_desired as f64 / desired_target_machines as f64)
615 * WORKLOAD_HEADROOM_FACTOR;
616
617 let selected = candidates
622 .iter()
623 .filter(|spec| {
624 spec.vcpu <= vcpu_cap
625 && allocatable_cpu(spec) >= target_cpu
626 && allocatable_memory_bytes(spec) as f64 >= target_memory
627 })
628 .min_by_key(|spec| spec.vcpu)
629 .or_else(|| {
630 candidates
633 .iter()
634 .rev()
635 .filter(|spec| spec.vcpu <= vcpu_cap)
636 .max_by_key(|spec| spec.vcpu)
637 })
638 .or_else(|| {
639 candidates.iter().min_by_key(|spec| spec.vcpu)
641 })
642 .ok_or_else(|| format!("no instance types available for platform {platform}"))?;
643
644 let max_machines = compute_max_machines(requirements, selected);
646 let min_machines = compute_min_machines(requirements, selected, max_machines);
647
648 Ok(InstanceSelection {
649 instance_type: selected.name,
650 profile: selected.to_machine_profile_for_storage(requirements.max_ephemeral_storage_bytes),
651 min_machines,
652 max_machines,
653 })
654}
655
656pub fn select_family(requirements: &WorkloadRequirements) -> InstanceFamily {
662 if requirements.gpu.is_some() {
664 return InstanceFamily::GpuCompute;
665 }
666
667 if requirements.max_ephemeral_storage_bytes > STORAGE_OPTIMIZED_THRESHOLD {
669 return InstanceFamily::StorageOptimized;
670 }
671
672 if requirements.total_cpu_at_max < 2.0 {
674 return InstanceFamily::Burstable;
675 }
676
677 InstanceFamily::GeneralPurpose
679}
680
681fn compute_max_machines(requirements: &WorkloadRequirements, instance: &InstanceTypeSpec) -> u32 {
683 let cpu_with_headroom = requirements.total_cpu_at_max * WORKLOAD_HEADROOM_FACTOR;
684 let cpu_machines = (cpu_with_headroom / allocatable_cpu(instance)).ceil() as u32;
685
686 let mem_with_headroom =
687 requirements.total_memory_bytes_at_max as f64 * WORKLOAD_HEADROOM_FACTOR;
688 let mem_machines =
689 (mem_with_headroom / allocatable_memory_bytes(instance) as f64).ceil() as u32;
690
691 cpu_machines
693 .max(mem_machines)
694 .max(1)
695 .min(MAX_MACHINES_PER_CLUSTER)
696}
697
698fn compute_min_machines(
700 requirements: &WorkloadRequirements,
701 instance: &InstanceTypeSpec,
702 max_machines: u32,
703) -> u32 {
704 let cpu_with_headroom = requirements.total_cpu_at_desired * WORKLOAD_HEADROOM_FACTOR;
705 let cpu_machines = (cpu_with_headroom / allocatable_cpu(instance)).ceil() as u32;
706
707 let mem_with_headroom =
708 requirements.total_memory_bytes_at_desired as f64 * WORKLOAD_HEADROOM_FACTOR;
709 let mem_machines =
710 (mem_with_headroom / allocatable_memory_bytes(instance) as f64).ceil() as u32;
711
712 cpu_machines
713 .max(mem_machines)
714 .max(1)
715 .min(2)
716 .min(max_machines)
717}
718
719fn desired_target_machines(requirements: &WorkloadRequirements) -> u32 {
720 if requirements.total_cpu_at_desired >= 2.0
721 || requirements.total_memory_bytes_at_desired >= 4 * GI
722 {
723 2
724 } else {
725 1
726 }
727}
728
729pub(crate) fn allocatable_cpu(instance: &InstanceTypeSpec) -> f64 {
730 (instance.vcpu as f64 - SYSTEM_RESERVE_CPU).max(0.25)
731}
732
733pub(crate) fn allocatable_memory_bytes(instance: &InstanceTypeSpec) -> u64 {
734 instance
735 .memory_bytes
736 .saturating_sub(system_reserve_memory_bytes(instance.memory_bytes))
737 .max(256 * MI)
738}
739
740fn system_reserve_memory_bytes(memory_bytes: u64) -> u64 {
741 if memory_bytes < 4 * GI {
742 256 * MI
743 } else if memory_bytes < 16 * GI {
744 512 * MI
745 } else {
746 GI
747 }
748}
749
750#[cfg(test)]
755mod tests {
756 use super::*;
757 use crate::BinaryTarget;
758
759 #[test]
762 fn test_parse_cpu_plain() {
763 assert_eq!(parse_cpu("1").unwrap(), 1.0);
764 assert_eq!(parse_cpu("0.5").unwrap(), 0.5);
765 assert_eq!(parse_cpu("2.0").unwrap(), 2.0);
766 assert_eq!(parse_cpu("16").unwrap(), 16.0);
767 }
768
769 #[test]
770 fn test_parse_cpu_millicore() {
771 assert_eq!(parse_cpu("500m").unwrap(), 0.5);
772 assert_eq!(parse_cpu("250m").unwrap(), 0.25);
773 assert_eq!(parse_cpu("1000m").unwrap(), 1.0);
774 assert_eq!(parse_cpu("100m").unwrap(), 0.1);
775 }
776
777 #[test]
778 fn test_parse_cpu_invalid() {
779 assert!(parse_cpu("").is_err());
780 assert!(parse_cpu("abc").is_err());
781 assert!(parse_cpu("m").is_err());
782 }
783
784 #[test]
785 fn test_parse_memory_binary_suffixes() {
786 assert_eq!(parse_memory_bytes("1Ki").unwrap(), 1024);
787 assert_eq!(parse_memory_bytes("1Mi").unwrap(), 1024 * 1024);
788 assert_eq!(parse_memory_bytes("1Gi").unwrap(), 1024 * 1024 * 1024);
789 assert_eq!(parse_memory_bytes("4Gi").unwrap(), 4 * 1024 * 1024 * 1024);
790 assert_eq!(parse_memory_bytes("512Mi").unwrap(), 512 * 1024 * 1024);
791 assert_eq!(
792 parse_memory_bytes("1Ti").unwrap(),
793 1024u64 * 1024 * 1024 * 1024
794 );
795 }
796
797 #[test]
798 fn test_parse_memory_decimal_suffixes() {
799 assert_eq!(parse_memory_bytes("1k").unwrap(), 1000);
800 assert_eq!(parse_memory_bytes("1M").unwrap(), 1_000_000);
801 assert_eq!(parse_memory_bytes("1G").unwrap(), 1_000_000_000);
802 assert_eq!(parse_memory_bytes("1T").unwrap(), 1_000_000_000_000);
803 }
804
805 #[test]
806 fn test_parse_memory_plain_bytes() {
807 assert_eq!(parse_memory_bytes("1024").unwrap(), 1024);
808 assert_eq!(parse_memory_bytes("0").unwrap(), 0);
809 }
810
811 #[test]
812 fn test_parse_memory_invalid() {
813 assert!(parse_memory_bytes("").is_err());
814 assert!(parse_memory_bytes("abc").is_err());
815 assert!(parse_memory_bytes("Gi").is_err());
816 }
817
818 #[test]
819 fn test_parse_memory_fractional() {
820 assert_eq!(parse_memory_bytes("0.5Gi").unwrap(), GI / 2);
821 assert_eq!(parse_memory_bytes("1.5Gi").unwrap(), GI + GI / 2);
822 }
823
824 #[test]
827 fn test_catalog_has_entries_for_all_cloud_platforms() {
828 assert!(!catalog_for_platform(Platform::Aws).is_empty());
829 assert!(!catalog_for_platform(Platform::Gcp).is_empty());
830 assert!(!catalog_for_platform(Platform::Azure).is_empty());
831 }
832
833 #[test]
834 fn test_catalog_no_entries_for_non_cloud_platforms() {
835 assert!(catalog_for_platform(Platform::Local).is_empty());
836 assert!(catalog_for_platform(Platform::Kubernetes).is_empty());
837 }
838
839 #[test]
840 fn test_find_known_instance_type() {
841 let spec =
842 find_instance_type(Platform::Aws, "m7g.2xlarge").expect("should find m7g.2xlarge");
843 assert_eq!(spec.vcpu, 8);
844 assert_eq!(spec.memory_bytes, 32 * GI);
845 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
846 }
847
848 #[test]
849 fn test_find_aws_c8i_nested_virt_instance_type() {
850 let spec = find_instance_type(Platform::Aws, "c8i.large").expect("should find c8i.large");
851 assert_eq!(spec.vcpu, 2);
852 assert_eq!(spec.memory_bytes, 4 * GI);
853 assert_eq!(spec.family, InstanceFamily::ComputeOptimized);
854 assert_eq!(spec.architecture, Architecture::X86_64);
855 assert!(spec.is_nested_virt_capable());
856 }
857
858 #[test]
859 fn test_find_unknown_instance_type() {
860 assert!(find_instance_type(Platform::Aws, "nonexistent.xlarge").is_none());
861 }
862
863 #[test]
864 fn test_find_wrong_platform() {
865 assert!(find_instance_type(Platform::Gcp, "m7g.2xlarge").is_none());
866 }
867
868 #[test]
869 fn test_to_machine_profile() {
870 let spec = find_instance_type(Platform::Aws, "m7g.2xlarge").unwrap();
871 let profile = spec.to_machine_profile();
872 assert_eq!(profile.cpu, "8.0");
873 assert_eq!(profile.memory_bytes, 32 * GI);
874 assert_eq!(profile.ephemeral_storage_bytes, 20 * GI);
875 assert!(profile.gpu.is_none());
876 }
877
878 #[test]
879 fn test_to_machine_profile_with_gpu() {
880 let spec = find_instance_type(Platform::Aws, "p4d.24xlarge").unwrap();
881 let profile = spec.to_machine_profile();
882 let gpu = profile.gpu.as_ref().expect("should have GPU");
883 assert_eq!(gpu.gpu_type, "nvidia-a100");
884 assert_eq!(gpu.count, 8);
885 }
886
887 #[test]
890 fn small_cpu_with_large_memory_falls_back_from_burstable() {
891 let requirements = WorkloadRequirements {
892 total_cpu_at_desired: 0.5,
893 total_memory_bytes_at_desired: 8 * GI,
894 total_cpu_at_max: 0.5,
895 total_memory_bytes_at_max: 8 * GI,
896 max_cpu_per_container: 0.5,
897 max_memory_per_container: 8 * GI,
898 max_ephemeral_storage_bytes: 0,
899 gpu: None,
900 architecture: Some(Architecture::X86_64),
901 nested_virt: false,
902 };
903 let selected = select_instance_type(Platform::Gcp, &requirements).unwrap();
904 let machine = find_instance_type(Platform::Gcp, selected.instance_type).unwrap();
905 assert!(
906 allocatable_memory_bytes(machine) as f64 >= 8.0 * GI as f64 * WORKLOAD_HEADROOM_FACTOR
907 );
908 }
909
910 #[test]
911 fn test_select_burstable_for_small_workload() {
912 let req = WorkloadRequirements {
913 total_cpu_at_desired: 1.0,
914 total_memory_bytes_at_desired: 2 * GI,
915 total_cpu_at_max: 1.0,
916 total_memory_bytes_at_max: 2 * GI,
917 max_cpu_per_container: 0.5,
918 max_memory_per_container: 1 * GI,
919 max_ephemeral_storage_bytes: 10 * GI,
920 gpu: None,
921 architecture: None,
922 nested_virt: false,
923 };
924 let sel = select_instance_type(Platform::Aws, &req).unwrap();
925 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
926 assert_eq!(spec.family, InstanceFamily::Burstable);
927 }
928
929 #[test]
930 fn test_selects_smallest_burstable_machine_with_real_headroom() {
931 let req = WorkloadRequirements {
932 total_cpu_at_desired: 1.0,
933 total_memory_bytes_at_desired: 2 * GI,
934 total_cpu_at_max: 1.0,
935 total_memory_bytes_at_max: 2 * GI,
936 max_cpu_per_container: 1.0,
937 max_memory_per_container: 2 * GI,
938 max_ephemeral_storage_bytes: 10 * GI,
939 gpu: None,
940 architecture: None,
941 nested_virt: false,
942 };
943
944 let selection = select_instance_type(Platform::Aws, &req).unwrap();
945
946 assert_eq!(selection.instance_type, "t4g.medium");
947 assert_eq!(selection.min_machines, 1);
948 assert_eq!(selection.max_machines, 1);
949 }
950
951 #[test]
952 fn test_select_general_purpose_for_standard_workload() {
953 let req = WorkloadRequirements {
955 total_cpu_at_desired: 20.0,
956 total_memory_bytes_at_desired: 80 * GI,
957 total_cpu_at_max: 20.0,
958 total_memory_bytes_at_max: 80 * GI,
959 max_cpu_per_container: 2.0,
960 max_memory_per_container: 8 * GI,
961 max_ephemeral_storage_bytes: 10 * GI,
962 gpu: None,
963 architecture: None,
964 nested_virt: false,
965 };
966 let sel = select_instance_type(Platform::Aws, &req).unwrap();
967 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
968 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
969 }
970
971 #[test]
972 fn test_select_general_purpose_even_for_cpu_heavy() {
973 let req = WorkloadRequirements {
975 total_cpu_at_desired: 20.0,
976 total_memory_bytes_at_desired: 20 * GI,
977 total_cpu_at_max: 20.0,
978 total_memory_bytes_at_max: 20 * GI,
979 max_cpu_per_container: 2.0,
980 max_memory_per_container: 2 * GI,
981 max_ephemeral_storage_bytes: 10 * GI,
982 gpu: None,
983 architecture: None,
984 nested_virt: false,
985 };
986 let sel = select_instance_type(Platform::Aws, &req).unwrap();
987 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
988 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
989 }
990
991 #[test]
992 fn test_select_storage_optimized_for_large_ephemeral() {
993 let req = WorkloadRequirements {
994 total_cpu_at_desired: 8.0,
995 total_memory_bytes_at_desired: 32 * GI,
996 total_cpu_at_max: 8.0,
997 total_memory_bytes_at_max: 32 * GI,
998 max_cpu_per_container: 2.0,
999 max_memory_per_container: 8 * GI,
1000 max_ephemeral_storage_bytes: 500 * GI,
1001 gpu: None,
1002 architecture: Some(Architecture::X86_64),
1003 nested_virt: false,
1004 };
1005 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1006 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1007 assert_eq!(spec.family, InstanceFamily::StorageOptimized);
1008 }
1009
1010 #[test]
1011 fn test_select_configurable_storage_above_fixed_local_catalog() {
1012 let req = WorkloadRequirements {
1013 total_cpu_at_desired: 8.0,
1014 total_memory_bytes_at_desired: 32 * GI,
1015 total_cpu_at_max: 8.0,
1016 total_memory_bytes_at_max: 32 * GI,
1017 max_cpu_per_container: 2.0,
1018 max_memory_per_container: 8 * GI,
1019 max_ephemeral_storage_bytes: 8_000 * GI,
1020 gpu: None,
1021 architecture: Some(Architecture::X86_64),
1022 nested_virt: false,
1023 };
1024 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1025 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1026 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
1027 assert_eq!(sel.profile.ephemeral_storage_bytes, 8_000 * GI);
1028 }
1029
1030 #[test]
1031 fn test_configurable_storage_fallback_retains_standard_vcpu_cap() {
1032 let req = WorkloadRequirements {
1033 total_cpu_at_desired: 70.0,
1034 total_memory_bytes_at_desired: 140 * GI,
1035 total_cpu_at_max: 70.0,
1036 total_memory_bytes_at_max: 140 * GI,
1037 max_cpu_per_container: 2.0,
1038 max_memory_per_container: 4 * GI,
1039 max_ephemeral_storage_bytes: 8_000 * GI,
1040 gpu: None,
1041 architecture: Some(Architecture::X86_64),
1042 nested_virt: false,
1043 };
1044
1045 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1046 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1047 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
1048 assert!(spec.vcpu <= MAX_STANDARD_VCPU);
1049 assert!(sel.max_machines > 1);
1050 }
1051
1052 #[test]
1053 fn test_nested_virtualization_storage_falls_back_to_configurable_disk() {
1054 for platform in [Platform::Aws, Platform::Gcp, Platform::Azure] {
1055 let req = WorkloadRequirements {
1056 total_cpu_at_desired: 2.0,
1057 total_memory_bytes_at_desired: 8 * GI,
1058 total_cpu_at_max: 2.0,
1059 total_memory_bytes_at_max: 8 * GI,
1060 max_cpu_per_container: 2.0,
1061 max_memory_per_container: 8 * GI,
1062 max_ephemeral_storage_bytes: 500 * GI,
1063 gpu: None,
1064 architecture: Some(Architecture::X86_64),
1065 nested_virt: true,
1066 };
1067
1068 let sel = select_instance_type(platform, &req)
1069 .unwrap_or_else(|error| panic!("{platform} should support fallback: {error}"));
1070 let spec = find_instance_type(platform, sel.instance_type).unwrap();
1071 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
1072 assert!(spec.is_nested_virt_capable());
1073 assert_eq!(sel.profile.ephemeral_storage_bytes, 500 * GI);
1074 }
1075 }
1076
1077 #[test]
1078 fn unset_architecture_resolves_to_the_one_offering_nested_virtualization() {
1079 let req = WorkloadRequirements {
1080 total_cpu_at_desired: 2.0,
1081 total_memory_bytes_at_desired: 4 * GI,
1082 total_cpu_at_max: 2.0,
1083 total_memory_bytes_at_max: 4 * GI,
1084 max_cpu_per_container: 2.0,
1085 max_memory_per_container: 4 * GI,
1086 max_ephemeral_storage_bytes: 20 * GI,
1087 gpu: None,
1088 architecture: None,
1089 nested_virt: true,
1090 };
1091
1092 for platform in [Platform::Aws, Platform::Gcp, Platform::Azure] {
1093 let selection = select_instance_type(platform, &req)
1094 .unwrap_or_else(|message| panic!("{platform}: {message}"));
1095 let spec = find_instance_type(platform, selection.instance_type)
1096 .expect("selected machine should exist in the catalog");
1097 assert!(spec.is_nested_virt_capable(), "{platform}: {}", spec.name);
1098 assert_eq!(spec.architecture, Architecture::X86_64, "{platform}");
1099 assert_eq!(selection.profile.architecture, Some(Architecture::X86_64));
1100 }
1101
1102 let oversized = WorkloadRequirements {
1105 max_cpu_per_container: 1000.0,
1106 total_cpu_at_desired: 1000.0,
1107 total_cpu_at_max: 1000.0,
1108 ..req.clone()
1109 };
1110 let message = select_instance_type(Platform::Aws, &oversized)
1111 .expect_err("no AWS machine fits 1000 vCPU per replica");
1112 assert!(
1113 message.starts_with("Arm64: architecture Arm64 is unavailable"),
1114 "{message}"
1115 );
1116 assert!(
1117 message.contains("; X86_64: no aws machine can fit one replica"),
1118 "{message}"
1119 );
1120
1121 let arm = WorkloadRequirements {
1123 architecture: Some(Architecture::Arm64),
1124 ..req
1125 };
1126 assert!(select_instance_type(Platform::Aws, &arm).is_err());
1127 }
1128
1129 #[test]
1130 fn test_configurable_disk_rejects_capacity_above_provider_limit() {
1131 for (platform, requested) in [
1132 (Platform::Aws, 100 * TI),
1133 (Platform::Gcp, 100 * TI),
1134 (Platform::Azure, 8_000 * GI),
1135 ] {
1136 let req = WorkloadRequirements {
1137 total_cpu_at_desired: 2.0,
1138 total_memory_bytes_at_desired: 8 * GI,
1139 total_cpu_at_max: 2.0,
1140 total_memory_bytes_at_max: 8 * GI,
1141 max_cpu_per_container: 2.0,
1142 max_memory_per_container: 8 * GI,
1143 max_ephemeral_storage_bytes: requested,
1144 gpu: None,
1145 architecture: Some(Architecture::X86_64),
1146 nested_virt: true,
1147 };
1148
1149 assert!(select_instance_type(platform, &req).is_err());
1150 }
1151 }
1152
1153 #[test]
1154 fn test_configurable_disk_limits_account_for_controller_rounding() {
1155 for (platform, disk_limit_gib) in [
1156 (Platform::Aws, 64 * 1024),
1157 (Platform::Gcp, 64 * 1024),
1158 (Platform::Azure, 4_095),
1159 ] {
1160 let max = max_configurable_ephemeral_storage_bytes(platform).unwrap();
1161 let materialized_disk_gib = |requested_bytes: u64| {
1162 let requested_gib = requested_bytes.div_ceil(GI);
1163 (requested_gib * 5).div_ceil(4) + 12
1164 };
1165
1166 assert!(materialized_disk_gib(max) <= disk_limit_gib);
1167 assert!(materialized_disk_gib(max + 1) > disk_limit_gib);
1168 }
1169 }
1170
1171 #[test]
1172 fn test_gpu_local_storage_is_not_treated_as_resizable() {
1173 let spec = find_instance_type(Platform::Aws, "g5.xlarge").unwrap();
1174 assert_eq!(spec.family, InstanceFamily::GpuCompute);
1175 assert!(!spec.has_configurable_ephemeral_storage());
1176 }
1177
1178 #[test]
1179 fn test_select_gpu_instance() {
1180 let req = WorkloadRequirements {
1181 total_cpu_at_desired: 8.0,
1182 total_memory_bytes_at_desired: 32 * GI,
1183 total_cpu_at_max: 8.0,
1184 total_memory_bytes_at_max: 32 * GI,
1185 max_cpu_per_container: 4.0,
1186 max_memory_per_container: 16 * GI,
1187 max_ephemeral_storage_bytes: 10 * GI,
1188 gpu: Some(GpuSpec {
1189 gpu_type: "nvidia-a100".to_string(),
1190 count: 1,
1191 }),
1192 architecture: Some(Architecture::X86_64),
1193 nested_virt: false,
1194 };
1195 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1196 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1197 assert_eq!(spec.family, InstanceFamily::GpuCompute);
1198 assert!(spec.gpu.is_some());
1199 }
1200
1201 #[test]
1202 fn test_select_uses_each_cloud_image_target_architecture() {
1203 let req = WorkloadRequirements {
1204 total_cpu_at_desired: 4.0,
1205 total_memory_bytes_at_desired: 16 * GI,
1206 total_cpu_at_max: 4.0,
1207 total_memory_bytes_at_max: 16 * GI,
1208 max_cpu_per_container: 1.0,
1209 max_memory_per_container: 4 * GI,
1210 max_ephemeral_storage_bytes: 10 * GI,
1211 gpu: None,
1212 architecture: None,
1213 nested_virt: false,
1214 };
1215 for platform in [Platform::Aws, Platform::Gcp, Platform::Azure] {
1216 let sel = select_instance_type(platform, &req)
1217 .unwrap_or_else(|error| panic!("selection failed for {platform}: {error}"));
1218 let spec = find_instance_type(platform, sel.instance_type)
1219 .expect("selected machine should exist in the catalog");
1220 assert_eq!(
1221 Some(spec.architecture),
1222 default_architecture(platform),
1223 "machine architecture must match the image target for {platform}"
1224 );
1225 }
1226 }
1227
1228 #[test]
1229 fn test_machine_count_reasonable() {
1230 let req = WorkloadRequirements {
1232 total_cpu_at_desired: 20.0,
1233 total_memory_bytes_at_desired: 40 * GI,
1234 total_cpu_at_max: 20.0,
1235 total_memory_bytes_at_max: 40 * GI,
1236 max_cpu_per_container: 1.0,
1237 max_memory_per_container: 2 * GI,
1238 max_ephemeral_storage_bytes: 10 * GI,
1239 gpu: None,
1240 architecture: None,
1241 nested_virt: false,
1242 };
1243 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1244 assert!(sel.min_machines >= 1);
1245 assert!(sel.max_machines <= MAX_MACHINES_PER_CLUSTER);
1246 assert!(sel.max_machines >= sel.min_machines);
1247 }
1248
1249 #[test]
1250 fn test_instance_size_capped_at_8_vcpu() {
1251 let req = WorkloadRequirements {
1253 total_cpu_at_desired: 70.0,
1254 total_memory_bytes_at_desired: 140 * GI,
1255 total_cpu_at_max: 70.0,
1256 total_memory_bytes_at_max: 140 * GI,
1257 max_cpu_per_container: 2.0,
1258 max_memory_per_container: 4 * GI,
1259 max_ephemeral_storage_bytes: 10 * GI,
1260 gpu: None,
1261 architecture: None,
1262 nested_virt: false,
1263 };
1264 let sel = select_instance_type(Platform::Gcp, &req).unwrap();
1265 let spec = find_instance_type(Platform::Gcp, sel.instance_type).unwrap();
1266 assert!(
1267 spec.vcpu <= MAX_STANDARD_VCPU,
1268 "selected {} with {} vCPUs, expected <= {}",
1269 spec.name,
1270 spec.vcpu,
1271 MAX_STANDARD_VCPU
1272 );
1273 assert_eq!(spec.family, InstanceFamily::GeneralPurpose);
1274 assert!(sel.max_machines > 1);
1276 }
1277
1278 #[test]
1279 fn test_larger_autoscaled_workload_gets_reasonable_instance() {
1280 let req = WorkloadRequirements {
1283 total_cpu_at_desired: 70.0,
1284 total_memory_bytes_at_desired: 140 * GI,
1285 total_cpu_at_max: 70.0, total_memory_bytes_at_max: 140 * GI, max_cpu_per_container: 2.0,
1288 max_memory_per_container: 4 * GI,
1289 max_ephemeral_storage_bytes: 20 * GI,
1290 gpu: None,
1291 architecture: None,
1292 nested_virt: false,
1293 };
1294 let sel = select_instance_type(Platform::Gcp, &req).unwrap();
1295 assert_eq!(sel.instance_type, "n2-standard-8");
1297 assert!(sel.max_machines >= 2);
1298 }
1299
1300 #[test]
1307 fn test_select_aws_picks_m8i_when_nested_virt_required() {
1308 let req = WorkloadRequirements {
1309 total_cpu_at_desired: 4.0,
1310 total_memory_bytes_at_desired: 8 * GI,
1311 total_cpu_at_max: 4.0,
1312 total_memory_bytes_at_max: 8 * GI,
1313 max_cpu_per_container: 4.0,
1314 max_memory_per_container: 8 * GI,
1315 max_ephemeral_storage_bytes: 10 * GI,
1316 gpu: None,
1317 architecture: Some(Architecture::X86_64),
1318 nested_virt: true,
1319 };
1320 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1321 assert!(
1322 sel.instance_type.starts_with("m8i.")
1323 || sel.instance_type.starts_with("c8i.")
1324 || sel.instance_type.starts_with("r8i."),
1325 "expected an m8i/c8i/r8i instance, got {}",
1326 sel.instance_type
1327 );
1328 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1329 assert!(spec.is_nested_virt_capable());
1330 }
1331
1332 #[test]
1333 fn test_select_gcp_picks_n2_when_nested_virt_required() {
1334 let req = WorkloadRequirements {
1335 total_cpu_at_desired: 4.0,
1336 total_memory_bytes_at_desired: 8 * GI,
1337 total_cpu_at_max: 4.0,
1338 total_memory_bytes_at_max: 8 * GI,
1339 max_cpu_per_container: 4.0,
1340 max_memory_per_container: 8 * GI,
1341 max_ephemeral_storage_bytes: 0,
1342 architecture: Some(Architecture::X86_64),
1343 gpu: None,
1344 nested_virt: true,
1345 };
1346
1347 let selection = select_instance_type(Platform::Gcp, &req).unwrap();
1348 assert_eq!(selection.instance_type, "n2-standard-8");
1349 assert!(find_instance_type(Platform::Gcp, selection.instance_type)
1350 .unwrap()
1351 .is_nested_virt_capable());
1352 }
1353
1354 #[test]
1355 fn test_select_azure_picks_dsv5_when_nested_virt_required() {
1356 let req = WorkloadRequirements {
1357 total_cpu_at_desired: 4.0,
1358 total_memory_bytes_at_desired: 8 * GI,
1359 total_cpu_at_max: 4.0,
1360 total_memory_bytes_at_max: 8 * GI,
1361 max_cpu_per_container: 4.0,
1362 max_memory_per_container: 8 * GI,
1363 max_ephemeral_storage_bytes: 0,
1364 architecture: Some(Architecture::X86_64),
1365 gpu: None,
1366 nested_virt: true,
1367 };
1368
1369 let selection = select_instance_type(Platform::Azure, &req).unwrap();
1370 assert_eq!(selection.instance_type, "Standard_D8s_v5");
1371 assert!(find_instance_type(Platform::Azure, selection.instance_type)
1372 .unwrap()
1373 .is_nested_virt_capable());
1374 }
1375
1376 #[test]
1377 fn test_select_aws_defaults_to_image_target_architecture() {
1378 let req = WorkloadRequirements {
1379 total_cpu_at_desired: 4.0,
1380 total_memory_bytes_at_desired: 8 * GI,
1381 total_cpu_at_max: 4.0,
1382 total_memory_bytes_at_max: 8 * GI,
1383 max_cpu_per_container: 4.0,
1384 max_memory_per_container: 8 * GI,
1385 max_ephemeral_storage_bytes: 10 * GI,
1386 gpu: None,
1387 architecture: None,
1388 nested_virt: false,
1389 };
1390 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1391 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1392 assert_eq!(spec.architecture, Architecture::Arm64);
1393 }
1394
1395 #[test]
1396 fn test_cloud_defaults_match_image_target_architectures() {
1397 for platform in [Platform::Aws, Platform::Gcp, Platform::Azure] {
1398 let target = BinaryTarget::defaults_for_platform(platform)
1399 .into_iter()
1400 .next()
1401 .expect("managed cloud should have a default image target");
1402 let image_architecture = match target.oci_arch() {
1403 "arm64" => Architecture::Arm64,
1404 "amd64" => Architecture::X86_64,
1405 architecture => {
1406 panic!("unsupported managed-cloud image architecture {architecture}")
1407 }
1408 };
1409
1410 assert_eq!(default_architecture(platform), Some(image_architecture));
1411 }
1412 }
1413
1414 #[test]
1416 fn test_select_aws_uses_graviton_for_explicit_arm64() {
1417 let req = WorkloadRequirements {
1418 total_cpu_at_desired: 4.0,
1419 total_memory_bytes_at_desired: 8 * GI,
1420 total_cpu_at_max: 4.0,
1421 total_memory_bytes_at_max: 8 * GI,
1422 max_cpu_per_container: 4.0,
1423 max_memory_per_container: 8 * GI,
1424 max_ephemeral_storage_bytes: 10 * GI,
1425 gpu: None,
1426 architecture: Some(Architecture::Arm64),
1427 nested_virt: false,
1428 };
1429 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1430 let spec = find_instance_type(Platform::Aws, sel.instance_type).unwrap();
1431 assert_eq!(spec.architecture, Architecture::Arm64);
1432 }
1433
1434 #[test]
1435 fn test_select_rejects_explicit_architecture_missing_from_cloud_catalog() {
1436 let req = WorkloadRequirements {
1437 total_cpu_at_desired: 1.0,
1438 total_memory_bytes_at_desired: 2 * GI,
1439 total_cpu_at_max: 1.0,
1440 total_memory_bytes_at_max: 2 * GI,
1441 max_cpu_per_container: 1.0,
1442 max_memory_per_container: 2 * GI,
1443 max_ephemeral_storage_bytes: 10 * GI,
1444 gpu: None,
1445 architecture: Some(Architecture::Arm64),
1446 nested_virt: false,
1447 };
1448
1449 let error = select_instance_type(Platform::Gcp, &req)
1450 .expect_err("GCP catalog has no ARM64 machine");
1451
1452 assert!(error.contains("architecture Arm64 is unavailable"));
1453 }
1454
1455 #[test]
1456 fn test_profile_has_required_fields() {
1457 let req = WorkloadRequirements {
1458 total_cpu_at_desired: 4.0,
1459 total_memory_bytes_at_desired: 16 * GI,
1460 total_cpu_at_max: 4.0,
1461 total_memory_bytes_at_max: 16 * GI,
1462 max_cpu_per_container: 1.0,
1463 max_memory_per_container: 4 * GI,
1464 max_ephemeral_storage_bytes: 10 * GI,
1465 gpu: None,
1466 architecture: None,
1467 nested_virt: false,
1468 };
1469 let sel = select_instance_type(Platform::Aws, &req).unwrap();
1470 assert!(!sel.profile.cpu.is_empty());
1471 assert!(sel.profile.memory_bytes > 0);
1472 assert!(sel.profile.ephemeral_storage_bytes > 0);
1473 }
1474
1475 #[test]
1476 fn test_error_for_unsupported_gpu_type() {
1477 let req = WorkloadRequirements {
1478 total_cpu_at_desired: 8.0,
1479 total_memory_bytes_at_desired: 32 * GI,
1480 total_cpu_at_max: 8.0,
1481 total_memory_bytes_at_max: 32 * GI,
1482 max_cpu_per_container: 4.0,
1483 max_memory_per_container: 16 * GI,
1484 max_ephemeral_storage_bytes: 10 * GI,
1485 gpu: Some(GpuSpec {
1486 gpu_type: "amd-mi300".to_string(),
1487 count: 1,
1488 }),
1489 architecture: None,
1490 nested_virt: false,
1491 };
1492 let result = select_instance_type(Platform::Aws, &req);
1493 assert!(result.is_err());
1494 }
1495
1496 type SelectionShape = (
1499 &'static str,
1500 f64,
1501 u64,
1502 u32,
1503 u32,
1504 u64,
1505 Option<(&'static str, u32)>,
1506 );
1507
1508 #[rustfmt::skip]
1509 const SELECTION_SHAPES: &[SelectionShape] = &[
1510 ("tiny", 0.25, 512 * MI, 1, 1, 0, None),
1511 ("burstable", 1.0, 2 * GI, 1, 1, 10 * GI, None),
1512 ("burstable-big-memory", 0.5, 8 * GI, 1, 1, 0, None),
1513 ("small", 1.0, 2 * GI, 2, 4, 10 * GI, None),
1514 ("medium", 4.0, 8 * GI, 1, 1, 10 * GI, None),
1515 ("general", 2.0, 8 * GI, 4, 10, 20 * GI, None),
1516 ("many-replicas", 2.0, 4 * GI, 35, 35, 20 * GI, None),
1517 ("large-replica", 12.0, 48 * GI, 1, 2, 20 * GI, None),
1518 ("huge-replica", 60.0, 240 * GI, 1, 1, 20 * GI, None),
1519 ("memory-heavy", 4.0, 60 * GI, 2, 4, 20 * GI, None),
1520 ("storage-300g", 2.0, 8 * GI, 2, 2, 300 * GI, None),
1521 ("storage-1t", 4.0, 16 * GI, 2, 4, 1000 * GI, None),
1522 ("storage-2t", 4.0, 16 * GI, 2, 4, 2000 * GI, None),
1523 ("storage-8t", 2.0, 8 * GI, 2, 2, 8000 * GI, None),
1524 ("storage-100t", 2.0, 8 * GI, 1, 1, 100 * TI, None),
1525 ("gpu-t4", 2.0, 8 * GI, 1, 1, 10 * GI, Some(("nvidia-t4", 1))),
1526 ("gpu-a10g", 2.0, 8 * GI, 1, 1, 10 * GI, Some(("nvidia-a10g", 1))),
1527 ("gpu-a100", 4.0, 16 * GI, 1, 1, 10 * GI, Some(("nvidia-a100", 1))),
1528 ("gpu-a100x8", 16.0, 128 * GI, 1, 1, 10 * GI, Some(("nvidia-a100", 8))),
1529 ("gpu-h100x8", 16.0, 128 * GI, 1, 1, 10 * GI, Some(("nvidia-h100", 8))),
1530 ];
1531
1532 fn render_selection_matrix() -> String {
1535 let mut table = String::new();
1536 for platform in [Platform::Aws, Platform::Gcp, Platform::Azure] {
1537 for architecture in [None, Some(Architecture::Arm64), Some(Architecture::X86_64)] {
1538 for nested_virt in [false, true] {
1539 for &(shape, cpu, memory, desired, max, storage, gpu) in SELECTION_SHAPES {
1540 let requirements = WorkloadRequirements {
1541 total_cpu_at_desired: cpu * desired as f64,
1542 total_memory_bytes_at_desired: memory * desired as u64,
1543 total_cpu_at_max: cpu * max as f64,
1544 total_memory_bytes_at_max: memory * max as u64,
1545 max_cpu_per_container: cpu,
1546 max_memory_per_container: memory,
1547 max_ephemeral_storage_bytes: storage,
1548 gpu: gpu.map(|(gpu_type, count)| GpuSpec {
1549 gpu_type: gpu_type.to_string(),
1550 count,
1551 }),
1552 architecture,
1553 nested_virt,
1554 };
1555 let pick = match select_instance_type(platform, &requirements) {
1556 Ok(selection) => format!(
1557 "{} min={} max={} memory={} storage={}",
1558 selection.instance_type,
1559 selection.min_machines,
1560 selection.max_machines,
1561 selection.profile.memory_bytes,
1562 selection.profile.ephemeral_storage_bytes
1563 ),
1564 Err(error) => format!("error: {error}"),
1565 };
1566 let architecture = architecture
1567 .map_or("default".to_string(), |architecture| {
1568 format!("{architecture:?}")
1569 });
1570 table.push_str(&format!(
1571 "{platform} {architecture} nested={nested_virt} {shape} -> {pick}\n"
1572 ));
1573 }
1574 }
1575 }
1576 }
1577 table
1578 }
1579
1580 #[rustfmt::skip]
1584 const ACCEPTED_PICK_CHANGES: &[(&str, &str)] = &[
1585 (
1586 "aws default nested=false gpu-t4",
1587 "error: Arm64: architecture Arm64 is unavailable for this workload on platform aws; \
1588 X86_64: no instance type for GPU type 'nvidia-t4' x1 on platform aws",
1589 ),
1590 (
1591 "aws default nested=false gpu-a10g",
1592 "g5.xlarge min=1 max=1 memory=17179869184 storage=268435456000",
1593 ),
1594 (
1595 "aws X86_64 nested=false gpu-t4",
1596 "error: no instance type for GPU type 'nvidia-t4' x1 on platform aws",
1597 ),
1598 (
1599 "aws X86_64 nested=false gpu-a10g",
1600 "g5.xlarge min=1 max=1 memory=17179869184 storage=268435456000",
1601 ),
1602 ];
1603
1604 #[test]
1608 fn automatic_picks_match_the_baseline_except_accepted_changes() {
1609 let baseline = include_str!("instance_catalog/auto_selection_baseline.txt");
1610 let rendered = render_selection_matrix();
1611 assert_eq!(baseline.lines().count(), rendered.lines().count());
1612 fn split(line: &str) -> (&str, &str) {
1613 line.split_once(" -> ").unwrap_or((line, ""))
1614 }
1615 for (before, now) in baseline.lines().zip(rendered.lines()) {
1616 let ((case, _), (now_case, pick)) = (split(before), split(now));
1617 assert_eq!(case, now_case);
1618 match ACCEPTED_PICK_CHANGES
1619 .iter()
1620 .find(|(accepted, _)| *accepted == case)
1621 {
1622 Some((_, accepted_pick)) => {
1623 assert_ne!(
1624 before, now,
1625 "{case} no longer changes; drop it from the list"
1626 );
1627 assert_eq!(pick, *accepted_pick, "{case}");
1628 }
1629 None => assert_eq!(before, now, "automatic pick changed"),
1630 }
1631 }
1632 }
1633
1634 type PinnedMachine = (
1635 Platform,
1636 &'static str,
1637 u32,
1638 u64,
1639 u64,
1640 Option<(&'static str, u32)>,
1641 bool,
1642 );
1643
1644 #[rustfmt::skip]
1647 const PINNED_MACHINES: &[PinnedMachine] = &[
1648 (Platform::Aws, "t4g.micro", 2, GI, 20 * GI, None, false),
1649 (Platform::Aws, "t4g.small", 2, 2 * GI, 20 * GI, None, false),
1650 (Platform::Aws, "t4g.medium", 2, 4 * GI, 20 * GI, None, false),
1651 (Platform::Aws, "t4g.large", 2, 8 * GI, 20 * GI, None, false),
1652 (Platform::Aws, "t3.xlarge", 4, 16 * GI, 20 * GI, None, false),
1653 (Platform::Aws, "t4g.xlarge", 4, 16 * GI, 20 * GI, None, false),
1654 (Platform::Aws, "m7g.medium", 1, 4 * GI, 20 * GI, None, false),
1655 (Platform::Aws, "m7i.large", 2, 8 * GI, 20 * GI, None, false),
1656 (Platform::Aws, "m7g.large", 2, 8 * GI, 20 * GI, None, false),
1657 (Platform::Aws, "m8i.large", 2, 8 * GI, 20 * GI, None, true),
1658 (Platform::Aws, "m7i.xlarge", 4, 16 * GI, 20 * GI, None, false),
1659 (Platform::Aws, "m7g.xlarge", 4, 16 * GI, 20 * GI, None, false),
1660 (Platform::Aws, "m8i.xlarge", 4, 16 * GI, 20 * GI, None, true),
1661 (Platform::Aws, "m7i.2xlarge", 8, 32 * GI, 20 * GI, None, false),
1662 (Platform::Aws, "m7g.2xlarge", 8, 32 * GI, 20 * GI, None, false),
1663 (Platform::Aws, "m8i.2xlarge", 8, 32 * GI, 20 * GI, None, true),
1664 (Platform::Aws, "m7i.4xlarge", 16, 64 * GI, 20 * GI, None, false),
1665 (Platform::Aws, "m7g.4xlarge", 16, 64 * GI, 20 * GI, None, false),
1666 (Platform::Aws, "m8i.4xlarge", 16, 64 * GI, 20 * GI, None, true),
1667 (Platform::Aws, "c7g.medium", 1, 2 * GI, 20 * GI, None, false),
1668 (Platform::Aws, "c8g.medium", 1, 2 * GI, 20 * GI, None, false),
1669 (Platform::Aws, "c7g.large", 2, 4 * GI, 20 * GI, None, false),
1670 (Platform::Aws, "c8g.large", 2, 4 * GI, 20 * GI, None, false),
1671 (Platform::Aws, "c8i.large", 2, 4 * GI, 20 * GI, None, true),
1672 (Platform::Aws, "c7g.xlarge", 4, 8 * GI, 20 * GI, None, false),
1673 (Platform::Aws, "c8g.xlarge", 4, 8 * GI, 20 * GI, None, false),
1674 (Platform::Aws, "c8i.xlarge", 4, 8 * GI, 20 * GI, None, true),
1675 (Platform::Aws, "c7g.2xlarge", 8, 16 * GI, 20 * GI, None, false),
1676 (Platform::Aws, "c8g.2xlarge", 8, 16 * GI, 20 * GI, None, false),
1677 (Platform::Aws, "c8i.2xlarge", 8, 16 * GI, 20 * GI, None, true),
1678 (Platform::Aws, "c7g.4xlarge", 16, 32 * GI, 20 * GI, None, false),
1679 (Platform::Aws, "c8g.4xlarge", 16, 32 * GI, 20 * GI, None, false),
1680 (Platform::Aws, "c8i.4xlarge", 16, 32 * GI, 20 * GI, None, true),
1681 (Platform::Aws, "r7g.medium", 1, 8 * GI, 20 * GI, None, false),
1682 (Platform::Aws, "r7g.large", 2, 16 * GI, 20 * GI, None, false),
1683 (Platform::Aws, "r6a.large", 2, 16 * GI, 20 * GI, None, false),
1684 (Platform::Aws, "r7i.large", 2, 16 * GI, 20 * GI, None, false),
1685 (Platform::Aws, "r7g.xlarge", 4, 32 * GI, 20 * GI, None, false),
1686 (Platform::Aws, "r6a.xlarge", 4, 32 * GI, 20 * GI, None, false),
1687 (Platform::Aws, "r7i.xlarge", 4, 32 * GI, 20 * GI, None, false),
1688 (Platform::Aws, "r7g.2xlarge", 8, 64 * GI, 20 * GI, None, false),
1689 (Platform::Aws, "r6a.2xlarge", 8, 64 * GI, 20 * GI, None, false),
1690 (Platform::Aws, "r7i.2xlarge", 8, 64 * GI, 20 * GI, None, false),
1691 (Platform::Aws, "r7g.4xlarge", 16, 128 * GI, 20 * GI, None, false),
1692 (Platform::Aws, "r6a.4xlarge", 16, 128 * GI, 20 * GI, None, false),
1693 (Platform::Aws, "r7i.4xlarge", 16, 128 * GI, 20 * GI, None, false),
1694 (Platform::Aws, "i4i.xlarge", 4, 32 * GI, 937 * GI, None, false),
1695 (Platform::Aws, "i4i.2xlarge", 8, 64 * GI, 1875 * GI, None, false),
1696 (Platform::Aws, "i4i.4xlarge", 16, 128 * GI, 3750 * GI, None, false),
1697 (Platform::Aws, "i4i.8xlarge", 32, 256 * GI, 7500 * GI, None, false),
1698 (Platform::Aws, "g5.xlarge", 4, 16 * GI, 250 * GI, Some(("nvidia-t4", 1)), false),
1699 (Platform::Aws, "g5.2xlarge", 8, 32 * GI, 450 * GI, Some(("nvidia-t4", 1)), false),
1700 (Platform::Aws, "p4d.24xlarge", 96, 1152 * GI, 8000 * GI, Some(("nvidia-a100", 8)), false),
1701 (Platform::Aws, "p5.48xlarge", 192, 2048 * GI, 8000 * GI, Some(("nvidia-h100", 8)), false),
1702 (Platform::Gcp, "e2-micro", 2, GI, 20 * GI, None, false),
1703 (Platform::Gcp, "e2-small", 2, 2 * GI, 20 * GI, None, false),
1704 (Platform::Gcp, "e2-medium", 2, 4 * GI, 20 * GI, None, false),
1705 (Platform::Gcp, "n2-standard-2", 2, 8 * GI, 20 * GI, None, true),
1706 (Platform::Gcp, "n2-standard-4", 4, 16 * GI, 20 * GI, None, true),
1707 (Platform::Gcp, "n2-standard-8", 8, 32 * GI, 20 * GI, None, true),
1708 (Platform::Gcp, "n2-standard-16", 16, 64 * GI, 20 * GI, None, true),
1709 (Platform::Gcp, "c3-standard-4", 4, 8 * GI, 20 * GI, None, false),
1710 (Platform::Gcp, "c3-standard-8", 8, 16 * GI, 20 * GI, None, false),
1711 (Platform::Gcp, "n2-highmem-2", 2, 16 * GI, 20 * GI, None, false),
1712 (Platform::Gcp, "n2-highmem-4", 4, 32 * GI, 20 * GI, None, false),
1713 (Platform::Gcp, "n2-highmem-8", 8, 64 * GI, 20 * GI, None, false),
1714 (Platform::Gcp, "n2-highmem-16", 16, 128 * GI, 20 * GI, None, false),
1715 (Platform::Gcp, "n2-highmem-32", 32, 256 * GI, 20 * GI, None, false),
1716 (Platform::Gcp, "c3d-standard-8", 8, 32 * GI, 480 * GI, None, false),
1717 (Platform::Gcp, "c3d-standard-16", 16, 64 * GI, 960 * GI, None, false),
1718 (Platform::Gcp, "c3d-standard-30", 30, 120 * GI, 1920 * GI, None, false),
1719 (Platform::Gcp, "n1-standard-4-t4", 4, 15 * GI, 100 * GI, Some(("nvidia-t4", 1)), false),
1720 (Platform::Gcp, "a2-highgpu-1g", 12, 85 * GI, 100 * GI, Some(("nvidia-a100", 1)), false),
1721 (Platform::Gcp, "a2-highgpu-8g", 96, 1360 * GI, 100 * GI, Some(("nvidia-a100", 8)), false),
1722 (Platform::Gcp, "a3-highgpu-8g", 208, 1872 * GI, 100 * GI, Some(("nvidia-h100", 8)), false),
1723 (Platform::Azure, "Standard_B1s", 1, GI, 20 * GI, None, false),
1724 (Platform::Azure, "Standard_B2s", 2, 4 * GI, 20 * GI, None, false),
1725 (Platform::Azure, "Standard_B2ms", 2, 8 * GI, 20 * GI, None, false),
1726 (Platform::Azure, "Standard_B4ms", 4, 16 * GI, 20 * GI, None, false),
1727 (Platform::Azure, "Standard_D2s_v4", 2, 8 * GI, 20 * GI, None, false),
1728 (Platform::Azure, "Standard_D2s_v5", 2, 8 * GI, 20 * GI, None, true),
1729 (Platform::Azure, "Standard_D4s_v5", 4, 16 * GI, 20 * GI, None, true),
1730 (Platform::Azure, "Standard_D8s_v5", 8, 32 * GI, 20 * GI, None, true),
1731 (Platform::Azure, "Standard_D16s_v5", 16, 64 * GI, 20 * GI, None, true),
1732 (Platform::Azure, "Standard_D2s_v6", 2, 8 * GI, 20 * GI, None, true),
1733 (Platform::Azure, "Standard_D4s_v6", 4, 16 * GI, 20 * GI, None, true),
1734 (Platform::Azure, "Standard_D8s_v6", 8, 32 * GI, 20 * GI, None, true),
1735 (Platform::Azure, "Standard_D16s_v6", 16, 64 * GI, 20 * GI, None, true),
1736 (Platform::Azure, "Standard_F2s_v2", 2, 4 * GI, 20 * GI, None, true),
1737 (Platform::Azure, "Standard_F4s_v2", 4, 8 * GI, 20 * GI, None, true),
1738 (Platform::Azure, "Standard_F8s_v2", 8, 16 * GI, 20 * GI, None, true),
1739 (Platform::Azure, "Standard_F16s_v2", 16, 32 * GI, 20 * GI, None, true),
1740 (Platform::Azure, "Standard_E2s_v5", 2, 16 * GI, 20 * GI, None, true),
1741 (Platform::Azure, "Standard_E4s_v5", 4, 32 * GI, 20 * GI, None, true),
1742 (Platform::Azure, "Standard_E8s_v5", 8, 64 * GI, 20 * GI, None, true),
1743 (Platform::Azure, "Standard_E16s_v5", 16, 128 * GI, 20 * GI, None, true),
1744 (Platform::Azure, "Standard_L8s_v3", 8, 64 * GI, 1788 * GI, None, false),
1745 (Platform::Azure, "Standard_L16s_v3", 16, 128 * GI, 3576 * GI, None, false),
1746 (Platform::Azure, "Standard_L32s_v3", 32, 256 * GI, 7154 * GI, None, false),
1747 (Platform::Azure, "Standard_NC4as_T4_v3", 4, 28 * GI, 176 * GI, Some(("nvidia-t4", 1)), false),
1748 (Platform::Azure, "Standard_NC24ads_A100_v4", 24, 220 * GI, 958 * GI, Some(("nvidia-a100", 1)), false),
1749 (Platform::Azure, "Standard_NC96ads_A100_v4", 96, 880 * GI, 3916 * GI, Some(("nvidia-a100", 4)), false),
1750 (Platform::Azure, "Standard_ND96isr_H100_v5", 96, 1900 * GI, 1000 * GI, Some(("nvidia-h100", 8)), false),
1751 ];
1752
1753 const GPU_TYPE_CORRECTIONS: &[(&str, &str)] =
1755 &[("g5.xlarge", "nvidia-a10g"), ("g5.2xlarge", "nvidia-a10g")];
1756
1757 #[test]
1758 fn every_pinned_machine_is_still_in_the_catalog_with_its_values() {
1759 for &(platform, name, vcpu, memory_bytes, storage_bytes, gpu, nested_virt) in
1760 PINNED_MACHINES
1761 {
1762 let spec = find_instance_type(platform, name)
1763 .unwrap_or_else(|| panic!("{platform} machine {name} left the catalog"));
1764 let gpu = gpu.map(|(gpu_type, count)| {
1765 let corrected = GPU_TYPE_CORRECTIONS
1766 .iter()
1767 .find(|(corrected, _)| *corrected == name);
1768 (corrected.map_or(gpu_type, |(_, gpu_type)| *gpu_type), count)
1769 });
1770 assert_eq!(
1771 (
1772 spec.vcpu,
1773 spec.memory_bytes,
1774 spec.ephemeral_storage_bytes,
1775 spec.gpu.map(|gpu| (gpu.gpu_type, gpu.count)),
1776 spec.nested_virt,
1777 ),
1778 (vcpu, memory_bytes, storage_bytes, gpu, nested_virt),
1779 "{platform} {name}"
1780 );
1781 }
1782 }
1783
1784 #[test]
1785 fn automatic_selection_considers_exactly_the_pinned_machines() {
1786 let pinned: std::collections::HashSet<_> = PINNED_MACHINES
1787 .iter()
1788 .map(|&(platform, name, ..)| (platform, name))
1789 .collect();
1790 let auto_select: std::collections::HashSet<_> = CATALOG
1791 .iter()
1792 .filter(|spec| spec.auto_select)
1793 .map(|spec| (spec.platform, spec.name))
1794 .collect();
1795 assert_eq!(auto_select, pinned);
1796 }
1797
1798 fn general_requirements() -> WorkloadRequirements {
1799 WorkloadRequirements {
1800 total_cpu_at_desired: 3.0,
1801 total_memory_bytes_at_desired: 12 * GI,
1802 total_cpu_at_max: 3.0,
1803 total_memory_bytes_at_max: 12 * GI,
1804 max_cpu_per_container: 1.0,
1805 max_memory_per_container: 4 * GI,
1806 max_ephemeral_storage_bytes: 10 * GI,
1807 gpu: None,
1808 architecture: None,
1809 nested_virt: true,
1810 }
1811 }
1812
1813 #[test]
1814 fn azure_auto_selection_prefers_dsv5_then_dsv6_in_catalog_order() {
1815 let series = auto_select_series(Platform::Azure);
1816 let position = |name| series.iter().position(|series| *series == name);
1817 assert!(
1818 position("Dsv5").unwrap() < position("Dsv6").unwrap(),
1819 "{series:?}"
1820 );
1821 }
1822
1823 #[test]
1824 fn with_room_everywhere_the_pick_is_the_plain_pick() {
1825 let requirements = general_requirements();
1826 let plain = select_instance_type(Platform::Azure, &requirements).unwrap();
1827 let with_room =
1828 select_instance_type_with_room(Platform::Azure, &requirements, 2, &|_, _| true)
1829 .unwrap();
1830 assert_eq!(plain.instance_type, with_room.instance_type);
1831 assert!(
1832 with_room.instance_type.ends_with("_v5"),
1833 "{}",
1834 with_room.instance_type
1835 );
1836 }
1837
1838 #[test]
1839 fn a_series_without_room_is_skipped_for_the_next_one() {
1840 let requirements = general_requirements();
1841 let selection =
1842 select_instance_type_with_room(Platform::Azure, &requirements, 2, &|spec, _| {
1843 spec.series != "Dsv5"
1844 })
1845 .unwrap();
1846 assert_eq!(selection.instance_type, "Standard_D4s_v6");
1847 }
1848
1849 #[test]
1850 fn room_is_asked_for_the_requested_machines() {
1851 let requirements = general_requirements();
1852 let asked = std::cell::RefCell::new(Vec::new());
1853 select_instance_type_with_room(Platform::Azure, &requirements, 3, &|spec, machines| {
1854 asked.borrow_mut().push((spec.series, machines));
1855 true
1856 })
1857 .unwrap();
1858 assert!(asked.borrow().iter().all(|(_, machines)| *machines == 3));
1859 }
1860
1861 #[test]
1864 fn local_storage_is_preferred_among_the_series_with_room() {
1865 let requirements = WorkloadRequirements {
1866 max_ephemeral_storage_bytes: 500 * GI,
1867 nested_virt: false,
1868 ..general_requirements()
1869 };
1870 let plain = select_instance_type(Platform::Azure, &requirements).unwrap();
1871 let plain_spec = find_instance_type(Platform::Azure, plain.instance_type).unwrap();
1872 assert!(
1873 !plain_spec.has_configurable_ephemeral_storage(),
1874 "the plain pick has a local disk: {}",
1875 plain.instance_type
1876 );
1877 let with_room =
1878 select_instance_type_with_room(Platform::Azure, &requirements, 1, &|_, _| true)
1879 .unwrap();
1880 assert_eq!(with_room.instance_type, plain.instance_type);
1881 let local_only =
1883 select_instance_type_with_room(Platform::Azure, &requirements, 1, &|spec, _| {
1884 spec.series == plain_spec.series || spec.series.starts_with("Dsv")
1885 })
1886 .unwrap();
1887 assert_eq!(local_only.instance_type, plain.instance_type);
1888 }
1889
1890 #[test]
1891 fn with_no_room_anywhere_the_plain_pick_is_kept_for_the_quota_refusal() {
1892 let requirements = general_requirements();
1893 let plain = select_instance_type(Platform::Azure, &requirements).unwrap();
1894 let none = select_instance_type_with_room(Platform::Azure, &requirements, 2, &|_, _| false)
1895 .unwrap();
1896 assert_eq!(plain.instance_type, none.instance_type);
1897 }
1898
1899 #[test]
1900 fn bare_metal_rows_run_vms_and_are_never_picked_automatically() {
1901 let metal: Vec<_> = CATALOG.iter().filter(|spec| spec.bare_metal).collect();
1902 assert_eq!(
1903 metal.iter().map(|spec| spec.name).collect::<Vec<_>>(),
1904 [
1905 "m8id.metal-48xl",
1906 "m8id.metal-96xl",
1907 "c8id.metal-48xl",
1908 "c8id.metal-96xl",
1909 "c5.metal",
1910 ]
1911 );
1912 for spec in CATALOG {
1913 assert_eq!(spec.bare_metal, spec.name.contains(".metal"), "{}", spec.name);
1914 }
1915 for spec in metal {
1916 assert!(spec.nested_virt && !spec.auto_select, "{}", spec.name);
1917 }
1918 }
1919
1920 #[test]
1922 fn test_catalog_instance_types_sorted_by_vcpu_within_series() {
1923 let mut finished = std::collections::HashSet::new();
1924 for window in CATALOG.windows(2) {
1925 let (previous, next) = (&window[0], &window[1]);
1926 if (previous.platform, previous.series) == (next.platform, next.series) {
1927 assert!(
1928 previous.vcpu <= next.vcpu,
1929 "{} ({}) before {} ({})",
1930 previous.name,
1931 previous.vcpu,
1932 next.name,
1933 next.vcpu
1934 );
1935 } else {
1936 finished.insert((previous.platform, previous.series));
1937 assert!(
1938 !finished.contains(&(next.platform, next.series)),
1939 "series {} is split",
1940 next.series
1941 );
1942 }
1943 }
1944 }
1945}