github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/template/visitor/secretbuilder/helpers_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package secretbuilder 19 20 import ( 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 25 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 26 csov1 "github.com/zntrio/harp/v2/pkg/cso/v1" 27 "github.com/zntrio/harp/v2/pkg/template/engine" 28 ) 29 30 func TestSuffix(t *testing.T) { 31 type args struct { 32 templateContext engine.Context 33 ring csov1.Ring 34 secretPath string 35 item *bundlev1.SecretSuffix 36 data interface{} 37 } 38 tests := []struct { 39 name string 40 args args 41 want map[string]interface{} 42 wantErr bool 43 }{ 44 { 45 name: "nil", 46 args: args{}, 47 wantErr: true, 48 }, 49 { 50 name: "invalid template function", 51 args: args{ 52 templateContext: engine.NewContext(), 53 ring: csov1.RingInfra, 54 secretPath: "infra/aws/foo/us-east-1/rds/database/root_credentials", 55 item: &bundlev1.SecretSuffix{ 56 Template: `{{ foo }}`, 57 }, 58 }, 59 wantErr: true, 60 }, 61 { 62 name: "invalid json", 63 args: args{ 64 templateContext: engine.NewContext(), 65 ring: csov1.RingInfra, 66 secretPath: "infra/aws/foo/us-east-1/rds/database/root_credentials", 67 item: &bundlev1.SecretSuffix{ 68 Template: `{"foo`, 69 }, 70 }, 71 wantErr: true, 72 }, 73 { 74 name: "valid", 75 args: args{ 76 templateContext: engine.NewContext(), 77 ring: csov1.RingInfra, 78 secretPath: "infra/aws/foo/us-east-1/rds/database/root_credentials", 79 item: &bundlev1.SecretSuffix{ 80 Template: `{"foo":"123456"}`, 81 }, 82 }, 83 wantErr: false, 84 want: map[string]interface{}{ 85 "foo": "123456", 86 }, 87 }, 88 } 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 got, err := renderSuffix(tt.args.templateContext, tt.args.secretPath, tt.args.item, tt.args.data) 92 if (err != nil) != tt.wantErr { 93 t.Errorf("Suffix() error = %v, wantErr %v", err, tt.wantErr) 94 return 95 } 96 if !cmp.Equal(got, tt.want) { 97 t.Errorf("Suffix() = %v, want %v", got, tt.want) 98 } 99 }) 100 } 101 }