Repository navigation
Expand file tree
/
Copy patherrors_message_test.go
More file actions
194 lines (169 loc) · 5.34 KB
/
Copy patherrors_message_test.go
File metadata and controls
194 lines (169 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2026 Appvia Ltd.
// SPDX-License-Identifier: Apache-2.0
package ipam
import (
"errors"
"net/netip"
"strings"
"testing"
"github.com/appvia/ipam/storage/azblob"
"github.com/appvia/ipam/storage/memory"
)
func TestOpErrorDoesNotRepeatPool(t *testing.T) {
t.Parallel()
tests := map[string]struct {
err *OpError
want string
}{
"create pool": {
err: &OpError{Op: "create pool", Pool: "X", Err: ErrConflict},
want: "create pool X: ipam: conflict",
},
"delete pool with reason": {
err: &OpError{Op: "delete pool", Pool: "prod", Reason: "pool_not_empty", Err: ErrConflict},
want: "delete pool prod (pool_not_empty): ipam: conflict",
},
"get pool": {
err: &OpError{Op: "get pool", Pool: "prod", Err: ErrNotFound},
want: "get pool prod: ipam: not found",
},
"allocate keeps the pool label": {
err: &OpError{Op: "allocate", Pool: "prod", ID: "a1", Err: ErrPoolExhausted},
want: "allocate pool prod allocation a1: ipam: pool exhausted",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
if got := tc.err.Error(); got != tc.want {
t.Fatalf("Error() = %q, want %q", got, tc.want)
}
if !errors.Is(tc.err, tc.err.Err) {
t.Fatalf("errors.Is lost the sentinel %v", tc.err.Err)
}
})
}
}
func TestOpenStorageAzblobErrorIsNotDoubled(t *testing.T) {
t.Setenv("AZURE_STORAGE_CONNECTION_STRING", "")
_, err := OpenStorage(WithAllowUnverifiedBackend(t.Context()), "azblob://mycontainer/prefix")
if err == nil {
t.Fatal("OpenStorage succeeded without a credential")
}
if !errors.Is(err, ErrInvalidArgument) || !errors.Is(err, azblob.ErrInvalidArgument) {
t.Fatalf("err %v lost ErrInvalidArgument or azblob.ErrInvalidArgument", err)
}
msg := err.Error()
if strings.Count(msg, "invalid argument") != 1 {
t.Fatalf("message %q repeats \"invalid argument\"", msg)
}
if !strings.HasPrefix(msg, "ipam: invalid argument: azblob: ") {
t.Fatalf("message %q does not read \"ipam: invalid argument: azblob: ...\"", msg)
}
}
func TestInvalidCIDRCarriesReason(t *testing.T) {
t.Parallel()
client := newTestClient(memory.New())
ctx := t.Context()
for _, spec := range []PoolSpec{
{ID: "prod", CIDRs: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")}},
{
ID: "multi",
CIDRs: []netip.Prefix{netip.MustParsePrefix("10.1.0.0/20"), netip.MustParsePrefix("10.0.0.0/16")},
},
{
ID: "v6multi",
CIDRs: []netip.Prefix{netip.MustParsePrefix("fd00::/48"), netip.MustParsePrefix("fd01::/40")},
},
} {
if _, err := client.CreatePool(ctx, spec); err != nil {
t.Fatal(err)
}
}
tests := map[string]struct {
pool string
spec AllocationSpec
want string
}{
"outside the pool": {
pool: "prod",
spec: AllocationSpec{CIDR: netip.MustParsePrefix("10.9.0.0/24")},
want: "cidr 10.9.0.0/24 is outside pool prod (10.0.0.0/16)",
},
"prefix shorter than the pool": {
pool: "prod",
spec: AllocationSpec{PrefixLength: 8},
want: "prefix length 8 is shorter than pool prod (/16)",
},
"prefix longer than the address": {
pool: "prod",
spec: AllocationSpec{PrefixLength: 33},
want: "prefix length 33 is longer than the /32 addresses of pool prod",
},
"multi-CIDR shorter names the largest block": {
pool: "multi",
spec: AllocationSpec{PrefixLength: 12},
want: "prefix length 12 is shorter than the largest block of pool multi (/16)",
},
"multi-CIDR outside lists every CIDR": {
pool: "multi",
spec: AllocationSpec{CIDR: netip.MustParsePrefix("10.2.0.0/24")},
want: "cidr 10.2.0.0/24 is outside pool multi (10.0.0.0/16, 10.1.0.0/20)",
},
"multi-CIDR longer": {
pool: "multi",
spec: AllocationSpec{PrefixLength: 33},
want: "prefix length 33 is longer than the /32 addresses of pool multi",
},
"IPv6 multi-CIDR shorter names the largest block": {
pool: "v6multi",
spec: AllocationSpec{PrefixLength: 32},
want: "prefix length 32 is shorter than the largest block of pool v6multi (/40)",
},
"IPv4 CIDR on an IPv6 pool is outside": {
pool: "v6multi",
spec: AllocationSpec{CIDR: netip.MustParsePrefix("10.0.0.0/24")},
want: "cidr 10.0.0.0/24 is outside pool v6multi (fd00::/48, fd01::/40)",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
_, err := client.Allocate(ctx, tc.pool, tc.spec)
if !errors.Is(err, ErrInvalidCIDR) {
t.Fatalf("err = %v, want ErrInvalidCIDR", err)
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("message %q lacks %q", err.Error(), tc.want)
}
})
}
}
func TestInvalidCIDRReasonOmitsPoolMetadata(t *testing.T) {
t.Parallel()
client := newTestClient(memory.New())
ctx := t.Context()
if _, err := client.CreatePool(ctx, PoolSpec{
ID: "tagged",
CIDRs: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16")},
Description: "DESCSECRET",
Labels: map[string]string{"owner": "LABELSECRET"},
}); err != nil {
t.Fatal(err)
}
for _, spec := range []AllocationSpec{
{CIDR: netip.MustParsePrefix("11.0.0.0/24")},
{PrefixLength: 8},
{PrefixLength: 33},
} {
_, err := client.Allocate(ctx, "tagged", spec)
if !errors.Is(err, ErrInvalidCIDR) {
t.Fatalf("err = %v, want ErrInvalidCIDR", err)
}
for _, leak := range []string{"owner", "LABELSECRET", "DESCSECRET"} {
if strings.Contains(err.Error(), leak) {
t.Fatalf("message %q leaks pool metadata %q", err.Error(), leak)
}
}
}
}