66 "reflect"
77 "testing"
88
9+ "github.com/google/uuid"
910 "github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1011 "github.com/stackitcloud/stackit-sdk-go/services/iaas"
1112)
@@ -25,6 +26,8 @@ type IaaSClientMocked struct {
2526 GetAttachedProjectsResp * iaas.ProjectListResponse
2627 GetNetworkAreaRangeFails bool
2728 GetNetworkAreaRangeResp * iaas.NetworkRange
29+ GetSecurityGroupFails bool
30+ ApiGetSecurityGroupResp * iaas.SecurityGroup
2831}
2932
3033func (m * IaaSClientMocked ) GetPublicIPExecute (_ context.Context , _ , _ string ) (* iaas.PublicIp , error ) {
@@ -76,6 +79,13 @@ func (m *IaaSClientMocked) GetNetworkAreaRangeExecute(_ context.Context, _, _, _
7679 return m .GetNetworkAreaRangeResp , nil
7780}
7881
82+ func (m * IaaSClientMocked ) GetSecurityGroupExecute (ctx context.Context , projectId string , securityGroupId string ) (* iaas.SecurityGroup , error ) {
83+ if m .GetSecurityGroupFails {
84+ return nil , fmt .Errorf ("could not get security group" )
85+ }
86+ return m .ApiGetSecurityGroupResp , nil
87+ }
88+
7989func TestGetPublicIp (t * testing.T ) {
8090 type args struct {
8191 getPublicIpFails bool
@@ -551,3 +561,53 @@ func TestGetNetworkRangeFromAPIResponse(t *testing.T) {
551561 })
552562 }
553563}
564+
565+ func TestSecurityGetGroupName (t * testing.T ) {
566+ testProjectId := uuid .NewString ()
567+ type args struct {
568+ securityGroupId string
569+ }
570+ tests := []struct {
571+ name string
572+ args args
573+ getGroupResp * iaas.SecurityGroup
574+ want string
575+ wantErr bool
576+ }{
577+ {
578+ name : "base" ,
579+ args : args {
580+ securityGroupId : uuid .NewString (),
581+ },
582+ getGroupResp : & iaas.SecurityGroup {
583+ Name : utils .Ptr ("test-group" ),
584+ },
585+ want : "test-group" ,
586+ wantErr : false ,
587+ },
588+ {
589+ name : "get fails" ,
590+ args : args {
591+ securityGroupId : uuid .NewString (),
592+ },
593+ wantErr : true ,
594+ },
595+ }
596+ for _ , tt := range tests {
597+ t .Run (tt .name , func (t * testing.T ) {
598+ m := & IaaSClientMocked {
599+ GetSecurityGroupFails : tt .wantErr ,
600+ ApiGetSecurityGroupResp : tt .getGroupResp ,
601+ }
602+
603+ got , err := GetSecurityGroupName (context .Background (), m , testProjectId , tt .args .securityGroupId )
604+ if (err != nil ) != tt .wantErr {
605+ t .Errorf ("GetGroupName() error = %v, wantErr %v" , err , tt .wantErr )
606+ return
607+ }
608+ if got != tt .want {
609+ t .Errorf ("GetGroupName() = %v, want %v" , got , tt .want )
610+ }
611+ })
612+ }
613+ }
0 commit comments