| 1 |
/* |
| 2 |
Copyright 2024 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 portforward |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/stretchr/testify/assert" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
"k8s.io/apimachinery/pkg/util/httpstream" |
| 27 |
) |
| 28 |
|
| 29 |
func TestFallbackDialer(t *testing.T) { |
| 30 |
primaryProtocol := "primary.fake.protocol" |
| 31 |
secondaryProtocol := "secondary.fake.protocol" |
| 32 |
protocols := []string{primaryProtocol, secondaryProtocol} |
| 33 |
// If primary dialer error is nil, then no fallback and primary negotiated protocol returned. |
| 34 |
primary := &fakeDialer{dialed: false, negotiatedProtocol: primaryProtocol} |
| 35 |
secondary := &fakeDialer{dialed: false, negotiatedProtocol: secondaryProtocol} |
| 36 |
fallbackDialer := NewFallbackDialer(primary, secondary, notCalled) |
| 37 |
_, negotiated, err := fallbackDialer.Dial(protocols...) |
| 38 |
assert.True(t, primary.dialed, "no fallback; primary should have dialed") |
| 39 |
assert.False(t, secondary.dialed, "no fallback; secondary should *not* have dialed") |
| 40 |
assert.Equal(t, primaryProtocol, negotiated, "primary negotiated protocol returned") |
| 41 |
require.NoError(t, err, "error from primary dialer should be nil") |
| 42 |
// If primary dialer error is upgrade error, then fallback returning secondary dial response. |
| 43 |
primary = &fakeDialer{dialed: false, negotiatedProtocol: primaryProtocol, err: &httpstream.UpgradeFailureError{}} |
| 44 |
secondary = &fakeDialer{dialed: false, negotiatedProtocol: secondaryProtocol} |
| 45 |
fallbackDialer = NewFallbackDialer(primary, secondary, httpstream.IsUpgradeFailure) |
| 46 |
_, negotiated, err = fallbackDialer.Dial(protocols...) |
| 47 |
assert.True(t, primary.dialed, "fallback; primary should have dialed") |
| 48 |
assert.True(t, secondary.dialed, "fallback; secondary should have dialed") |
| 49 |
assert.Equal(t, secondaryProtocol, negotiated, "negotiated protocol is from secondary dialer") |
| 50 |
require.NoError(t, err, "error from secondary dialer should be nil") |
| 51 |
// If primary dialer error is https proxy dialing error, then fallback returning secondary dial response. |
| 52 |
primary = &fakeDialer{negotiatedProtocol: primaryProtocol, err: errors.New("proxy: unknown scheme: https")} |
| 53 |
secondary = &fakeDialer{negotiatedProtocol: secondaryProtocol} |
| 54 |
fallbackDialer = NewFallbackDialer(primary, secondary, func(err error) bool { |
| 55 |
return httpstream.IsUpgradeFailure(err) || httpstream.IsHTTPSProxyError(err) |
| 56 |
}) |
| 57 |
_, negotiated, err = fallbackDialer.Dial(protocols...) |
| 58 |
assert.True(t, primary.dialed, "fallback; primary should have dialed") |
| 59 |
assert.True(t, secondary.dialed, "fallback; secondary should have dialed") |
| 60 |
assert.Equal(t, secondaryProtocol, negotiated, "negotiated protocol is from secondary dialer") |
| 61 |
require.NoError(t, err, "error from secondary dialer should be nil") |
| 62 |
// If primary dialer returns non-upgrade error, then primary error is returned. |
| 63 |
nonUpgradeErr := fmt.Errorf("This is a non-upgrade error") |
| 64 |
primary = &fakeDialer{dialed: false, err: nonUpgradeErr} |
| 65 |
secondary = &fakeDialer{dialed: false} |
| 66 |
fallbackDialer = NewFallbackDialer(primary, secondary, httpstream.IsUpgradeFailure) |
| 67 |
_, _, err = fallbackDialer.Dial(protocols...) |
| 68 |
assert.True(t, primary.dialed, "no fallback; primary should have dialed") |
| 69 |
assert.False(t, secondary.dialed, "no fallback; secondary should *not* have dialed") |
| 70 |
assert.Equal(t, nonUpgradeErr, err, "error is from primary dialer") |
| 71 |
} |
| 72 |
|
| 73 |
func notCalled(err error) bool { return false } |