github.com/opentofu/opentofu@v1.7.1/internal/encryption/keyprovider/output_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package keyprovider_test 7 8 import ( 9 "testing" 10 11 "github.com/opentofu/opentofu/internal/encryption/keyprovider" 12 "github.com/zclconf/go-cty/cty" 13 ) 14 15 func TestOutputCty(t *testing.T) { 16 testCases := map[string]struct { 17 output keyprovider.Output 18 expectedOutput cty.Value 19 }{ 20 "empty": { 21 output: keyprovider.Output{}, 22 expectedOutput: cty.ObjectVal(map[string]cty.Value{ 23 "encryption_key": cty.NullVal(cty.List(cty.Number)), 24 "decryption_key": cty.NullVal(cty.List(cty.Number)), 25 }), 26 }, 27 "encryption-key-only": { 28 output: keyprovider.Output{ 29 EncryptionKey: []byte("Hello world!"), 30 }, 31 expectedOutput: cty.ObjectVal(map[string]cty.Value{ 32 "encryption_key": cty.ListVal([]cty.Value{ 33 cty.NumberIntVal(int64('H')), 34 cty.NumberIntVal(int64('e')), 35 cty.NumberIntVal(int64('l')), 36 cty.NumberIntVal(int64('l')), 37 cty.NumberIntVal(int64('o')), 38 cty.NumberIntVal(int64(' ')), 39 cty.NumberIntVal(int64('w')), 40 cty.NumberIntVal(int64('o')), 41 cty.NumberIntVal(int64('r')), 42 cty.NumberIntVal(int64('l')), 43 cty.NumberIntVal(int64('d')), 44 cty.NumberIntVal(int64('!')), 45 }), 46 "decryption_key": cty.NullVal(cty.List(cty.Number)), 47 }), 48 }, 49 "both-keys": { 50 output: keyprovider.Output{ 51 EncryptionKey: []byte("Hello world!"), 52 DecryptionKey: []byte("Hello world!"), 53 }, 54 expectedOutput: cty.ObjectVal(map[string]cty.Value{ 55 "encryption_key": cty.ListVal([]cty.Value{ 56 cty.NumberIntVal(int64('H')), 57 cty.NumberIntVal(int64('e')), 58 cty.NumberIntVal(int64('l')), 59 cty.NumberIntVal(int64('l')), 60 cty.NumberIntVal(int64('o')), 61 cty.NumberIntVal(int64(' ')), 62 cty.NumberIntVal(int64('w')), 63 cty.NumberIntVal(int64('o')), 64 cty.NumberIntVal(int64('r')), 65 cty.NumberIntVal(int64('l')), 66 cty.NumberIntVal(int64('d')), 67 cty.NumberIntVal(int64('!')), 68 }), 69 "decryption_key": cty.ListVal([]cty.Value{ 70 cty.NumberIntVal(int64('H')), 71 cty.NumberIntVal(int64('e')), 72 cty.NumberIntVal(int64('l')), 73 cty.NumberIntVal(int64('l')), 74 cty.NumberIntVal(int64('o')), 75 cty.NumberIntVal(int64(' ')), 76 cty.NumberIntVal(int64('w')), 77 cty.NumberIntVal(int64('o')), 78 cty.NumberIntVal(int64('r')), 79 cty.NumberIntVal(int64('l')), 80 cty.NumberIntVal(int64('d')), 81 cty.NumberIntVal(int64('!')), 82 }), 83 }), 84 }, 85 } 86 for name, tc := range testCases { 87 t.Run(name, func(t *testing.T) { 88 val := tc.output.Cty() 89 if !val.Equals(tc.expectedOutput).True() { 90 t.Fatalf("Incorrect cty output value:\n%v\nexpected:\n%v)", val, tc.expectedOutput) 91 } 92 }) 93 } 94 }