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