Skip to content

Commit 1ef8486

Browse files
authored
refactor: optimize slice allocations and meta checks (#655)
* refactor: optimize slice allocations and meta checks Preallocate slice capacities and consolidate meta check appends across the codebase to reduce allocations, clarify test check composition, and resolve golangci-lint errors in the GitHub Actions CI runs. Changes include preallocating error/slice capacities, replacing repeated make([]T,0) with capacity-aware makes (many mock tests use capacity 1 for reference arrays), and refactoring test check builders to use a metaChecks variable and a single composed slice before calling `resource.ComposeTestCheckFunc`. * fix: remove dead checks in network policy tests Removed empty if blocks in `checkNetworkPolicyResourceAttributes` and `verifyNetworkPolicyResourceCreation` that looked up `ENABLE_POLICY_ENV_TEST` but had no body. The checks were no-ops and triggered staticcheck SA4006 ("this value of found is never used"). Inline the workspace name directly, matching the pattern used in other policy acceptance tests. No test behavior changes. --------- Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent c7c58af commit 1ef8486

40 files changed

Lines changed: 133 additions & 130 deletions

File tree

internal/helper/openapi_v3_schema_validator/openapi_v3_schema_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type OpenAPIV3SchemaValidator struct {
4444
}
4545

4646
func (validator *OpenAPIV3SchemaValidator) ValidateRequiredFields(objectValues map[string]interface{}) (errs []error) {
47-
errs = make([]error, 0)
47+
errs = make([]error, 0, len(validator.Schema))
4848

4949
for k, v := range validator.Schema {
5050
objectValue := objectValues[k]

internal/resources/cluster/data_source_cluster_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ func TestAcceptanceForAttachClusterDataSource(t *testing.T) {
3636
}
3737

3838
func checkDataSourceAttributes() resource.TestCheckFunc {
39-
var check = []resource.TestCheckFunc{
39+
metaChecks := testhelper.MetaDataSourceAttributeCheck(testhelper.ClusterDataSourceName, testhelper.ClusterResourceName)
40+
check := make([]resource.TestCheckFunc, 0, 3+len(metaChecks))
41+
check = append(check,
4042
verifyClusterDataSource(testhelper.ClusterDataSourceName),
4143
resource.TestCheckResourceAttrPair(testhelper.ClusterDataSourceName, "name", testhelper.ClusterResourceName, "name"),
4244
resource.TestCheckResourceAttrSet(testhelper.ClusterDataSourceName, "id"),
43-
}
44-
45-
check = append(check, testhelper.MetaDataSourceAttributeCheck(testhelper.ClusterDataSourceName, testhelper.ClusterResourceName)...)
45+
)
46+
check = append(check, metaChecks...)
4647

4748
return resource.ComposeTestCheckFunc(check...)
4849
}

internal/resources/cluster/nodepools/resource_node_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func flattenTkgServiceVsphere(tkgServiceVsphere *nodepoolmodel.VmwareTanzuManage
547547
flattenTkgServiceVsphereData[storageClassKey] = tkgServiceVsphere.StorageClass
548548
flattenTkgServiceVsphereData[failureDomainKey] = tkgServiceVsphere.FailureDomain
549549

550-
vls := make([]interface{}, 0)
550+
vls := make([]interface{}, 0, len(tkgServiceVsphere.Volumes))
551551
for _, vl := range tkgServiceVsphere.Volumes {
552552
vls = append(vls, flattenTKGSVolumes(vl))
553553
}

internal/resources/cluster/tkgaws/resource_tkg_aws.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func ConstructTKGAWSClusterSpec(data []interface{}) (spec *tkgawsmodel.VmwareTan
6666
func FlattenTKGAWSClusterSpec(spec *tkgawsmodel.VmwareTanzuManageV1alpha1ClusterInfrastructureTkgawsSpec) (data []interface{}) {
6767
flattenSpecData := make(map[string]interface{})
6868

69-
acs := make([]interface{}, 0)
69+
acs := make([]interface{}, 0, len(spec.AdvancedConfigs))
7070

7171
for _, ac := range spec.AdvancedConfigs {
7272
acs = append(acs, common.FlattenAdvancedConfig(ac))

internal/resources/cluster/tkgvsphere/resource_tkgvsphere.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func ConstructTKGVsphereClusterSpec(data []interface{}) (spec *tkgvspheremodel.V
6868
func FlattenTKGVsphereClusterSpec(spec *tkgvspheremodel.VmwareTanzuManageV1alpha1ClusterInfrastructureTkgvsphereSpec) (data []interface{}) {
6969
flattenSpecData := make(map[string]interface{})
7070

71-
acs := make([]interface{}, 0)
71+
acs := make([]interface{}, 0, len(spec.AdvancedConfigs))
7272

7373
for _, ac := range spec.AdvancedConfigs {
7474
acs = append(acs, common.FlattenAdvancedConfig(ac))

internal/resources/clustergroup/data_source_cluster_group_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ data "%s" "%s" {
6161
}
6262

6363
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
64-
var check = []resource.TestCheckFunc{
64+
metaChecks := testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)
65+
check := make([]resource.TestCheckFunc, 0, 3+len(metaChecks))
66+
check = append(check,
6567
verifyClusterGroupDataSource(dataSourceName),
6668
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
6769
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
68-
}
69-
70-
check = append(check, testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)...)
70+
)
71+
check = append(check, metaChecks...)
7172

7273
return resource.ComposeTestCheckFunc(check...)
7374
}

internal/resources/clustergroup/resource_cluster_group_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ resource "%s" "%s" {
7171
}
7272

7373
func checkResourceAttributes(provider *schema.Provider, resourceName, clusterGroupName string) resource.TestCheckFunc {
74-
var check = []resource.TestCheckFunc{
74+
metaChecks := testhelper.MetaResourceAttributeCheck(resourceName)
75+
check := make([]resource.TestCheckFunc, 0, 2+len(metaChecks))
76+
check = append(check,
7577
verifyClusterGroupResourceCreation(provider, resourceName, clusterGroupName),
7678
resource.TestCheckResourceAttr(resourceName, "name", clusterGroupName),
77-
}
78-
79-
check = append(check, testhelper.MetaResourceAttributeCheck(resourceName)...)
79+
)
80+
check = append(check, metaChecks...)
8081

8182
return resource.ComposeTestCheckFunc(check...)
8283
}

internal/resources/credential/data_source_credential_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ data "%s" "%s" {
7171
}
7272

7373
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
74-
var check = []resource.TestCheckFunc{
75-
verifyClusterGroupDataSource(dataSourceName),
76-
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
77-
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
78-
}
79-
8074
checks := []resource.TestCheckFunc{
8175
resource.TestCheckResourceAttr(resourceName, "meta.#", "1"),
8276
resource.TestCheckResourceAttrSet(resourceName, "meta.0.uid"),
8377
}
8478

79+
check := make([]resource.TestCheckFunc, 0, 3+len(checks))
80+
check = append(check,
81+
verifyClusterGroupDataSource(dataSourceName),
82+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
83+
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
84+
)
8585
check = append(check, checks...)
8686

8787
return resource.ComposeTestCheckFunc(check...)

internal/resources/credential/resource_credential_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,16 @@ resource "%s" "%s" {
171171
}
172172

173173
func checkResourceAttributes(provider *schema.Provider, resourceName, credentialName string) resource.TestCheckFunc {
174-
var check = []resource.TestCheckFunc{
175-
verifyCredentialResourceCreation(provider, resourceName, credentialName),
176-
resource.TestCheckResourceAttr(resourceName, "name", credentialName),
177-
}
178-
179174
checks := []resource.TestCheckFunc{
180175
resource.TestCheckResourceAttr(resourceName, "meta.#", "1"),
181176
resource.TestCheckResourceAttrSet(resourceName, "meta.0.uid"),
182177
}
183178

179+
check := make([]resource.TestCheckFunc, 0, 2+len(checks))
180+
check = append(check,
181+
verifyCredentialResourceCreation(provider, resourceName, credentialName),
182+
resource.TestCheckResourceAttr(resourceName, "name", credentialName),
183+
)
184184
check = append(check, checks...)
185185

186186
return resource.ComposeTestCheckFunc(check...)

internal/resources/customiamrole/resource_custom_iam_role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func formatAggregationRuleData(tfAggregationRule []interface{}, modelAggregation
241241
}
242242
}
243243

244-
aggregationRule = make([]interface{}, 0)
244+
aggregationRule = make([]interface{}, 0, 1)
245245
aggregationRuleMap := make(map[string]interface{})
246246
aggregationRuleMap[ClusterRoleSelectorKey] = clusterRoleSelector
247247
aggregationRule = append(aggregationRule, aggregationRuleMap)

0 commit comments

Comments
 (0)