| 1 |
/* |
| 2 |
Copyright 2022 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package util |
| 18 |
|
| 19 |
import ( |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestParseEndpoint(t *testing.T) { |
| 25 |
testCases := []struct { |
| 26 |
desc string |
| 27 |
endpoint string |
| 28 |
want string |
| 29 |
}{ |
| 30 |
{ |
| 31 |
desc: "path with prefix", |
| 32 |
endpoint: "unix:///@path", |
| 33 |
want: "@path", |
| 34 |
}, |
| 35 |
{ |
| 36 |
desc: "path without prefix", |
| 37 |
endpoint: "unix:///path", |
| 38 |
want: "/path", |
| 39 |
}, |
| 40 |
} |
| 41 |
|
| 42 |
for _, tt := range testCases { |
| 43 |
t.Run(tt.desc, func(t *testing.T) { |
| 44 |
got, err := ParseEndpoint(tt.endpoint) |
| 45 |
if err != nil { |
| 46 |
t.Errorf("ParseEndpoint(%q) error: %v", tt.endpoint, err) |
| 47 |
} |
| 48 |
if got != tt.want { |
| 49 |
t.Errorf("ParseEndpoint(%q) = %q, want %q", tt.endpoint, got, tt.want) |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestParseEndpointError(t *testing.T) { |
| 56 |
testCases := []struct { |
| 57 |
desc string |
| 58 |
endpoint string |
| 59 |
wantErr string |
| 60 |
}{ |
| 61 |
{ |
| 62 |
desc: "empty endpoint", |
| 63 |
endpoint: "", |
| 64 |
wantErr: "remote KMS provider can't use empty string as endpoint", |
| 65 |
}, |
| 66 |
{ |
| 67 |
desc: "invalid scheme", |
| 68 |
endpoint: "http:///path", |
| 69 |
wantErr: "unsupported scheme \"http\" for remote KMS provider", |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
for _, tt := range testCases { |
| 74 |
t.Run(tt.desc, func(t *testing.T) { |
| 75 |
_, err := ParseEndpoint(tt.endpoint) |
| 76 |
if err == nil { |
| 77 |
t.Errorf("ParseEndpoint(%q) error: %v", tt.endpoint, err) |
| 78 |
} |
| 79 |
if !strings.Contains(err.Error(), tt.wantErr) { |
| 80 |
t.Errorf("ParseEndpoint(%q) = %q, want %q", tt.endpoint, err, tt.wantErr) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
} |