2017-03-30 16:33:54 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2018-11-21 22:06:03 +01:00
|
|
|
"context"
|
2017-03-30 16:33:54 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2021-01-20 19:18:02 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/helper/acctest"
|
2017-03-30 16:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_impl(t *testing.T) {
|
|
|
|
var _ backend.Backend = new(Backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig(t *testing.T) {
|
|
|
|
// This test just instantiates the client. Shouldn't make any actual
|
|
|
|
// requests nor incur any costs.
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"storage_account_name": "tfaccount",
|
|
|
|
"container_name": "tfcontainer",
|
|
|
|
"key": "state",
|
2020-06-25 11:50:16 +02:00
|
|
|
"snapshot": false,
|
2017-03-30 16:33:54 +02:00
|
|
|
// Access Key must be Base64
|
|
|
|
"access_key": "QUNDRVNTX0tFWQ0K",
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
|
|
|
if b.containerName != "tfcontainer" {
|
|
|
|
t.Fatalf("Incorrect bucketName was populated")
|
|
|
|
}
|
|
|
|
if b.keyName != "state" {
|
|
|
|
t.Fatalf("Incorrect keyName was populated")
|
|
|
|
}
|
2020-06-25 11:50:16 +02:00
|
|
|
if b.snapshot != false {
|
|
|
|
t.Fatalf("Incorrect snapshot was populated")
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
func TestBackendAccessKeyBasic(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
armClient.destroyTestResources(ctx, res)
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-30 16:33:54 +02:00
|
|
|
"storage_account_name": res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStates(t, b)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 16:52:27 +01:00
|
|
|
func TestBackendManagedServiceIdentityBasic(t *testing.T) {
|
|
|
|
testAccAzureBackendRunningInAzure(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-22 16:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
|
|
|
"use_msi": true,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
2018-11-22 16:52:27 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-22 16:52:27 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:02:33 +01:00
|
|
|
func TestBackendSASTokenBasic(t *testing.T) {
|
2018-11-21 22:06:03 +01:00
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2018-11-22 18:02:33 +01:00
|
|
|
|
|
|
|
sasToken, err := buildSasToken(res.storageAccountName, res.storageAccountAccessKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error building SAS Token: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"sas_token": *sasToken,
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-22 18:02:33 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:58:33 +02:00
|
|
|
func TestBackendServicePrincipalClientCertificateBasic(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
|
|
|
|
clientCertPassword := os.Getenv("ARM_CLIENT_CERTIFICATE_PASSWORD")
|
|
|
|
clientCertPath := os.Getenv("ARM_CLIENT_CERTIFICATE_PATH")
|
|
|
|
if clientCertPath == "" {
|
|
|
|
t.Skip("Skipping since `ARM_CLIENT_CERTIFICATE_PATH` is not specified!")
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_certificate_password": clientCertPassword,
|
|
|
|
"client_certificate_path": clientCertPath,
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendServicePrincipalClientSecretBasic(t *testing.T) {
|
2018-11-22 18:02:33 +01:00
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-21 22:06:03 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-22 18:02:33 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-30 16:33:54 +02:00
|
|
|
"storage_account_name": res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:58:33 +02:00
|
|
|
func TestBackendServicePrincipalClientSecretCustomEndpoint(t *testing.T) {
|
2018-11-26 14:42:16 +01:00
|
|
|
testAccAzureBackend(t)
|
|
|
|
|
|
|
|
// this is only applicable for Azure Stack.
|
|
|
|
endpoint := os.Getenv("ARM_ENDPOINT")
|
|
|
|
if endpoint == "" {
|
|
|
|
t.Skip("Skipping as ARM_ENDPOINT isn't configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
|
|
|
"endpoint": endpoint,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
backend.TestBackendStates(t, b)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
func TestBackendAccessKeyLocked(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
2017-03-30 16:33:54 +02:00
|
|
|
rs := acctest.RandString(4)
|
2018-11-21 22:06:03 +01:00
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2018-11-21 22:06:03 +01:00
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
|
|
|
backend.TestBackendStateForceUnlock(t, b1, b2)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
func TestBackendServicePrincipalLocked(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2018-11-21 22:06:03 +01:00
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
|
|
|
backend.TestBackendStateForceUnlock(t, b1, b2)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|