Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDPCP-10154 - increase unit test coverage for the datahub package #63

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
- name: Go Coverage
uses: gwatts/[email protected]
with:
coverage-threshold: 10.1
coverage-threshold: 17.3
cover-pkg: ./...
ignore-pattern: |
/cdp-sdk-go/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" {

instance_group = [
{
node_count = 0
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
node_count = 0
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
attached_volume_configuration = [
{
volume_size = 100
Expand All @@ -35,11 +35,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" {
recipes = []
},
{
node_count = 1
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
node_count = 1
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
attached_volume_configuration = [
{
volume_size = 100
Expand All @@ -51,11 +51,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" {
recipes = []
},
{
node_count = 3
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
node_count = 3
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
attached_volume_configuration = [
{
volume_size = 300
Expand All @@ -67,11 +67,11 @@ resource "cdp_datahub_gcp_cluster" "gcp-cluster" {
recipes = []
},
{
node_count = 3
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
node_count = 3
instance_group_name = "<value>"
instance_group_type = "<value>"
instance_type = "<value>"
root_volume_size = 100
attached_volume_configuration = [
{
volume_size = 300
Expand Down
4 changes: 2 additions & 2 deletions examples/resources/cdp_datalake_gcp_datalake/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// permissions and limitations governing your use of the file.

resource "cdp_datalake_gcp_datalake" "example" {
datalake_name = "<value>"
environment_name = "<value>"
datalake_name = "<value>"
environment_name = "<value>"
cloud_provider_configuration = {
service_account_email = "<value>"
storage_location = "<value>"
Expand Down
86 changes: 86 additions & 0 deletions resources/datahub/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions resources/datahub/gcp_model_datahub_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
}
27 changes: 27 additions & 0 deletions resources/datahub/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Loading