github.com/qiniu/go-sdk/v7@v7.27.0/internal/configfile/config_file_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package configfile 5 6 import ( 7 "os" 8 "testing" 9 ) 10 11 func TestLoad(t *testing.T) { 12 file, err := os.CreateTemp("", "config.toml") 13 if err != nil { 14 t.Fatal(err) 15 } 16 defer os.Remove(file.Name()) 17 defer file.Close() 18 19 _, err = file.WriteString(` 20 [default] 21 access_key = "QINIU_ACCESS_KEY_1" 22 secret_key = "QINIU_SECRET_KEY_1" 23 24 [private-cloud] 25 access_key = "QINIU_ACCESS_KEY_2" 26 secret_key = "QINIU_SECRET_KEY_2" 27 bucket_url = "https://uc.qbox.me" 28 disable_secure_protocol = true 29 30 [private-cloud-2] 31 access_key = "QINIU_ACCESS_KEY_3" 32 secret_key = "QINIU_SECRET_KEY_3" 33 bucket_url = ["https://uc.qbox.me", "https://uc.qiniuapi.com"] 34 `) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 os.Setenv("QINIU_CONFIG_FILE", file.Name()) 40 defer os.Unsetenv("QINIU_CONFIG_FILE") 41 if err = load(); err != nil { 42 t.Fatal(err) 43 } 44 defer func() { 45 profileConfigs = nil 46 }() 47 if len(profileConfigs) != 3 { 48 t.Fatal("Unexpected profile configs") 49 } 50 if profileConfigs["default"].AccessKey != "QINIU_ACCESS_KEY_1" { 51 t.Fatal("Unexpected access key") 52 } 53 if profileConfigs["default"].SecretKey != "QINIU_SECRET_KEY_1" { 54 t.Fatal("Unexpected secret key") 55 } 56 if profileConfigs["default"].BucketURL != nil { 57 t.Fatal("Unexpected bucket url") 58 } 59 if profileConfigs["default"].DisableSecureProtocol { 60 t.Fatal("Unexpected disable secure protocol") 61 } 62 if profileConfigs["private-cloud"].AccessKey != "QINIU_ACCESS_KEY_2" { 63 t.Fatal("Unexpected access key") 64 } 65 if profileConfigs["private-cloud"].SecretKey != "QINIU_SECRET_KEY_2" { 66 t.Fatal("Unexpected secret key") 67 } 68 if profileConfigs["private-cloud"].BucketURL.(string) != "https://uc.qbox.me" { 69 t.Fatal("Unexpected bucket url") 70 } 71 if !profileConfigs["private-cloud"].DisableSecureProtocol { 72 t.Fatal("Unexpected disable secure protocol") 73 } 74 if profileConfigs["private-cloud-2"].AccessKey != "QINIU_ACCESS_KEY_3" { 75 t.Fatal("Unexpected access key") 76 } 77 if profileConfigs["private-cloud-2"].SecretKey != "QINIU_SECRET_KEY_3" { 78 t.Fatal("Unexpected secret key") 79 } 80 if bucketUrls, ok := profileConfigs["private-cloud-2"].BucketURL.([]interface{}); !ok { 81 t.Fatal("Unexpected bucket url") 82 } else { 83 if bucketUrls[0].(string) != "https://uc.qbox.me" { 84 t.Fatal("Unexpected bucket url") 85 } 86 if bucketUrls[1].(string) != "https://uc.qiniuapi.com" { 87 t.Fatal("Unexpected bucket url") 88 } 89 } 90 if profileConfigs["private-cloud-2"].DisableSecureProtocol { 91 t.Fatal("Unexpected disable secure protocol") 92 } 93 94 os.Setenv("QINIU_PROFILE", "private-cloud") 95 defer os.Unsetenv("QINIU_PROFILE") 96 97 accessKey, secretKey, err := CredentialsFromConfigFile() 98 if err != nil { 99 t.Fatal(err) 100 } 101 if accessKey != "QINIU_ACCESS_KEY_2" { 102 t.Fatal("Unexpected access key") 103 } 104 if secretKey != "QINIU_SECRET_KEY_2" { 105 t.Fatal("Unexpected secret key") 106 } 107 108 bucketUrls, err := BucketURLsFromConfigFile() 109 if err != nil { 110 t.Fatal(err) 111 } 112 if len(bucketUrls) != 1 { 113 t.Fatal("Unexpected bucket urls") 114 } 115 if bucketUrls[0] != "https://uc.qbox.me" { 116 t.Fatal("Unexpected bucket url") 117 } 118 119 os.Setenv("QINIU_PROFILE", "private-cloud-2") 120 defer os.Unsetenv("QINIU_PROFILE") 121 122 accessKey, secretKey, err = CredentialsFromConfigFile() 123 if err != nil { 124 t.Fatal(err) 125 } 126 if accessKey != "QINIU_ACCESS_KEY_3" { 127 t.Fatal("Unexpected access key") 128 } 129 if secretKey != "QINIU_SECRET_KEY_3" { 130 t.Fatal("Unexpected secret key") 131 } 132 133 bucketUrls, err = BucketURLsFromConfigFile() 134 if err != nil { 135 t.Fatal(err) 136 } 137 if len(bucketUrls) != 2 { 138 t.Fatal("Unexpected bucket urls") 139 } 140 if bucketUrls[0] != "https://uc.qbox.me" { 141 t.Fatal("Unexpected bucket url") 142 } 143 if bucketUrls[1] != "https://uc.qiniuapi.com" { 144 t.Fatal("Unexpected bucket url") 145 } 146 }