From 2aed2fd96fd8de5cb409a9f6b5ddd9cf1ad77446 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 1 Jul 2014 10:28:42 -0700 Subject: [PATCH] Fix various order-dependent tests --- config/loader_test.go | 40 ++++++++++++++++++++++++++++++++----- depgraph/graph_test.go | 11 ++++++++-- digraph/tarjan_test.go | 17 +++++++++------- terraform/state.go | 9 ++++++++- terraform/terraform_test.go | 16 +++++++-------- 5 files changed, 70 insertions(+), 23 deletions(-) diff --git a/config/loader_test.go b/config/loader_test.go index 7e319a197..e447bfb8e 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -3,6 +3,7 @@ package config import ( "fmt" "path/filepath" + "sort" "strings" "testing" ) @@ -95,10 +96,25 @@ func TestLoad_variables(t *testing.T) { // string value for comparison in tests. func providerConfigsStr(pcs map[string]*ProviderConfig) string { result := "" - for n, pc := range pcs { + + ns := make([]string, 0, len(pcs)) + for n, _ := range pcs { + ns = append(ns, n) + } + sort.Strings(ns) + + for _, n := range ns { + pc := pcs[n] + result += fmt.Sprintf("%s\n", n) + keys := make([]string, 0, len(pc.RawConfig.Raw)) for k, _ := range pc.RawConfig.Raw { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { result += fmt.Sprintf(" %s\n", k) } @@ -133,7 +149,13 @@ func resourcesStr(rs []*Resource) string { r.Type, r.Name) + ks := make([]string, 0, len(r.RawConfig.Raw)) for k, _ := range r.RawConfig.Raw { + ks = append(ks, k) + } + sort.Strings(ks) + + for _, k := range ks { result += fmt.Sprintf(" %s\n", k) } @@ -162,7 +184,15 @@ func resourcesStr(rs []*Resource) string { // string value for comparison in tests. func variablesStr(vs map[string]*Variable) string { result := "" - for k, v := range vs { + ks := make([]string, 0, len(vs)) + for k, _ := range vs { + ks = append(ks, k) + } + sort.Strings(ks) + + for _, k := range ks { + v := vs[k] + if v.Default == "" { v.Default = "<>" } @@ -226,13 +256,13 @@ foo ` const variablesVariablesStr = ` -foo - <> - <> bar <> <> baz foo <> +foo + <> + <> ` diff --git a/depgraph/graph_test.go b/depgraph/graph_test.go index a6dc60a1f..9b943c8df 100644 --- a/depgraph/graph_test.go +++ b/depgraph/graph_test.go @@ -3,6 +3,7 @@ package depgraph import ( "fmt" "reflect" + "sort" "strings" "sync" "testing" @@ -152,10 +153,16 @@ d -> b`) } cycle := vErr.Cycles[0] - if cycle[0].Name != "d" { + cycleNodes := make([]string, len(cycle)) + for i, c := range cycle { + cycleNodes[i] = c.Name + } + sort.Strings(cycleNodes) + + if cycleNodes[0] != "b" { t.Fatalf("bad: %v", cycle) } - if cycle[1].Name != "b" { + if cycleNodes[1] != "d" { t.Fatalf("bad: %v", cycle) } } diff --git a/digraph/tarjan_test.go b/digraph/tarjan_test.go index 10def8051..86f0904a3 100644 --- a/digraph/tarjan_test.go +++ b/digraph/tarjan_test.go @@ -1,6 +1,8 @@ package digraph import ( + "reflect" + "sort" "testing" ) @@ -63,13 +65,14 @@ g -> a t.Fatalf("bad: %v", sccs) } - if cycle[0].(*BasicNode).Name != "g" { - t.Fatalf("bad: %v", cycle) + cycleNodes := make([]string, len(cycle)) + for i, c := range cycle { + cycleNodes[i] = c.(*BasicNode).Name } - if cycle[1].(*BasicNode).Name != "c" { - t.Fatalf("bad: %v", cycle) - } - if cycle[2].(*BasicNode).Name != "a" { - t.Fatalf("bad: %v", cycle) + sort.Strings(cycleNodes) + + expected := []string{"a", "c", "g"} + if !reflect.DeepEqual(cycleNodes, expected) { + t.Fatalf("bad: %#v", cycleNodes) } } diff --git a/terraform/state.go b/terraform/state.go index 4ac4d09b2..171870d0b 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -71,7 +71,14 @@ func (s *State) String() string { buf.WriteString(fmt.Sprintf("%s:\n", k)) buf.WriteString(fmt.Sprintf(" ID = %s\n", id)) - for ak, av := range rs.Attributes { + attrKeys := make([]string, 0, len(rs.Attributes)) + for ak, _ := range rs.Attributes { + attrKeys = append(attrKeys, ak) + } + sort.Strings(attrKeys) + + for _, ak := range attrKeys { + av := rs.Attributes[ak] buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av)) } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index dea495dd2..6350eb50c 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -675,24 +675,24 @@ func (h *HookRecordApplyOrder) PreApply( const testTerraformApplyStr = ` aws_instance.bar: ID = foo - type = aws_instance foo = bar + type = aws_instance aws_instance.foo: ID = foo - type = aws_instance num = 2 + type = aws_instance ` const testTerraformApplyComputeStr = ` aws_instance.bar: ID = foo - type = aws_instance foo = computed_dynamical + type = aws_instance aws_instance.foo: ID = foo - type = aws_instance - num = 2 dynamical = computed_dynamical + num = 2 + type = aws_instance ` const testTerraformApplyDestroyStr = ` @@ -705,19 +705,19 @@ aws_instance.foo: const testTerraformApplyUnknownAttrStr = ` aws_instance.foo: ID = foo - type = aws_instance num = 2 + type = aws_instance ` const testTerraformApplyVarsStr = ` aws_instance.bar: ID = foo - type = aws_instance foo = bar + type = aws_instance aws_instance.foo: ID = foo - type = aws_instance num = 2 + type = aws_instance ` const testTerraformPlanStr = `