github.com/magodo/terraform@v0.11.12-beta1/terraform/module_dependencies_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/davecgh/go-spew/spew" 7 "github.com/hashicorp/terraform/config/module" 8 "github.com/hashicorp/terraform/moduledeps" 9 "github.com/hashicorp/terraform/plugin/discovery" 10 ) 11 12 func TestModuleTreeDependencies(t *testing.T) { 13 tests := map[string]struct { 14 ConfigDir string // directory name from test-fixtures dir 15 State *State 16 Want *moduledeps.Module 17 }{ 18 "no config or state": { 19 "", 20 nil, 21 &moduledeps.Module{ 22 Name: "root", 23 Providers: moduledeps.Providers{}, 24 Children: nil, 25 }, 26 }, 27 "empty config no state": { 28 "empty", 29 nil, 30 &moduledeps.Module{ 31 Name: "root", 32 Providers: moduledeps.Providers{}, 33 Children: nil, 34 }, 35 }, 36 "explicit provider": { 37 "module-deps-explicit-provider", 38 nil, 39 &moduledeps.Module{ 40 Name: "root", 41 Providers: moduledeps.Providers{ 42 "foo": moduledeps.ProviderDependency{ 43 Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(), 44 Reason: moduledeps.ProviderDependencyExplicit, 45 }, 46 "foo.bar": moduledeps.ProviderDependency{ 47 Constraints: discovery.ConstraintStr(">=2.0.0").MustParse(), 48 Reason: moduledeps.ProviderDependencyExplicit, 49 }, 50 }, 51 Children: nil, 52 }, 53 }, 54 "explicit provider unconstrained": { 55 "module-deps-explicit-provider-unconstrained", 56 nil, 57 &moduledeps.Module{ 58 Name: "root", 59 Providers: moduledeps.Providers{ 60 "foo": moduledeps.ProviderDependency{ 61 Constraints: discovery.AllVersions, 62 Reason: moduledeps.ProviderDependencyExplicit, 63 }, 64 }, 65 Children: nil, 66 }, 67 }, 68 "implicit provider": { 69 "module-deps-implicit-provider", 70 nil, 71 &moduledeps.Module{ 72 Name: "root", 73 Providers: moduledeps.Providers{ 74 "foo": moduledeps.ProviderDependency{ 75 Constraints: discovery.AllVersions, 76 Reason: moduledeps.ProviderDependencyImplicit, 77 }, 78 "foo.baz": moduledeps.ProviderDependency{ 79 Constraints: discovery.AllVersions, 80 Reason: moduledeps.ProviderDependencyImplicit, 81 }, 82 }, 83 Children: nil, 84 }, 85 }, 86 "explicit provider with resource": { 87 "module-deps-explicit-provider-resource", 88 nil, 89 &moduledeps.Module{ 90 Name: "root", 91 Providers: moduledeps.Providers{ 92 "foo": moduledeps.ProviderDependency{ 93 Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(), 94 Reason: moduledeps.ProviderDependencyExplicit, 95 }, 96 }, 97 Children: nil, 98 }, 99 }, 100 "inheritance of providers": { 101 "module-deps-inherit-provider", 102 nil, 103 &moduledeps.Module{ 104 Name: "root", 105 Providers: moduledeps.Providers{ 106 "foo": moduledeps.ProviderDependency{ 107 Constraints: discovery.AllVersions, 108 Reason: moduledeps.ProviderDependencyExplicit, 109 }, 110 "bar": moduledeps.ProviderDependency{ 111 Constraints: discovery.AllVersions, 112 Reason: moduledeps.ProviderDependencyExplicit, 113 }, 114 }, 115 Children: []*moduledeps.Module{ 116 { 117 Name: "child", 118 Providers: moduledeps.Providers{ 119 "foo": moduledeps.ProviderDependency{ 120 Constraints: discovery.AllVersions, 121 Reason: moduledeps.ProviderDependencyInherited, 122 }, 123 "baz": moduledeps.ProviderDependency{ 124 Constraints: discovery.AllVersions, 125 Reason: moduledeps.ProviderDependencyImplicit, 126 }, 127 }, 128 Children: []*moduledeps.Module{ 129 { 130 Name: "grandchild", 131 Providers: moduledeps.Providers{ 132 "bar": moduledeps.ProviderDependency{ 133 Constraints: discovery.AllVersions, 134 Reason: moduledeps.ProviderDependencyInherited, 135 }, 136 "foo": moduledeps.ProviderDependency{ 137 Constraints: discovery.AllVersions, 138 Reason: moduledeps.ProviderDependencyExplicit, 139 }, 140 }, 141 }, 142 }, 143 }, 144 }, 145 }, 146 }, 147 "provider from state": { 148 "empty", 149 &State{ 150 Modules: []*ModuleState{ 151 { 152 Path: []string{"root"}, 153 Resources: map[string]*ResourceState{ 154 "foo_bar.baz": { 155 Type: "foo_bar", 156 Provider: "", 157 }, 158 }, 159 }, 160 }, 161 }, 162 &moduledeps.Module{ 163 Name: "root", 164 Providers: moduledeps.Providers{ 165 "foo": moduledeps.ProviderDependency{ 166 Constraints: discovery.AllVersions, 167 Reason: moduledeps.ProviderDependencyFromState, 168 }, 169 }, 170 Children: nil, 171 }, 172 }, 173 "providers in both config and state": { 174 "module-deps-explicit-provider", 175 &State{ 176 Modules: []*ModuleState{ 177 { 178 Path: []string{"root"}, 179 Resources: map[string]*ResourceState{ 180 "foo_bar.test1": { 181 Type: "foo_bar", 182 Provider: "", 183 }, 184 "foo_bar.test2": { 185 Type: "foo_bar", 186 Provider: "foo.bar", 187 }, 188 "baz_bar.test": { 189 Type: "baz_bar", 190 Provider: "", 191 }, 192 }, 193 }, 194 // note that we've skipped root.child intentionally here, 195 // to verify that we'll infer it based on the following 196 // module rather than crashing. 197 { 198 Path: []string{"root", "child", "grandchild"}, 199 Resources: map[string]*ResourceState{ 200 "banana_skin.test": { 201 Type: "banana_skin", 202 Provider: "", 203 }, 204 }, 205 }, 206 }, 207 }, 208 &moduledeps.Module{ 209 Name: "root", 210 Providers: moduledeps.Providers{ 211 "foo": moduledeps.ProviderDependency{ 212 Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(), 213 Reason: moduledeps.ProviderDependencyExplicit, 214 }, 215 "foo.bar": moduledeps.ProviderDependency{ 216 Constraints: discovery.ConstraintStr(">=2.0.0").MustParse(), 217 Reason: moduledeps.ProviderDependencyExplicit, 218 }, 219 "baz": moduledeps.ProviderDependency{ 220 Constraints: discovery.AllVersions, 221 Reason: moduledeps.ProviderDependencyFromState, 222 }, 223 }, 224 Children: []*moduledeps.Module{ 225 { 226 Name: "child", 227 Children: []*moduledeps.Module{ 228 { 229 Name: "grandchild", 230 Providers: moduledeps.Providers{ 231 "banana": moduledeps.ProviderDependency{ 232 Constraints: discovery.AllVersions, 233 Reason: moduledeps.ProviderDependencyFromState, 234 }, 235 }, 236 }, 237 }, 238 }, 239 }, 240 }, 241 }, 242 } 243 244 for name, test := range tests { 245 t.Run(name, func(t *testing.T) { 246 var root *module.Tree 247 if test.ConfigDir != "" { 248 root = testModule(t, test.ConfigDir) 249 } 250 251 got := ModuleTreeDependencies(root, test.State) 252 if !got.Equal(test.Want) { 253 t.Errorf( 254 "wrong dependency tree\ngot: %s\nwant: %s", 255 spew.Sdump(got), 256 spew.Sdump(test.Want), 257 ) 258 } 259 }) 260 } 261 }