@@ -47,6 +47,7 @@ func TestCollectMetrics_Extensible(t *testing.T) {
4747 // Define all expected metric keys from types.go
4848 expectedKeys := []string {
4949 COMMAND_FLAGS ,
50+ MACHINE_TYPE ,
5051 REGION ,
5152 ZONE ,
5253 IS_TEST_DATA ,
@@ -73,47 +74,66 @@ func TestCollectMetrics_Extensible(t *testing.T) {
7374 _ = cmd .Flags ().Set ("project" , "test-project" )
7475 },
7576 setupCollector : func (c * Collector ) {
76- // Mock the blueprint variables to include region and zone
77+ // Mock the blueprint variables and modules
7778 c .blueprint = config.Blueprint {
7879 Vars : config .NewDict (map [string ]cty.Value {
7980 "region" : cty .StringVal ("us-central1" ),
8081 "zone" : cty .StringVal ("us-central1-a" ),
8182 }),
83+ Groups : []config.Group {
84+ {
85+ Name : config .GroupName ("primary" ),
86+ Modules : []config.Module {
87+ {
88+ ID : config .ModuleID ("compute_pool" ),
89+ // Ensure the source matches the machineTypeModulePattern ".*modules.compute.*"
90+ Source : "modules/compute/vm-instance" ,
91+ Settings : config .NewDict (map [string ]cty.Value {
92+ "machine_type" : cty .StringVal ("c2-standard-8" ),
93+ }),
94+ },
95+ },
96+ },
97+ },
8298 }
8399 },
84100 expectedValues : map [string ]string {
85- COMMAND_FLAGS : "force,project" ,
86101 IS_TEST_DATA : "true" ,
87102 EXIT_CODE : "0" ,
103+ COMMAND_FLAGS : "force,project" ,
88104 REGION : "us-central1" ,
89105 ZONE : "us-central1-a" ,
106+ MACHINE_TYPE : "c2-standard-8" ,
90107 },
91108 },
92109 {
93- name : "Failure exit code with missing region and zone " ,
94- errorCode : 1 , // Simulating a failure
110+ name : "Failure exit code with missing region, zone, and machine type " ,
111+ errorCode : 1 ,
95112 setupCmd : func (cmd * cobra.Command ) {
96- // No flags set for this test case
113+ // No flags set
97114 },
98115 setupCollector : func (c * Collector ) {
99- // Blueprint with empty vars to simulate missing region and zone
116+ // Blueprint with empty vars and no compute modules
100117 c .blueprint = config.Blueprint {
101- Vars : config .NewDict (map [string ]cty.Value {}),
118+ Vars : config .NewDict (map [string ]cty.Value {}),
119+ Groups : []config.Group {},
102120 }
103121 },
104122 expectedValues : map [string ]string {
105123 IS_TEST_DATA : "true" ,
106- EXIT_CODE : "1" , // Verify failure code is captured
107- COMMAND_FLAGS : "" , // Verify empty flags
108- REGION : "" , // Verify missing region
109- ZONE : "" , // Verify missing zone
124+ EXIT_CODE : "1" ,
125+ COMMAND_FLAGS : "" ,
126+ REGION : "" ,
127+ ZONE : "" ,
128+ MACHINE_TYPE : "" , // Verify empty machine type when no matching modules exist
110129 },
111130 },
112131 }
113132
114133 for _ , tt := range tests {
115134 t .Run (tt .name , func (t * testing.T ) {
116135 cmd := & cobra.Command {Use : "mock" }
136+
117137 // Execute the setup function to apply flags to the command
118138 if tt .setupCmd != nil {
119139 tt .setupCmd (cmd )
@@ -423,3 +443,125 @@ func TestGetKeyFromBlueprint(t *testing.T) {
423443 })
424444 }
425445}
446+
447+ // TestGetMachineType verifies that machine types are correctly extracted from the blueprint.
448+ func TestGetMachineType (t * testing.T ) {
449+ tests := []struct {
450+ name string
451+ setupBp func () config.Blueprint
452+ expected string
453+ }{
454+ {
455+ name : "Single machine type in module" ,
456+ setupBp : func () config.Blueprint {
457+ return config.Blueprint {
458+ Groups : []config.Group {
459+ {
460+ Name : config .GroupName ("primary" ),
461+ Modules : []config.Module {
462+ {
463+ ID : config .ModuleID ("compute_pool" ),
464+ Source : "modules/compute/vm-instance" ,
465+ Settings : config .NewDict (map [string ]cty.Value {
466+ "machine_type" : cty .StringVal ("c2-standard-8" ),
467+ }),
468+ },
469+ },
470+ },
471+ },
472+ }
473+ },
474+ expected : "c2-standard-8" ,
475+ },
476+ {
477+ name : "Multiple different machine types" ,
478+ setupBp : func () config.Blueprint {
479+ return config.Blueprint {
480+ Groups : []config.Group {
481+ {
482+ Name : config .GroupName ("primary" ),
483+ Modules : []config.Module {
484+ {
485+ ID : config .ModuleID ("login_node" ),
486+ Source : "modules/compute/vm-instance" ,
487+ Settings : config .NewDict (map [string ]cty.Value {
488+ "machine_type" : cty .StringVal ("n2-standard-2" ),
489+ }),
490+ },
491+ {
492+ ID : config .ModuleID ("lcontroller_node" ),
493+ Source : "modules/compute/vm-instance" ,
494+ Settings : config .NewDict (map [string ]cty.Value {
495+ "machine_type" : cty .StringVal ("n2-standard-2" ),
496+ }),
497+ },
498+ {
499+ ID : config .ModuleID ("compute_pool" ),
500+ Source : "modules/compute/gke-node-pool" ,
501+ Settings : config .NewDict (map [string ]cty.Value {
502+ "machine_type" : cty .StringVal ("c2-standard-8" ),
503+ }),
504+ },
505+ },
506+ },
507+ },
508+ }
509+ },
510+ expected : "n2-standard-2,c2-standard-8" ,
511+ },
512+ {
513+ name : "TPU node type module (schedmd-slurm-gcp-v6-nodeset-tpu)" ,
514+ setupBp : func () config.Blueprint {
515+ return config.Blueprint {
516+ Groups : []config.Group {
517+ {
518+ Name : config .GroupName ("primary" ),
519+ Modules : []config.Module {
520+ {
521+ ID : config .ModuleID ("tpu_nodeset" ),
522+ Source : "community/modules/compute/schedmd-slurm-gcp-v6-nodeset-tpu" ,
523+ Settings : config .NewDict (map [string ]cty.Value {
524+ "node_type" : cty .StringVal ("v4-8" ),
525+ }),
526+ },
527+ },
528+ },
529+ },
530+ }
531+ },
532+ expected : "v4-8" ,
533+ },
534+ {
535+ name : "No machine types in modules" ,
536+ setupBp : func () config.Blueprint {
537+ return config.Blueprint {
538+ Groups : []config.Group {
539+ {
540+ Name : config .GroupName ("primary" ),
541+ Modules : []config.Module {
542+ {
543+ ID : config .ModuleID ("vpc_network" ),
544+ Source : "modules/network/vpc" ,
545+ Settings : config .NewDict (map [string ]cty.Value {}),
546+ },
547+ },
548+ },
549+ },
550+ }
551+ },
552+ expected : "" ,
553+ },
554+ }
555+
556+ for _ , tt := range tests {
557+ t .Run (tt .name , func (t * testing.T ) {
558+ bp := tt .setupBp ()
559+
560+ actual := getMachineType (bp )
561+
562+ if actual != tt .expected {
563+ t .Errorf ("getMachineType() = %q, want %q" , actual , tt .expected )
564+ }
565+ })
566+ }
567+ }
0 commit comments