github.com/oam-dev/kubevela@v1.9.11/pkg/controller/utils/capability_config_test.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestGetTerraformConfigurationFromRemote(t *testing.T) { 29 type want struct { 30 config string 31 errMsg string 32 } 33 34 type args struct { 35 name string 36 url string 37 path string 38 data []byte 39 variableFile string 40 } 41 cases := map[string]struct { 42 args args 43 want want 44 }{ 45 "valid": { 46 args: args{ 47 name: "valid", 48 url: "https://github.com/kubevela-contrib/terraform-modules.git", 49 path: "unittest/", 50 data: []byte(` 51 variable "aaa" { 52 type = list(object({ 53 type = string 54 sourceArn = string 55 config = string 56 })) 57 default = [] 58 }`), 59 variableFile: "main.tf", 60 }, 61 want: want{ 62 config: ` 63 variable "aaa" { 64 type = list(object({ 65 type = string 66 sourceArn = string 67 config = string 68 })) 69 default = [] 70 }`, 71 }, 72 }, 73 "configuration is remote with path": { 74 args: args{ 75 name: "aws-subnet", 76 url: "https://github.com/kubevela-contrib/terraform-modules.git", 77 path: "unittest/aws/subnet", 78 data: []byte(` 79 variable "aaa" { 80 type = list(object({ 81 type = string 82 sourceArn = string 83 config = string 84 })) 85 default = [] 86 }`), 87 variableFile: "variables.tf", 88 }, 89 want: want{ 90 config: ` 91 variable "aaa" { 92 type = list(object({ 93 type = string 94 sourceArn = string 95 config = string 96 })) 97 default = [] 98 }`, 99 }, 100 }, 101 } 102 103 for name, tc := range cases { 104 t.Run(name, func(t *testing.T) { 105 home, _ := os.UserHomeDir() 106 path := filepath.Join(home, ".vela", "terraform") 107 tmpPath := filepath.Join(path, tc.args.name, tc.args.path) 108 if len(tc.args.data) > 0 { 109 err := os.MkdirAll(tmpPath, os.ModePerm) 110 assert.NoError(t, err) 111 err = os.WriteFile(filepath.Clean(filepath.Join(tmpPath, tc.args.variableFile)), tc.args.data, 0644) 112 assert.NoError(t, err) 113 } 114 defer os.RemoveAll(tmpPath) 115 116 conf, err := GetTerraformConfigurationFromRemote(tc.args.name, tc.args.url, tc.args.path, nil) 117 if tc.want.errMsg != "" { 118 if err != nil && !strings.Contains(err.Error(), tc.want.errMsg) { 119 t.Errorf("\n%s\nGetTerraformConfigurationFromRemote(...): -want error %v, +got error:%s", name, err, tc.want.errMsg) 120 } 121 } else { 122 assert.Equal(t, tc.want.config, conf) 123 } 124 }) 125 } 126 }