@@ -588,12 +588,13 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
588588 defer mockCtrl .Finish ()
589589
590590 tests := []struct {
591- name string
592- incoming * expinfrav1.AWSLaunchTemplate
593- existing * expinfrav1.AWSLaunchTemplate
594- expect func (m * mocks.MockEC2APIMockRecorder )
595- want bool
596- wantErr bool
591+ name string
592+ incoming * expinfrav1.AWSLaunchTemplate
593+ existing * expinfrav1.AWSLaunchTemplate
594+ expect func (m * mocks.MockEC2APIMockRecorder )
595+ want bool
596+ wantNeedsUpdateReason string
597+ wantErr bool
597598 }{
598599 {
599600 name : "only core security groups, order shouldn't matter" ,
@@ -639,8 +640,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
639640 {ID : aws .String ("sg-999" )},
640641 },
641642 },
642- want : true ,
643- wantErr : false ,
643+ want : true ,
644+ wantNeedsUpdateReason : "AdditionalSecurityGroupsIDs" ,
645+ wantErr : false ,
644646 },
645647 {
646648 name : "new additional security group" ,
@@ -657,8 +659,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
657659 {ID : aws .String ("sg-999" )},
658660 },
659661 },
660- want : true ,
661- wantErr : false ,
662+ want : true ,
663+ wantNeedsUpdateReason : "AdditionalSecurityGroupsIDs" ,
664+ wantErr : false ,
662665 },
663666 {
664667 name : "Should return true if incoming IamInstanceProfile is not same as existing IamInstanceProfile" ,
@@ -672,7 +675,8 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
672675 },
673676 IamInstanceProfile : "some-other-profile" ,
674677 },
675- want : true ,
678+ want : true ,
679+ wantNeedsUpdateReason : "IamInstanceProfile" ,
676680 },
677681 {
678682 name : "Should return true if incoming InstanceType is not same as existing InstanceType" ,
@@ -686,7 +690,8 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
686690 },
687691 InstanceType : "t3.large" ,
688692 },
689- want : true ,
693+ want : true ,
694+ wantNeedsUpdateReason : "InstanceType" ,
690695 },
691696 {
692697 name : "new additional security group with filters" ,
@@ -706,8 +711,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
706711 m .DescribeSecurityGroups (context .TODO (), gomock .Eq (& ec2.DescribeSecurityGroupsInput {Filters : []ec2types.Filter {{Name : aws .String ("sg-2" ), Values : []string {"test-2" }}}})).
707712 Return (& ec2.DescribeSecurityGroupsOutput {SecurityGroups : []ec2types.SecurityGroup {{GroupId : aws .String ("sg-2" )}}}, nil )
708713 },
709- want : true ,
710- wantErr : false ,
714+ want : true ,
715+ wantNeedsUpdateReason : "AdditionalSecurityGroupsIDs" ,
716+ wantErr : false ,
711717 },
712718 {
713719 name : "new launch template instance metadata options, requiring IMDSv2" ,
@@ -723,8 +729,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
723729 {ID : aws .String ("sg-222" )},
724730 },
725731 },
726- want : true ,
727- wantErr : false ,
732+ want : true ,
733+ wantNeedsUpdateReason : "InstanceMetadataOptions" ,
734+ wantErr : false ,
728735 },
729736 {
730737 name : "new launch template instance metadata options, removing IMDSv2 requirement" ,
@@ -735,8 +742,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
735742 HTTPTokens : infrav1 .HTTPTokensStateRequired ,
736743 },
737744 },
738- want : true ,
739- wantErr : false ,
745+ want : true ,
746+ wantNeedsUpdateReason : "InstanceMetadataOptions" ,
747+ wantErr : false ,
740748 },
741749 {
742750 name : "Should return true if incoming SpotMarketOptions is different from existing SpotMarketOptions" ,
@@ -754,8 +762,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
754762 MaxPrice : aws .String ("0.05" ),
755763 },
756764 },
757- want : true ,
758- wantErr : false ,
765+ want : true ,
766+ wantNeedsUpdateReason : "SpotMarketOptions" ,
767+ wantErr : false ,
759768 },
760769 {
761770 name : "Should return true if incoming adds SpotMarketOptions and existing has none" ,
@@ -771,8 +780,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
771780 },
772781 SpotMarketOptions : nil ,
773782 },
774- want : true ,
775- wantErr : false ,
783+ want : true ,
784+ wantNeedsUpdateReason : "SpotMarketOptions" ,
785+ wantErr : false ,
776786 },
777787 {
778788 name : "Should return true if incoming removes SpotMarketOptions and existing has some" ,
@@ -788,8 +798,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
788798 MaxPrice : aws .String ("0.05" ),
789799 },
790800 },
791- want : true ,
792- wantErr : false ,
801+ want : true ,
802+ wantNeedsUpdateReason : "SpotMarketOptions" ,
803+ wantErr : false ,
793804 },
794805 {
795806 name : "Should return true if SSH key names are different" ,
@@ -803,8 +814,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
803814 },
804815 SSHKeyName : aws .String ("old-key" ),
805816 },
806- want : true ,
807- wantErr : false ,
817+ want : true ,
818+ wantNeedsUpdateReason : "SSHKeyName" ,
819+ wantErr : false ,
808820 },
809821 {
810822 name : "Should return true if one has SSH key name and other doesn't" ,
@@ -818,8 +830,25 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
818830 },
819831 SSHKeyName : nil ,
820832 },
821- want : true ,
822- wantErr : false ,
833+ want : true ,
834+ wantNeedsUpdateReason : "SSHKeyName" ,
835+ wantErr : false ,
836+ },
837+ {
838+ name : "Should return false if no SSH key is set in the spec and AWS returns no key pair as well" ,
839+ incoming : & expinfrav1.AWSLaunchTemplate {
840+ SSHKeyName : aws .String ("" ), // explicit empty string
841+ },
842+ existing : & expinfrav1.AWSLaunchTemplate {
843+ AdditionalSecurityGroups : []infrav1.AWSResourceReference {
844+ {ID : aws .String ("sg-111" )},
845+ {ID : aws .String ("sg-222" )},
846+ },
847+ SSHKeyName : nil ,
848+ },
849+ want : false ,
850+ wantNeedsUpdateReason : "" ,
851+ wantErr : false ,
823852 },
824853 {
825854 name : "Should return true if incoming PrivateDNSName is different from existing PrivateDNSName" ,
@@ -841,8 +870,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
841870 HostnameType : aws .String ("ip-name" ),
842871 },
843872 },
844- want : true ,
845- wantErr : false ,
873+ want : true ,
874+ wantNeedsUpdateReason : "PrivateDNSName" ,
875+ wantErr : false ,
846876 },
847877 {
848878 name : "Should return true if incoming adds PrivateDNSName and existing has none" ,
@@ -860,8 +890,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
860890 },
861891 PrivateDNSName : nil ,
862892 },
863- want : true ,
864- wantErr : false ,
893+ want : true ,
894+ wantNeedsUpdateReason : "PrivateDNSName" ,
895+ wantErr : false ,
865896 },
866897 {
867898 name : "Should return true if incoming removes PrivateDNSName and existing has some" ,
@@ -879,8 +910,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
879910 HostnameType : aws .String ("resource-name" ),
880911 },
881912 },
882- want : true ,
883- wantErr : false ,
913+ want : true ,
914+ wantNeedsUpdateReason : "PrivateDNSName" ,
915+ wantErr : false ,
884916 },
885917 {
886918 name : "Should return true if capacity reservation IDs are different" ,
@@ -894,8 +926,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
894926 },
895927 CapacityReservationID : aws .String ("old-reservation" ),
896928 },
897- want : true ,
898- wantErr : false ,
929+ want : true ,
930+ wantNeedsUpdateReason : "CapacityReservationID" ,
931+ wantErr : false ,
899932 },
900933 {
901934 name : "Should return true if one has capacity reservation ID and other doesn't" ,
@@ -909,8 +942,9 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
909942 },
910943 CapacityReservationID : nil ,
911944 },
912- want : true ,
913- wantErr : false ,
945+ want : true ,
946+ wantNeedsUpdateReason : "CapacityReservationID" ,
947+ wantErr : false ,
914948 },
915949 }
916950 for _ , tt := range tests {
@@ -948,12 +982,13 @@ func TestServiceLaunchTemplateNeedsUpdate(t *testing.T) {
948982 tt .expect (mockEC2Client .EXPECT ())
949983 }
950984
951- got , err := s .LaunchTemplateNeedsUpdate (machinePoolScope , tt .incoming , tt .existing )
985+ got , gotNeedsUpdateReason , err := s .LaunchTemplateNeedsUpdate (machinePoolScope , tt .incoming , tt .existing )
952986 if tt .wantErr {
953987 g .Expect (err ).To (HaveOccurred ())
954988 return
955989 }
956990 g .Expect (err ).NotTo (HaveOccurred ())
991+ g .Expect (gotNeedsUpdateReason ).Should (Equal (tt .wantNeedsUpdateReason ))
957992 g .Expect (got ).Should (Equal (tt .want ))
958993 })
959994 }
0 commit comments