github.com/openshift/terraform@v0.11.12-beta1/terraform/state_filter_test.go (about) 1 package terraform 2 3 import ( 4 "os" 5 "path/filepath" 6 "reflect" 7 "testing" 8 ) 9 10 func TestStateFilterFilter(t *testing.T) { 11 cases := map[string]struct { 12 State string 13 Filters []string 14 Expected []string 15 }{ 16 "all": { 17 "small.tfstate", 18 []string{}, 19 []string{ 20 "*terraform.ResourceState: aws_key_pair.onprem", 21 "*terraform.InstanceState: aws_key_pair.onprem", 22 "*terraform.ModuleState: module.bootstrap", 23 "*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a", 24 "*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a", 25 "*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns", 26 "*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns", 27 "*terraform.ResourceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 28 "*terraform.InstanceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 29 }, 30 }, 31 32 "single resource": { 33 "small.tfstate", 34 []string{"aws_key_pair.onprem"}, 35 []string{ 36 "*terraform.ResourceState: aws_key_pair.onprem", 37 "*terraform.InstanceState: aws_key_pair.onprem", 38 }, 39 }, 40 41 "single resource from minimal state": { 42 "single-minimal-resource.tfstate", 43 []string{"aws_instance.web"}, 44 []string{ 45 "*terraform.ResourceState: aws_instance.web", 46 "*terraform.InstanceState: aws_instance.web", 47 }, 48 }, 49 50 "single resource with similar names": { 51 "small_test_instance.tfstate", 52 []string{"test_instance.foo"}, 53 []string{ 54 "*terraform.ResourceState: test_instance.foo", 55 "*terraform.InstanceState: test_instance.foo", 56 }, 57 }, 58 59 "single instance": { 60 "small.tfstate", 61 []string{"aws_key_pair.onprem.primary"}, 62 []string{ 63 "*terraform.InstanceState: aws_key_pair.onprem", 64 }, 65 }, 66 67 "module filter": { 68 "complete.tfstate", 69 []string{"module.bootstrap"}, 70 []string{ 71 "*terraform.ModuleState: module.bootstrap", 72 "*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a", 73 "*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a", 74 "*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns", 75 "*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns", 76 "*terraform.ResourceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 77 "*terraform.InstanceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 78 }, 79 }, 80 81 "resource in module": { 82 "complete.tfstate", 83 []string{"module.bootstrap.aws_route53_zone.oasis-consul-bootstrap"}, 84 []string{ 85 "*terraform.ResourceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 86 "*terraform.InstanceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap", 87 }, 88 }, 89 90 "resource in module 2": { 91 "resource-in-module-2.tfstate", 92 []string{"module.foo.aws_instance.foo"}, 93 []string{}, 94 }, 95 96 "single count index": { 97 "complete.tfstate", 98 []string{"module.consul.aws_instance.consul-green[0]"}, 99 []string{ 100 "*terraform.ResourceState: module.consul.aws_instance.consul-green[0]", 101 "*terraform.InstanceState: module.consul.aws_instance.consul-green[0]", 102 }, 103 }, 104 105 "no count index": { 106 "complete.tfstate", 107 []string{"module.consul.aws_instance.consul-green"}, 108 []string{ 109 "*terraform.ResourceState: module.consul.aws_instance.consul-green[0]", 110 "*terraform.InstanceState: module.consul.aws_instance.consul-green[0]", 111 "*terraform.ResourceState: module.consul.aws_instance.consul-green[1]", 112 "*terraform.InstanceState: module.consul.aws_instance.consul-green[1]", 113 "*terraform.ResourceState: module.consul.aws_instance.consul-green[2]", 114 "*terraform.InstanceState: module.consul.aws_instance.consul-green[2]", 115 }, 116 }, 117 118 "nested modules": { 119 "nested-modules.tfstate", 120 []string{"module.outer"}, 121 []string{ 122 "*terraform.ModuleState: module.outer", 123 "*terraform.ModuleState: module.outer.module.child1", 124 "*terraform.ResourceState: module.outer.module.child1.aws_instance.foo", 125 "*terraform.InstanceState: module.outer.module.child1.aws_instance.foo", 126 "*terraform.ModuleState: module.outer.module.child2", 127 "*terraform.ResourceState: module.outer.module.child2.aws_instance.foo", 128 "*terraform.InstanceState: module.outer.module.child2.aws_instance.foo", 129 }, 130 }, 131 } 132 133 for n, tc := range cases { 134 // Load our state 135 f, err := os.Open(filepath.Join("./test-fixtures", "state-filter", tc.State)) 136 if err != nil { 137 t.Fatalf("%q: err: %s", n, err) 138 } 139 140 state, err := ReadState(f) 141 f.Close() 142 if err != nil { 143 t.Fatalf("%q: err: %s", n, err) 144 } 145 146 // Create the filter 147 filter := &StateFilter{State: state} 148 149 // Filter! 150 results, err := filter.Filter(tc.Filters...) 151 if err != nil { 152 t.Fatalf("%q: err: %s", n, err) 153 } 154 155 actual := make([]string, len(results)) 156 for i, result := range results { 157 actual[i] = result.String() 158 } 159 160 if !reflect.DeepEqual(actual, tc.Expected) { 161 t.Fatalf("%q: expected, then actual\n\n%#v\n\n%#v", n, tc.Expected, actual) 162 } 163 } 164 }