diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41ec08b6..aaa865fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -101,7 +101,7 @@ jobs: - name: Go Coverage uses: gwatts/go-coverage-action@v1.3.0 with: - coverage-threshold: 10.1 + coverage-threshold: 17.3 cover-pkg: ./... ignore-pattern: | /cdp-sdk-go/ diff --git a/examples/resources/cdp_datahub_gcp_cluster/with_instance_groups/resource.tf b/examples/resources/cdp_datahub_gcp_cluster/with_instance_groups/resource.tf index 43241460..c4a8ec59 100644 --- a/examples/resources/cdp_datahub_gcp_cluster/with_instance_groups/resource.tf +++ b/examples/resources/cdp_datahub_gcp_cluster/with_instance_groups/resource.tf @@ -19,11 +19,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" { instance_group = [ { - node_count = 0 - instance_group_name = "" - instance_group_type = "" - instance_type = "" - root_volume_size = 100 + node_count = 0 + instance_group_name = "" + instance_group_type = "" + instance_type = "" + root_volume_size = 100 attached_volume_configuration = [ { volume_size = 100 @@ -35,11 +35,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" { recipes = [] }, { - node_count = 1 - instance_group_name = "" - instance_group_type = "" - instance_type = "" - root_volume_size = 100 + node_count = 1 + instance_group_name = "" + instance_group_type = "" + instance_type = "" + root_volume_size = 100 attached_volume_configuration = [ { volume_size = 100 @@ -51,11 +51,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" { recipes = [] }, { - node_count = 3 - instance_group_name = "" - instance_group_type = "" - instance_type = "" - root_volume_size = 100 + node_count = 3 + instance_group_name = "" + instance_group_type = "" + instance_type = "" + root_volume_size = 100 attached_volume_configuration = [ { volume_size = 300 @@ -67,11 +67,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" { recipes = [] }, { - node_count = 3 - instance_group_name = "" - instance_group_type = "" - instance_type = "" - root_volume_size = 100 + node_count = 3 + instance_group_name = "" + instance_group_type = "" + instance_type = "" + root_volume_size = 100 attached_volume_configuration = [ { volume_size = 300 diff --git a/examples/resources/cdp_datalake_gcp_datalake/resource.tf b/examples/resources/cdp_datalake_gcp_datalake/resource.tf index 2b3fbebe..a75cb543 100644 --- a/examples/resources/cdp_datalake_gcp_datalake/resource.tf +++ b/examples/resources/cdp_datalake_gcp_datalake/resource.tf @@ -9,8 +9,8 @@ // permissions and limitations governing your use of the file. resource "cdp_datalake_gcp_datalake" "example" { - datalake_name = "" - environment_name = "" + datalake_name = "" + environment_name = "" cloud_provider_configuration = { service_account_email = "" storage_location = "" diff --git a/resources/datahub/converter_test.go b/resources/datahub/converter_test.go index af444301..8362c4a1 100644 --- a/resources/datahub/converter_test.go +++ b/resources/datahub/converter_test.go @@ -205,6 +205,92 @@ func TestFromModelToGcpRequestClusterDefinition(t *testing.T) { compareStrings(got.ClusterDefinitionName, input.ClusterDefinition.ValueString(), t) } +func TestFromModelToAzureRequestBasicFields(t *testing.T) { + input := datahubResourceModel{ + Name: types.StringValue("someClusterName"), + Environment: types.StringValue("someEnvironment"), + ClusterTemplate: types.StringValue("someClusterTemplateNameOrCRN"), + } + got := fromModelToAzureRequest(input, nil) + + compareStrings(got.ClusterName, input.Name.ValueString(), t) + compareStrings(got.EnvironmentName, input.Environment.ValueString(), t) + compareStrings(got.ClusterTemplateName, input.ClusterTemplate.ValueString(), t) +} + +func TestFromModelToAzureRequestRecipe(t *testing.T) { + recipes := []types.String{types.StringValue("recipe1"), types.StringValue("recipe2")} + igs := []InstanceGroup{{Recipes: recipes}} + input := datahubResourceModel{InstanceGroup: igs} + + got := fromModelToAzureRequest(input, nil) + + compareInts(len(got.InstanceGroups), len(input.InstanceGroup), t) + compareInts(len(got.InstanceGroups[0].RecipeNames), len(input.InstanceGroup[0].Recipes), t) + + for _, convertedRecipe := range got.InstanceGroups[0].RecipeNames { + var contains bool + for _, originalRecipe := range input.InstanceGroup[0].Recipes { + if originalRecipe.ValueString() == convertedRecipe { + contains = true + } + } + if !contains { + t.Errorf("Instance group does not contain recipe: %s", convertedRecipe) + } + } +} + +func TestFromModelToAzureRequestAttachedVolumeConfiguration(t *testing.T) { + avcs := []AttachedVolumeConfiguration{{ + VolumeSize: types.Int64Value(100), + VolumeCount: types.Int64Value(1), + VolumeType: types.StringValue("ephemeral"), + }} + igs := []InstanceGroup{{AttachedVolumeConfiguration: avcs}} + input := datahubResourceModel{InstanceGroup: igs} + + got := fromModelToAzureRequest(input, nil) + + compareInts(len(got.InstanceGroups), len(input.InstanceGroup), t) + compareInts(len(got.InstanceGroups[0].AttachedVolumeConfiguration), len(avcs), t) + + resultAvcs := got.InstanceGroups[0].AttachedVolumeConfiguration[0] + compareInt32PointerToTypesInt64(resultAvcs.VolumeCount, avcs[0].VolumeCount, t) + compareInt32PointerToTypesInt64(resultAvcs.VolumeSize, avcs[0].VolumeSize, t) + compareStrings(*resultAvcs.VolumeType, avcs[0].VolumeType.ValueString(), t) +} + +func TestFromModelToAzureRequestInstanceGroups(t *testing.T) { + igs := []InstanceGroup{{ + NodeCount: types.Int64Value(1), + InstanceGroupName: types.StringValue("gateway"), + InstanceGroupType: types.StringValue("CORE"), + InstanceType: types.StringValue("m5.xlarge"), + RootVolumeSize: types.Int64Value(100), + RecoveryMode: types.StringValue("MANUAL"), + }} + + input := datahubResourceModel{InstanceGroup: igs} + + got := fromModelToAzureRequest(input, nil) + + compareInts(len(got.InstanceGroups), len(igs), t) + resultIg := got.InstanceGroups[0] + compareStrings(*resultIg.InstanceGroupName, igs[0].InstanceGroupName.ValueString(), t) + compareStrings(*resultIg.InstanceGroupType, igs[0].InstanceGroupType.ValueString(), t) + compareStrings(*resultIg.InstanceType, igs[0].InstanceType.ValueString(), t) + compareInt32PointerToTypesInt64(resultIg.RootVolumeSize, igs[0].RootVolumeSize, t) + compareStrings(resultIg.RecoveryMode, igs[0].RecoveryMode.ValueString(), t) +} + +func TestFromModelToAzureRequestClusterDefinition(t *testing.T) { + input := datahubResourceModel{ClusterDefinition: types.StringValue("SomeClusterDef")} + got := fromModelToAzureRequest(input, nil) + + compareStrings(got.ClusterDefinitionName, input.ClusterDefinition.ValueString(), t) +} + func compareStrings(got string, expected string, t *testing.T) { if got != expected { t.Errorf("Assertion error! Expected: %s, got: %s", expected, got) diff --git a/resources/datahub/gcp_model_datahub_test.go b/resources/datahub/gcp_model_datahub_test.go new file mode 100644 index 00000000..6157c0af --- /dev/null +++ b/resources/datahub/gcp_model_datahub_test.go @@ -0,0 +1,52 @@ +// Copyright 2023 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 datahub + +import ( + "github.com/hashicorp/terraform-plugin-framework/types" + "testing" +) + +func TestForceDeleteRequestedForGcp(t *testing.T) { + tests := []struct { + name string + model *gcpDatahubResourceModel + expectedResult bool + }{ + { + name: "when DestroyOptions nil", + model: &gcpDatahubResourceModel{DestroyOptions: nil}, + expectedResult: false, + }, + { + name: "when DestroyOptions not nil but ForceDeleteCluster is", + model: &gcpDatahubResourceModel{DestroyOptions: &DestroyOptions{ForceDeleteCluster: types.BoolNull()}}, + expectedResult: false, + }, + { + name: "when neither DestroyOptions or ForceDeleteCluster are nil but ForceDeleteCluster is false", + model: &gcpDatahubResourceModel{DestroyOptions: &DestroyOptions{ForceDeleteCluster: types.BoolValue(false)}}, + expectedResult: false, + }, + { + name: "when ForceDeleteCluster is true", + model: &gcpDatahubResourceModel{DestroyOptions: &DestroyOptions{ForceDeleteCluster: types.BoolValue(true)}}, + expectedResult: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if test.model.forceDeleteRequested() != test.expectedResult { + t.Errorf("Did not get the expected output! Expected: %t, got: %t", test.expectedResult, test.model.forceDeleteRequested()) + } + }) + } +} diff --git a/resources/datahub/utils_test.go b/resources/datahub/utils_test.go index b309fdf2..652b30f9 100644 --- a/resources/datahub/utils_test.go +++ b/resources/datahub/utils_test.go @@ -81,6 +81,33 @@ func TestIsNotFoundError(t *testing.T) { } } +func TestIsInternalServerError(t *testing.T) { + type testCase struct { + description string + input operations.DescribeClusterDefault + expectedResult bool + } + for _, scenario := range []testCase{ + { + description: "Test with status: NOT_FOUND", + input: createDefaultInputWithStatus("NOT_FOUND", "Clustar cannot be found."), + expectedResult: false, + }, + { + description: "Test with status: UNKNOWN", + input: createDefaultInputWithStatus("UNKNOWN", "Internal server error occurred."), + expectedResult: true, + }, + } { + t.Run(scenario.description, func(t *testing.T) { + result := isInternalServerError(&scenario.input) + if scenario.expectedResult != result { + t.Errorf("Test result ('%t') does not match with the expectation ('%t')", result, scenario.expectedResult) + } + }) + } +} + func createOkInputWithStatus(status string) operations.DescribeClusterOK { sum := &models.Cluster{Status: status} pl := &models.DescribeClusterResponse{Cluster: sum}