@@ -340,49 +340,83 @@ func TestEnrollCommandRejectsOverlongMetadataNames(t *testing.T) {
340340 require .ErrorContains (t , err , "Node group name must be 255 characters or fewer" )
341341}
342342
343- func TestValidateReservedPairMetadata (t * testing.T ) {
343+ func TestValidatedOptionalMetadataFlagValueSupportsIndependentReservedUpdates (t * testing.T ) {
344344 strPtr := func (v string ) * string { return & v }
345345 tests := []struct {
346- name string
347- nodeGroup * string
348- computeZone * string
349- wantErr bool
346+ name string
347+ nodeGroupArg * string
348+ computeZoneArg * string
349+ expectNodeGroup * string
350+ expectComputeZone * string
350351 }{
351- {name : "both omitted" , nodeGroup : nil , computeZone : nil , wantErr : false },
352- {name : "both empty" , nodeGroup : strPtr ("" ), computeZone : strPtr ("" ), wantErr : false },
353- {name : "both non-empty" , nodeGroup : strPtr ("ng-a" ), computeZone : strPtr ("cz-a" ), wantErr : false },
354- {name : "node-group only" , nodeGroup : strPtr ("ng-a" ), computeZone : nil , wantErr : true },
355- {name : "compute-zone only" , nodeGroup : nil , computeZone : strPtr ("cz-a" ), wantErr : true },
356- {name : "node-group empty compute-zone non-empty" , nodeGroup : strPtr ("" ), computeZone : strPtr ("cz-a" ), wantErr : true },
357- {name : "node-group non-empty compute-zone empty" , nodeGroup : strPtr ("ng-a" ), computeZone : strPtr ("" ), wantErr : true },
352+ {
353+ name : "clear compute-zone and set node-group" ,
354+ nodeGroupArg : strPtr ("xx" ),
355+ computeZoneArg : strPtr ("" ),
356+ expectNodeGroup : strPtr ("xx" ),
357+ expectComputeZone : strPtr ("" ),
358+ },
359+ {
360+ name : "set compute-zone and clear node-group" ,
361+ nodeGroupArg : strPtr ("" ),
362+ computeZoneArg : strPtr ("xx" ),
363+ expectNodeGroup : strPtr ("" ),
364+ expectComputeZone : strPtr ("xx" ),
365+ },
366+ {
367+ name : "set compute-zone and omit node-group" ,
368+ nodeGroupArg : nil ,
369+ computeZoneArg : strPtr ("xx" ),
370+ expectNodeGroup : nil ,
371+ expectComputeZone : strPtr ("xx" ),
372+ },
373+ {
374+ name : "set node-group and omit compute-zone" ,
375+ nodeGroupArg : strPtr ("xx" ),
376+ computeZoneArg : nil ,
377+ expectNodeGroup : strPtr ("xx" ),
378+ expectComputeZone : nil ,
379+ },
380+ {
381+ name : "clear compute-zone and omit node-group" ,
382+ nodeGroupArg : nil ,
383+ computeZoneArg : strPtr ("" ),
384+ expectNodeGroup : nil ,
385+ expectComputeZone : strPtr ("" ),
386+ },
387+ {
388+ name : "clear node-group and omit compute-zone" ,
389+ nodeGroupArg : strPtr ("" ),
390+ computeZoneArg : nil ,
391+ expectNodeGroup : strPtr ("" ),
392+ expectComputeZone : nil ,
393+ },
358394 }
359395
360396 for _ , tc := range tests {
361397 t .Run (tc .name , func (t * testing.T ) {
362- err := validateReservedPairMetadata (tc .nodeGroup , tc .computeZone )
363- if tc .wantErr {
364- require .ErrorContains (t , err , "--node-group and --compute-zone must be both omitted, both empty, or both non-empty" )
365- return
398+ flagSet := flag .NewFlagSet ("enroll" , flag .ContinueOnError )
399+ flagSet .String ("node-group" , "" , "" )
400+ flagSet .String ("compute-zone" , "" , "" )
401+ if tc .nodeGroupArg != nil {
402+ require .NoError (t , flagSet .Set ("node-group" , * tc .nodeGroupArg ))
366403 }
404+ if tc .computeZoneArg != nil {
405+ require .NoError (t , flagSet .Set ("compute-zone" , * tc .computeZoneArg ))
406+ }
407+ cliContext := cli .NewContext (cli .NewApp (), flagSet , nil )
408+
409+ nodeGroup , err := validatedOptionalMetadataFlagValue (cliContext , "node-group" , "Node group" )
367410 require .NoError (t , err )
411+ computeZone , err := validatedOptionalMetadataFlagValue (cliContext , "compute-zone" , "Compute zone" )
412+ require .NoError (t , err )
413+
414+ assert .Equal (t , tc .expectNodeGroup , nodeGroup )
415+ assert .Equal (t , tc .expectComputeZone , computeZone )
368416 })
369417 }
370418}
371419
372- func TestEnrollCommandRejectsMixedReservedPairValues (t * testing.T ) {
373- app := App ()
374- app .Writer = & bytes.Buffer {}
375-
376- err := app .Run ([]string {
377- "fleetint" , "enroll" ,
378- "--endpoint" , "https://example.com" ,
379- "--token" , "token" ,
380- "--node-group" , "" ,
381- "--compute-zone" , "cz-a" ,
382- })
383- require .ErrorContains (t , err , "--node-group and --compute-zone must be both omitted, both empty, or both non-empty" )
384- }
385-
386420func TestEnrollCommandRejectsReservedUnassignedName (t * testing.T ) {
387421 app := App ()
388422 app .Writer = & bytes.Buffer {}
0 commit comments