|
| 1 | +package databricks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/databrickslabs/databricks-terraform/client/model" |
| 9 | + "github.com/databrickslabs/databricks-terraform/client/service" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccAzureBlobMount_correctly_mounts(t *testing.T) { |
| 16 | + terraformToApply := testAccAzureBlobMount_correctly_mounts() |
| 17 | + var clusterInfo model.ClusterInfo |
| 18 | + var azureBlobMount AzureBlobMount |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + Providers: testAccProviders, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + Config: terraformToApply, |
| 25 | + Check: resource.ComposeTestCheckFunc( |
| 26 | + testAccAzureBlobMount_cluster_exists("databricks_cluster.cluster", &clusterInfo), |
| 27 | + testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo), |
| 28 | + ), |
| 29 | + }, |
| 30 | + { |
| 31 | + PreConfig: func() { |
| 32 | + client := testAccProvider.Meta().(service.DBApiClient) |
| 33 | + err := azureBlobMount.Delete(client, clusterInfo.ClusterID) |
| 34 | + assert.NoError(t, err, "TestAccAzureBlobMount_correctly_mounts: Failed to remove the mount.") |
| 35 | + }, |
| 36 | + Config: terraformToApply, |
| 37 | + Check: resource.ComposeTestCheckFunc( |
| 38 | + testAccAzureBlobMount_mount_exists("databricks_azure_blob_mount.mount", &azureBlobMount, &clusterInfo), |
| 39 | + ), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func testAccAzureBlobMount_cluster_exists(n string, clusterInfo *model.ClusterInfo) resource.TestCheckFunc { |
| 46 | + return func(s *terraform.State) error { |
| 47 | + // find the corresponding state object |
| 48 | + rs, ok := s.RootModule().Resources[n] |
| 49 | + if !ok { |
| 50 | + return fmt.Errorf("Not found: %s", n) |
| 51 | + } |
| 52 | + |
| 53 | + // retrieve the configured client from the test setup |
| 54 | + client := testAccProvider.Meta().(service.DBApiClient) |
| 55 | + resp, err := client.Clusters().Get(rs.Primary.ID) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // If no error, assign the response Widget attribute to the widget pointer |
| 61 | + *clusterInfo = resp |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func testAccAzureBlobMount_mount_exists(n string, azureBlobMount *AzureBlobMount, clusterInfo *model.ClusterInfo) resource.TestCheckFunc { |
| 67 | + return func(s *terraform.State) error { |
| 68 | + // find the corresponding state object |
| 69 | + rs, ok := s.RootModule().Resources[n] |
| 70 | + if !ok { |
| 71 | + return fmt.Errorf("Not found in tfstate: %s", n) |
| 72 | + } |
| 73 | + |
| 74 | + authType := rs.Primary.Attributes["auth_type"] |
| 75 | + containerName := rs.Primary.Attributes["container_name"] |
| 76 | + storageAccountName := rs.Primary.Attributes["storage_account_name"] |
| 77 | + directory := rs.Primary.Attributes["directory"] |
| 78 | + mountName := rs.Primary.Attributes["mount_name"] |
| 79 | + tokenSecretScope := rs.Primary.Attributes["token_secret_scope"] |
| 80 | + tokenSecretKey := rs.Primary.Attributes["token_secret_key"] |
| 81 | + |
| 82 | + blobMount := NewAzureBlobMount(containerName, storageAccountName, directory, mountName, authType, |
| 83 | + tokenSecretScope, tokenSecretKey) |
| 84 | + |
| 85 | + client := testAccProvider.Meta().(service.DBApiClient) |
| 86 | + cluster_id := clusterInfo.ClusterID |
| 87 | + |
| 88 | + message, err := blobMount.Read(client, cluster_id) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("Error reading the mount %s: error %s", message, err) |
| 91 | + } |
| 92 | + |
| 93 | + *azureBlobMount = *blobMount |
| 94 | + return nil |
| 95 | + |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func testAccAzureBlobMount_correctly_mounts() string { |
| 100 | + clientID := os.Getenv("ARM_CLIENT_ID") |
| 101 | + clientSecret := os.Getenv("ARM_CLIENT_SECRET") |
| 102 | + tenantID := os.Getenv("ARM_TENANT_ID") |
| 103 | + subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") |
| 104 | + workspaceName := os.Getenv("TEST_WORKSPACE_NAME") |
| 105 | + resourceGroupName := os.Getenv("TEST_RESOURCE_GROUP") |
| 106 | + managedResourceGroupName := os.Getenv("TEST_MANAGED_RESOURCE_GROUP") |
| 107 | + location := os.Getenv("TEST_LOCATION") |
| 108 | + blobAccountKey := os.Getenv("TEST_STORAGE_ACCOUNT_KEY") |
| 109 | + blobAccountName := os.Getenv("TEST_STORAGE_ACCOUNT_NAME") |
| 110 | + |
| 111 | + definition := fmt.Sprintf(` |
| 112 | + provider "databricks" { |
| 113 | + azure_auth = { |
| 114 | + client_id = "%[1]s" |
| 115 | + client_secret = "%[2]s" |
| 116 | + tenant_id = "%[3]s" |
| 117 | + subscription_id = "%[4]s" |
| 118 | +
|
| 119 | + workspace_name = "%[5]s" |
| 120 | + resource_group = "%[6]s" |
| 121 | + managed_resource_group = "%[7]s" |
| 122 | + azure_region = "%[8]s" |
| 123 | + } |
| 124 | + } |
| 125 | +
|
| 126 | + resource "databricks_cluster" "cluster" { |
| 127 | + num_workers = 1 |
| 128 | + spark_version = "6.4.x-scala2.11" |
| 129 | + node_type_id = "Standard_D3_v2" |
| 130 | + # Don't spend too much, turn off cluster after 15mins |
| 131 | + autotermination_minutes = 15 |
| 132 | + } |
| 133 | +
|
| 134 | + resource "databricks_secret_scope" "terraform" { |
| 135 | + # Add the cluster ID into the secret scope to ensure |
| 136 | + # it doesn't clash with one used by another test |
| 137 | + name = "terraform${databricks_cluster.cluster.cluster_id}" |
| 138 | + initial_manage_principal = "users" |
| 139 | + } |
| 140 | + |
| 141 | + resource "databricks_secret" "storage_key" { |
| 142 | + key = "blob_storage_key" |
| 143 | + string_value = "%[10]s" |
| 144 | + scope = databricks_secret_scope.terraform.name |
| 145 | + } |
| 146 | + |
| 147 | + resource "databricks_azure_blob_mount" "mount" { |
| 148 | + cluster_id = databricks_cluster.cluster.id |
| 149 | + container_name = "dev" # Created by prereqs.tf |
| 150 | + storage_account_name = "%[9]s" |
| 151 | + mount_name = "dev" |
| 152 | + auth_type = "ACCESS_KEY" |
| 153 | + token_secret_scope = databricks_secret_scope.terraform.name |
| 154 | + token_secret_key = databricks_secret.storage_key.key |
| 155 | + } |
| 156 | +
|
| 157 | +`, clientID, clientSecret, tenantID, subscriptionID, workspaceName, resourceGroupName, managedResourceGroupName, location, blobAccountName, blobAccountKey) |
| 158 | + return definition |
| 159 | +} |
0 commit comments