github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/http/backend_test.go (about)

     1  package http
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hugorut/terraform/src/configs"
     9  	"github.com/zclconf/go-cty/cty"
    10  
    11  	"github.com/hugorut/terraform/src/backend"
    12  )
    13  
    14  func TestBackend_impl(t *testing.T) {
    15  	var _ backend.Backend = new(Backend)
    16  }
    17  
    18  func TestHTTPClientFactory(t *testing.T) {
    19  	// defaults
    20  
    21  	conf := map[string]cty.Value{
    22  		"address": cty.StringVal("http://127.0.0.1:8888/foo"),
    23  	}
    24  	b := backend.TestBackendConfig(t, New(), configs.SynthBody("synth", conf)).(*Backend)
    25  	client := b.client
    26  
    27  	if client == nil {
    28  		t.Fatal("Unexpected failure, address")
    29  	}
    30  	if client.URL.String() != "http://127.0.0.1:8888/foo" {
    31  		t.Fatalf("Expected address \"%s\", got \"%s\"", conf["address"], client.URL.String())
    32  	}
    33  	if client.UpdateMethod != "POST" {
    34  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "POST", client.UpdateMethod)
    35  	}
    36  	if client.LockURL != nil || client.LockMethod != "LOCK" {
    37  		t.Fatal("Unexpected lock_address or lock_method")
    38  	}
    39  	if client.UnlockURL != nil || client.UnlockMethod != "UNLOCK" {
    40  		t.Fatal("Unexpected unlock_address or unlock_method")
    41  	}
    42  	if client.Username != "" || client.Password != "" {
    43  		t.Fatal("Unexpected username or password")
    44  	}
    45  
    46  	// custom
    47  	conf = map[string]cty.Value{
    48  		"address":        cty.StringVal("http://127.0.0.1:8888/foo"),
    49  		"update_method":  cty.StringVal("BLAH"),
    50  		"lock_address":   cty.StringVal("http://127.0.0.1:8888/bar"),
    51  		"lock_method":    cty.StringVal("BLIP"),
    52  		"unlock_address": cty.StringVal("http://127.0.0.1:8888/baz"),
    53  		"unlock_method":  cty.StringVal("BLOOP"),
    54  		"username":       cty.StringVal("user"),
    55  		"password":       cty.StringVal("pass"),
    56  		"retry_max":      cty.StringVal("999"),
    57  		"retry_wait_min": cty.StringVal("15"),
    58  		"retry_wait_max": cty.StringVal("150"),
    59  	}
    60  
    61  	b = backend.TestBackendConfig(t, New(), configs.SynthBody("synth", conf)).(*Backend)
    62  	client = b.client
    63  
    64  	if client == nil {
    65  		t.Fatal("Unexpected failure, update_method")
    66  	}
    67  	if client.UpdateMethod != "BLAH" {
    68  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "BLAH", client.UpdateMethod)
    69  	}
    70  	if client.LockURL.String() != conf["lock_address"].AsString() || client.LockMethod != "BLIP" {
    71  		t.Fatalf("Unexpected lock_address \"%s\" vs \"%s\" or lock_method \"%s\" vs \"%s\"", client.LockURL.String(),
    72  			conf["lock_address"].AsString(), client.LockMethod, conf["lock_method"])
    73  	}
    74  	if client.UnlockURL.String() != conf["unlock_address"].AsString() || client.UnlockMethod != "BLOOP" {
    75  		t.Fatalf("Unexpected unlock_address \"%s\" vs \"%s\" or unlock_method \"%s\" vs \"%s\"", client.UnlockURL.String(),
    76  			conf["unlock_address"].AsString(), client.UnlockMethod, conf["unlock_method"])
    77  	}
    78  	if client.Username != "user" || client.Password != "pass" {
    79  		t.Fatalf("Unexpected username \"%s\" vs \"%s\" or password \"%s\" vs \"%s\"", client.Username, conf["username"],
    80  			client.Password, conf["password"])
    81  	}
    82  	if client.Client.RetryMax != 999 {
    83  		t.Fatalf("Expected retry_max \"%d\", got \"%d\"", 999, client.Client.RetryMax)
    84  	}
    85  	if client.Client.RetryWaitMin != 15*time.Second {
    86  		t.Fatalf("Expected retry_wait_min \"%s\", got \"%s\"", 15*time.Second, client.Client.RetryWaitMin)
    87  	}
    88  	if client.Client.RetryWaitMax != 150*time.Second {
    89  		t.Fatalf("Expected retry_wait_max \"%s\", got \"%s\"", 150*time.Second, client.Client.RetryWaitMax)
    90  	}
    91  }
    92  
    93  func TestHTTPClientFactoryWithEnv(t *testing.T) {
    94  	// env
    95  	conf := map[string]string{
    96  		"address":        "http://127.0.0.1:8888/foo",
    97  		"update_method":  "BLAH",
    98  		"lock_address":   "http://127.0.0.1:8888/bar",
    99  		"lock_method":    "BLIP",
   100  		"unlock_address": "http://127.0.0.1:8888/baz",
   101  		"unlock_method":  "BLOOP",
   102  		"username":       "user",
   103  		"password":       "pass",
   104  		"retry_max":      "999",
   105  		"retry_wait_min": "15",
   106  		"retry_wait_max": "150",
   107  	}
   108  
   109  	defer testWithEnv(t, "TF_HTTP_ADDRESS", conf["address"])()
   110  	defer testWithEnv(t, "TF_HTTP_UPDATE_METHOD", conf["update_method"])()
   111  	defer testWithEnv(t, "TF_HTTP_LOCK_ADDRESS", conf["lock_address"])()
   112  	defer testWithEnv(t, "TF_HTTP_UNLOCK_ADDRESS", conf["unlock_address"])()
   113  	defer testWithEnv(t, "TF_HTTP_LOCK_METHOD", conf["lock_method"])()
   114  	defer testWithEnv(t, "TF_HTTP_UNLOCK_METHOD", conf["unlock_method"])()
   115  	defer testWithEnv(t, "TF_HTTP_USERNAME", conf["username"])()
   116  	defer testWithEnv(t, "TF_HTTP_PASSWORD", conf["password"])()
   117  	defer testWithEnv(t, "TF_HTTP_RETRY_MAX", conf["retry_max"])()
   118  	defer testWithEnv(t, "TF_HTTP_RETRY_WAIT_MIN", conf["retry_wait_min"])()
   119  	defer testWithEnv(t, "TF_HTTP_RETRY_WAIT_MAX", conf["retry_wait_max"])()
   120  
   121  	b := backend.TestBackendConfig(t, New(), nil).(*Backend)
   122  	client := b.client
   123  
   124  	if client == nil {
   125  		t.Fatal("Unexpected failure, EnvDefaultFunc")
   126  	}
   127  	if client.UpdateMethod != "BLAH" {
   128  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "BLAH", client.UpdateMethod)
   129  	}
   130  	if client.LockURL.String() != conf["lock_address"] || client.LockMethod != "BLIP" {
   131  		t.Fatalf("Unexpected lock_address \"%s\" vs \"%s\" or lock_method \"%s\" vs \"%s\"", client.LockURL.String(),
   132  			conf["lock_address"], client.LockMethod, conf["lock_method"])
   133  	}
   134  	if client.UnlockURL.String() != conf["unlock_address"] || client.UnlockMethod != "BLOOP" {
   135  		t.Fatalf("Unexpected unlock_address \"%s\" vs \"%s\" or unlock_method \"%s\" vs \"%s\"", client.UnlockURL.String(),
   136  			conf["unlock_address"], client.UnlockMethod, conf["unlock_method"])
   137  	}
   138  	if client.Username != "user" || client.Password != "pass" {
   139  		t.Fatalf("Unexpected username \"%s\" vs \"%s\" or password \"%s\" vs \"%s\"", client.Username, conf["username"],
   140  			client.Password, conf["password"])
   141  	}
   142  	if client.Client.RetryMax != 999 {
   143  		t.Fatalf("Expected retry_max \"%d\", got \"%d\"", 999, client.Client.RetryMax)
   144  	}
   145  	if client.Client.RetryWaitMin != 15*time.Second {
   146  		t.Fatalf("Expected retry_wait_min \"%s\", got \"%s\"", 15*time.Second, client.Client.RetryWaitMin)
   147  	}
   148  	if client.Client.RetryWaitMax != 150*time.Second {
   149  		t.Fatalf("Expected retry_wait_max \"%s\", got \"%s\"", 150*time.Second, client.Client.RetryWaitMax)
   150  	}
   151  }
   152  
   153  // testWithEnv sets an environment variable and returns a deferable func to clean up
   154  func testWithEnv(t *testing.T, key string, value string) func() {
   155  	if err := os.Setenv(key, value); err != nil {
   156  		t.Fatalf("err: %v", err)
   157  	}
   158  
   159  	return func() {
   160  		if err := os.Unsetenv(key); err != nil {
   161  			t.Fatalf("err: %v", err)
   162  		}
   163  	}
   164  }