github.com/tilt-dev/tilt@v0.36.0/internal/k8s/portforward_test.go (about)

     1  package k8s
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"k8s.io/apimachinery/pkg/util/httpstream"
     8  
     9  	"github.com/tilt-dev/tilt/internal/testutils"
    10  )
    11  
    12  type fakeDialer struct {
    13  	dialed             bool
    14  	conn               httpstream.Connection
    15  	err                error
    16  	negotiatedProtocol string
    17  }
    18  
    19  func (d *fakeDialer) Dial(protocols ...string) (httpstream.Connection, string, error) {
    20  	d.dialed = true
    21  	return d.conn, d.negotiatedProtocol, d.err
    22  }
    23  
    24  var fakeNewPodDialer = newPodDialerFn(func(namespace Namespace, podID PodID) (httpstream.Dialer, error) {
    25  	return &fakeDialer{}, nil
    26  })
    27  
    28  func TestPortForwardEmptyHost(t *testing.T) {
    29  	ctx := testutils.LoggerCtx()
    30  	client := portForwardClient{newPodDialer: fakeNewPodDialer}
    31  	pf, err := client.CreatePortForwarder(ctx, "default", "podid", 8080, 8080, "")
    32  	assert.NoError(t, err)
    33  	assert.Equal(t, []string{"127.0.0.1", "::1"}, pf.Addresses())
    34  }
    35  
    36  func TestPortForwardLocalhost(t *testing.T) {
    37  	ctx := testutils.LoggerCtx()
    38  	client := portForwardClient{newPodDialer: fakeNewPodDialer}
    39  	pf, err := client.CreatePortForwarder(ctx, "default", "podid", 8080, 8080, "localhost")
    40  	assert.NoError(t, err)
    41  	assert.Equal(t, []string{"127.0.0.1", "::1"}, pf.Addresses())
    42  }
    43  
    44  func TestPortForwardInvalidDomain(t *testing.T) {
    45  	ctx := testutils.LoggerCtx()
    46  	client := portForwardClient{newPodDialer: fakeNewPodDialer}
    47  	_, err := client.CreatePortForwarder(ctx, "default", "podid", 8080, 8080, "domain.invalid")
    48  	if assert.Error(t, err) {
    49  		assert.Contains(t, err.Error(), "failed to look up address for domain.invalid")
    50  	}
    51  }
    52  
    53  func TestPortForwardAllHosts(t *testing.T) {
    54  	ctx := testutils.LoggerCtx()
    55  	client := portForwardClient{newPodDialer: fakeNewPodDialer}
    56  	pf, err := client.CreatePortForwarder(ctx, "default", "podid", 8080, 8080, "0.0.0.0")
    57  	assert.NoError(t, err)
    58  	assert.Equal(t, []string{"0.0.0.0"}, pf.Addresses())
    59  }