github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/secret_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 22 "gopkg.in/yaml.v3" 23 ) 24 25 func TestSecret(t *testing.T) { 26 tests := []struct { 27 yaml string 28 want Vault 29 }{ 30 { 31 yaml: `{ "engine": { "name": "kv-v2", "path": "ops" }, "path": "production/db", "field": "password" }`, 32 want: Vault{ 33 Engine: &VaultEngine{ 34 Name: "kv-v2", 35 Path: "ops", 36 }, 37 Path: "production/db", 38 Field: "password", 39 }, 40 }, 41 { 42 yaml: `"production/db/password"`, 43 want: Vault{ 44 Path: "production/db", 45 Field: "password", 46 }, 47 }, 48 { 49 yaml: `"production/db/password@ops"`, 50 want: Vault{ 51 Path: "production/db", 52 Field: "password", 53 Engine: &VaultEngine{ 54 Name: "kv-v2", 55 Path: "ops", 56 }, 57 }, 58 }, 59 } 60 61 for i, test := range tests { 62 got := new(Vault) 63 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 64 t.Error(err) 65 return 66 } 67 if diff := cmp.Diff(got, &test.want); diff != "" { 68 t.Errorf("Unexpected parsing results for test %v", i) 69 t.Log(diff) 70 } 71 } 72 } 73 74 func TestVault_Error(t *testing.T) { 75 err := yaml.Unmarshal([]byte("[]"), new(Vault)) 76 if err == nil || err.Error() != "failed to unmarshal vault" { 77 t.Errorf("Expect error, got %s", err) 78 } 79 }