2014-06-24 23:58:57 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-07-01 03:18:56 +02:00
|
|
|
"reflect"
|
2014-06-24 23:58:57 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-06-25 21:58:27 +02:00
|
|
|
func TestGraph(t *testing.T) {
|
2014-06-24 23:58:57 +02:00
|
|
|
config := testConfig(t, "graph-basic")
|
|
|
|
|
2014-07-01 06:21:13 +02:00
|
|
|
g, err := Graph(&GraphOpts{Config: config})
|
|
|
|
if err != nil {
|
2014-06-24 23:58:57 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 06:25:15 +02:00
|
|
|
func TestGraph_configRequired(t *testing.T) {
|
|
|
|
if _, err := Graph(new(GraphOpts)); err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:42:29 +02:00
|
|
|
func TestGraph_count(t *testing.T) {
|
|
|
|
config := testConfig(t, "graph-count")
|
|
|
|
|
|
|
|
g, err := Graph(&GraphOpts{Config: config})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphCountStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:58:27 +02:00
|
|
|
func TestGraph_cycle(t *testing.T) {
|
2014-06-24 23:58:57 +02:00
|
|
|
config := testConfig(t, "graph-cycle")
|
|
|
|
|
2014-07-01 06:21:13 +02:00
|
|
|
_, err := Graph(&GraphOpts{Config: config})
|
|
|
|
if err == nil {
|
2014-06-24 23:58:57 +02:00
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 03:20:03 +02:00
|
|
|
func TestGraph_dependsOn(t *testing.T) {
|
|
|
|
config := testConfig(t, "graph-depends-on")
|
|
|
|
|
|
|
|
g, err := Graph(&GraphOpts{Config: config})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphDependsStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 01:08:50 +02:00
|
|
|
func TestGraph_dependsOnCount(t *testing.T) {
|
|
|
|
config := testConfig(t, "graph-depends-on-count")
|
|
|
|
|
|
|
|
g, err := Graph(&GraphOpts{Config: config})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphDependsCountStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:58:27 +02:00
|
|
|
func TestGraph_state(t *testing.T) {
|
2014-06-24 23:58:57 +02:00
|
|
|
config := testConfig(t, "graph-basic")
|
|
|
|
state := &State{
|
2014-09-17 00:09:40 +02:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.old": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-24 23:58:57 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-01 06:21:13 +02:00
|
|
|
g, err := Graph(&GraphOpts{Config: config, State: state})
|
|
|
|
if err != nil {
|
2014-06-24 23:58:57 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphStateStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 23:53:32 +02:00
|
|
|
func TestGraphFull(t *testing.T) {
|
|
|
|
rpAws := new(MockResourceProvider)
|
|
|
|
rpOS := new(MockResourceProvider)
|
|
|
|
|
|
|
|
rpAws.ResourcesReturn = []ResourceType{
|
|
|
|
ResourceType{Name: "aws_instance"},
|
|
|
|
ResourceType{Name: "aws_load_balancer"},
|
|
|
|
ResourceType{Name: "aws_security_group"},
|
|
|
|
}
|
|
|
|
rpOS.ResourcesReturn = []ResourceType{
|
|
|
|
ResourceType{Name: "openstack_floating_ip"},
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(rpAws),
|
|
|
|
"open": testProviderFuncFixed(rpOS),
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, "graph-basic")
|
2014-07-01 06:21:13 +02:00
|
|
|
g, err := Graph(&GraphOpts{Config: c, Providers: ps})
|
|
|
|
if err != nil {
|
2014-06-25 23:53:32 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper to help get us the provider for a resource.
|
|
|
|
graphProvider := func(n string) ResourceProvider {
|
|
|
|
return g.Noun(n).Meta.(*GraphNodeResource).Resource.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a couple
|
|
|
|
if graphProvider("aws_instance.web") != rpAws {
|
|
|
|
t.Fatalf("bad: %#v", graphProvider("aws_instance.web"))
|
|
|
|
}
|
|
|
|
if graphProvider("openstack_floating_ip.random") != rpOS {
|
|
|
|
t.Fatalf("bad: %#v", graphProvider("openstack_floating_ip.random"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that all providers have been set
|
|
|
|
for _, n := range g.Nouns {
|
|
|
|
switch m := n.Meta.(type) {
|
|
|
|
case *GraphNodeResource:
|
|
|
|
if m.Resource.Provider == nil {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
case *GraphNodeResourceProvider:
|
|
|
|
if len(m.Providers) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", m)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 21:42:50 +02:00
|
|
|
func TestGraphProvisioners(t *testing.T) {
|
|
|
|
rpAws := new(MockResourceProvider)
|
|
|
|
provShell := new(MockResourceProvisioner)
|
|
|
|
provWinRM := new(MockResourceProvisioner)
|
|
|
|
|
|
|
|
rpAws.ResourcesReturn = []ResourceType{
|
|
|
|
ResourceType{Name: "aws_instance"},
|
|
|
|
ResourceType{Name: "aws_load_balancer"},
|
|
|
|
ResourceType{Name: "aws_security_group"},
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := map[string]ResourceProvisionerFactory{
|
|
|
|
"shell": testProvisionerFuncFixed(provShell),
|
|
|
|
"winrm": testProvisionerFuncFixed(provWinRM),
|
|
|
|
}
|
|
|
|
|
|
|
|
pf := map[string]ResourceProviderFactory{
|
|
|
|
"aws": testProviderFuncFixed(rpAws),
|
|
|
|
}
|
|
|
|
|
|
|
|
c := testConfig(t, "graph-provisioners")
|
|
|
|
g, err := Graph(&GraphOpts{Config: c, Providers: pf, Provisioners: ps})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper to help get us the provider for a resource.
|
|
|
|
graphProvisioner := func(n string, idx int) *ResourceProvisionerConfig {
|
|
|
|
return g.Noun(n).Meta.(*GraphNodeResource).Resource.Provisioners[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper to verify depedencies
|
|
|
|
depends := func(a, b string) bool {
|
|
|
|
aNoun := g.Noun(a)
|
|
|
|
bNoun := g.Noun(b)
|
|
|
|
for _, dep := range aNoun.Deps {
|
|
|
|
if dep.Source == aNoun && dep.Target == bNoun {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a couple
|
|
|
|
prov := graphProvisioner("aws_instance.web", 0)
|
|
|
|
if prov.Provisioner != provWinRM {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
|
|
|
if prov.RawConfig.Config()["cmd"] != "echo foo" {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
|
|
|
|
|
|
|
prov = graphProvisioner("aws_instance.web", 1)
|
|
|
|
if prov.Provisioner != provWinRM {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
|
|
|
if prov.RawConfig.Config()["cmd"] != "echo bar" {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
|
|
|
|
|
|
|
prov = graphProvisioner("aws_load_balancer.weblb", 0)
|
|
|
|
if prov.Provisioner != provShell {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
|
|
|
if prov.RawConfig.Config()["cmd"] != "add ${aws_instance.web.id}" {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
2014-07-11 20:49:22 +02:00
|
|
|
if prov.ConnInfo == nil || len(prov.ConnInfo.Raw) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", prov)
|
|
|
|
}
|
2014-07-08 21:42:50 +02:00
|
|
|
|
|
|
|
// Check that the variable dependency is handled
|
|
|
|
if !depends("aws_load_balancer.weblb", "aws_instance.web") {
|
|
|
|
t.Fatalf("missing dependency from provisioner variable")
|
|
|
|
}
|
2014-07-11 20:49:22 +02:00
|
|
|
|
|
|
|
// Check that the connection variable dependency is handled
|
|
|
|
if !depends("aws_load_balancer.weblb", "aws_security_group.firewall") {
|
|
|
|
t.Fatalf("missing dependency from provisioner connection")
|
|
|
|
}
|
2014-07-08 21:42:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-01 03:18:56 +02:00
|
|
|
func TestGraphAddDiff(t *testing.T) {
|
|
|
|
config := testConfig(t, "graph-diff")
|
|
|
|
diff := &Diff{
|
2014-09-18 01:33:24 +02:00
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"aws_instance.foo": &InstanceDiff{
|
2014-07-01 03:18:56 +02:00
|
|
|
Attributes: map[string]*ResourceAttrDiff{
|
|
|
|
"foo": &ResourceAttrDiff{
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-01 06:21:13 +02:00
|
|
|
g, err := Graph(&GraphOpts{Config: config, Diff: diff})
|
|
|
|
if err != nil {
|
2014-07-01 03:18:56 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphDiffStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the state has been added
|
|
|
|
n := g.Noun("aws_instance.foo")
|
|
|
|
rn := n.Meta.(*GraphNodeResource)
|
|
|
|
|
|
|
|
expected2 := diff.Resources["aws_instance.foo"]
|
|
|
|
actual2 := rn.Resource.Diff
|
|
|
|
if !reflect.DeepEqual(actual2, expected2) {
|
|
|
|
t.Fatalf("bad: %#v", actual2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:10:44 +02:00
|
|
|
func TestGraphAddDiff_destroy(t *testing.T) {
|
|
|
|
config := testConfig(t, "graph-diff-destroy")
|
2014-07-01 06:21:13 +02:00
|
|
|
diff := &Diff{
|
2014-09-18 01:33:24 +02:00
|
|
|
Resources: map[string]*InstanceDiff{
|
|
|
|
"aws_instance.foo": &InstanceDiff{
|
2014-07-01 06:21:13 +02:00
|
|
|
Destroy: true,
|
|
|
|
},
|
2014-09-18 01:33:24 +02:00
|
|
|
"aws_instance.bar": &InstanceDiff{
|
2014-07-01 06:21:13 +02:00
|
|
|
Destroy: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-07-01 04:10:44 +02:00
|
|
|
state := &State{
|
2014-09-17 00:09:40 +02:00
|
|
|
Modules: []*ModuleState{
|
|
|
|
&ModuleState{
|
|
|
|
Path: rootModulePath,
|
|
|
|
Resources: map[string]*ResourceState{
|
|
|
|
"aws_instance.foo": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
|
|
|
},
|
2014-07-01 04:10:44 +02:00
|
|
|
|
2014-09-17 00:09:40 +02:00
|
|
|
"aws_instance.bar": &ResourceState{
|
|
|
|
Type: "aws_instance",
|
|
|
|
Dependencies: []string{"foo"},
|
|
|
|
Primary: &InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
2014-07-01 04:10:44 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-07 21:53:01 +02:00
|
|
|
diffHash := checksumStruct(t, diff)
|
|
|
|
|
2014-07-01 06:21:13 +02:00
|
|
|
g, err := Graph(&GraphOpts{
|
|
|
|
Config: config,
|
|
|
|
Diff: diff,
|
|
|
|
State: state,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-07-01 04:10:44 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(g.String())
|
|
|
|
expected := strings.TrimSpace(testTerraformGraphDiffDestroyStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the state has been added
|
2014-07-07 21:53:01 +02:00
|
|
|
n := g.Noun("aws_instance.foo (destroy)")
|
2014-07-01 04:10:44 +02:00
|
|
|
rn := n.Meta.(*GraphNodeResource)
|
|
|
|
|
2014-09-18 01:33:24 +02:00
|
|
|
expected2 := &InstanceDiff{Destroy: true}
|
2014-07-01 04:10:44 +02:00
|
|
|
actual2 := rn.Resource.Diff
|
|
|
|
if !reflect.DeepEqual(actual2, expected2) {
|
|
|
|
t.Fatalf("bad: %#v", actual2)
|
|
|
|
}
|
2014-07-07 21:53:01 +02:00
|
|
|
|
|
|
|
// Verify that our original structure has not been modified
|
|
|
|
diffHash2 := checksumStruct(t, diff)
|
|
|
|
if diffHash != diffHash2 {
|
|
|
|
t.Fatal("diff has been modified")
|
|
|
|
}
|
2014-07-01 04:10:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 23:58:57 +02:00
|
|
|
const testTerraformGraphStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.web
|
|
|
|
aws_instance.web -> aws_security_group.firewall
|
|
|
|
aws_instance.web -> provider.aws
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_load_balancer.weblb -> aws_instance.web
|
|
|
|
aws_load_balancer.weblb -> provider.aws
|
|
|
|
aws_security_group.firewall
|
|
|
|
aws_security_group.firewall -> provider.aws
|
|
|
|
openstack_floating_ip.random
|
|
|
|
provider.aws
|
|
|
|
provider.aws -> openstack_floating_ip.random
|
|
|
|
root
|
|
|
|
root -> aws_instance.web
|
|
|
|
root -> aws_load_balancer.weblb
|
|
|
|
root -> aws_security_group.firewall
|
|
|
|
root -> openstack_floating_ip.random
|
|
|
|
`
|
|
|
|
|
2014-07-04 05:42:29 +02:00
|
|
|
const testTerraformGraphCountStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.web
|
|
|
|
aws_instance.web -> aws_instance.web.0
|
|
|
|
aws_instance.web -> aws_instance.web.1
|
|
|
|
aws_instance.web -> aws_instance.web.2
|
|
|
|
aws_instance.web.0
|
|
|
|
aws_instance.web.1
|
|
|
|
aws_instance.web.2
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_load_balancer.weblb -> aws_instance.web
|
|
|
|
root
|
|
|
|
root -> aws_instance.web
|
|
|
|
root -> aws_load_balancer.weblb
|
|
|
|
`
|
|
|
|
|
2014-07-23 03:20:03 +02:00
|
|
|
const testTerraformGraphDependsStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.db
|
|
|
|
aws_instance.db -> aws_instance.web
|
|
|
|
aws_instance.web
|
|
|
|
root
|
|
|
|
root -> aws_instance.db
|
|
|
|
root -> aws_instance.web
|
|
|
|
`
|
|
|
|
|
2014-08-31 01:08:50 +02:00
|
|
|
const testTerraformGraphDependsCountStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.db
|
|
|
|
aws_instance.db -> aws_instance.db.0
|
|
|
|
aws_instance.db -> aws_instance.db.1
|
|
|
|
aws_instance.db.0
|
|
|
|
aws_instance.db.0 -> aws_instance.web
|
|
|
|
aws_instance.db.1
|
|
|
|
aws_instance.db.1 -> aws_instance.web
|
|
|
|
aws_instance.web
|
|
|
|
root
|
|
|
|
root -> aws_instance.db
|
|
|
|
root -> aws_instance.web
|
|
|
|
`
|
|
|
|
|
2014-07-01 03:18:56 +02:00
|
|
|
const testTerraformGraphDiffStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.foo
|
|
|
|
root
|
|
|
|
root -> aws_instance.foo
|
|
|
|
`
|
|
|
|
|
2014-07-01 04:10:44 +02:00
|
|
|
const testTerraformGraphDiffDestroyStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.bar
|
|
|
|
aws_instance.bar -> aws_instance.bar (destroy)
|
2014-07-08 00:37:50 +02:00
|
|
|
aws_instance.bar -> aws_instance.foo
|
2014-07-08 00:43:32 +02:00
|
|
|
aws_instance.bar -> provider.aws
|
2014-07-01 04:10:44 +02:00
|
|
|
aws_instance.bar (destroy)
|
2014-07-08 00:43:32 +02:00
|
|
|
aws_instance.bar (destroy) -> provider.aws
|
2014-07-01 04:10:44 +02:00
|
|
|
aws_instance.foo
|
|
|
|
aws_instance.foo -> aws_instance.foo (destroy)
|
2014-07-08 00:43:32 +02:00
|
|
|
aws_instance.foo -> provider.aws
|
2014-07-01 04:10:44 +02:00
|
|
|
aws_instance.foo (destroy)
|
|
|
|
aws_instance.foo (destroy) -> aws_instance.bar (destroy)
|
2014-07-08 00:43:32 +02:00
|
|
|
aws_instance.foo (destroy) -> provider.aws
|
|
|
|
provider.aws
|
2014-07-01 04:10:44 +02:00
|
|
|
root
|
|
|
|
root -> aws_instance.bar
|
|
|
|
root -> aws_instance.foo
|
|
|
|
`
|
|
|
|
|
2014-06-24 23:58:57 +02:00
|
|
|
const testTerraformGraphStateStr = `
|
|
|
|
root: root
|
|
|
|
aws_instance.old
|
|
|
|
aws_instance.old -> provider.aws
|
|
|
|
aws_instance.web
|
|
|
|
aws_instance.web -> aws_security_group.firewall
|
|
|
|
aws_instance.web -> provider.aws
|
|
|
|
aws_load_balancer.weblb
|
|
|
|
aws_load_balancer.weblb -> aws_instance.web
|
|
|
|
aws_load_balancer.weblb -> provider.aws
|
|
|
|
aws_security_group.firewall
|
|
|
|
aws_security_group.firewall -> provider.aws
|
|
|
|
openstack_floating_ip.random
|
|
|
|
provider.aws
|
|
|
|
provider.aws -> openstack_floating_ip.random
|
|
|
|
root
|
|
|
|
root -> aws_instance.old
|
|
|
|
root -> aws_instance.web
|
|
|
|
root -> aws_load_balancer.weblb
|
|
|
|
root -> aws_security_group.firewall
|
|
|
|
root -> openstack_floating_ip.random
|
|
|
|
`
|