github.com/medzin/terraform@v0.11.11/moduledeps/module_test.go (about) 1 package moduledeps 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/plugin/discovery" 9 ) 10 11 func TestModuleWalkTree(t *testing.T) { 12 type walkStep struct { 13 Path []string 14 ParentName string 15 } 16 17 tests := []struct { 18 Root *Module 19 WalkOrder []walkStep 20 }{ 21 { 22 &Module{ 23 Name: "root", 24 Children: nil, 25 }, 26 []walkStep{ 27 { 28 Path: []string{"root"}, 29 ParentName: "", 30 }, 31 }, 32 }, 33 { 34 &Module{ 35 Name: "root", 36 Children: []*Module{ 37 { 38 Name: "child", 39 }, 40 }, 41 }, 42 []walkStep{ 43 { 44 Path: []string{"root"}, 45 ParentName: "", 46 }, 47 { 48 Path: []string{"root", "child"}, 49 ParentName: "root", 50 }, 51 }, 52 }, 53 { 54 &Module{ 55 Name: "root", 56 Children: []*Module{ 57 { 58 Name: "child", 59 Children: []*Module{ 60 { 61 Name: "grandchild", 62 }, 63 }, 64 }, 65 }, 66 }, 67 []walkStep{ 68 { 69 Path: []string{"root"}, 70 ParentName: "", 71 }, 72 { 73 Path: []string{"root", "child"}, 74 ParentName: "root", 75 }, 76 { 77 Path: []string{"root", "child", "grandchild"}, 78 ParentName: "child", 79 }, 80 }, 81 }, 82 { 83 &Module{ 84 Name: "root", 85 Children: []*Module{ 86 { 87 Name: "child1", 88 Children: []*Module{ 89 { 90 Name: "grandchild1", 91 }, 92 }, 93 }, 94 { 95 Name: "child2", 96 Children: []*Module{ 97 { 98 Name: "grandchild2", 99 }, 100 }, 101 }, 102 }, 103 }, 104 []walkStep{ 105 { 106 Path: []string{"root"}, 107 ParentName: "", 108 }, 109 { 110 Path: []string{"root", "child1"}, 111 ParentName: "root", 112 }, 113 { 114 Path: []string{"root", "child1", "grandchild1"}, 115 ParentName: "child1", 116 }, 117 { 118 Path: []string{"root", "child2"}, 119 ParentName: "root", 120 }, 121 { 122 Path: []string{"root", "child2", "grandchild2"}, 123 ParentName: "child2", 124 }, 125 }, 126 }, 127 } 128 129 for i, test := range tests { 130 t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { 131 wo := test.WalkOrder 132 test.Root.WalkTree(func(path []string, parent *Module, current *Module) error { 133 if len(wo) == 0 { 134 t.Fatalf("ran out of walk steps while expecting one for %#v", path) 135 } 136 step := wo[0] 137 wo = wo[1:] 138 if got, want := path, step.Path; !reflect.DeepEqual(got, want) { 139 t.Errorf("wrong path %#v; want %#v", got, want) 140 } 141 parentName := "" 142 if parent != nil { 143 parentName = parent.Name 144 } 145 if got, want := parentName, step.ParentName; got != want { 146 t.Errorf("wrong parent name %q; want %q", got, want) 147 } 148 149 if got, want := current.Name, path[len(path)-1]; got != want { 150 t.Errorf("mismatching current.Name %q and final path element %q", got, want) 151 } 152 return nil 153 }) 154 }) 155 } 156 } 157 158 func TestModuleSortChildren(t *testing.T) { 159 m := &Module{ 160 Name: "root", 161 Children: []*Module{ 162 { 163 Name: "apple", 164 }, 165 { 166 Name: "zebra", 167 }, 168 { 169 Name: "xylophone", 170 }, 171 { 172 Name: "pig", 173 }, 174 }, 175 } 176 177 m.SortChildren() 178 179 want := []string{"apple", "pig", "xylophone", "zebra"} 180 var got []string 181 for _, c := range m.Children { 182 got = append(got, c.Name) 183 } 184 185 if !reflect.DeepEqual(want, got) { 186 t.Errorf("wrong order %#v; want %#v", want, got) 187 } 188 } 189 190 func TestModulePluginRequirements(t *testing.T) { 191 m := &Module{ 192 Name: "root", 193 Providers: Providers{ 194 "foo": ProviderDependency{ 195 Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(), 196 }, 197 "foo.bar": ProviderDependency{ 198 Constraints: discovery.ConstraintStr(">=2.0.0").MustParse(), 199 }, 200 "baz": ProviderDependency{ 201 Constraints: discovery.ConstraintStr(">=3.0.0").MustParse(), 202 }, 203 }, 204 } 205 206 reqd := m.PluginRequirements() 207 if len(reqd) != 2 { 208 t.Errorf("wrong number of elements in %#v; want 2", reqd) 209 } 210 if got, want := reqd["foo"].Versions.String(), ">=1.0.0,>=2.0.0"; got != want { 211 t.Errorf("wrong combination of versions for 'foo' %q; want %q", got, want) 212 } 213 if got, want := reqd["baz"].Versions.String(), ">=3.0.0"; got != want { 214 t.Errorf("wrong combination of versions for 'baz' %q; want %q", got, want) 215 } 216 }