github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/http/backend_test.go (about)

     1  package http
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/terraform/configs"
     8  	"github.com/zclconf/go-cty/cty"
     9  
    10  	"github.com/hashicorp/terraform/backend"
    11  )
    12  
    13  func TestBackend_impl(t *testing.T) {
    14  	var _ backend.Backend = new(Backend)
    15  }
    16  
    17  func TestHTTPClientFactory(t *testing.T) {
    18  	// defaults
    19  
    20  	conf := map[string]cty.Value{
    21  		"address": cty.StringVal("http://127.0.0.1:8888/foo"),
    22  	}
    23  	b := backend.TestBackendConfig(t, New(), configs.SynthBody("synth", conf)).(*Backend)
    24  	client := b.client
    25  
    26  	if client == nil {
    27  		t.Fatal("Unexpected failure, address")
    28  	}
    29  	if client.URL.String() != "http://127.0.0.1:8888/foo" {
    30  		t.Fatalf("Expected address \"%s\", got \"%s\"", conf["address"], client.URL.String())
    31  	}
    32  	if client.UpdateMethod != "POST" {
    33  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "POST", client.UpdateMethod)
    34  	}
    35  	if client.LockURL != nil || client.LockMethod != "LOCK" {
    36  		t.Fatal("Unexpected lock_address or lock_method")
    37  	}
    38  	if client.UnlockURL != nil || client.UnlockMethod != "UNLOCK" {
    39  		t.Fatal("Unexpected unlock_address or unlock_method")
    40  	}
    41  	if client.Username != "" || client.Password != "" {
    42  		t.Fatal("Unexpected username or password")
    43  	}
    44  
    45  	// custom
    46  	conf = map[string]cty.Value{
    47  		"address":        cty.StringVal("http://127.0.0.1:8888/foo"),
    48  		"update_method":  cty.StringVal("BLAH"),
    49  		"lock_address":   cty.StringVal("http://127.0.0.1:8888/bar"),
    50  		"lock_method":    cty.StringVal("BLIP"),
    51  		"unlock_address": cty.StringVal("http://127.0.0.1:8888/baz"),
    52  		"unlock_method":  cty.StringVal("BLOOP"),
    53  		"username":       cty.StringVal("user"),
    54  		"password":       cty.StringVal("pass"),
    55  		"retry_max":      cty.StringVal("999"),
    56  		"retry_wait_min": cty.StringVal("15"),
    57  		"retry_wait_max": cty.StringVal("150"),
    58  	}
    59  
    60  	b = backend.TestBackendConfig(t, New(), configs.SynthBody("synth", conf)).(*Backend)
    61  	client = b.client
    62  
    63  	if client == nil {
    64  		t.Fatal("Unexpected failure, update_method")
    65  	}
    66  	if client.UpdateMethod != "BLAH" {
    67  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "BLAH", client.UpdateMethod)
    68  	}
    69  	if client.LockURL.String() != conf["lock_address"].AsString() || client.LockMethod != "BLIP" {
    70  		t.Fatalf("Unexpected lock_address \"%s\" vs \"%s\" or lock_method \"%s\" vs \"%s\"", client.LockURL.String(),
    71  			conf["lock_address"].AsString(), client.LockMethod, conf["lock_method"])
    72  	}
    73  	if client.UnlockURL.String() != conf["unlock_address"].AsString() || client.UnlockMethod != "BLOOP" {
    74  		t.Fatalf("Unexpected unlock_address \"%s\" vs \"%s\" or unlock_method \"%s\" vs \"%s\"", client.UnlockURL.String(),
    75  			conf["unlock_address"].AsString(), client.UnlockMethod, conf["unlock_method"])
    76  	}
    77  	if client.Username != "user" || client.Password != "pass" {
    78  		t.Fatalf("Unexpected username \"%s\" vs \"%s\" or password \"%s\" vs \"%s\"", client.Username, conf["username"],
    79  			client.Password, conf["password"])
    80  	}
    81  	if client.Client.RetryMax != 999 {
    82  		t.Fatalf("Expected retry_max \"%d\", got \"%d\"", 999, client.Client.RetryMax)
    83  	}
    84  	if client.Client.RetryWaitMin != 15*time.Second {
    85  		t.Fatalf("Expected retry_wait_min \"%s\", got \"%s\"", 15*time.Second, client.Client.RetryWaitMin)
    86  	}
    87  	if client.Client.RetryWaitMax != 150*time.Second {
    88  		t.Fatalf("Expected retry_wait_max \"%s\", got \"%s\"", 150*time.Second, client.Client.RetryWaitMax)
    89  	}
    90  }