github.com/jameswoolfenden/terraform@v0.11.12-beta1/svchost/auth/helper_program_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/svchost"
     9  )
    10  
    11  func TestHelperProgramCredentialsSource(t *testing.T) {
    12  	wd, err := os.Getwd()
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	program := filepath.Join(wd, "test-helper/test-helper")
    18  	t.Logf("testing with helper at %s", program)
    19  
    20  	src := HelperProgramCredentialsSource(program)
    21  
    22  	t.Run("happy path", func(t *testing.T) {
    23  		creds, err := src.ForHost(svchost.Hostname("example.com"))
    24  		if err != nil {
    25  			t.Fatal(err)
    26  		}
    27  		if tokCreds, isTok := creds.(HostCredentialsToken); isTok {
    28  			if got, want := string(tokCreds), "example-token"; got != want {
    29  				t.Errorf("wrong token %q; want %q", got, want)
    30  			}
    31  		} else {
    32  			t.Errorf("wrong type of credentials %T", creds)
    33  		}
    34  	})
    35  	t.Run("no credentials", func(t *testing.T) {
    36  		creds, err := src.ForHost(svchost.Hostname("nothing.example.com"))
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		if creds != nil {
    41  			t.Errorf("got credentials; want nil")
    42  		}
    43  	})
    44  	t.Run("unsupported credentials type", func(t *testing.T) {
    45  		creds, err := src.ForHost(svchost.Hostname("other-cred-type.example.com"))
    46  		if err != nil {
    47  			t.Fatal(err)
    48  		}
    49  		if creds != nil {
    50  			t.Errorf("got credentials; want nil")
    51  		}
    52  	})
    53  	t.Run("lookup error", func(t *testing.T) {
    54  		_, err := src.ForHost(svchost.Hostname("fail.example.com"))
    55  		if err == nil {
    56  			t.Error("completed successfully; want error")
    57  		}
    58  	})
    59  }