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