-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DWX-18731 Add hive creation and deletion tests
- acceptance tests - unit tests
- Loading branch information
Showing
3 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2024 Cloudera. All Rights Reserved. | ||
// | ||
// This file is licensed under the Apache License Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, either express or implied. Refer to the License for the specific | ||
// permissions and limitations governing your use of the file. | ||
|
||
package dw_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" | ||
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" | ||
"github.com/cloudera/terraform-provider-cdp/cdpacctest" | ||
"github.com/cloudera/terraform-provider-cdp/utils" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"os" | ||
"strings" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
type hiveTestParameters struct { | ||
Name string | ||
ClusterID string | ||
DatabaseCatalogID string | ||
} | ||
|
||
var ( | ||
preCheckOnce sync.Once | ||
) | ||
|
||
func HivePreCheck(t *testing.T) { | ||
preCheckOnce.Do(func() { | ||
errMsg := "AWS Terraform acceptance testing requires environment variable %s to be set" | ||
if _, ok := os.LookupEnv("CDW_CLUSTER_ID"); !ok { | ||
t.Fatalf(errMsg, "CDW_CLUSTER_ID") | ||
} | ||
if _, ok := os.LookupEnv("CDW_DATABASE_CATALOG_ID"); !ok { | ||
t.Fatalf(errMsg, "CDW_DATABASE_CATALOG_ID") | ||
} | ||
}) | ||
} | ||
|
||
func TestAccHive_basic(t *testing.T) { | ||
params := hiveTestParameters{ | ||
Name: cdpacctest.RandomShortWithPrefix(cdpacctest.ResourcePrefix), | ||
ClusterID: os.Getenv("CDW_CLUSTER_ID"), | ||
DatabaseCatalogID: os.Getenv("CDW_DATABASE_CATALOG_ID"), | ||
} | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
cdpacctest.PreCheck(t) | ||
HivePreCheck(t) | ||
}, | ||
ProtoV6ProviderFactories: cdpacctest.TestAccProtoV6ProviderFactories, | ||
CheckDestroy: testCheckHiveDestroy, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: utils.Concat( | ||
cdpacctest.TestAccCdpProviderConfig(), | ||
testAccHiveBasicConfig(params)), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "name", params.Name), | ||
resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "cluster_id", params.ClusterID), | ||
resource.TestCheckResourceAttr("cdp_vw_hive.test_hive", "database_catalog_id", params.DatabaseCatalogID), | ||
), | ||
}, | ||
// TODO ImportState testing | ||
// TODO Update and Read testing | ||
// Delete testing automatically occurs in TestCase | ||
}, | ||
}) | ||
} | ||
|
||
func testAccHiveBasicConfig(params hiveTestParameters) string { | ||
return fmt.Sprintf(` | ||
resource "cdp_vw_hive" "test_hive" { | ||
cluster_id = %[1]q | ||
database_catalog_id = %[2]q | ||
name = %[3]q | ||
} | ||
`, params.ClusterID, params.DatabaseCatalogID, params.Name) | ||
} | ||
|
||
func testCheckHiveDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "cdp_vw_hive" { | ||
continue | ||
} | ||
|
||
cdpClient := cdpacctest.GetCdpClientForAccTest() | ||
params := operations.NewDescribeVwParamsWithContext(context.Background()) | ||
clusterID := rs.Primary.Attributes["cluster_id"] | ||
params.WithInput(&models.DescribeVwRequest{ | ||
VwID: &rs.Primary.ID, | ||
ClusterID: &clusterID, | ||
}) | ||
|
||
_, err := cdpClient.Dw.Operations.DescribeVw(params) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "404") { | ||
continue | ||
} | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2024 Cloudera. All Rights Reserved. | ||
// | ||
// This file is licensed under the Apache License Version 2.0 (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, either express or implied. Refer to the License for the specific | ||
// permissions and limitations governing your use of the file. | ||
|
||
package dw | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" | ||
dwclient "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client" | ||
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" | ||
"github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/models" | ||
mocks "github.com/cloudera/terraform-provider-cdp/mocks/github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/dw/client/operations" | ||
"github.com/go-openapi/runtime" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
"testing" | ||
) | ||
|
||
type MockTransport struct { | ||
runtime.ClientTransport | ||
} | ||
|
||
func NewDwApi(client *mocks.MockDwClientService) *hiveResource { | ||
return &hiveResource{ | ||
client: &cdp.Client{ | ||
Dw: &dwclient.Dw{ | ||
Operations: client, | ||
Transport: MockTransport{}, | ||
}}} | ||
} | ||
|
||
func createRawHiveResource() tftypes.Value { | ||
return tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
"cluster_id": tftypes.String, | ||
"database_catalog_id": tftypes.String, | ||
"name": tftypes.String, | ||
}}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, ""), | ||
"cluster_id": tftypes.NewValue(tftypes.String, "cluster-id"), | ||
"database_catalog_id": tftypes.NewValue(tftypes.String, "database-catalog-id"), | ||
"name": tftypes.NewValue(tftypes.String, ""), | ||
}) | ||
} | ||
|
||
type HiveTestSuite struct { | ||
suite.Suite | ||
expectedCreateResponse *operations.CreateVwOK | ||
} | ||
|
||
func TestHive(t *testing.T) { | ||
suite.Run(t, new(HiveTestSuite)) | ||
} | ||
|
||
func (suite *HiveTestSuite) SetupTest() { | ||
suite.expectedCreateResponse = &operations.CreateVwOK{Payload: &models.CreateVwResponse{ | ||
VwID: "test-id", | ||
}} | ||
|
||
} | ||
|
||
func (suite *HiveTestSuite) TestHiveMetadata() { | ||
ctx := context.TODO() | ||
client := new(mocks.MockDwClientService) | ||
dwApi := NewDwApi(client) | ||
|
||
req := resource.MetadataRequest{ProviderTypeName: "dw"} | ||
resp := &resource.MetadataResponse{} | ||
|
||
// Function under test | ||
dwApi.Metadata(ctx, req, resp) | ||
suite.Equal("dw_vw_hive", resp.TypeName) | ||
} | ||
|
||
func (suite *HiveTestSuite) TestHiveSchema() { | ||
ctx := context.TODO() | ||
client := new(mocks.MockDwClientService) | ||
dwApi := NewDwApi(client) | ||
|
||
req := resource.SchemaRequest{} | ||
resp := &resource.SchemaResponse{} | ||
|
||
// Function under test | ||
dwApi.Schema(ctx, req, resp) | ||
suite.Equal(hiveSchema, resp.Schema) | ||
} | ||
|
||
func (suite *HiveTestSuite) TestHiveCreate_basic() { | ||
ctx := context.TODO() | ||
expectedDescribeResponse := &operations.DescribeVwOK{ | ||
Payload: &models.DescribeVwResponse{ | ||
Vw: &models.VwSummary{ | ||
ID: "test-id", | ||
DbcID: "database-catalog-id", | ||
Name: "test-name", | ||
VwType: models.VwTypeHive, | ||
}}} | ||
|
||
client := new(mocks.MockDwClientService) | ||
client.On("CreateVw", mock.Anything).Return(suite.expectedCreateResponse, nil) | ||
client.On("DescribeVw", mock.Anything).Return(expectedDescribeResponse, nil) | ||
dwApi := NewDwApi(client) | ||
|
||
req := resource.CreateRequest{ | ||
Plan: tfsdk.Plan{ | ||
Raw: createRawHiveResource(), | ||
Schema: hiveSchema, | ||
}, | ||
} | ||
|
||
resp := &resource.CreateResponse{ | ||
State: tfsdk.State{ | ||
Schema: hiveSchema, | ||
}, | ||
} | ||
|
||
// Function under test | ||
dwApi.Create(ctx, req, resp) | ||
var result hiveResourceModel | ||
resp.State.Get(ctx, &result) | ||
suite.False(resp.Diagnostics.HasError()) | ||
suite.Equal("test-id", result.ID.ValueString()) | ||
suite.Equal("database-catalog-id", result.DbCatalogID.ValueString()) | ||
suite.Equal("cluster-id", result.ClusterID.ValueString()) | ||
suite.Equal("test-name", result.Name.ValueString()) | ||
} | ||
|
||
func (suite *HiveTestSuite) TestHiveDeletion_basic() { | ||
ctx := context.TODO() | ||
client := new(mocks.MockDwClientService) | ||
client.On("DeleteVw", mock.Anything).Return(&operations.DeleteVwOK{}, nil) | ||
dwApi := NewDwApi(client) | ||
|
||
req := resource.DeleteRequest{ | ||
State: tfsdk.State{ | ||
Schema: hiveSchema, | ||
Raw: createRawHiveResource(), | ||
}, | ||
} | ||
|
||
resp := &resource.DeleteResponse{} | ||
|
||
// Function under test | ||
dwApi.Delete(ctx, req, resp) | ||
suite.False(resp.Diagnostics.HasError()) | ||
} |