github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/azure/backend_test.go (about) 1 package azure 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/backend" 9 "github.com/hashicorp/terraform/helper/acctest" 10 ) 11 12 func TestBackend_impl(t *testing.T) { 13 var _ backend.Backend = new(Backend) 14 } 15 16 func TestBackendConfig(t *testing.T) { 17 // This test just instantiates the client. Shouldn't make any actual 18 // requests nor incur any costs. 19 20 config := map[string]interface{}{ 21 "storage_account_name": "tfaccount", 22 "container_name": "tfcontainer", 23 "key": "state", 24 // Access Key must be Base64 25 "access_key": "QUNDRVNTX0tFWQ0K", 26 } 27 28 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend) 29 30 if b.containerName != "tfcontainer" { 31 t.Fatalf("Incorrect bucketName was populated") 32 } 33 if b.keyName != "state" { 34 t.Fatalf("Incorrect keyName was populated") 35 } 36 } 37 38 func TestBackendAccessKeyBasic(t *testing.T) { 39 testAccAzureBackend(t) 40 rs := acctest.RandString(4) 41 res := testResourceNames(rs, "testState") 42 armClient := buildTestClient(t, res) 43 44 ctx := context.TODO() 45 err := armClient.buildTestResources(ctx, &res) 46 defer armClient.destroyTestResources(ctx, res) 47 if err != nil { 48 armClient.destroyTestResources(ctx, res) 49 t.Fatalf("Error creating Test Resources: %q", err) 50 } 51 52 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 53 "storage_account_name": res.storageAccountName, 54 "container_name": res.storageContainerName, 55 "key": res.storageKeyName, 56 "access_key": res.storageAccountAccessKey, 57 "environment": os.Getenv("ARM_ENVIRONMENT"), 58 "endpoint": os.Getenv("ARM_ENDPOINT"), 59 })).(*Backend) 60 61 backend.TestBackendStates(t, b) 62 } 63 64 func TestBackendManagedServiceIdentityBasic(t *testing.T) { 65 testAccAzureBackendRunningInAzure(t) 66 rs := acctest.RandString(4) 67 res := testResourceNames(rs, "testState") 68 armClient := buildTestClient(t, res) 69 70 ctx := context.TODO() 71 err := armClient.buildTestResources(ctx, &res) 72 defer armClient.destroyTestResources(ctx, res) 73 if err != nil { 74 t.Fatalf("Error creating Test Resources: %q", err) 75 } 76 77 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 78 "storage_account_name": res.storageAccountName, 79 "container_name": res.storageContainerName, 80 "key": res.storageKeyName, 81 "resource_group_name": res.resourceGroup, 82 "use_msi": true, 83 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 84 "tenant_id": os.Getenv("ARM_TENANT_ID"), 85 "environment": os.Getenv("ARM_ENVIRONMENT"), 86 "endpoint": os.Getenv("ARM_ENDPOINT"), 87 })).(*Backend) 88 89 backend.TestBackendStates(t, b) 90 } 91 92 func TestBackendSASTokenBasic(t *testing.T) { 93 testAccAzureBackend(t) 94 rs := acctest.RandString(4) 95 res := testResourceNames(rs, "testState") 96 armClient := buildTestClient(t, res) 97 98 ctx := context.TODO() 99 err := armClient.buildTestResources(ctx, &res) 100 defer armClient.destroyTestResources(ctx, res) 101 if err != nil { 102 t.Fatalf("Error creating Test Resources: %q", err) 103 } 104 105 sasToken, err := buildSasToken(res.storageAccountName, res.storageAccountAccessKey) 106 if err != nil { 107 t.Fatalf("Error building SAS Token: %+v", err) 108 } 109 110 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 111 "storage_account_name": res.storageAccountName, 112 "container_name": res.storageContainerName, 113 "key": res.storageKeyName, 114 "sas_token": *sasToken, 115 "environment": os.Getenv("ARM_ENVIRONMENT"), 116 "endpoint": os.Getenv("ARM_ENDPOINT"), 117 })).(*Backend) 118 119 backend.TestBackendStates(t, b) 120 } 121 122 func TestBackendServicePrincipalBasic(t *testing.T) { 123 testAccAzureBackend(t) 124 rs := acctest.RandString(4) 125 res := testResourceNames(rs, "testState") 126 armClient := buildTestClient(t, res) 127 128 ctx := context.TODO() 129 err := armClient.buildTestResources(ctx, &res) 130 defer armClient.destroyTestResources(ctx, res) 131 if err != nil { 132 t.Fatalf("Error creating Test Resources: %q", err) 133 } 134 135 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 136 "storage_account_name": res.storageAccountName, 137 "container_name": res.storageContainerName, 138 "key": res.storageKeyName, 139 "resource_group_name": res.resourceGroup, 140 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 141 "tenant_id": os.Getenv("ARM_TENANT_ID"), 142 "client_id": os.Getenv("ARM_CLIENT_ID"), 143 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 144 "environment": os.Getenv("ARM_ENVIRONMENT"), 145 "endpoint": os.Getenv("ARM_ENDPOINT"), 146 })).(*Backend) 147 148 backend.TestBackendStates(t, b) 149 } 150 151 func TestBackendServicePrincipalCustomEndpoint(t *testing.T) { 152 testAccAzureBackend(t) 153 154 // this is only applicable for Azure Stack. 155 endpoint := os.Getenv("ARM_ENDPOINT") 156 if endpoint == "" { 157 t.Skip("Skipping as ARM_ENDPOINT isn't configured") 158 } 159 160 rs := acctest.RandString(4) 161 res := testResourceNames(rs, "testState") 162 armClient := buildTestClient(t, res) 163 164 ctx := context.TODO() 165 err := armClient.buildTestResources(ctx, &res) 166 defer armClient.destroyTestResources(ctx, res) 167 if err != nil { 168 t.Fatalf("Error creating Test Resources: %q", err) 169 } 170 171 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 172 "storage_account_name": res.storageAccountName, 173 "container_name": res.storageContainerName, 174 "key": res.storageKeyName, 175 "resource_group_name": res.resourceGroup, 176 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 177 "tenant_id": os.Getenv("ARM_TENANT_ID"), 178 "client_id": os.Getenv("ARM_CLIENT_ID"), 179 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 180 "environment": os.Getenv("ARM_ENVIRONMENT"), 181 "endpoint": endpoint, 182 })).(*Backend) 183 184 backend.TestBackendStates(t, b) 185 } 186 187 func TestBackendAccessKeyLocked(t *testing.T) { 188 testAccAzureBackend(t) 189 rs := acctest.RandString(4) 190 res := testResourceNames(rs, "testState") 191 armClient := buildTestClient(t, res) 192 193 ctx := context.TODO() 194 err := armClient.buildTestResources(ctx, &res) 195 defer armClient.destroyTestResources(ctx, res) 196 if err != nil { 197 t.Fatalf("Error creating Test Resources: %q", err) 198 } 199 200 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 201 "storage_account_name": res.storageAccountName, 202 "container_name": res.storageContainerName, 203 "key": res.storageKeyName, 204 "access_key": res.storageAccountAccessKey, 205 "environment": os.Getenv("ARM_ENVIRONMENT"), 206 "endpoint": os.Getenv("ARM_ENDPOINT"), 207 })).(*Backend) 208 209 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 210 "storage_account_name": res.storageAccountName, 211 "container_name": res.storageContainerName, 212 "key": res.storageKeyName, 213 "access_key": res.storageAccountAccessKey, 214 "environment": os.Getenv("ARM_ENVIRONMENT"), 215 "endpoint": os.Getenv("ARM_ENDPOINT"), 216 })).(*Backend) 217 218 backend.TestBackendStateLocks(t, b1, b2) 219 backend.TestBackendStateForceUnlock(t, b1, b2) 220 } 221 222 func TestBackendServicePrincipalLocked(t *testing.T) { 223 testAccAzureBackend(t) 224 rs := acctest.RandString(4) 225 res := testResourceNames(rs, "testState") 226 armClient := buildTestClient(t, res) 227 228 ctx := context.TODO() 229 err := armClient.buildTestResources(ctx, &res) 230 defer armClient.destroyTestResources(ctx, res) 231 if err != nil { 232 t.Fatalf("Error creating Test Resources: %q", err) 233 } 234 235 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 236 "storage_account_name": res.storageAccountName, 237 "container_name": res.storageContainerName, 238 "key": res.storageKeyName, 239 "access_key": res.storageAccountAccessKey, 240 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 241 "tenant_id": os.Getenv("ARM_TENANT_ID"), 242 "client_id": os.Getenv("ARM_CLIENT_ID"), 243 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 244 "environment": os.Getenv("ARM_ENVIRONMENT"), 245 "endpoint": os.Getenv("ARM_ENDPOINT"), 246 })).(*Backend) 247 248 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 249 "storage_account_name": res.storageAccountName, 250 "container_name": res.storageContainerName, 251 "key": res.storageKeyName, 252 "access_key": res.storageAccountAccessKey, 253 "subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"), 254 "tenant_id": os.Getenv("ARM_TENANT_ID"), 255 "client_id": os.Getenv("ARM_CLIENT_ID"), 256 "client_secret": os.Getenv("ARM_CLIENT_SECRET"), 257 "environment": os.Getenv("ARM_ENVIRONMENT"), 258 "endpoint": os.Getenv("ARM_ENDPOINT"), 259 })).(*Backend) 260 261 backend.TestBackendStateLocks(t, b1, b2) 262 backend.TestBackendStateForceUnlock(t, b1, b2) 263 }