...

Source file src/k8s.io/client-go/tools/portforward/fallback_dialer_test.go

Documentation: k8s.io/client-go/tools/portforward

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

View as plain text