Skip to content

Commit 334d287

Browse files
author
Alexandre Catalan
committed
test(registry): add comprehensive edge case coverage across commands
- Add TestFetchArtifactories unit tests covering pagination, limit, and error handling - Add test cases for proxy access secret without key and empty flags in create - Add test case for proxy without description in describe output - Add test case for nil model in create and update outputResult - Add JSON output format test case in replication-adapter list Relates to STACKITCRE-382
1 parent 5569dd2 commit 334d287

5 files changed

Lines changed: 189 additions & 1 deletion

File tree

‎internal/cmd/registry/artifactory/create/create_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ func TestParseInput(t *testing.T) {
174174
}),
175175
isValid: false,
176176
},
177+
{
178+
description: "proxy access secret without key",
179+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
180+
flagValues[proxyUrlFlag] = testProxyUrl
181+
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
182+
flagValues[proxyAccessSecretFlag] = "@" + testSecretFile
183+
}),
184+
isValid: false,
185+
},
186+
{
187+
description: "no values",
188+
flagValues: map[string]string{},
189+
isValid: false,
190+
},
177191
{
178192
description: "proxy description without proxy url",
179193
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
@@ -235,6 +249,21 @@ func TestBuildRequest(t *testing.T) {
235249
model: fixtureInputModel(),
236250
expectedRequest: fixtureRequest(),
237251
},
252+
{
253+
description: "with proxy minimal",
254+
model: fixtureInputModel(func(model *inputModel) {
255+
model.ProxyUrl = utils.Ptr(testProxyUrl)
256+
model.ProxyRegistryType = utils.Ptr(testProxyRegistryType)
257+
}),
258+
expectedRequest: testClient.DefaultAPI.CreateArtifactory(testCtx, testProjectId, testRegion).
259+
CreateArtifactoryPayload(registry.CreateArtifactoryPayload{
260+
Name: testName,
261+
Proxy: &registry.CreateProxyRequest{
262+
Url: testProxyUrl,
263+
RegistryType: testProxyRegistryType,
264+
},
265+
}),
266+
},
238267
{
239268
description: "with proxy full",
240269
model: fixtureInputModel(func(model *inputModel) {
@@ -287,6 +316,14 @@ func TestOutputResult(t *testing.T) {
287316
args args
288317
wantErr bool
289318
}{
319+
{
320+
name: "nil model",
321+
args: args{
322+
model: nil,
323+
resp: &registry.Artifactory{Id: "xxx"},
324+
},
325+
wantErr: true,
326+
},
290327
{
291328
name: "empty response",
292329
args: args{

‎internal/cmd/registry/artifactory/describe/describe_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ func TestOutputResult(t *testing.T) {
212212
},
213213
wantErr: false,
214214
},
215+
{
216+
name: "with proxy without description",
217+
args: args{
218+
artifactory: &registry.Artifactory{
219+
Id: testArtifactoryId,
220+
Name: "my-artifactory",
221+
State: registry.ARTIFACTORYSTATE_ACTIVE,
222+
Url: "https://example.com",
223+
ConsumedQuotaBytes: 1024,
224+
CreateTime: time.Now(),
225+
Proxy: &registry.Proxy{
226+
Url: "https://registry-1.docker.io",
227+
Description: nil,
228+
RegistryType: "docker-hub",
229+
},
230+
},
231+
},
232+
wantErr: false,
233+
},
215234
{
216235
name: "nil artifactory",
217236
args: args{

‎internal/cmd/registry/artifactory/list/list_test.go‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package list
22

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

78
"github.com/google/go-cmp/cmp"
@@ -216,3 +217,108 @@ func TestOutputResult(t *testing.T) {
216217
})
217218
}
218219
}
220+
221+
func TestFetchArtifactories(t *testing.T) {
222+
tests := []struct {
223+
description string
224+
model *inputModel
225+
mockResponses []*registry.ArtifactoryList
226+
mockErrors []error
227+
expectedItems int
228+
wantErr bool
229+
}{
230+
{
231+
description: "single page, no limit",
232+
model: fixtureInputModel(),
233+
mockResponses: []*registry.ArtifactoryList{
234+
{
235+
Artifactories: []registry.Artifactory{
236+
{Id: "1", Name: "reg-1"},
237+
{Id: "2", Name: "reg-2"},
238+
},
239+
},
240+
},
241+
expectedItems: 2,
242+
wantErr: false,
243+
},
244+
{
245+
description: "multi page with pagination",
246+
model: fixtureInputModel(),
247+
mockResponses: []*registry.ArtifactoryList{
248+
{
249+
Artifactories: []registry.Artifactory{
250+
{Id: "1", Name: "reg-1"},
251+
},
252+
NextPageToken: utils.Ptr("token-1"),
253+
},
254+
{
255+
Artifactories: []registry.Artifactory{
256+
{Id: "2", Name: "reg-2"},
257+
},
258+
},
259+
},
260+
expectedItems: 2,
261+
wantErr: false,
262+
},
263+
{
264+
description: "with limit smaller than available",
265+
model: fixtureInputModel(func(model *inputModel) {
266+
model.Limit = utils.Ptr(int64(1))
267+
}),
268+
mockResponses: []*registry.ArtifactoryList{
269+
{
270+
Artifactories: []registry.Artifactory{
271+
{Id: "1", Name: "reg-1"},
272+
{Id: "2", Name: "reg-2"},
273+
},
274+
},
275+
},
276+
expectedItems: 1,
277+
wantErr: false,
278+
},
279+
{
280+
description: "api call fails",
281+
model: fixtureInputModel(),
282+
mockErrors: []error{
283+
fmt.Errorf("server error"),
284+
},
285+
wantErr: true,
286+
},
287+
{
288+
description: "nil response",
289+
model: fixtureInputModel(),
290+
mockResponses: []*registry.ArtifactoryList{
291+
nil,
292+
},
293+
wantErr: true,
294+
},
295+
}
296+
297+
for _, tt := range tests {
298+
t.Run(tt.description, func(t *testing.T) {
299+
callIndex := 0
300+
mock := registry.DefaultAPIServiceMock{
301+
ListArtifactoriesExecuteMock: utils.Ptr(func(_ registry.ApiListArtifactoriesRequest) (*registry.ArtifactoryList, error) {
302+
idx := callIndex
303+
callIndex++
304+
if idx < len(tt.mockErrors) && tt.mockErrors[idx] != nil {
305+
return nil, tt.mockErrors[idx]
306+
}
307+
if idx < len(tt.mockResponses) {
308+
return tt.mockResponses[idx], nil
309+
}
310+
return &registry.ArtifactoryList{}, nil
311+
}),
312+
}
313+
client := &registry.APIClient{DefaultAPI: mock}
314+
315+
got, err := fetchArtifactories(testCtx, tt.model, client)
316+
if (err != nil) != tt.wantErr {
317+
t.Fatalf("fetchArtifactories() error = %v, wantErr %v", err, tt.wantErr)
318+
}
319+
if !tt.wantErr && len(got) != tt.expectedItems {
320+
t.Errorf("fetchArtifactories() returned %d items, want %d", len(got), tt.expectedItems)
321+
}
322+
})
323+
}
324+
}

‎internal/cmd/registry/artifactory/update/update_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ func TestOutputResult(t *testing.T) {
315315
args args
316316
wantErr bool
317317
}{
318+
{
319+
name: "nil model",
320+
args: args{
321+
model: nil,
322+
artifactoryLabel: "my-artifactory",
323+
resp: &registry.Artifactory{Id: testArtifactoryId},
324+
},
325+
wantErr: true,
326+
},
318327
{
319328
name: "updated successfully sync",
320329
args: args{

‎internal/cmd/registry/replication-adapter/list/list_test.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
1111

1212
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -163,6 +164,7 @@ func TestBuildRequest(t *testing.T) {
163164

164165
func TestOutputResult(t *testing.T) {
165166
type args struct {
167+
outputFormat string
166168
projectLabel string
167169
adapters []registry.ReplicationAdapter
168170
}
@@ -212,13 +214,28 @@ func TestOutputResult(t *testing.T) {
212214
},
213215
wantErr: false,
214216
},
217+
{
218+
name: "json output format",
219+
args: args{
220+
outputFormat: print.JSONOutputFormat,
221+
projectLabel: "my-project",
222+
adapters: []registry.ReplicationAdapter{
223+
{
224+
RegistryType: "docker-hub",
225+
DisplayName: "Docker Hub",
226+
CredentialType: registry.REPLICATIONADAPTERCREDENTIALTYPE_BASIC,
227+
},
228+
},
229+
},
230+
wantErr: false,
231+
},
215232
}
216233

217234
params := testparams.NewTestParams()
218235

219236
for _, tt := range tests {
220237
t.Run(tt.name, func(t *testing.T) {
221-
err := outputResult(params.Printer, "", tt.args.projectLabel, tt.args.adapters)
238+
err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.adapters)
222239
if (err != nil) != tt.wantErr {
223240
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
224241
}

0 commit comments

Comments
 (0)