|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package util |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | +) |
| 22 | + |
| 23 | +func TestParseEndpoint(t *testing.T) { |
| 24 | + testCases := []struct { |
| 25 | + name string |
| 26 | + endpoint string |
| 27 | + expScheme string |
| 28 | + expAddr string |
| 29 | + expErr error |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "valid unix endpoint 1", |
| 33 | + endpoint: "unix:///csi/csi.sock", |
| 34 | + expScheme: "unix", |
| 35 | + expAddr: "/csi/csi.sock", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "valid unix endpoint 2", |
| 39 | + endpoint: "unix://csi/csi.sock", |
| 40 | + expScheme: "unix", |
| 41 | + expAddr: "/csi/csi.sock", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "valid unix endpoint 3", |
| 45 | + endpoint: "unix:/csi/csi.sock", |
| 46 | + expScheme: "unix", |
| 47 | + expAddr: "/csi/csi.sock", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "valid tcp endpoint", |
| 51 | + endpoint: "tcp:///127.0.0.1/", |
| 52 | + expScheme: "tcp", |
| 53 | + expAddr: "/127.0.0.1", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "valid tcp endpoint", |
| 57 | + endpoint: "tcp:///127.0.0.1", |
| 58 | + expScheme: "tcp", |
| 59 | + expAddr: "/127.0.0.1", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "invalid endpoint", |
| 63 | + endpoint: "http://127.0.0.1", |
| 64 | + expErr: fmt.Errorf("unsupported protocol: http"), |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range testCases { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + scheme, addr, err := ParseEndpoint(tc.endpoint) |
| 71 | + |
| 72 | + if tc.expErr != nil { |
| 73 | + assert.EqualError(t, err, tc.expErr.Error()) |
| 74 | + } else { |
| 75 | + assert.Nil(t, err) |
| 76 | + assert.Equal(t, scheme, tc.expScheme, "scheme mismatches") |
| 77 | + assert.Equal(t, addr, tc.expAddr, "address mismatches") |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments