github.com/blend/go-sdk@v1.20220411.3/vault/config_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package vault
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/configutil"
    17  	"github.com/blend/go-sdk/env"
    18  )
    19  
    20  var (
    21  	_ configutil.Resolver = (*Config)(nil)
    22  )
    23  
    24  func TestConfigIsZero(t *testing.T) {
    25  	assert := assert.New(t)
    26  
    27  	assert.True(Config{}.IsZero())
    28  	assert.False(Config{Token: "garbage"}.IsZero())
    29  }
    30  
    31  func TestConfig(t *testing.T) {
    32  	assert := assert.New(t)
    33  
    34  	cfg := Config{}
    35  	assert.Equal(DefaultAddr, cfg.AddrOrDefault())
    36  	assert.Empty(cfg.Token)
    37  	assert.Equal(DefaultTimeout, cfg.TimeoutOrDefault())
    38  	assert.Empty(cfg.RootCAs)
    39  }
    40  
    41  func TestResolveTimeout(t *testing.T) {
    42  	assert := assert.New(t)
    43  	ctx := context.Background()
    44  	const fiveSeconds = "5s"
    45  	defer env.Env().Restore(EnvVarVaultTimeout)
    46  
    47  	env.Env().Set(EnvVarVaultTimeout, fiveSeconds)
    48  	cfg := &Config{}
    49  	err := cfg.Resolve(ctx)
    50  	assert.Nil(err)
    51  	assert.Equal(time.Second*5, cfg.TimeoutOrDefault())
    52  
    53  	env.Env().Delete(EnvVarVaultTimeout)
    54  	cfg2 := &Config{}
    55  	err = cfg2.Resolve(ctx)
    56  	assert.Equal(DefaultTimeout, cfg2.TimeoutOrDefault())
    57  	assert.Nil(err)
    58  }