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