|
| 1 | +package sanitize |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + tfjson "github.com/hashicorp/terraform-json" |
| 8 | +) |
| 9 | + |
| 10 | +type testChangeCase struct { |
| 11 | + name string |
| 12 | + old *tfjson.Change |
| 13 | + expected *tfjson.Change |
| 14 | +} |
| 15 | + |
| 16 | +func changeCases() []testChangeCase { |
| 17 | + return []testChangeCase{ |
| 18 | + { |
| 19 | + name: "basic", |
| 20 | + old: &tfjson.Change{ |
| 21 | + Before: map[string]interface{}{ |
| 22 | + "foo": map[string]interface{}{"a": "foo"}, |
| 23 | + "bar": map[string]interface{}{"a": "foo"}, |
| 24 | + "baz": map[string]interface{}{"a": "foo"}, |
| 25 | + "qux": map[string]interface{}{ |
| 26 | + "a": map[string]interface{}{ |
| 27 | + "b": "foo", |
| 28 | + }, |
| 29 | + "c": "bar", |
| 30 | + }, |
| 31 | + "quxx": map[string]interface{}{ |
| 32 | + "a": map[string]interface{}{ |
| 33 | + "b": "foo", |
| 34 | + }, |
| 35 | + "c": "bar", |
| 36 | + }, |
| 37 | + }, |
| 38 | + After: map[string]interface{}{ |
| 39 | + "one": map[string]interface{}{"x": "one"}, |
| 40 | + "two": map[string]interface{}{"x": "one"}, |
| 41 | + "three": map[string]interface{}{"x": "one"}, |
| 42 | + "four": map[string]interface{}{ |
| 43 | + "x": map[string]interface{}{ |
| 44 | + "y": "one", |
| 45 | + }, |
| 46 | + "z": "two", |
| 47 | + }, |
| 48 | + "five": map[string]interface{}{ |
| 49 | + "x": map[string]interface{}{ |
| 50 | + "y": "one", |
| 51 | + }, |
| 52 | + "z": "two", |
| 53 | + }, |
| 54 | + }, |
| 55 | + BeforeSensitive: map[string]interface{}{ |
| 56 | + "foo": map[string]interface{}{}, |
| 57 | + "bar": true, |
| 58 | + "baz": map[string]interface{}{"a": true}, |
| 59 | + "qux": map[string]interface{}{}, |
| 60 | + "quxx": map[string]interface{}{"c": true}, |
| 61 | + }, |
| 62 | + AfterSensitive: map[string]interface{}{ |
| 63 | + "one": map[string]interface{}{}, |
| 64 | + "two": true, |
| 65 | + "three": map[string]interface{}{"x": true}, |
| 66 | + "four": map[string]interface{}{}, |
| 67 | + "five": map[string]interface{}{"z": true}, |
| 68 | + }, |
| 69 | + }, |
| 70 | + expected: &tfjson.Change{ |
| 71 | + Before: map[string]interface{}{ |
| 72 | + "foo": map[string]interface{}{"a": "foo"}, |
| 73 | + "bar": DefaultSensitiveValue, |
| 74 | + "baz": map[string]interface{}{"a": DefaultSensitiveValue}, |
| 75 | + "qux": map[string]interface{}{ |
| 76 | + "a": map[string]interface{}{ |
| 77 | + "b": "foo", |
| 78 | + }, |
| 79 | + "c": "bar", |
| 80 | + }, |
| 81 | + "quxx": map[string]interface{}{ |
| 82 | + "a": map[string]interface{}{ |
| 83 | + "b": "foo", |
| 84 | + }, |
| 85 | + "c": DefaultSensitiveValue, |
| 86 | + }, |
| 87 | + }, |
| 88 | + After: map[string]interface{}{ |
| 89 | + "one": map[string]interface{}{"x": "one"}, |
| 90 | + "two": DefaultSensitiveValue, |
| 91 | + "three": map[string]interface{}{"x": DefaultSensitiveValue}, |
| 92 | + "four": map[string]interface{}{ |
| 93 | + "x": map[string]interface{}{ |
| 94 | + "y": "one", |
| 95 | + }, |
| 96 | + "z": "two", |
| 97 | + }, |
| 98 | + "five": map[string]interface{}{ |
| 99 | + "x": map[string]interface{}{ |
| 100 | + "y": "one", |
| 101 | + }, |
| 102 | + "z": DefaultSensitiveValue, |
| 103 | + }, |
| 104 | + }, |
| 105 | + BeforeSensitive: map[string]interface{}{ |
| 106 | + "foo": map[string]interface{}{}, |
| 107 | + "bar": true, |
| 108 | + "baz": map[string]interface{}{"a": true}, |
| 109 | + "qux": map[string]interface{}{}, |
| 110 | + "quxx": map[string]interface{}{"c": true}, |
| 111 | + }, |
| 112 | + AfterSensitive: map[string]interface{}{ |
| 113 | + "one": map[string]interface{}{}, |
| 114 | + "two": true, |
| 115 | + "three": map[string]interface{}{"x": true}, |
| 116 | + "four": map[string]interface{}{}, |
| 117 | + "five": map[string]interface{}{"z": true}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestSanitizeChange(t *testing.T) { |
| 125 | + for i, tc := range changeCases() { |
| 126 | + tc := tc |
| 127 | + t.Run(tc.name, func(t *testing.T) { |
| 128 | + actual, err := SanitizeChange(tc.old, DefaultSensitiveValue) |
| 129 | + if err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + |
| 133 | + if diff := cmp.Diff(tc.expected, actual); diff != "" { |
| 134 | + t.Errorf("SanitizeChange() mismatch (-expected +actual):\n%s", diff) |
| 135 | + } |
| 136 | + |
| 137 | + if diff := cmp.Diff(changeCases()[i].old, tc.old); diff != "" { |
| 138 | + t.Errorf("SanitizeChange() altered original (-expected +actual):\n%s", diff) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments