github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/json/output_test.go (about) 1 package json 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/hashicorp/terraform/internal/addrs" 9 "github.com/hashicorp/terraform/internal/lang/marks" 10 "github.com/hashicorp/terraform/internal/plans" 11 "github.com/hashicorp/terraform/internal/states" 12 "github.com/zclconf/go-cty/cty" 13 ) 14 15 func TestOutputsFromMap(t *testing.T) { 16 got, diags := OutputsFromMap(map[string]*states.OutputValue{ 17 // Normal non-sensitive output 18 "boop": { 19 Value: cty.NumberIntVal(1234), 20 }, 21 // Sensitive string output 22 "beep": { 23 Value: cty.StringVal("horse-battery").Mark(marks.Sensitive), 24 Sensitive: true, 25 }, 26 // Sensitive object output which is marked at the leaf 27 "blorp": { 28 Value: cty.ObjectVal(map[string]cty.Value{ 29 "a": cty.ObjectVal(map[string]cty.Value{ 30 "b": cty.ObjectVal(map[string]cty.Value{ 31 "c": cty.StringVal("oh, hi").Mark(marks.Sensitive), 32 }), 33 }), 34 }), 35 Sensitive: true, 36 }, 37 // Null value 38 "honk": { 39 Value: cty.NullVal(cty.Map(cty.Bool)), 40 }, 41 }) 42 if len(diags) > 0 { 43 t.Fatal(diags.Err()) 44 } 45 46 want := Outputs{ 47 "boop": { 48 Sensitive: false, 49 Type: json.RawMessage(`"number"`), 50 Value: json.RawMessage(`1234`), 51 }, 52 "beep": { 53 Sensitive: true, 54 Type: json.RawMessage(`"string"`), 55 }, 56 "blorp": { 57 Sensitive: true, 58 Type: json.RawMessage(`["object",{"a":["object",{"b":["object",{"c":"string"}]}]}]`), 59 }, 60 "honk": { 61 Sensitive: false, 62 Type: json.RawMessage(`["map","bool"]`), 63 Value: json.RawMessage(`null`), 64 }, 65 } 66 67 if !cmp.Equal(want, got) { 68 t.Fatalf("unexpected result\n%s", cmp.Diff(want, got)) 69 } 70 } 71 72 func TestOutputsFromChanges(t *testing.T) { 73 root := addrs.RootModuleInstance 74 num, err := plans.NewDynamicValue(cty.NumberIntVal(1234), cty.Number) 75 if err != nil { 76 t.Fatalf("unexpected error creating dynamic value: %v", err) 77 } 78 str, err := plans.NewDynamicValue(cty.StringVal("1234"), cty.String) 79 if err != nil { 80 t.Fatalf("unexpected error creating dynamic value: %v", err) 81 } 82 83 got := OutputsFromChanges([]*plans.OutputChangeSrc{ 84 // Unchanged output "boop", value 1234 85 { 86 Addr: root.OutputValue("boop"), 87 ChangeSrc: plans.ChangeSrc{ 88 Action: plans.NoOp, 89 Before: num, 90 After: num, 91 }, 92 Sensitive: false, 93 }, 94 // New output "beep", value 1234 95 { 96 Addr: root.OutputValue("beep"), 97 ChangeSrc: plans.ChangeSrc{ 98 Action: plans.Create, 99 Before: nil, 100 After: num, 101 }, 102 Sensitive: false, 103 }, 104 // Deleted output "blorp", prior value 1234 105 { 106 Addr: root.OutputValue("blorp"), 107 ChangeSrc: plans.ChangeSrc{ 108 Action: plans.Delete, 109 Before: num, 110 After: nil, 111 }, 112 Sensitive: false, 113 }, 114 // Updated output "honk", prior value 1234, new value "1234" 115 { 116 Addr: root.OutputValue("honk"), 117 ChangeSrc: plans.ChangeSrc{ 118 Action: plans.Update, 119 Before: num, 120 After: str, 121 }, 122 Sensitive: false, 123 }, 124 // New sensitive output "secret", value "1234" 125 { 126 Addr: root.OutputValue("secret"), 127 ChangeSrc: plans.ChangeSrc{ 128 Action: plans.Create, 129 Before: nil, 130 After: str, 131 }, 132 Sensitive: true, 133 }, 134 }) 135 136 want := Outputs{ 137 "boop": { 138 Action: "noop", 139 Sensitive: false, 140 }, 141 "beep": { 142 Action: "create", 143 Sensitive: false, 144 }, 145 "blorp": { 146 Action: "delete", 147 Sensitive: false, 148 }, 149 "honk": { 150 Action: "update", 151 Sensitive: false, 152 }, 153 "secret": { 154 Action: "create", 155 Sensitive: true, 156 }, 157 } 158 159 if !cmp.Equal(want, got) { 160 t.Fatalf("unexpected result\n%s", cmp.Diff(want, got)) 161 } 162 } 163 164 func TestOutputs_String(t *testing.T) { 165 outputs := Outputs{ 166 "boop": { 167 Sensitive: false, 168 Type: json.RawMessage(`"number"`), 169 Value: json.RawMessage(`1234`), 170 }, 171 "beep": { 172 Sensitive: true, 173 Type: json.RawMessage(`"string"`), 174 Value: json.RawMessage(`"horse-battery"`), 175 }, 176 } 177 if got, want := outputs.String(), "Outputs: 2"; got != want { 178 t.Fatalf("unexpected value\n got: %q\nwant: %q", got, want) 179 } 180 }