github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/lang/funcs/sensitive_test.go (about) 1 package funcs 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/internal/lang/marks" 8 "github.com/zclconf/go-cty/cty" 9 ) 10 11 func TestSensitive(t *testing.T) { 12 tests := []struct { 13 Input cty.Value 14 WantErr string 15 }{ 16 { 17 cty.NumberIntVal(1), 18 ``, 19 }, 20 { 21 // Unknown values stay unknown while becoming sensitive 22 cty.UnknownVal(cty.String), 23 ``, 24 }, 25 { 26 // Null values stay unknown while becoming sensitive 27 cty.NullVal(cty.String), 28 ``, 29 }, 30 { 31 // DynamicVal can be marked as sensitive 32 cty.DynamicVal, 33 ``, 34 }, 35 { 36 // The marking is shallow only 37 cty.ListVal([]cty.Value{cty.NumberIntVal(1)}), 38 ``, 39 }, 40 { 41 // A value already marked is allowed and stays marked 42 cty.NumberIntVal(1).Mark(marks.Sensitive), 43 ``, 44 }, 45 { 46 // A value with some non-standard mark gets "fixed" to be marked 47 // with the standard "sensitive" mark. (This situation occurring 48 // would imply an inconsistency/bug elsewhere, so we're just 49 // being robust about it here.) 50 cty.NumberIntVal(1).Mark("bloop"), 51 ``, 52 }, 53 { 54 // A value deep already marked is allowed and stays marked, 55 // _and_ we'll also mark the outer collection as sensitive. 56 cty.ListVal([]cty.Value{cty.NumberIntVal(1).Mark(marks.Sensitive)}), 57 ``, 58 }, 59 } 60 61 for _, test := range tests { 62 t.Run(fmt.Sprintf("sensitive(%#v)", test.Input), func(t *testing.T) { 63 got, err := Sensitive(test.Input) 64 65 if test.WantErr != "" { 66 if err == nil { 67 t.Fatal("succeeded; want error") 68 } 69 if got, want := err.Error(), test.WantErr; got != want { 70 t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want) 71 } 72 return 73 } else if err != nil { 74 t.Fatalf("unexpected error: %s", err) 75 } 76 77 if !got.HasMark(marks.Sensitive) { 78 t.Errorf("result is not marked sensitive") 79 } 80 81 gotRaw, gotMarks := got.Unmark() 82 if len(gotMarks) != 1 { 83 // We're only expecting to have the "sensitive" mark we checked 84 // above. Any others are an error, even if they happen to 85 // appear alongside "sensitive". (We might change this rule 86 // if someday we decide to use marks for some additional 87 // unrelated thing in Terraform, but currently we assume that 88 // _all_ marks imply sensitive, and so returning any other 89 // marks would be confusing.) 90 t.Errorf("extraneous marks %#v", gotMarks) 91 } 92 93 // Disregarding shallow marks, the result should have the same 94 // effective value as the input. 95 wantRaw, _ := test.Input.Unmark() 96 if !gotRaw.RawEquals(wantRaw) { 97 t.Errorf("wrong unmarked result\ngot: %#v\nwant: %#v", got, wantRaw) 98 } 99 }) 100 } 101 } 102 103 func TestNonsensitive(t *testing.T) { 104 tests := []struct { 105 Input cty.Value 106 WantErr string 107 }{ 108 { 109 cty.NumberIntVal(1).Mark(marks.Sensitive), 110 ``, 111 }, 112 { 113 cty.DynamicVal.Mark(marks.Sensitive), 114 ``, 115 }, 116 { 117 cty.UnknownVal(cty.String).Mark(marks.Sensitive), 118 ``, 119 }, 120 { 121 cty.NullVal(cty.EmptyObject).Mark(marks.Sensitive), 122 ``, 123 }, 124 { 125 // The inner sensitive remains afterwards 126 cty.ListVal([]cty.Value{cty.NumberIntVal(1).Mark(marks.Sensitive)}).Mark(marks.Sensitive), 127 ``, 128 }, 129 130 // Passing a value that is already non-sensitive is an error, 131 // because this function should always be used with specific 132 // intention, not just as a "make everything visible" hammer. 133 { 134 cty.NumberIntVal(1), 135 `the given value is not sensitive, so this call is redundant`, 136 }, 137 { 138 cty.NullVal(cty.String), 139 `the given value is not sensitive, so this call is redundant`, 140 }, 141 142 // Unknown values may become sensitive once they are known, so we 143 // permit them to be marked nonsensitive. 144 { 145 cty.DynamicVal, 146 ``, 147 }, 148 { 149 cty.UnknownVal(cty.String), 150 ``, 151 }, 152 } 153 154 for _, test := range tests { 155 t.Run(fmt.Sprintf("nonsensitive(%#v)", test.Input), func(t *testing.T) { 156 got, err := Nonsensitive(test.Input) 157 158 if test.WantErr != "" { 159 if err == nil { 160 t.Fatal("succeeded; want error") 161 } 162 if got, want := err.Error(), test.WantErr; got != want { 163 t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want) 164 } 165 return 166 } else if err != nil { 167 t.Fatalf("unexpected error: %s", err) 168 } 169 170 if got.HasMark(marks.Sensitive) { 171 t.Errorf("result is still marked sensitive") 172 } 173 wantRaw, _ := test.Input.Unmark() 174 if !got.RawEquals(wantRaw) { 175 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Input) 176 } 177 }) 178 } 179 }