Skip to content

Commit ae86056

Browse files
author
Alexandre Catalan
committed
fix(registry): add nil check and unit tests for GetArtifactoryName
1 parent a7a41c4 commit ae86056

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎internal/pkg/services/registry/utils/utils.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ func GetArtifactoryName(ctx context.Context, apiClient registry.DefaultAPI, proj
1212
if err != nil {
1313
return "", fmt.Errorf("get artifactory: %w", err)
1414
}
15+
if resp == nil {
16+
return "", fmt.Errorf("get artifactory: empty response")
17+
}
1518
return resp.Name, nil
1619
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/google/uuid"
9+
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
10+
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
12+
)
13+
14+
var (
15+
testProjectId = uuid.NewString()
16+
testArtifactoryId = uuid.NewString()
17+
)
18+
19+
const (
20+
testRegion = "eu01"
21+
testArtifactoryName = "my-artifactory"
22+
)
23+
24+
type registryClientMocked struct {
25+
getArtifactoryFails bool
26+
getArtifactoryResp *registry.Artifactory
27+
}
28+
29+
func (m *registryClientMocked) newMock() registry.DefaultAPI {
30+
return registry.DefaultAPIServiceMock{
31+
GetArtifactoryExecuteMock: utils.Ptr(func(_ registry.ApiGetArtifactoryRequest) (*registry.Artifactory, error) {
32+
if m.getArtifactoryFails {
33+
return nil, fmt.Errorf("could not get artifactory")
34+
}
35+
return m.getArtifactoryResp, nil
36+
}),
37+
}
38+
}
39+
40+
func TestGetArtifactoryName(t *testing.T) {
41+
tests := []struct {
42+
description string
43+
getArtifactoryFails bool
44+
getArtifactoryResp *registry.Artifactory
45+
isValid bool
46+
expectedOutput string
47+
}{
48+
{
49+
description: "base",
50+
getArtifactoryResp: &registry.Artifactory{
51+
Name: testArtifactoryName,
52+
},
53+
isValid: true,
54+
expectedOutput: testArtifactoryName,
55+
},
56+
{
57+
description: "get artifactory fails",
58+
getArtifactoryFails: true,
59+
isValid: false,
60+
},
61+
{
62+
description: "empty response (nil)",
63+
getArtifactoryResp: nil,
64+
isValid: false,
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.description, func(t *testing.T) {
70+
client := &registryClientMocked{
71+
getArtifactoryFails: tt.getArtifactoryFails,
72+
getArtifactoryResp: tt.getArtifactoryResp,
73+
}
74+
75+
output, err := GetArtifactoryName(context.Background(), client.newMock(), testProjectId, testRegion, testArtifactoryId)
76+
77+
if tt.isValid && err != nil {
78+
t.Errorf("failed on valid input: %v", err)
79+
}
80+
if !tt.isValid && err == nil {
81+
t.Errorf("did not fail on invalid input")
82+
}
83+
if tt.isValid && output != tt.expectedOutput {
84+
t.Errorf("expected output %q, got %q", tt.expectedOutput, output)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)