|
| 1 | +package databricks |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 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/acctest" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAccAWSGroupResource(t *testing.T) { |
| 17 | + var Group model.Group |
| 18 | + // generate a random name for each tokenInfo test run, to avoid |
| 19 | + // collisions from multiple concurrent tests. |
| 20 | + // the acctest package includes many helpers such as RandStringFromCharSet |
| 21 | + // See https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/acctest |
| 22 | + //scope := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) |
| 23 | + randomStr := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 24 | + displayName := fmt.Sprintf("tf group test %s", randomStr) |
| 25 | + newDisplayName := fmt.Sprintf("new tf group test %s", randomStr) |
| 26 | + resource.Test(t, resource.TestCase{ |
| 27 | + Providers: testAccProviders, |
| 28 | + CheckDestroy: testAWSGroupResourceDestroy, |
| 29 | + Steps: []resource.TestStep{ |
| 30 | + { |
| 31 | + // use a dynamic configuration with the random name from above |
| 32 | + Config: testAWSDatabricksGroup(displayName), |
| 33 | + // compose a basic test, checking both remote and local values |
| 34 | + Check: resource.ComposeTestCheckFunc( |
| 35 | + // query the API to retrieve the tokenInfo object |
| 36 | + testAWSGroupResourceExists("databricks_group.my_group", &Group, t), |
| 37 | + // verify remote values |
| 38 | + testAWSGroupValues(t, &Group, displayName), |
| 39 | + // verify local values |
| 40 | + resource.TestCheckResourceAttr("databricks_group.my_group", "display_name", displayName), |
| 41 | + ), |
| 42 | + Destroy: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + // use a dynamic configuration with the random name from above |
| 46 | + Config: testAWSDatabricksGroup(newDisplayName), |
| 47 | + // test to see if new resource is attempted to be planned |
| 48 | + PlanOnly: true, |
| 49 | + ExpectNonEmptyPlan: true, |
| 50 | + Destroy: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + ResourceName: "databricks_group.my_group", |
| 54 | + ImportState: true, |
| 55 | + ImportStateVerify: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func TestAccAWSGroupResource_verify_entitlements(t *testing.T) { |
| 62 | + var Group model.Group |
| 63 | + // generate a random name for each tokenInfo test run, to avoid |
| 64 | + // collisions from multiple concurrent tests. |
| 65 | + // the acctest package includes many helpers such as RandStringFromCharSet |
| 66 | + // See https://godoc.org/github.com/hashicorp/terraform-plugin-sdk/helper/acctest |
| 67 | + //scope := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) |
| 68 | + randomStr := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) |
| 69 | + displayName := fmt.Sprintf("tf group test %s", randomStr) |
| 70 | + newDisplayName := fmt.Sprintf("new tf group test %s", randomStr) |
| 71 | + resource.Test(t, resource.TestCase{ |
| 72 | + Providers: testAccProviders, |
| 73 | + CheckDestroy: testAWSGroupResourceDestroy, |
| 74 | + Steps: []resource.TestStep{ |
| 75 | + { |
| 76 | + // use a dynamic configuration with the random name from above |
| 77 | + Config: testAWSDatabricksGroupEntitlements(displayName, "true", "true"), |
| 78 | + // compose a basic test, checking both remote and local values |
| 79 | + Check: resource.ComposeTestCheckFunc( |
| 80 | + // query the API to retrieve the tokenInfo object |
| 81 | + testAWSGroupResourceExists("databricks_group.my_group", &Group, t), |
| 82 | + // verify remote values |
| 83 | + testAWSGroupValues(t, &Group, displayName), |
| 84 | + // verify local values |
| 85 | + resource.TestCheckResourceAttr("databricks_group.my_group", "allow_cluster_create", "true"), |
| 86 | + resource.TestCheckResourceAttr("databricks_group.my_group", "allow_instance_pool_create", "true"), |
| 87 | + ), |
| 88 | + Destroy: false, |
| 89 | + }, |
| 90 | + // Remove entitlements and expect a non empty plan |
| 91 | + { |
| 92 | + // use a dynamic configuration with the random name from above |
| 93 | + Config: testAWSDatabricksGroup(newDisplayName), |
| 94 | + // test to see if new resource is attempted to be planned |
| 95 | + PlanOnly: true, |
| 96 | + ExpectNonEmptyPlan: true, |
| 97 | + Destroy: false, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +func testAWSGroupResourceDestroy(s *terraform.State) error { |
| 104 | + client := testAccProvider.Meta().(*service.DBApiClient) |
| 105 | + for _, rs := range s.RootModule().Resources { |
| 106 | + if rs.Type != "databricks_group" { |
| 107 | + continue |
| 108 | + } |
| 109 | + _, err := client.Users().Read(rs.Primary.ID) |
| 110 | + if err != nil { |
| 111 | + return nil |
| 112 | + } |
| 113 | + return errors.New("resource Group is not cleaned up") |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func testAWSGroupValues(t *testing.T, group *model.Group, displayName string) resource.TestCheckFunc { |
| 119 | + return func(s *terraform.State) error { |
| 120 | + assert.True(t, group.DisplayName == displayName) |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// testAccCheckTokenResourceExists queries the API and retrieves the matching Widget. |
| 126 | +func testAWSGroupResourceExists(n string, group *model.Group, t *testing.T) resource.TestCheckFunc { |
| 127 | + return func(s *terraform.State) error { |
| 128 | + // find the corresponding state object |
| 129 | + rs, ok := s.RootModule().Resources[n] |
| 130 | + if !ok { |
| 131 | + return fmt.Errorf("Not found: %s", n) |
| 132 | + } |
| 133 | + |
| 134 | + // retrieve the configured client from the test setup |
| 135 | + conn := testAccProvider.Meta().(*service.DBApiClient) |
| 136 | + resp, err := conn.Groups().Read(rs.Primary.ID) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + // If no error, assign the response Widget attribute to the widget pointer |
| 142 | + *group = resp |
| 143 | + return nil |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func testAWSDatabricksGroup(groupName string) string { |
| 148 | + return fmt.Sprintf(` |
| 149 | + resource "databricks_group" "my_group" { |
| 150 | + display_name = "%s" |
| 151 | + } |
| 152 | + `, groupName) |
| 153 | +} |
| 154 | + |
| 155 | +func testAWSDatabricksGroupEntitlements(groupName, allowClusterCreate, allowPoolCreate string) string { |
| 156 | + return fmt.Sprintf(` |
| 157 | + resource "databricks_group" "my_group" { |
| 158 | + display_name = "%s" |
| 159 | + allow_cluster_create = %s |
| 160 | + allow_instance_pool_create = %s |
| 161 | + } |
| 162 | + `, groupName, allowClusterCreate, allowPoolCreate) |
| 163 | +} |
0 commit comments