Skip to content

Commit 1e03333

Browse files
committed
fix possible nil pointer issue // extend unit tests
1 parent c43c354 commit 1e03333

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

internal/pkg/services/logs/utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ package utils
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/stackitcloud/stackit-sdk-go/services/logs"
89
)
910

11+
var (
12+
ErrResponseNil = errors.New("response is nil")
13+
ErrNameNil = errors.New("display name is nil")
14+
)
15+
1016
type LogsClient interface {
1117
GetLogsInstanceExecute(ctx context.Context, projectId, regionId, instanceId string) (*logs.LogsInstance, error)
1218
}
@@ -15,6 +21,10 @@ func GetInstanceName(ctx context.Context, apiClient LogsClient, projectId, regio
1521
resp, err := apiClient.GetLogsInstanceExecute(ctx, projectId, regionId, instanceId)
1622
if err != nil {
1723
return "", fmt.Errorf("get Logs instance: %w", err)
24+
} else if resp == nil {
25+
return "", ErrResponseNil
26+
} else if resp.DisplayName == nil {
27+
return "", ErrNameNil
1828
}
1929
return *resp.DisplayName, nil
2030
}

internal/pkg/services/logs/utils/utils_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ func TestGetInstanceName(t *testing.T) {
5454
getInstanceFails: true,
5555
isValid: false,
5656
},
57+
{
58+
description: "response is nil",
59+
getInstanceFails: false,
60+
getInstanceResp: nil,
61+
isValid: false,
62+
},
63+
{
64+
description: "name in response is nil",
65+
getInstanceFails: false,
66+
getInstanceResp: &logs.LogsInstance{
67+
DisplayName: nil,
68+
},
69+
isValid: false,
70+
},
5771
}
5872

5973
for _, tt := range tests {

0 commit comments

Comments
 (0)