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"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
armClient.destroyTestResources(ctx, res)
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
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-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-21 22:06:03 +01:00
|
|
|
func TestBackendServicePrincipalBasic(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)
|
|
|
|
if err != nil {
|
|
|
|
armClient.destroyTestResources(ctx, res)
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
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,
|
|
|
|
"arm_subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"arm_tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"arm_client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"arm_client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
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)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2018-11-21 22:06:03 +01:00
|
|
|
armClient.destroyTestResources(ctx, res)
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
2018-11-21 22:06:03 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
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,
|
|
|
|
})).(*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,
|
|
|
|
})).(*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)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
2018-11-21 22:06:03 +01:00
|
|
|
armClient.destroyTestResources(ctx, res)
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
2017-03-30 16:33:54 +02:00
|
|
|
}
|
2018-11-21 22:06:03 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
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,
|
|
|
|
"arm_subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"arm_tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"arm_client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"arm_client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
|
|
|
})).(*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,
|
|
|
|
"arm_subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"arm_tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"arm_client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"arm_client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
|
|
|
})).(*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
|
|
|
}
|