github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/config/raw_config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/gob"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/hil/ast"
     9  )
    10  
    11  func TestNewRawConfig(t *testing.T) {
    12  	raw := map[string]interface{}{
    13  		"foo": "${var.bar}",
    14  		"bar": `${file("boom.txt")}`,
    15  	}
    16  
    17  	rc, err := NewRawConfig(raw)
    18  	if err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  
    22  	if len(rc.Interpolations) != 2 {
    23  		t.Fatalf("bad: %#v", rc.Interpolations)
    24  	}
    25  	if len(rc.Variables) != 1 {
    26  		t.Fatalf("bad: %#v", rc.Variables)
    27  	}
    28  }
    29  
    30  func TestRawConfig(t *testing.T) {
    31  	raw := map[string]interface{}{
    32  		"foo": "${var.bar}",
    33  	}
    34  
    35  	rc, err := NewRawConfig(raw)
    36  	if err != nil {
    37  		t.Fatalf("err: %s", err)
    38  	}
    39  
    40  	// Before interpolate, Config() should be the raw
    41  	if !reflect.DeepEqual(rc.Config(), raw) {
    42  		t.Fatalf("bad: %#v", rc.Config())
    43  	}
    44  
    45  	vars := map[string]ast.Variable{
    46  		"var.bar": ast.Variable{
    47  			Value: "baz",
    48  			Type:  ast.TypeString,
    49  		},
    50  	}
    51  	if err := rc.Interpolate(vars); err != nil {
    52  		t.Fatalf("err: %s", err)
    53  	}
    54  
    55  	actual := rc.Config()
    56  	expected := map[string]interface{}{
    57  		"foo": "baz",
    58  	}
    59  
    60  	if !reflect.DeepEqual(actual, expected) {
    61  		t.Fatalf("bad: %#v", actual)
    62  	}
    63  	if len(rc.UnknownKeys()) != 0 {
    64  		t.Fatalf("bad: %#v", rc.UnknownKeys())
    65  	}
    66  }
    67  
    68  func TestRawConfig_double(t *testing.T) {
    69  	raw := map[string]interface{}{
    70  		"foo": "${var.bar}",
    71  	}
    72  
    73  	rc, err := NewRawConfig(raw)
    74  	if err != nil {
    75  		t.Fatalf("err: %s", err)
    76  	}
    77  
    78  	vars := map[string]ast.Variable{
    79  		"var.bar": ast.Variable{
    80  			Value: "baz",
    81  			Type:  ast.TypeString,
    82  		},
    83  	}
    84  	if err := rc.Interpolate(vars); err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  
    88  	actual := rc.Config()
    89  	expected := map[string]interface{}{
    90  		"foo": "baz",
    91  	}
    92  
    93  	if !reflect.DeepEqual(actual, expected) {
    94  		t.Fatalf("bad: %#v", actual)
    95  	}
    96  
    97  	vars = map[string]ast.Variable{
    98  		"var.bar": ast.Variable{
    99  			Value: "what",
   100  			Type:  ast.TypeString,
   101  		},
   102  	}
   103  	if err := rc.Interpolate(vars); err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  
   107  	actual = rc.Config()
   108  	expected = map[string]interface{}{
   109  		"foo": "what",
   110  	}
   111  
   112  	if !reflect.DeepEqual(actual, expected) {
   113  		t.Fatalf("bad: %#v", actual)
   114  	}
   115  }
   116  
   117  func TestRawConfigInterpolate_escaped(t *testing.T) {
   118  	raw := map[string]interface{}{
   119  		"foo": "bar-$${baz}",
   120  	}
   121  
   122  	rc, err := NewRawConfig(raw)
   123  	if err != nil {
   124  		t.Fatalf("err: %s", err)
   125  	}
   126  
   127  	// Before interpolate, Config() should be the raw
   128  	if !reflect.DeepEqual(rc.Config(), raw) {
   129  		t.Fatalf("bad: %#v", rc.Config())
   130  	}
   131  
   132  	if err := rc.Interpolate(nil); err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  
   136  	actual := rc.Config()
   137  	expected := map[string]interface{}{
   138  		"foo": "bar-${baz}",
   139  	}
   140  
   141  	if !reflect.DeepEqual(actual, expected) {
   142  		t.Fatalf("bad: %#v", actual)
   143  	}
   144  	if len(rc.UnknownKeys()) != 0 {
   145  		t.Fatalf("bad: %#v", rc.UnknownKeys())
   146  	}
   147  }
   148  
   149  func TestRawConfig_merge(t *testing.T) {
   150  	raw1 := map[string]interface{}{
   151  		"foo": "${var.foo}",
   152  		"bar": "${var.bar}",
   153  	}
   154  
   155  	rc1, err := NewRawConfig(raw1)
   156  	if err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  
   160  	{
   161  		vars := map[string]ast.Variable{
   162  			"var.foo": ast.Variable{
   163  				Value: "foovalue",
   164  				Type:  ast.TypeString,
   165  			},
   166  			"var.bar": ast.Variable{
   167  				Value: "nope",
   168  				Type:  ast.TypeString,
   169  			},
   170  		}
   171  		if err := rc1.Interpolate(vars); err != nil {
   172  			t.Fatalf("err: %s", err)
   173  		}
   174  	}
   175  
   176  	raw2 := map[string]interface{}{
   177  		"bar": "${var.bar}",
   178  		"baz": "${var.baz}",
   179  	}
   180  
   181  	rc2, err := NewRawConfig(raw2)
   182  	if err != nil {
   183  		t.Fatalf("err: %s", err)
   184  	}
   185  
   186  	{
   187  		vars := map[string]ast.Variable{
   188  			"var.bar": ast.Variable{
   189  				Value: "barvalue",
   190  				Type:  ast.TypeString,
   191  			},
   192  			"var.baz": ast.Variable{
   193  				Value: UnknownVariableValue,
   194  				Type:  ast.TypeString,
   195  			},
   196  		}
   197  		if err := rc2.Interpolate(vars); err != nil {
   198  			t.Fatalf("err: %s", err)
   199  		}
   200  	}
   201  
   202  	// Merge the two
   203  	rc3 := rc1.Merge(rc2)
   204  
   205  	// Raw should be merged
   206  	raw3 := map[string]interface{}{
   207  		"foo": "${var.foo}",
   208  		"bar": "${var.bar}",
   209  		"baz": "${var.baz}",
   210  	}
   211  	if !reflect.DeepEqual(rc3.Raw, raw3) {
   212  		t.Fatalf("bad: %#v", rc3.Raw)
   213  	}
   214  
   215  	actual := rc3.Config()
   216  	expected := map[string]interface{}{
   217  		"foo": "foovalue",
   218  		"bar": "barvalue",
   219  	}
   220  	if !reflect.DeepEqual(actual, expected) {
   221  		t.Fatalf("bad: %#v", actual)
   222  	}
   223  
   224  	expectedKeys := []string{"baz"}
   225  	if !reflect.DeepEqual(rc3.UnknownKeys(), expectedKeys) {
   226  		t.Fatalf("bad: %#v", rc3.UnknownKeys())
   227  	}
   228  }
   229  
   230  func TestRawConfig_syntax(t *testing.T) {
   231  	raw := map[string]interface{}{
   232  		"foo": "${var",
   233  	}
   234  
   235  	if _, err := NewRawConfig(raw); err == nil {
   236  		t.Fatal("should error")
   237  	}
   238  }
   239  
   240  func TestRawConfig_unknown(t *testing.T) {
   241  	raw := map[string]interface{}{
   242  		"foo": "${var.bar}",
   243  	}
   244  
   245  	rc, err := NewRawConfig(raw)
   246  	if err != nil {
   247  		t.Fatalf("err: %s", err)
   248  	}
   249  
   250  	vars := map[string]ast.Variable{
   251  		"var.bar": ast.Variable{
   252  			Value: UnknownVariableValue,
   253  			Type:  ast.TypeString,
   254  		},
   255  	}
   256  	if err := rc.Interpolate(vars); err != nil {
   257  		t.Fatalf("err: %s", err)
   258  	}
   259  
   260  	actual := rc.Config()
   261  	expected := map[string]interface{}{}
   262  
   263  	if !reflect.DeepEqual(actual, expected) {
   264  		t.Fatalf("bad: %#v", actual)
   265  	}
   266  
   267  	expectedKeys := []string{"foo"}
   268  	if !reflect.DeepEqual(rc.UnknownKeys(), expectedKeys) {
   269  		t.Fatalf("bad: %#v", rc.UnknownKeys())
   270  	}
   271  }
   272  
   273  func TestRawConfig_unknownPartial(t *testing.T) {
   274  	raw := map[string]interface{}{
   275  		"foo": "${var.bar}/32",
   276  	}
   277  
   278  	rc, err := NewRawConfig(raw)
   279  	if err != nil {
   280  		t.Fatalf("err: %s", err)
   281  	}
   282  
   283  	vars := map[string]ast.Variable{
   284  		"var.bar": ast.Variable{
   285  			Value: UnknownVariableValue,
   286  			Type:  ast.TypeString,
   287  		},
   288  	}
   289  	if err := rc.Interpolate(vars); err != nil {
   290  		t.Fatalf("err: %s", err)
   291  	}
   292  
   293  	actual := rc.Config()
   294  	expected := map[string]interface{}{}
   295  
   296  	if !reflect.DeepEqual(actual, expected) {
   297  		t.Fatalf("bad: %#v", actual)
   298  	}
   299  
   300  	expectedKeys := []string{"foo"}
   301  	if !reflect.DeepEqual(rc.UnknownKeys(), expectedKeys) {
   302  		t.Fatalf("bad: %#v", rc.UnknownKeys())
   303  	}
   304  }
   305  
   306  func TestRawConfigValue(t *testing.T) {
   307  	raw := map[string]interface{}{
   308  		"foo": "${var.bar}",
   309  	}
   310  
   311  	rc, err := NewRawConfig(raw)
   312  	if err != nil {
   313  		t.Fatalf("err: %s", err)
   314  	}
   315  
   316  	rc.Key = ""
   317  	if rc.Value() != nil {
   318  		t.Fatalf("bad: %#v", rc.Value())
   319  	}
   320  
   321  	rc.Key = "foo"
   322  	if rc.Value() != "${var.bar}" {
   323  		t.Fatalf("err: %#v", rc.Value())
   324  	}
   325  
   326  	vars := map[string]ast.Variable{
   327  		"var.bar": ast.Variable{
   328  			Value: "baz",
   329  			Type:  ast.TypeString,
   330  		},
   331  	}
   332  	if err := rc.Interpolate(vars); err != nil {
   333  		t.Fatalf("err: %s", err)
   334  	}
   335  
   336  	if rc.Value() != "baz" {
   337  		t.Fatalf("bad: %#v", rc.Value())
   338  	}
   339  }
   340  
   341  func TestRawConfig_implGob(t *testing.T) {
   342  	var _ gob.GobDecoder = new(RawConfig)
   343  	var _ gob.GobEncoder = new(RawConfig)
   344  }