|
| 1 | +// Copyright 2026 NetApp, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +package gcp |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + tridentconfig "github.com/netapp/trident/config" |
| 13 | + "github.com/netapp/trident/storage_drivers/gcp/api" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGCPCommon_DefaultCreateTimeout(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + ctx tridentconfig.DriverContext |
| 20 | + expected time.Duration |
| 21 | + }{ |
| 22 | + {"Docker", tridentconfig.ContextDocker, tridentconfig.DockerCreateTimeout}, |
| 23 | + {"CSI", tridentconfig.ContextCSI, api.VolumeCreateTimeout}, |
| 24 | + {"Empty", tridentconfig.DriverContext(""), api.VolumeCreateTimeout}, |
| 25 | + {"Unknown", tridentconfig.DriverContext("k8s"), api.VolumeCreateTimeout}, |
| 26 | + } |
| 27 | + for _, tt := range tests { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + got := DefaultCreateTimeout(tt.ctx) |
| 30 | + assert.Equal(t, tt.expected, got) |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestGCPCommon_FixGCPLabelKey(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + in string |
| 39 | + wantOut string |
| 40 | + wantOK bool |
| 41 | + }{ |
| 42 | + {"Empty", "", "", false}, |
| 43 | + {"Valid lowercase", "valid_key", "valid_key", true}, |
| 44 | + {"Uppercase normalized", "ValidKey", "validkey", true}, |
| 45 | + {"Starts with digit", "1key", "", false}, |
| 46 | + {"Starts with hyphen", "-key", "", false}, |
| 47 | + {"Disallowed chars replaced", "key.with.dots", "key_with_dots", true}, |
| 48 | + {"Spaces to underscores", "key with spaces", "key_with_spaces", true}, |
| 49 | + {"Unicode letter allowed", "këy", "këy", true}, |
| 50 | + {"Multibyte lowercase first rune", "émoji", "émoji", true}, |
| 51 | + {"Too long truncated", string(make([]byte, 70)), "", false}, // all zeros, first rune not lower |
| 52 | + } |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + gotOut, gotOK := FixGCPLabelKey(tt.in) |
| 56 | + assert.Equal(t, tt.wantOK, gotOK, "ok") |
| 57 | + if tt.wantOK { |
| 58 | + assert.Equal(t, tt.wantOut, gotOut) |
| 59 | + assert.True(t, len(gotOut) <= api.MaxLabelLength, "length <= MaxLabelLength") |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | + // Key that is long but valid (starts with letter) gets truncated to 63 |
| 64 | + longKey := "a" + string(make([]byte, api.MaxLabelLength+10)) |
| 65 | + out, ok := FixGCPLabelKey(longKey) |
| 66 | + assert.True(t, ok) |
| 67 | + assert.Equal(t, api.MaxLabelLength, len(out)) |
| 68 | +} |
| 69 | + |
| 70 | +func TestGCPCommon_FixGCPLabelValue(t *testing.T) { |
| 71 | + tests := []struct { |
| 72 | + name string |
| 73 | + in string |
| 74 | + wantOut string |
| 75 | + }{ |
| 76 | + {"Empty", "", ""}, |
| 77 | + {"Valid", "value-123", "value-123"}, |
| 78 | + {"Uppercase normalized", "Value", "value"}, |
| 79 | + {"Dots replaced", "v.a.l", "v_a_l"}, |
| 80 | + {"Spaces replaced", "a b c", "a_b_c"}, |
| 81 | + } |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + got := FixGCPLabelValue(tt.in) |
| 85 | + assert.Equal(t, tt.wantOut, got) |
| 86 | + if len(got) > 0 { |
| 87 | + assert.True(t, len(got) <= api.MaxLabelLength, "length <= MaxLabelLength") |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + // Long value truncated |
| 92 | + longVal := "v" + string(make([]byte, api.MaxLabelLength+5)) |
| 93 | + got := FixGCPLabelValue(longVal) |
| 94 | + assert.Equal(t, api.MaxLabelLength, len(got)) |
| 95 | +} |
| 96 | + |
| 97 | +func TestGCPCommon_ErrRefreshGCNVResourceCache(t *testing.T) { |
| 98 | + err := errors.New("connection refused") |
| 99 | + wrapped := ErrRefreshGCNVResourceCache(err) |
| 100 | + assert.Error(t, wrapped) |
| 101 | + assert.Contains(t, wrapped.Error(), "could not update GCNV resource cache") |
| 102 | + assert.Contains(t, wrapped.Error(), "connection refused") |
| 103 | +} |
0 commit comments