Skip to content

Commit 76d91fb

Browse files
committed
fix(cdn) review fixes
- test Min - test JoinStringMap - rm superfluous var for constant - rm file committed by accident - add nil checks when dereferencing pointers
1 parent 90f05e9 commit 76d91fb

File tree

5 files changed

+90
-12
lines changed

5 files changed

+90
-12
lines changed

internal/cmd/beta/cdn/distribution/create/create_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"k8s.io/utils/ptr"
2121
)
2222

23-
var projectIdFlag = globalflags.ProjectIdFlag
24-
2523
type testCtxKey struct{}
2624

2725
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -32,8 +30,8 @@ const testRegions = cdn.REGION_EU
3230

3331
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
3432
flagValues := map[string]string{
35-
projectIdFlag: testProjectId,
36-
flagRegion: string(testRegions),
33+
globalflags.ProjectIdFlag: testProjectId,
34+
flagRegion: string(testRegions),
3735
}
3836
flagsHTTPBackend()(flagValues)
3937
for _, mod := range mods {
@@ -202,21 +200,21 @@ func TestParseInput(t *testing.T) {
202200
{
203201
description: "project id missing",
204202
flagValues: fixtureFlagValues(func(m map[string]string) {
205-
delete(m, projectIdFlag)
203+
delete(m, globalflags.ProjectIdFlag)
206204
}),
207205
isValid: false,
208206
},
209207
{
210208
description: "project id invalid 1",
211209
flagValues: fixtureFlagValues(func(m map[string]string) {
212-
m[projectIdFlag] = ""
210+
m[globalflags.ProjectIdFlag] = ""
213211
}),
214212
isValid: false,
215213
},
216214
{
217215
description: "project id invalid 2",
218216
flagValues: fixtureFlagValues(func(m map[string]string) {
219-
m[projectIdFlag] = "invalid-uuid"
217+
m[globalflags.ProjectIdFlag] = "invalid-uuid"
220218
}),
221219
isValid: false,
222220
},

internal/cmd/beta/cdn/distribution/describe/describe.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,19 @@ func buildDistributionTable(d *cdn.Distribution) tables.Table {
167167
} else if d.Config.Backend.HttpBackend != nil {
168168
h := d.Config.Backend.HttpBackend
169169
var geofencing []string
170-
for k, v := range *h.Geofencing {
171-
geofencing = append(geofencing, fmt.Sprintf("%s: %s", k, strings.Join(v, ", ")))
170+
if h.Geofencing != nil {
171+
for k, v := range *h.Geofencing {
172+
geofencing = append(geofencing, fmt.Sprintf("%s: %s", k, strings.Join(v, ", ")))
173+
}
172174
}
173175
table.AddRow("BACKEND TYPE", "HTTP")
174176
table.AddSeparator()
175177
table.AddRow("HTTP ORIGIN URL", utils.PtrString(h.OriginUrl))
176178
table.AddSeparator()
177-
table.AddRow("HTTP ORIGIN REQUEST HEADERS", utils.JoinStringMap(*h.OriginRequestHeaders, ": ", ", "))
178-
table.AddSeparator()
179+
if h.OriginRequestHeaders != nil {
180+
table.AddRow("HTTP ORIGIN REQUEST HEADERS", utils.JoinStringMap(*h.OriginRequestHeaders, ": ", ", "))
181+
table.AddSeparator()
182+
}
179183
table.AddRow("HTTP GEOFENCING PROPERTIES", strings.Join(geofencing, "\n"))
180184
table.AddSeparator()
181185
}

internal/cmd/beta/cdn/domain/describe/describe.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/pkg/utils/strings_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,39 @@ func TestTruncate(t *testing.T) {
3030
})
3131
}
3232
}
33+
34+
func TestJoinStringMap(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
m map[string]string
38+
want string
39+
}{
40+
{
41+
name: "nil map",
42+
m: nil,
43+
want: "",
44+
},
45+
{
46+
name: "empty map",
47+
m: map[string]string{},
48+
want: "",
49+
},
50+
{
51+
name: "single element",
52+
m: map[string]string{"key1": "value1"},
53+
want: "key1=value1",
54+
},
55+
{
56+
name: "multiple elements",
57+
m: map[string]string{"key1": "value1", "key2": "value2"},
58+
want: "key1=value1, key2=value2",
59+
},
60+
}
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
if got := JoinStringMap(tt.m, "=", ", "); got != tt.want {
64+
t.Errorf("JoinStringMap() = %v, want %v", got, tt.want)
65+
}
66+
})
67+
}
68+
}

internal/pkg/utils/utils_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,3 +590,44 @@ func TestGetSliceFromPointer(t *testing.T) {
590590
})
591591
}
592592
}
593+
594+
func TestMin(t *testing.T) {
595+
tests := []struct {
596+
name string
597+
a, b int
598+
want int
599+
}{
600+
{
601+
name: "min(0, 0) = 0",
602+
a: 0,
603+
b: 0,
604+
want: 0,
605+
},
606+
{
607+
name: "min(1, 2) = 1",
608+
a: 1,
609+
b: 2,
610+
want: 1,
611+
},
612+
{
613+
name: "min(2, 1) = 1",
614+
a: 2,
615+
b: 1,
616+
want: 1,
617+
},
618+
{
619+
name: "min(-1, 1) = -1",
620+
a: -1,
621+
b: 1,
622+
want: -1,
623+
},
624+
}
625+
for _, tt := range tests {
626+
t.Run(tt.name, func(t *testing.T) {
627+
if got := Min(tt.a, tt.b); got != tt.want {
628+
t.Errorf("Min() = %v, want %v", got, tt.want)
629+
}
630+
})
631+
}
632+
633+
}

0 commit comments

Comments
 (0)