Skip to content

Commit 6d08f97

Browse files
committed
order attributes to make tests deterministic
1 parent 658e124 commit 6d08f97

File tree

1 file changed

+64
-84
lines changed

1 file changed

+64
-84
lines changed

internal/convert/adv2v2.go

Lines changed: 64 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -200,51 +200,25 @@ func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSi
200200
return nil
201201
}
202202

203-
// Helper function to add attributes in order
204-
func addAttributesInOrder(targetBody *hclwrite.Body, sourceAttrs map[string]*hclwrite.Attribute,
205-
orderedNames []string) {
206-
// Add ordered attributes first
207-
for _, name := range orderedNames {
208-
if attr, exists := sourceAttrs[name]; exists {
209-
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(hcl.GetAttrExpr(attr)))
210-
}
211-
}
212-
213-
// Add any remaining attributes alphabetically
214-
var remainingAttrs []string
215-
for name := range sourceAttrs {
216-
found := false
217-
for _, orderedName := range orderedNames {
218-
if name == orderedName {
219-
found = true
220-
break
221-
}
222-
}
223-
if !found {
224-
remainingAttrs = append(remainingAttrs, name)
225-
}
226-
}
227-
slices.Sort(remainingAttrs)
228-
for _, name := range remainingAttrs {
229-
attr := sourceAttrs[name]
230-
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(hcl.GetAttrExpr(attr)))
231-
}
232-
}
233-
234203
// Helper function to process blocks for region configs
235204
func processRegionConfigBlocks(targetBody *hclwrite.Body, blocks []*hclwrite.Block) {
236205
for _, block := range blocks {
237206
blockType := block.Type()
238207
blockFile := hclwrite.NewEmptyFile()
239208
blockBody := blockFile.Body()
240209

241-
// For electable_specs, use specific order: instance_size, node_count
242-
var attrOrder []string
243-
if blockType == "electable_specs" {
244-
attrOrder = []string{"instance_size", "node_count"}
210+
// Copy all attributes in deterministic order
211+
attrs := block.Body().Attributes()
212+
var names []string
213+
for name := range attrs {
214+
names = append(names, name)
215+
}
216+
slices.Sort(names)
217+
for _, name := range names {
218+
attr := attrs[name]
219+
blockBody.SetAttributeRaw(name, hcl.TokensFromExpr(hcl.GetAttrExpr(attr)))
245220
}
246221

247-
addAttributesInOrder(blockBody, block.Body().Attributes(), attrOrder)
248222
targetBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
249223
}
250224
}
@@ -261,14 +235,28 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
261235

262236
// Transform references in place for the dynamic config content
263237
transformDynamicBlockReferencesRecursive(dConfig.content.Body(), configBlockName, nRegion)
264-
// Also transform outer references
265-
for name, attr := range dConfig.content.Body().Attributes() {
238+
// Also transform outer references (with deterministic ordering)
239+
attrs := dConfig.content.Body().Attributes()
240+
var attrNames []string
241+
for name := range attrs {
242+
attrNames = append(attrNames, name)
243+
}
244+
slices.Sort(attrNames)
245+
for _, name := range attrNames {
246+
attr := attrs[name]
266247
expr := hcl.GetAttrExpr(attr)
267248
expr = replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
268249
dConfig.content.Body().SetAttributeRaw(name, hcl.TokensFromExpr(expr))
269250
}
270251
for _, block := range dConfig.content.Body().Blocks() {
271-
for name, attr := range block.Body().Attributes() {
252+
blockAttrs := block.Body().Attributes()
253+
var blockAttrNames []string
254+
for name := range blockAttrs {
255+
blockAttrNames = append(blockAttrNames, name)
256+
}
257+
slices.Sort(blockAttrNames)
258+
for _, name := range blockAttrNames {
259+
attr := blockAttrs[name]
272260
expr := hcl.GetAttrExpr(attr)
273261
expr = replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
274262
block.Body().SetAttributeRaw(name, hcl.TokensFromExpr(expr))
@@ -283,9 +271,17 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
283271
regionConfigFile := hclwrite.NewEmptyFile()
284272
regionConfigBody := regionConfigFile.Body()
285273

286-
// Add attributes in a specific order for consistency
287-
orderedAttrs := []string{"priority", "provider_name", "region_name"}
288-
addAttributesInOrder(regionConfigBody, dConfig.content.Body().Attributes(), orderedAttrs)
274+
// Copy all attributes in deterministic order
275+
configAttrs := dConfig.content.Body().Attributes()
276+
var configAttrNames []string
277+
for name := range configAttrs {
278+
configAttrNames = append(configAttrNames, name)
279+
}
280+
slices.Sort(configAttrNames)
281+
for _, name := range configAttrNames {
282+
attr := configAttrs[name]
283+
regionConfigBody.SetAttributeRaw(name, hcl.TokensFromExpr(hcl.GetAttrExpr(attr)))
284+
}
289285

290286
// Add all blocks generically as objects
291287
processRegionConfigBlocks(regionConfigBody, dConfig.content.Body().Blocks())
@@ -329,36 +325,21 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
329325

330326
// Helper function to add attributes with transformation
331327
func addAttributesWithTransform(targetBody *hclwrite.Body, sourceAttrs map[string]*hclwrite.Attribute,
332-
orderedNames []string, configBlockName string, transformFunc func(string) string) {
333-
// Add ordered attributes first
334-
for _, name := range orderedNames {
335-
if attr, exists := sourceAttrs[name]; exists {
336-
expr := hcl.GetAttrExpr(attr)
337-
// First replace nested dynamic block references
338-
expr = replaceDynamicBlockReferences(expr, configBlockName, nRegion)
339-
// Then replace outer dynamic block references
340-
expr = replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
341-
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
342-
}
328+
configBlockName string) {
329+
// Sort attribute names for deterministic output
330+
var names []string
331+
for name := range sourceAttrs {
332+
names = append(names, name)
343333
}
344-
345-
// Add any remaining attributes
346-
for name, attr := range sourceAttrs {
347-
found := false
348-
for _, orderedName := range orderedNames {
349-
if name == orderedName {
350-
found = true
351-
break
352-
}
353-
}
354-
if !found {
355-
expr := hcl.GetAttrExpr(attr)
356-
// First replace nested dynamic block references
357-
expr = replaceDynamicBlockReferences(expr, configBlockName, nRegion)
358-
// Then replace outer dynamic block references
359-
expr = replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
360-
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
361-
}
334+
slices.Sort(names)
335+
for _, name := range names {
336+
attr := sourceAttrs[name]
337+
expr := hcl.GetAttrExpr(attr)
338+
// First replace nested dynamic block references
339+
expr = replaceDynamicBlockReferences(expr, configBlockName, nRegion)
340+
// Then replace outer dynamic block references
341+
expr = replaceDynamicBlockReferences(expr, nRepSpecs, nSpec)
342+
targetBody.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
362343
}
363344
}
364345

@@ -377,22 +358,14 @@ func convertDynamicRepSpecsWithoutNumShards(resourceb *hclwrite.Body, dSpec, dCo
377358
configFile := hclwrite.NewEmptyFile()
378359
configb := configFile.Body()
379360

380-
// Copy and transform attributes in a specific order for consistency
381-
orderedAttrs := []string{"priority", "provider_name", "region_name"}
382-
addAttributesWithTransform(configb, dConfig.content.Body().Attributes(), orderedAttrs, configBlockName, nil)
361+
// Copy and transform attributes
362+
addAttributesWithTransform(configb, dConfig.content.Body().Attributes(), configBlockName)
383363

384364
// Process blocks and transform their references
385365
for _, block := range dConfig.content.Body().Blocks() {
386366
newBlock := configb.AppendNewBlock(block.Type(), block.Labels())
387367
newBlockb := newBlock.Body()
388-
389-
// Order attributes for consistency
390-
var attrOrder []string
391-
if block.Type() == "electable_specs" {
392-
attrOrder = []string{"instance_size", "node_count"}
393-
}
394-
395-
addAttributesWithTransform(newBlockb, block.Body().Attributes(), attrOrder, configBlockName, nil)
368+
addAttributesWithTransform(newBlockb, block.Body().Attributes(), configBlockName)
396369
}
397370

398371
// Process specs
@@ -494,8 +467,15 @@ func convertDynamicConfig(repSpecs *hclwrite.Body, dConfig dynamicBlock, diskSiz
494467
}
495468

496469
func transformDynamicBlockReferencesRecursive(body *hclwrite.Body, blockName, varName string) {
497-
// Transform attributes
498-
for name, attr := range body.Attributes() {
470+
// Transform attributes in deterministic order
471+
attrs := body.Attributes()
472+
var names []string
473+
for name := range attrs {
474+
names = append(names, name)
475+
}
476+
slices.Sort(names)
477+
for _, name := range names {
478+
attr := attrs[name]
499479
expr := replaceDynamicBlockReferences(hcl.GetAttrExpr(attr), blockName, varName)
500480
body.SetAttributeRaw(name, hcl.TokensFromExpr(expr))
501481
}

0 commit comments

Comments
 (0)