Skip to content

Commit

Permalink
Merge branch 'main' into johnc/end-to-end-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johncollinson2001 committed Sep 20, 2024
2 parents 8fe01dc + 8ced5a7 commit 1d76d60
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
9 changes: 9 additions & 0 deletions tests/end-to-end-tests/basic_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/stretchr/testify/assert"
)

/*
* TestBasicDeployment tests a basic deployment of the infrastructure using Terraform using the TF output variables.
*/
func TestBasicDeployment(t *testing.T) {
terraformFolder := "../../infrastructure"

Expand All @@ -18,6 +21,8 @@ func TestBasicDeployment(t *testing.T) {
vaultRedundancy := "LocallyRedundant"

// Setup stage
// ...

test_structure.RunTestStage(t, "setup", func() {
terraformOptions := &terraform.Options{
TerraformDir: terraformFolder,
Expand All @@ -37,6 +42,8 @@ func TestBasicDeployment(t *testing.T) {
})

// Validate stage
// ...

test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformFolder)

Expand All @@ -55,6 +62,8 @@ func TestBasicDeployment(t *testing.T) {
})

// Teardown stage
// ...

test_structure.RunTestStage(t, "teardown", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformFolder)

Expand Down
33 changes: 30 additions & 3 deletions tests/end-to-end-tests/full_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"github.com/stretchr/testify/assert"
)

/*
* TestFullDeployment tests the full deployment of the infrastructure using Terraform.
*/
func TestFullDeployment(t *testing.T) {
terraformFolder := "../../infrastructure"

Expand All @@ -23,6 +26,8 @@ func TestFullDeployment(t *testing.T) {
vaultRedundancy := "LocallyRedundant"

// Setup stage
// ...

test_structure.RunTestStage(t, "setup", func() {
terraformOptions := &terraform.Options{
TerraformDir: terraformFolder,
Expand All @@ -42,6 +47,8 @@ func TestFullDeployment(t *testing.T) {
})

// Validate stage
// ...

test_structure.RunTestStage(t, "validate", func() {
resourceGroupName := fmt.Sprintf("rg-nhsbackup-%s", vaultName)
fullVaultName := fmt.Sprintf("bvault-%s", vaultName)
Expand All @@ -66,13 +73,18 @@ func TestFullDeployment(t *testing.T) {
})

// Teardown stage
// ...

test_structure.RunTestStage(t, "teardown", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformFolder)

terraform.Destroy(t, terraformOptions)
})
}

/*
* Validates the resource group has been deployed correctly
*/
func ValidateResourceGroup(t *testing.T, subscriptionID string,
cred *azidentity.ClientSecretCredential, resourceGroupName string, vaultLocation string) {
// Create a new resource groups client
Expand All @@ -90,6 +102,9 @@ func ValidateResourceGroup(t *testing.T, subscriptionID string,
assert.Equal(t, vaultLocation, *resp.ResourceGroup.Location, "Resource group location does not match")
}

/*
* Validates the backup vault has been deployed correctly
*/
func ValidateBackupVault(t *testing.T, subscriptionID string, cred *azidentity.ClientSecretCredential, resourceGroupName string, vaultName string, vaultLocation string) {
// Create a new Data Protection Backup Vaults client
client, err := armdataprotection.NewBackupVaultsClient(subscriptionID, cred, nil)
Expand All @@ -109,6 +124,9 @@ func ValidateBackupVault(t *testing.T, subscriptionID string, cred *azidentity.C
assert.Equal(t, armdataprotection.StorageSettingStoreTypesVaultStore, *resp.BackupVaultResource.Properties.StorageSettings[0].DatastoreType, "Backup vault datastore type does not match")
}

/*
* Validates the backup policies have been deployed correctly
*/
func ValidateBackupPolicies(t *testing.T, subscriptionID string, cred *azidentity.ClientSecretCredential, resourceGroupName string, fullVaultName string, vaultName string) {
ctx := context.Background()

Expand Down Expand Up @@ -139,6 +157,9 @@ func ValidateBackupPolicies(t *testing.T, subscriptionID string, cred *azidentit
}
}

/*
* Validates the blob storage backup policy
*/
func ValidateBlobStoragePolicy(t *testing.T, policies []*armdataprotection.BaseBackupPolicyResource, vaultName string) {
blobStoragePolicyName := fmt.Sprintf("bkpol-%s-blobstorage", vaultName)
blobStoragePolicy := GetBackupPolicyForName(policies, blobStoragePolicyName)
Expand All @@ -160,7 +181,9 @@ func ValidateBlobStoragePolicy(t *testing.T, policies []*armdataprotection.BaseB
assert.Equal(t, "P7D", *deleteOption.Duration, "Expected the blob storage retention period to be P7D")
}

// Validates the managed disk backup policy
/*
* Validates the managed disk backup policy
*/
func ValidateManagedDiskPolicy(t *testing.T, policies []*armdataprotection.BaseBackupPolicyResource, vaultName string) {
managedDiskPolicyName := fmt.Sprintf("bkpol-%s-manageddisk", vaultName)
managedDiskPolicy := GetBackupPolicyForName(policies, managedDiskPolicyName)
Expand Down Expand Up @@ -195,7 +218,9 @@ func ValidateManagedDiskPolicy(t *testing.T, policies []*armdataprotection.BaseB
assert.Equal(t, "P7D", *deleteOption.Duration, "Expected the managed disk retention period to be P7D")
}

// Gets a backup policy from the provided list for the provided name
/*
* Gets a backup policy from the provided list for the provided name
*/
func GetBackupPolicyForName(policies []*armdataprotection.BaseBackupPolicyResource, name string) *armdataprotection.BaseBackupPolicyResource {
for _, policy := range policies {
if *policy.Name == name {
Expand All @@ -206,7 +231,9 @@ func GetBackupPolicyForName(policies []*armdataprotection.BaseBackupPolicyResour
return nil
}

// Gets a backup policy rules from the provided list for the provided name
/*
* Gets a backup policy rules from the provided list for the provided name
*/
func GetBackupPolicyRuleForName(policyRules []armdataprotection.BasePolicyRuleClassification, name string) armdataprotection.BasePolicyRuleClassification {
for _, policyRule := range policyRules {
if *policyRule.GetBasePolicyRule().Name == name {
Expand Down
9 changes: 0 additions & 9 deletions tests/integration-tests/backup_policy.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ run "create_blob_storage_policy" {
vault_name = run.setup_tests.vault_name
}

# Check that the id is as expected
assert {
condition = length(module.blob_storage_policy.id) > 0
error_message = "Blob storage policy id not as expected."
}

# Check that the name is as expected
assert {
condition = module.blob_storage_policy.name == "bkpol-${var.vault_name}-blobstorage"
error_message = "Blob storage policy name not as expected."
}
# Check that the vault id is as expected
assert {
condition = module.blob_storage_policy.vault_id == azurerm_data_protection_backup_vault.backup_vault.id
error_message = "Blob storage policy vault id not as expected."
}
# Check that the retention period is as expected
assert {
condition = module.blob_storage_policy.retention_period == "P7D"
error_message = "Blob storage policy retention period not as expected."
Expand All @@ -55,31 +51,26 @@ run "create_managed_disk_policy" {
vault_name = run.setup_tests.vault_name
}

# Check that the id is as expected
assert {
condition = length(module.managed_disk_policy.id) > 0
error_message = "Managed disk policy id not as expected."
}

# Check that the name is as expected
assert {
condition = module.managed_disk_policy.name == "bkpol-${var.vault_name}-manageddisk"
error_message = "Managed disk policy name not as expected."
}
# Check that the vault id is as expected
assert {
condition = module.managed_disk_policy.vault_id == azurerm_data_protection_backup_vault.backup_vault.id
error_message = "Managed disk policy vault id not as expected."
}
# Check that the retention period is as expected
assert {
condition = module.managed_disk_policy.retention_period == "P7D"
error_message = "Managed disk policy retention period not as expected."
}
# Check that the backup intervals is as expected
assert {
condition = can(module.managed_disk_policy.backup_intervals) && length(module.managed_disk_policy.backup_intervals) == 1 && module.managed_disk_policy.backup_intervals[0] == "R/2024-01-01T00:00:00+00:00/P1D"
error_message = "Managed disk policy backup intervals not as expected."
Expand Down
7 changes: 0 additions & 7 deletions tests/integration-tests/backup_vault.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,36 @@ run "create_backup_vault" {
vault_redundancy = "LocallyRedundant"
}

# Check that the name is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.name == "bvault-${var.vault_name}"
error_message = "Backup vault name not as expected."
}
# Check that the resource group is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.resource_group_name == azurerm_resource_group.resource_group.name
error_message = "Resource group not as expected."
}
# Check that the location is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.location == var.vault_location
error_message = "Backup vault location not as expected."
}
# Check that the datastore type is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.datastore_type == "VaultStore"
error_message = "Backup vault datastore type not as expected."
}
# Check that the redundancy is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.redundancy == var.vault_redundancy
error_message = "Backup vault redundancy not as expected."
}
# Check that soft delete is as expected
assert {
condition = azurerm_data_protection_backup_vault.backup_vault.soft_delete == "Off"
error_message = "Backup vault soft delete not as expected."
}
# Check that identity is as expected
assert {
condition = length(azurerm_data_protection_backup_vault.backup_vault.identity[0].principal_id) > 0
error_message = "Backup vault identity not as expected."
Expand Down
2 changes: 0 additions & 2 deletions tests/integration-tests/resource_group.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ run "create_resource_group" {
vault_location = "uksouth"
}

# Check that the name is as expected
assert {
condition = azurerm_resource_group.resource_group.name == "rg-nhsbackup-${var.vault_name}"
error_message = "Resource group name not as expected."
}
# Check that the location is as expected
assert {
condition = azurerm_resource_group.resource_group.location == var.vault_location
error_message = "Resource group location not as expected."
Expand Down

0 comments on commit 1d76d60

Please sign in to comment.