@@ -301,15 +301,16 @@ func (m *MockConsulClient) Agent() ConsulAgent {
301301
302302// MockAgent simulates the Agent part of the Consul client.
303303type MockAgent struct {
304- ServicesFunc func () (map [string ]* api.AgentService , error )
304+ ServicesFunc func () (map [string ]* api.AgentService , error )
305+ ServiceRegisterFunc func (reg * api.AgentServiceRegistration ) error
305306}
306307
307308func (m * MockAgent ) Services () (map [string ]* api.AgentService , error ) {
308309 return m .ServicesFunc ()
309310}
310311
311312func (m * MockAgent ) ServiceRegister (reg * api.AgentServiceRegistration ) error {
312- return nil
313+ return m . ServiceRegisterFunc ( reg )
313314}
314315
315316func TestNew (t * testing.T ) {
@@ -334,3 +335,164 @@ func TestNew(t *testing.T) {
334335 t .Errorf ("Expected ServiceID to be 'test-service', got '%s'" , tagit .ServiceID )
335336 }
336337}
338+
339+ func TestGetService (t * testing.T ) {
340+ tests := []struct {
341+ name string
342+ serviceID string
343+ mockServicesData map [string ]* api.AgentService
344+ mockServicesErr error
345+ expectErr bool
346+ expectService * api.AgentService
347+ }{
348+ {
349+ name : "Service Found" ,
350+ serviceID : "test-service" ,
351+ mockServicesData : map [string ]* api.AgentService {
352+ "test-service" : {
353+ ID : "test-service" ,
354+ Service : "test" ,
355+ Tags : []string {"tag1" , "tag2" },
356+ },
357+ },
358+ expectErr : false ,
359+ expectService : & api.AgentService {ID : "test-service" , Service : "test" , Tags : []string {"tag1" , "tag2" }},
360+ },
361+ {
362+ name : "Service Not Found" ,
363+ serviceID : "nonexistent-service" ,
364+ mockServicesData : map [string ]* api.AgentService {},
365+ expectErr : true ,
366+ expectService : nil ,
367+ },
368+ {
369+ name : "Consul Client Error" ,
370+ serviceID : "test-service" ,
371+ mockServicesErr : fmt .Errorf ("consul client error" ),
372+ expectErr : true ,
373+ expectService : nil ,
374+ },
375+ }
376+
377+ for _ , tt := range tests {
378+ t .Run (tt .name , func (t * testing.T ) {
379+ mockConsulClient := & MockConsulClient {
380+ MockAgent : & MockAgent {
381+ ServicesFunc : func () (map [string ]* api.AgentService , error ) {
382+ return tt .mockServicesData , tt .mockServicesErr
383+ },
384+ },
385+ }
386+ tagit := New (mockConsulClient , nil , tt .serviceID , "" , 0 , "" )
387+
388+ service , err := tagit .getService ()
389+
390+ if tt .expectErr && err == nil {
391+ t .Errorf ("Expected an error but got none" )
392+ } else if ! tt .expectErr && err != nil {
393+ t .Errorf ("Did not expect an error but got: %v" , err )
394+ }
395+
396+ if ! reflect .DeepEqual (service , tt .expectService ) {
397+ t .Errorf ("Expected service: %v, got: %v" , tt .expectService , service )
398+ }
399+ })
400+ }
401+ }
402+
403+ func TestGenerateNewTags (t * testing.T ) {
404+ tests := []struct {
405+ name string
406+ script string
407+ mockOutput string
408+ mockError error
409+ want []string
410+ wantErr bool
411+ }{
412+ {
413+ name : "Valid Script Output" ,
414+ script : "echo tag1 tag2" ,
415+ mockOutput : "tag1 tag2" ,
416+ want : []string {"tag-tag1" , "tag-tag2" },
417+ wantErr : false ,
418+ },
419+ {
420+ name : "Script Execution Error" ,
421+ script : "someinvalidcommand" ,
422+ mockError : fmt .Errorf ("command failed" ),
423+ want : nil ,
424+ wantErr : true ,
425+ },
426+ }
427+
428+ for _ , tt := range tests {
429+ t .Run (tt .name , func (t * testing.T ) {
430+ mockExecutor := & MockCommandExecutor {
431+ MockOutput : []byte (tt .mockOutput ),
432+ MockError : tt .mockError ,
433+ }
434+ tagit := TagIt {Script : tt .script , commandExecutor : mockExecutor , TagPrefix : "tag" }
435+
436+ got , err := tagit .generateNewTags ()
437+ if (err != nil ) != tt .wantErr {
438+ t .Fatalf ("generateNewTags() error = %v, wantErr %v" , err , tt .wantErr )
439+ }
440+ if ! reflect .DeepEqual (got , tt .want ) {
441+ t .Errorf ("generateNewTags() got = %v, want %v" , got , tt .want )
442+ }
443+ })
444+ }
445+ }
446+
447+ func TestUpdateConsulService (t * testing.T ) {
448+ tests := []struct {
449+ name string
450+ existingTags []string
451+ newTags []string
452+ mockRegisterErr error
453+ expectUpdate bool
454+ expectErr bool
455+ }{
456+ {
457+ name : "Update Needed" ,
458+ existingTags : []string {"tag1" , "tag2" },
459+ newTags : []string {"tag1" , "tag3" },
460+ expectUpdate : true ,
461+ expectErr : false ,
462+ },
463+ {
464+ name : "No Update Needed" ,
465+ existingTags : []string {"tag1" , "tag2" },
466+ newTags : []string {"tag1" , "tag2" },
467+ expectUpdate : false ,
468+ expectErr : false ,
469+ },
470+ {
471+ name : "Consul Register Error" ,
472+ existingTags : []string {"tag1" , "tag2" },
473+ newTags : []string {"tag1" , "tag3" },
474+ mockRegisterErr : fmt .Errorf ("consul error" ),
475+ expectUpdate : true ,
476+ expectErr : true ,
477+ },
478+ }
479+
480+ for _ , tt := range tests {
481+ t .Run (tt .name , func (t * testing.T ) {
482+ service := & api.AgentService {Tags : tt .existingTags }
483+ mockConsulClient := & MockConsulClient {
484+ MockAgent : & MockAgent {
485+ ServiceRegisterFunc : func (reg * api.AgentServiceRegistration ) error {
486+ return tt .mockRegisterErr
487+ },
488+ },
489+ }
490+ tagit := TagIt {client : mockConsulClient }
491+
492+ err := tagit .updateConsulService (service , tt .newTags )
493+ if (err != nil ) != tt .expectErr {
494+ t .Fatalf ("updateConsulService() error = %v, wantErr %v" , err , tt .expectErr )
495+ }
496+ })
497+ }
498+ }
0 commit comments