github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/dbfactory/oss_test.go (about) 1 // Copyright 2019 Dolthub, 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 dbfactory 16 17 import ( 18 "os" 19 "testing" 20 21 "github.com/aliyun/aliyun-oss-go-sdk/oss" 22 "github.com/stretchr/testify/assert" 23 24 "github.com/dolthub/dolt/go/libraries/doltcore/dconfig" 25 ) 26 27 func Test_readOssCredentialsFromFile(t *testing.T) { 28 creds, err := readOSSCredentialsFromFile("testdata/osscred/dolt_oss_credentials") 29 assert.Nil(t, err) 30 assert.Equal(t, 3, len(creds)) 31 } 32 33 func Test_ossConfigFromParams(t *testing.T) { 34 type args struct { 35 params map[string]interface{} 36 } 37 tests := []struct { 38 name string 39 args args 40 want ossCredential 41 }{ 42 { 43 name: "not exist", 44 args: args{ 45 params: nil, 46 }, 47 want: emptyOSSCredential, 48 }, 49 { 50 name: "get default profile", 51 args: args{ 52 params: map[string]interface{}{ 53 OSSCredsFileParam: "testdata/osscred/dolt_oss_credentials", 54 }, 55 }, 56 want: ossCredential{ 57 Endpoint: "oss-cn-hangzhou.aliyuncs.com", 58 AccessKeyID: "defaulttestid", 59 AccessKeySecret: "test secret", 60 }, 61 }, 62 { 63 name: "get default profile single cred", 64 args: args{ 65 params: map[string]interface{}{ 66 OSSCredsFileParam: "testdata/osscred/single_oss_cred", 67 }, 68 }, 69 want: ossCredential{ 70 Endpoint: "oss-cn-hangzhou.aliyuncs.com", 71 AccessKeyID: "testid", 72 AccessKeySecret: "test secret", 73 }, 74 }, 75 { 76 name: "get cred by profile", 77 args: args{ 78 params: map[string]interface{}{ 79 OSSCredsFileParam: "testdata/osscred/dolt_oss_credentials", 80 OSSCredsProfile: "prod", 81 }, 82 }, 83 want: ossCredential{ 84 Endpoint: "oss-cn-hangzhou.aliyuncs.com", 85 AccessKeyID: "prodid", 86 AccessKeySecret: "test secret", 87 }, 88 }, 89 { 90 name: "profile not exists", 91 args: args{ 92 params: map[string]interface{}{ 93 OSSCredsFileParam: "testdata/osscred/dolt_oss_credentials", 94 OSSCredsProfile: "notexists", 95 }, 96 }, 97 want: emptyOSSCredential, 98 }, 99 { 100 name: "empty cred file", 101 args: args{ 102 params: map[string]interface{}{ 103 OSSCredsFileParam: "testdata/osscred/empty_oss_cred", 104 }, 105 }, 106 want: emptyOSSCredential, 107 }, 108 } 109 for _, tt := range tests { 110 t.Run(tt.name, func(t *testing.T) { 111 assert.Equalf(t, tt.want, ossConfigFromParams(tt.args.params), "ossConfigFromParams(%v)", tt.args.params) 112 }) 113 } 114 } 115 116 func Test_getOSSClient(t *testing.T) { 117 type args struct { 118 opts ossCredential 119 } 120 tests := []struct { 121 name string 122 args args 123 before func() 124 after func() 125 want func(got *oss.Client) bool 126 wantErr bool 127 }{ 128 { 129 name: "get valid oss client", 130 args: args{ 131 opts: ossCredential{ 132 Endpoint: "testendpoint", 133 AccessKeyID: "testid", 134 AccessKeySecret: "testkey", 135 }, 136 }, 137 wantErr: false, 138 want: func(got *oss.Client) bool { 139 return got != nil 140 }, 141 }, 142 { 143 name: "get invalid oss client", 144 args: args{ 145 opts: ossCredential{ 146 Endpoint: "", 147 AccessKeyID: "testid", 148 AccessKeySecret: "testkey", 149 }, 150 }, 151 wantErr: true, 152 want: func(got *oss.Client) bool { 153 return got == nil 154 }, 155 }, 156 { 157 name: "get valid oss client from env", 158 before: func() { 159 os.Setenv(dconfig.EnvOssEndpoint, "testendpoint") 160 }, 161 after: func() { 162 os.Unsetenv(dconfig.EnvOssEndpoint) 163 }, 164 args: args{ 165 opts: ossCredential{ 166 Endpoint: "", 167 AccessKeyID: "testid", 168 AccessKeySecret: "testkey", 169 }, 170 }, 171 wantErr: false, 172 want: func(got *oss.Client) bool { 173 return got != nil 174 }, 175 }, 176 } 177 for _, tt := range tests { 178 t.Run(tt.name, func(t *testing.T) { 179 if tt.before != nil { 180 tt.before() 181 } 182 if tt.after != nil { 183 defer tt.after() 184 } 185 got, err := getOSSClient(tt.args.opts) 186 if tt.wantErr { 187 assert.Error(t, err) 188 } 189 assert.True(t, tt.want(got)) 190 }) 191 } 192 }