github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_artifacts_test.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "gopkg.in/yaml.v3" 22 ) 23 24 func TestArtifacts(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Artifacts 28 }{ 29 // bool value 30 { 31 yaml: `true`, 32 want: Artifacts{ 33 Enabled: true, 34 }, 35 }, 36 // struct value with key 37 { 38 yaml: `{ enabled: true, key: AWS1234566789 }`, 39 want: Artifacts{ 40 Enabled: true, 41 Key: &Secure{ 42 Decrypted: "AWS1234566789", 43 }, 44 }, 45 }, 46 // struct value with key as secret 47 { 48 yaml: `{ enabled: true, key: { secure: AWS1234566789 } }`, 49 want: Artifacts{ 50 Enabled: true, 51 Key: &Secure{ 52 Encrypted: "AWS1234566789", 53 }, 54 }, 55 }, 56 // struct value with alias aws_access_key_id, aws_secret_access_key 57 { 58 yaml: `{ enabled: true, aws_access_key_id: AWS1234566789, aws_secret_access_key: da39a3ee5 }`, 59 want: Artifacts{ 60 Enabled: true, 61 Key: &Secure{ 62 Decrypted: "AWS1234566789", 63 }, 64 Secret: &Secure{ 65 Decrypted: "da39a3ee5", 66 }, 67 }, 68 }, 69 // struct value with alias aws_access_key, aws_secret_key 70 { 71 yaml: `{ enabled: true, aws_access_key: AWS1234566789, aws_secret_key: da39a3ee5 }`, 72 want: Artifacts{ 73 Enabled: true, 74 Key: &Secure{ 75 Decrypted: "AWS1234566789", 76 }, 77 Secret: &Secure{ 78 Decrypted: "da39a3ee5", 79 }, 80 }, 81 }, 82 // struct value with alias access_key_id 83 { 84 yaml: `{ enabled: true, access_key_id: AWS1234566789, secret_access_key: da39a3ee5 }`, 85 want: Artifacts{ 86 Enabled: true, 87 Key: &Secure{ 88 Decrypted: "AWS1234566789", 89 }, 90 Secret: &Secure{ 91 Decrypted: "da39a3ee5", 92 }, 93 }, 94 }, 95 // struct value with alias access_key 96 { 97 yaml: `{ enabled: true, access_key: AWS1234566789, secret_key: da39a3ee5 }`, 98 want: Artifacts{ 99 Enabled: true, 100 Key: &Secure{ 101 Decrypted: "AWS1234566789", 102 }, 103 Secret: &Secure{ 104 Decrypted: "da39a3ee5", 105 }, 106 }, 107 }, 108 } 109 110 for i, test := range tests { 111 got := new(Artifacts) 112 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 113 t.Log(test.yaml) 114 t.Error(err) 115 return 116 } 117 if diff := cmp.Diff(got, &test.want); diff != "" { 118 t.Log(test.yaml) 119 t.Errorf("Unexpected parsing results for test %v", i) 120 t.Log(diff) 121 } 122 } 123 } 124 125 func TestArtifacts_Error(t *testing.T) { 126 err := yaml.Unmarshal([]byte("[[]]"), new(Artifacts)) 127 if err == nil || err.Error() != "failed to unmarshal artifacts" { 128 t.Errorf("Expect error, got %s", err) 129 } 130 }