github.com/GoogleCloudPlatform/terraformer@v0.8.18/terraformutils/walk_test.go (about)

     1  package terraformutils
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestEmptyWalkAndGet(t *testing.T) {
     9  	structure := map[string]interface{}{}
    10  	value := WalkAndGet("attr1", structure)
    11  
    12  	if !reflect.DeepEqual(value, []interface{}{}) {
    13  		t.Errorf("failed to get value %v", value)
    14  	}
    15  }
    16  
    17  func TestEmptyNestedWalkAndGet(t *testing.T) {
    18  	structure := map[string]map[string]interface{}{}
    19  	value := WalkAndGet("attr1.attr2", structure)
    20  
    21  	if !reflect.DeepEqual(value, []interface{}{}) {
    22  		t.Errorf("failed to get value %v", value)
    23  	}
    24  }
    25  
    26  func TestSimpleWalkAndGet(t *testing.T) {
    27  	structure := map[string]interface{}{
    28  		"attr1": "value",
    29  	}
    30  	value := WalkAndGet("attr1", structure)
    31  
    32  	if !reflect.DeepEqual(value, []interface{}{"value"}) {
    33  		t.Errorf("failed to get value %v", value)
    34  	}
    35  }
    36  
    37  func TestSimpleArrayWalkAndGet(t *testing.T) {
    38  	structure := map[string][]interface{}{
    39  		"attr1": {"value"},
    40  	}
    41  	value := WalkAndGet("attr1", structure)
    42  
    43  	if !reflect.DeepEqual(value, []interface{}{"value"}) {
    44  		t.Errorf("failed to get value %v", value)
    45  	}
    46  }
    47  
    48  func TestNestedWalkAndGet(t *testing.T) {
    49  	structure := map[string]map[string]interface{}{
    50  		"attr1": {
    51  			"attr2": "value",
    52  		},
    53  	}
    54  	value := WalkAndGet("attr1.attr2", structure)
    55  
    56  	if !reflect.DeepEqual(value, []interface{}{"value"}) {
    57  		t.Errorf("failed to get value %v", value)
    58  	}
    59  }
    60  
    61  func TestNestedWalkWithDotInKeyAndGet(t *testing.T) {
    62  	structure := map[string]map[string]interface{}{
    63  		"attr1": {
    64  			"attr2.attr3": "value",
    65  		},
    66  	}
    67  	value := WalkAndGet("attr1.attr2.attr3", structure)
    68  
    69  	if !reflect.DeepEqual(value, []interface{}{"value"}) {
    70  		t.Errorf("failed to get value %v", value)
    71  	}
    72  }
    73  
    74  func TestNestedArrayWalkAndGet(t *testing.T) {
    75  	structure := mapI("attr1", []interface{}{
    76  		mapI("attr2", "value1"),
    77  		mapI("attr2", "value2")})
    78  	value := WalkAndGet("attr1.attr2", structure)
    79  
    80  	if !reflect.DeepEqual(value, []interface{}{"value1", "value2"}) {
    81  		t.Errorf("failed to get value %v", value)
    82  	}
    83  }
    84  
    85  func TestNonExistingWalkAndGet(t *testing.T) {
    86  	structure := map[string]interface{}{
    87  		"attr1": "test",
    88  	}
    89  	value := WalkAndGet("attr1.attr2", structure)
    90  
    91  	if !reflect.DeepEqual(value, []interface{}{}) {
    92  		t.Errorf("failed to get value %v", value)
    93  	}
    94  }
    95  
    96  func TestSimpleWalkAndOverride(t *testing.T) {
    97  	structure := map[string]interface{}{
    98  		"attr1": "value",
    99  	}
   100  	WalkAndOverride("attr1", "value", "newValue", structure)
   101  
   102  	if structure["attr1"] != "newValue" {
   103  		t.Errorf("failed to set value")
   104  	}
   105  }
   106  
   107  func TestSimpleArrayWalkAndOverride(t *testing.T) {
   108  	structure := map[string][]interface{}{
   109  		"attr1": {"value"},
   110  	}
   111  	WalkAndOverride("attr1", "value", "newValue", structure)
   112  
   113  	if structure["attr1"][0] != "newValue" {
   114  		t.Errorf("failed to set value")
   115  	}
   116  }
   117  
   118  func TestSimpleWalkAndNotOverride(t *testing.T) {
   119  	structure := map[string]interface{}{
   120  		"attr1": "value",
   121  	}
   122  	WalkAndOverride("attr1", "differentValue", "newValue", structure)
   123  
   124  	if structure["attr1"] != "value" {
   125  		t.Errorf("failed to set value")
   126  	}
   127  }
   128  
   129  func TestNonExistentWalkAndOverride(t *testing.T) {
   130  	structure := map[string]interface{}{
   131  		"attr1": "value",
   132  	}
   133  	WalkAndOverride("attr1.nonExistentAttr", "value", "newValue", structure)
   134  
   135  	_, exists := structure["nonExistentAttr"]
   136  	if exists {
   137  		t.Errorf("failed to set value")
   138  	}
   139  }
   140  
   141  func TestNestedWalkAndOverride(t *testing.T) {
   142  	structure := map[string]map[string]interface{}{
   143  		"attr1": {
   144  			"attr2": "value",
   145  		},
   146  	}
   147  	WalkAndOverride("attr1.attr2", "value", "newValue", structure)
   148  
   149  	if structure["attr1"]["attr2"] != "newValue" {
   150  		t.Errorf("failed to set value")
   151  	}
   152  }
   153  
   154  func TestNestedArrayWalkAndOverride(t *testing.T) {
   155  	structure := mapI("attr1", []interface{}{
   156  		mapI("attr2", "value1"),
   157  		mapI("attr2", "value2")})
   158  	WalkAndOverride("attr1.attr2", "value2", "newValue", structure)
   159  
   160  	if structure["attr1"].([]interface{})[0].(map[string]interface{})["attr2"] != "value1" || structure["attr1"].([]interface{})[1].(map[string]interface{})["attr2"] != "newValue" {
   161  		t.Errorf("failed to set value")
   162  	}
   163  }
   164  
   165  func TestEmptyWalkAndCheckField(t *testing.T) {
   166  	structure := map[string]interface{}{}
   167  	value := WalkAndCheckField("attr1", structure)
   168  
   169  	if !reflect.DeepEqual(value, false) {
   170  		t.Errorf("failed to get value %v", value)
   171  	}
   172  }
   173  
   174  func TestSimpleWalkAndCheckField(t *testing.T) {
   175  	structure := map[string]interface{}{
   176  		"attr1": "value",
   177  	}
   178  	value := WalkAndCheckField("attr1", structure)
   179  
   180  	if !reflect.DeepEqual(value, true) {
   181  		t.Errorf("failed to get value %v", value)
   182  	}
   183  }