github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/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 with similar names": {
    42  			"small_test_instance.tfstate",
    43  			[]string{"test_instance.foo"},
    44  			[]string{
    45  				"*terraform.ResourceState: test_instance.foo",
    46  				"*terraform.InstanceState: test_instance.foo",
    47  			},
    48  		},
    49  
    50  		"single instance": {
    51  			"small.tfstate",
    52  			[]string{"aws_key_pair.onprem.primary"},
    53  			[]string{
    54  				"*terraform.InstanceState: aws_key_pair.onprem",
    55  			},
    56  		},
    57  
    58  		"module filter": {
    59  			"complete.tfstate",
    60  			[]string{"module.bootstrap"},
    61  			[]string{
    62  				"*terraform.ModuleState: module.bootstrap",
    63  				"*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a",
    64  				"*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-a",
    65  				"*terraform.ResourceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns",
    66  				"*terraform.InstanceState: module.bootstrap.aws_route53_record.oasis-consul-bootstrap-ns",
    67  				"*terraform.ResourceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap",
    68  				"*terraform.InstanceState: module.bootstrap.aws_route53_zone.oasis-consul-bootstrap",
    69  			},
    70  		},
    71  
    72  		"resource in module": {
    73  			"complete.tfstate",
    74  			[]string{"module.bootstrap.aws_route53_zone.oasis-consul-bootstrap"},
    75  			[]string{
    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 2": {
    82  			"resource-in-module-2.tfstate",
    83  			[]string{"module.foo.aws_instance.foo"},
    84  			[]string{},
    85  		},
    86  
    87  		"single count index": {
    88  			"complete.tfstate",
    89  			[]string{"module.consul.aws_instance.consul-green[0]"},
    90  			[]string{
    91  				"*terraform.ResourceState: module.consul.aws_instance.consul-green[0]",
    92  				"*terraform.InstanceState: module.consul.aws_instance.consul-green[0]",
    93  			},
    94  		},
    95  
    96  		"no count index": {
    97  			"complete.tfstate",
    98  			[]string{"module.consul.aws_instance.consul-green"},
    99  			[]string{
   100  				"*terraform.ResourceState: module.consul.aws_instance.consul-green[0]",
   101  				"*terraform.InstanceState: module.consul.aws_instance.consul-green[0]",
   102  				"*terraform.ResourceState: module.consul.aws_instance.consul-green[1]",
   103  				"*terraform.InstanceState: module.consul.aws_instance.consul-green[1]",
   104  				"*terraform.ResourceState: module.consul.aws_instance.consul-green[2]",
   105  				"*terraform.InstanceState: module.consul.aws_instance.consul-green[2]",
   106  			},
   107  		},
   108  
   109  		"nested modules": {
   110  			"nested-modules.tfstate",
   111  			[]string{"module.outer"},
   112  			[]string{
   113  				"*terraform.ModuleState: module.outer",
   114  				"*terraform.ModuleState: module.outer.module.child1",
   115  				"*terraform.ResourceState: module.outer.module.child1.aws_instance.foo",
   116  				"*terraform.InstanceState: module.outer.module.child1.aws_instance.foo",
   117  				"*terraform.ModuleState: module.outer.module.child2",
   118  				"*terraform.ResourceState: module.outer.module.child2.aws_instance.foo",
   119  				"*terraform.InstanceState: module.outer.module.child2.aws_instance.foo",
   120  			},
   121  		},
   122  	}
   123  
   124  	for n, tc := range cases {
   125  		// Load our state
   126  		f, err := os.Open(filepath.Join("./test-fixtures", "state-filter", tc.State))
   127  		if err != nil {
   128  			t.Fatalf("%q: err: %s", n, err)
   129  		}
   130  
   131  		state, err := ReadState(f)
   132  		f.Close()
   133  		if err != nil {
   134  			t.Fatalf("%q: err: %s", n, err)
   135  		}
   136  
   137  		// Create the filter
   138  		filter := &StateFilter{State: state}
   139  
   140  		// Filter!
   141  		results, err := filter.Filter(tc.Filters...)
   142  		if err != nil {
   143  			t.Fatalf("%q: err: %s", n, err)
   144  		}
   145  
   146  		actual := make([]string, len(results))
   147  		for i, result := range results {
   148  			actual[i] = result.String()
   149  		}
   150  
   151  		if !reflect.DeepEqual(actual, tc.Expected) {
   152  			t.Fatalf("%q: expected, then actual\n\n%#v\n\n%#v", n, tc.Expected, actual)
   153  		}
   154  	}
   155  }