get.porter.sh/porter@v1.3.0/pkg/grpc/portergrpc/context_test.go (about)

     1  package portergrpc
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"get.porter.sh/porter/pkg/porter"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGetPorterConnectionFromContextReturnsErrIfNoConnectionInContext(t *testing.T) {
    12  	ctx := context.Background()
    13  	p, err := GetPorterConnectionFromContext(ctx)
    14  	assert.Nil(t, p)
    15  	assert.EqualError(t, err, "Unable to find porter connection in context")
    16  }
    17  
    18  func TestGetPorterConnectionFromContextReturnsPorterConnection(t *testing.T) {
    19  	p := porter.New()
    20  	ctx := context.Background()
    21  	ctx = context.WithValue(ctx, porterConnCtxKey, p)
    22  	newP, err := GetPorterConnectionFromContext(ctx)
    23  	assert.Nil(t, err)
    24  	assert.Equal(t, p, newP)
    25  }
    26  func TestAddPorterConnectionToContextReturnsContextUpdatedWithPorterConnection(t *testing.T) {
    27  	p := porter.New()
    28  	ctx := context.Background()
    29  	ctx = AddPorterConnectionToContext(p, ctx)
    30  	newP, ok := ctx.Value(porterConnCtxKey).(*porter.Porter)
    31  	assert.True(t, ok)
    32  	assert.Equal(t, p, newP)
    33  }