github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/terraform/context_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func testContext2(t *testing.T, opts *ContextOpts) *Context {
    10  	return NewContext(opts)
    11  }
    12  
    13  func testApplyFn(
    14  	info *InstanceInfo,
    15  	s *InstanceState,
    16  	d *InstanceDiff) (*InstanceState, error) {
    17  	if d.Destroy {
    18  		return nil, nil
    19  	}
    20  
    21  	id := "foo"
    22  	if idAttr, ok := d.Attributes["id"]; ok && !idAttr.NewComputed {
    23  		id = idAttr.New
    24  	}
    25  
    26  	result := &InstanceState{
    27  		ID:         id,
    28  		Attributes: make(map[string]string),
    29  	}
    30  
    31  	// Copy all the prior attributes
    32  	for k, v := range s.Attributes {
    33  		result.Attributes[k] = v
    34  	}
    35  
    36  	if d != nil {
    37  		result = result.MergeDiff(d)
    38  	}
    39  	return result, nil
    40  }
    41  
    42  func testDiffFn(
    43  	info *InstanceInfo,
    44  	s *InstanceState,
    45  	c *ResourceConfig) (*InstanceDiff, error) {
    46  	var diff InstanceDiff
    47  	diff.Attributes = make(map[string]*ResourceAttrDiff)
    48  
    49  	for k, v := range c.Raw {
    50  		if _, ok := v.(string); !ok {
    51  			continue
    52  		}
    53  
    54  		if k == "nil" {
    55  			return nil, nil
    56  		}
    57  
    58  		// This key is used for other purposes
    59  		if k == "compute_value" {
    60  			continue
    61  		}
    62  
    63  		if k == "compute" {
    64  			attrDiff := &ResourceAttrDiff{
    65  				Old:         "",
    66  				New:         "",
    67  				NewComputed: true,
    68  			}
    69  
    70  			if cv, ok := c.Config["compute_value"]; ok {
    71  				if cv.(string) == "1" {
    72  					attrDiff.NewComputed = false
    73  					attrDiff.New = fmt.Sprintf("computed_%s", v.(string))
    74  				}
    75  			}
    76  
    77  			diff.Attributes[v.(string)] = attrDiff
    78  			continue
    79  		}
    80  
    81  		// If this key is not computed, then look it up in the
    82  		// cleaned config.
    83  		found := false
    84  		for _, ck := range c.ComputedKeys {
    85  			if ck == k {
    86  				found = true
    87  				break
    88  			}
    89  		}
    90  		if !found {
    91  			v = c.Config[k]
    92  		}
    93  
    94  		attrDiff := &ResourceAttrDiff{
    95  			Old: "",
    96  			New: v.(string),
    97  		}
    98  
    99  		if k == "require_new" {
   100  			attrDiff.RequiresNew = true
   101  		}
   102  		diff.Attributes[k] = attrDiff
   103  	}
   104  
   105  	for _, k := range c.ComputedKeys {
   106  		diff.Attributes[k] = &ResourceAttrDiff{
   107  			Old:         "",
   108  			NewComputed: true,
   109  		}
   110  	}
   111  
   112  	for k, v := range diff.Attributes {
   113  		if v.NewComputed {
   114  			continue
   115  		}
   116  
   117  		old, ok := s.Attributes[k]
   118  		if !ok {
   119  			continue
   120  		}
   121  		if old == v.New {
   122  			delete(diff.Attributes, k)
   123  		}
   124  	}
   125  
   126  	if !diff.Empty() {
   127  		diff.Attributes["type"] = &ResourceAttrDiff{
   128  			Old: "",
   129  			New: info.Type,
   130  		}
   131  	}
   132  
   133  	return &diff, nil
   134  }
   135  
   136  func testProvider(prefix string) *MockResourceProvider {
   137  	p := new(MockResourceProvider)
   138  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   139  		return s, nil
   140  	}
   141  	p.ResourcesReturn = []ResourceType{
   142  		ResourceType{
   143  			Name: fmt.Sprintf("%s_instance", prefix),
   144  		},
   145  	}
   146  
   147  	return p
   148  }
   149  
   150  func testProvisioner() *MockResourceProvisioner {
   151  	p := new(MockResourceProvisioner)
   152  	return p
   153  }
   154  
   155  func checkStateString(t *testing.T, state *State, expected string) {
   156  	actual := strings.TrimSpace(state.String())
   157  	expected = strings.TrimSpace(expected)
   158  
   159  	if actual != expected {
   160  		t.Fatalf("state does not match! actual:\n%s\n\nexpected:\n%s", actual, expected)
   161  	}
   162  }
   163  
   164  func resourceState(resourceType, resourceID string) *ResourceState {
   165  	return &ResourceState{
   166  		Type: resourceType,
   167  		Primary: &InstanceState{
   168  			ID: resourceID,
   169  		},
   170  	}
   171  }
   172  
   173  const testContextGraph = `
   174  root: root
   175  aws_instance.bar
   176    aws_instance.bar -> provider.aws
   177  aws_instance.foo
   178    aws_instance.foo -> provider.aws
   179  provider.aws
   180  root
   181    root -> aws_instance.bar
   182    root -> aws_instance.foo
   183  `
   184  
   185  const testContextRefreshModuleStr = `
   186  aws_instance.web: (1 tainted)
   187    ID = <not created>
   188    Tainted ID 1 = bar
   189  
   190  module.child:
   191    aws_instance.web:
   192      ID = new
   193  `
   194  
   195  const testContextRefreshOutputStr = `
   196  aws_instance.web:
   197    ID = foo
   198    foo = bar
   199  
   200  Outputs:
   201  
   202  foo = bar
   203  `
   204  
   205  const testContextRefreshOutputPartialStr = `
   206  <no state>
   207  `
   208  
   209  const testContextRefreshTaintedStr = `
   210  aws_instance.web: (1 tainted)
   211    ID = <not created>
   212    Tainted ID 1 = foo
   213  `