client_test.go 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. // client test
  2. // use gocheck, install gocheck to execute "go get gopkg.in/check.v1",
  3. // see https://labix.org/gocheck
  4. package oss
  5. import (
  6. "encoding/json"
  7. "io/ioutil"
  8. "log"
  9. "math/rand"
  10. "net"
  11. "net/http"
  12. "os"
  13. "reflect"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "testing"
  18. "time"
  19. . "gopkg.in/check.v1"
  20. )
  21. // Test hooks up gocheck into the "go test" runner.
  22. func Test(t *testing.T) {
  23. TestingT(t)
  24. }
  25. type OssClientSuite struct{}
  26. var _ = Suite(&OssClientSuite{})
  27. var (
  28. // Endpoint/ID/Key
  29. endpoint = os.Getenv("OSS_TEST_ENDPOINT")
  30. accessID = os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  31. accessKey = os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  32. // Proxy
  33. proxyHost = os.Getenv("OSS_TEST_PROXY_HOST")
  34. proxyUser = os.Getenv("OSS_TEST_PROXY_USER")
  35. proxyPasswd = os.Getenv("OSS_TEST_PROXY_PASSWORD")
  36. // STS
  37. stsaccessID = os.Getenv("OSS_TEST_STS_ID")
  38. stsaccessKey = os.Getenv("OSS_TEST_STS_KEY")
  39. stsARN = os.Getenv("OSS_TEST_STS_ARN")
  40. // Credential
  41. credentialAccessID = os.Getenv("OSS_CREDENTIAL_KEY_ID")
  42. credentialAccessKey = os.Getenv("OSS_CREDENTIAL_KEY_SECRET")
  43. credentialUID = os.Getenv("OSS_CREDENTIAL_UID")
  44. )
  45. var (
  46. // prefix of bucket name for bucket ops test
  47. bucketNamePrefix = "go-sdk-test-bucket-"
  48. // bucket name for object ops test
  49. bucketName = bucketNamePrefix + RandLowStr(6)
  50. archiveBucketName = bucketNamePrefix + "arch-" + RandLowStr(6)
  51. // object name for object ops test
  52. objectNamePrefix = "go-sdk-test-object-"
  53. // sts region is one and only hangzhou
  54. stsRegion = "cn-hangzhou"
  55. // Credentials
  56. credentialBucketName = bucketNamePrefix + RandLowStr(6)
  57. )
  58. var (
  59. logPath = "go_sdk_test_" + time.Now().Format("20060102_150405") + ".log"
  60. testLogFile, _ = os.OpenFile(logPath, os.O_RDWR|os.O_CREATE, 0664)
  61. testLogger = log.New(testLogFile, "", log.Ldate|log.Ltime|log.Lshortfile)
  62. letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  63. timeoutInOperation = 3 * time.Second
  64. )
  65. func RandStr(n int) string {
  66. b := make([]rune, n)
  67. randMarker := rand.New(rand.NewSource(time.Now().UnixNano()))
  68. for i := range b {
  69. b[i] = letters[randMarker.Intn(len(letters))]
  70. }
  71. return string(b)
  72. }
  73. func CreateFile(fileName, content string, c *C) {
  74. fout, err := os.Create(fileName)
  75. defer fout.Close()
  76. c.Assert(err, IsNil)
  77. _, err = fout.WriteString(content)
  78. c.Assert(err, IsNil)
  79. }
  80. func RandLowStr(n int) string {
  81. return strings.ToLower(RandStr(n))
  82. }
  83. func ForceDeleteBucket(client *Client, bucketName string, c *C) {
  84. bucket, err := client.Bucket(bucketName)
  85. c.Assert(err, IsNil)
  86. // Delete Object
  87. marker := Marker("")
  88. for {
  89. lor, err := bucket.ListObjects(marker)
  90. c.Assert(err, IsNil)
  91. for _, object := range lor.Objects {
  92. err = bucket.DeleteObject(object.Key)
  93. c.Assert(err, IsNil)
  94. }
  95. marker = Marker(lor.NextMarker)
  96. if !lor.IsTruncated {
  97. break
  98. }
  99. }
  100. // Delete Object Versions and DeleteMarks
  101. keyMarker := KeyMarker("")
  102. versionIdMarker := VersionIdMarker("")
  103. options := []Option{keyMarker, versionIdMarker}
  104. for {
  105. lor, err := bucket.ListObjectVersions(options...)
  106. if err != nil {
  107. break
  108. }
  109. for _, object := range lor.ObjectDeleteMarkers {
  110. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  111. c.Assert(err, IsNil)
  112. }
  113. for _, object := range lor.ObjectVersions {
  114. err = bucket.DeleteObject(object.Key, VersionId(object.VersionId))
  115. c.Assert(err, IsNil)
  116. }
  117. keyMarker = KeyMarker(lor.NextKeyMarker)
  118. versionIdMarker := VersionIdMarker(lor.NextVersionIdMarker)
  119. options = []Option{keyMarker, versionIdMarker}
  120. if !lor.IsTruncated {
  121. break
  122. }
  123. }
  124. // Delete Part
  125. keyMarker = KeyMarker("")
  126. uploadIDMarker := UploadIDMarker("")
  127. for {
  128. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  129. c.Assert(err, IsNil)
  130. for _, upload := range lmur.Uploads {
  131. var imur = InitiateMultipartUploadResult{Bucket: bucketName,
  132. Key: upload.Key, UploadID: upload.UploadID}
  133. err = bucket.AbortMultipartUpload(imur)
  134. c.Assert(err, IsNil)
  135. }
  136. keyMarker = KeyMarker(lmur.NextKeyMarker)
  137. uploadIDMarker = UploadIDMarker(lmur.NextUploadIDMarker)
  138. if !lmur.IsTruncated {
  139. break
  140. }
  141. }
  142. // delete live channel
  143. strMarker := ""
  144. for {
  145. result, err := bucket.ListLiveChannel(Marker(strMarker))
  146. c.Assert(err, IsNil)
  147. for _, channel := range result.LiveChannel {
  148. err := bucket.DeleteLiveChannel(channel.Name)
  149. c.Assert(err, IsNil)
  150. }
  151. if result.IsTruncated {
  152. strMarker = result.NextMarker
  153. } else {
  154. break
  155. }
  156. }
  157. // Delete Bucket
  158. err = client.DeleteBucket(bucketName)
  159. c.Assert(err, IsNil)
  160. }
  161. // SetUpSuite runs once when the suite starts running
  162. func (s *OssClientSuite) SetUpSuite(c *C) {
  163. client, err := New(endpoint, accessID, accessKey)
  164. c.Assert(err, IsNil)
  165. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  166. c.Assert(err, IsNil)
  167. for _, bucket := range lbr.Buckets {
  168. ForceDeleteBucket(client, bucket.Name, c)
  169. }
  170. testLogger.Println("test client started")
  171. }
  172. // TearDownSuite runs before each test or benchmark starts running
  173. func (s *OssClientSuite) TearDownSuite(c *C) {
  174. client, err := New(endpoint, accessID, accessKey)
  175. c.Assert(err, IsNil)
  176. lbr, err := client.ListBuckets(Prefix(bucketNamePrefix), MaxKeys(1000))
  177. c.Assert(err, IsNil)
  178. for _, bucket := range lbr.Buckets {
  179. s.deleteBucket(client, bucket.Name, c)
  180. }
  181. testLogger.Println("test client completed")
  182. }
  183. func (s *OssClientSuite) deleteBucket(client *Client, bucketName string, c *C) {
  184. ForceDeleteBucket(client, bucketName, c)
  185. }
  186. // SetUpTest runs after each test or benchmark runs
  187. func (s *OssClientSuite) SetUpTest(c *C) {
  188. }
  189. // TearDownTest runs once after all tests or benchmarks have finished running
  190. func (s *OssClientSuite) TearDownTest(c *C) {
  191. }
  192. // TestCreateBucket
  193. func (s *OssClientSuite) TestCreateBucket(c *C) {
  194. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  195. client, err := New(endpoint, accessID, accessKey)
  196. c.Assert(err, IsNil)
  197. // Create
  198. client.DeleteBucket(bucketNameTest)
  199. err = client.CreateBucket(bucketNameTest)
  200. c.Assert(err, IsNil)
  201. //sleep 3 seconds after create bucket
  202. time.Sleep(timeoutInOperation)
  203. // verify bucket is exist
  204. found, err := client.IsBucketExist(bucketNameTest)
  205. c.Assert(err, IsNil)
  206. c.Assert(found, Equals, true)
  207. res, err := client.GetBucketACL(bucketNameTest)
  208. c.Assert(err, IsNil)
  209. c.Assert(res.ACL, Equals, string(ACLPrivate))
  210. err = client.DeleteBucket(bucketNameTest)
  211. c.Assert(err, IsNil)
  212. time.Sleep(timeoutInOperation)
  213. // CreateBucket creates with ACLPublicRead
  214. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  215. c.Assert(err, IsNil)
  216. time.Sleep(timeoutInOperation)
  217. res, err = client.GetBucketACL(bucketNameTest)
  218. c.Assert(err, IsNil)
  219. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  220. err = client.DeleteBucket(bucketNameTest)
  221. c.Assert(err, IsNil)
  222. time.Sleep(timeoutInOperation)
  223. // ACLPublicReadWrite
  224. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  225. c.Assert(err, IsNil)
  226. time.Sleep(timeoutInOperation)
  227. res, err = client.GetBucketACL(bucketNameTest)
  228. c.Assert(err, IsNil)
  229. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  230. err = client.DeleteBucket(bucketNameTest)
  231. c.Assert(err, IsNil)
  232. time.Sleep(timeoutInOperation)
  233. // ACLPrivate
  234. err = client.CreateBucket(bucketNameTest, ACL(ACLPrivate))
  235. c.Assert(err, IsNil)
  236. time.Sleep(timeoutInOperation)
  237. res, err = client.GetBucketACL(bucketNameTest)
  238. c.Assert(err, IsNil)
  239. c.Assert(res.ACL, Equals, string(ACLPrivate))
  240. // Delete
  241. err = client.DeleteBucket(bucketNameTest)
  242. c.Assert(err, IsNil)
  243. // Create bucket with configuration and test GetBucketInfo
  244. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  245. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  246. err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
  247. c.Assert(err, IsNil)
  248. time.Sleep(timeoutInOperation)
  249. res, err := client.GetBucketInfo(bucketNameTest)
  250. c.Assert(err, IsNil)
  251. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  252. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  253. c.Assert(res.BucketInfo.ACL, Equals, string(ACLPublicRead))
  254. // Delete
  255. err = client.DeleteBucket(bucketNameTest)
  256. c.Assert(err, IsNil)
  257. }
  258. // Error put bucket with configuration
  259. err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
  260. c.Assert(err, NotNil)
  261. // Create bucket with configuration and test ListBuckets
  262. for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
  263. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  264. err = client.CreateBucket(bucketNameTest, StorageClass(storage))
  265. c.Assert(err, IsNil)
  266. time.Sleep(timeoutInOperation)
  267. res, err := client.GetBucketInfo(bucketNameTest)
  268. c.Assert(err, IsNil)
  269. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  270. c.Assert(res.BucketInfo.StorageClass, Equals, string(storage))
  271. // Delete
  272. err = client.DeleteBucket(bucketNameTest)
  273. c.Assert(err, IsNil)
  274. }
  275. }
  276. // TestCreateBucketNegative
  277. func (s *OssClientSuite) TestCreateBucketNegative(c *C) {
  278. client, err := New(endpoint, accessID, accessKey)
  279. c.Assert(err, IsNil)
  280. // Bucket name invalid
  281. err = client.CreateBucket("xx")
  282. c.Assert(err, NotNil)
  283. err = client.CreateBucket("XXXX")
  284. c.Assert(err, NotNil)
  285. testLogger.Println(err)
  286. err = client.CreateBucket("_bucket")
  287. c.Assert(err, NotNil)
  288. testLogger.Println(err)
  289. // ACL invalid
  290. err = client.CreateBucket(bucketNamePrefix+RandLowStr(6), ACL("InvaldAcl"))
  291. c.Assert(err, NotNil)
  292. testLogger.Println(err)
  293. }
  294. // TestDeleteBucket
  295. func (s *OssClientSuite) TestDeleteBucket(c *C) {
  296. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  297. client, err := New(endpoint, accessID, accessKey)
  298. c.Assert(err, IsNil)
  299. // Create
  300. err = client.CreateBucket(bucketNameTest)
  301. c.Assert(err, IsNil)
  302. time.Sleep(timeoutInOperation)
  303. // Check
  304. found, err := client.IsBucketExist(bucketNameTest)
  305. c.Assert(err, IsNil)
  306. c.Assert(found, Equals, true)
  307. // Delete
  308. err = client.DeleteBucket(bucketNameTest)
  309. c.Assert(err, IsNil)
  310. time.Sleep(timeoutInOperation)
  311. // Check
  312. found, err = client.IsBucketExist(bucketNameTest)
  313. c.Assert(err, IsNil)
  314. c.Assert(found, Equals, false)
  315. err = client.DeleteBucket(bucketNameTest)
  316. c.Assert(err, NotNil)
  317. }
  318. // TestDeleteBucketNegative
  319. func (s *OssClientSuite) TestDeleteBucketNegative(c *C) {
  320. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  321. client, err := New(endpoint, accessID, accessKey)
  322. c.Assert(err, IsNil)
  323. // Bucket name invalid
  324. err = client.DeleteBucket("xx")
  325. c.Assert(err, NotNil)
  326. err = client.DeleteBucket("XXXX")
  327. c.Assert(err, NotNil)
  328. err = client.DeleteBucket("_bucket")
  329. c.Assert(err, NotNil)
  330. // Delete no exist bucket
  331. err = client.DeleteBucket("notexist")
  332. c.Assert(err, NotNil)
  333. // No permission to delete, this ak/sk for js sdk
  334. err = client.CreateBucket(bucketNameTest)
  335. c.Assert(err, IsNil)
  336. accessID := "<accessKeyId>"
  337. accessKey := "<accessKeySecret>"
  338. clientOtherUser, err := New(endpoint, accessID, accessKey)
  339. c.Assert(err, IsNil)
  340. err = clientOtherUser.DeleteBucket(bucketNameTest)
  341. c.Assert(err, NotNil)
  342. err = client.DeleteBucket(bucketNameTest)
  343. c.Assert(err, IsNil)
  344. }
  345. // TestListBucket
  346. func (s *OssClientSuite) TestListBucket(c *C) {
  347. var prefix = bucketNamePrefix + RandLowStr(6)
  348. var bucketNameLbOne = prefix + "tlb1"
  349. var bucketNameLbTwo = prefix + "tlb2"
  350. var bucketNameLbThree = prefix + "tlb3"
  351. client, err := New(endpoint, accessID, accessKey)
  352. c.Assert(err, IsNil)
  353. // CreateBucket
  354. err = client.CreateBucket(bucketNameLbOne)
  355. c.Assert(err, IsNil)
  356. err = client.CreateBucket(bucketNameLbTwo)
  357. c.Assert(err, IsNil)
  358. err = client.CreateBucket(bucketNameLbThree)
  359. c.Assert(err, IsNil)
  360. // ListBuckets, specified prefix
  361. var respHeader http.Header
  362. lbr, err := client.ListBuckets(Prefix(prefix), MaxKeys(2), GetResponseHeader(&respHeader))
  363. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  364. c.Assert(err, IsNil)
  365. c.Assert(len(lbr.Buckets), Equals, 2)
  366. // ListBuckets, specified max keys
  367. lbr, err = client.ListBuckets(MaxKeys(2))
  368. c.Assert(err, IsNil)
  369. c.Assert(len(lbr.Buckets), Equals, 2)
  370. // ListBuckets, specified max keys
  371. lbr, err = client.ListBuckets(Marker(bucketNameLbOne), MaxKeys(1))
  372. c.Assert(err, IsNil)
  373. c.Assert(len(lbr.Buckets), Equals, 1)
  374. // ListBuckets, specified max keys
  375. lbr, err = client.ListBuckets(Marker(bucketNameLbOne))
  376. c.Assert(err, IsNil)
  377. c.Assert(len(lbr.Buckets) >= 2, Equals, true)
  378. // DeleteBucket
  379. err = client.DeleteBucket(bucketNameLbOne)
  380. c.Assert(err, IsNil)
  381. err = client.DeleteBucket(bucketNameLbTwo)
  382. c.Assert(err, IsNil)
  383. err = client.DeleteBucket(bucketNameLbThree)
  384. c.Assert(err, IsNil)
  385. }
  386. // TestListBucket
  387. func (s *OssClientSuite) TestIsBucketExist(c *C) {
  388. var prefix = bucketNamePrefix + RandLowStr(6)
  389. var bucketNameLbOne = prefix + "tibe1"
  390. var bucketNameLbTwo = prefix + "tibe11"
  391. var bucketNameLbThree = prefix + "tibe111"
  392. client, err := New(endpoint, accessID, accessKey)
  393. c.Assert(err, IsNil)
  394. // CreateBucket
  395. err = client.CreateBucket(bucketNameLbOne)
  396. c.Assert(err, IsNil)
  397. err = client.CreateBucket(bucketNameLbTwo)
  398. c.Assert(err, IsNil)
  399. err = client.CreateBucket(bucketNameLbThree)
  400. c.Assert(err, IsNil)
  401. // Exist
  402. exist, err := client.IsBucketExist(bucketNameLbTwo)
  403. c.Assert(err, IsNil)
  404. c.Assert(exist, Equals, true)
  405. exist, err = client.IsBucketExist(bucketNameLbThree)
  406. c.Assert(err, IsNil)
  407. c.Assert(exist, Equals, true)
  408. exist, err = client.IsBucketExist(bucketNameLbOne)
  409. c.Assert(err, IsNil)
  410. c.Assert(exist, Equals, true)
  411. // Not exist
  412. exist, err = client.IsBucketExist(prefix + "tibe")
  413. c.Assert(err, IsNil)
  414. c.Assert(exist, Equals, false)
  415. exist, err = client.IsBucketExist(prefix + "tibe1111")
  416. c.Assert(err, IsNil)
  417. c.Assert(exist, Equals, false)
  418. // Negative
  419. exist, err = client.IsBucketExist("BucketNameInvalid")
  420. c.Assert(err, NotNil)
  421. // DeleteBucket
  422. err = client.DeleteBucket(bucketNameLbOne)
  423. c.Assert(err, IsNil)
  424. err = client.DeleteBucket(bucketNameLbTwo)
  425. c.Assert(err, IsNil)
  426. err = client.DeleteBucket(bucketNameLbThree)
  427. c.Assert(err, IsNil)
  428. }
  429. // TestSetBucketAcl
  430. func (s *OssClientSuite) TestSetBucketAcl(c *C) {
  431. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  432. client, err := New(endpoint, accessID, accessKey)
  433. c.Assert(err, IsNil)
  434. // Private
  435. err = client.CreateBucket(bucketNameTest)
  436. c.Assert(err, IsNil)
  437. res, err := client.GetBucketACL(bucketNameTest)
  438. c.Assert(err, IsNil)
  439. c.Assert(res.ACL, Equals, string(ACLPrivate))
  440. // Set ACL_PUBLIC_R
  441. err = client.SetBucketACL(bucketNameTest, ACLPublicRead)
  442. c.Assert(err, IsNil)
  443. time.Sleep(timeoutInOperation)
  444. res, err = client.GetBucketACL(bucketNameTest)
  445. c.Assert(err, IsNil)
  446. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  447. // Set ACL_PUBLIC_RW
  448. err = client.SetBucketACL(bucketNameTest, ACLPublicReadWrite)
  449. c.Assert(err, IsNil)
  450. time.Sleep(timeoutInOperation)
  451. res, err = client.GetBucketACL(bucketNameTest)
  452. c.Assert(err, IsNil)
  453. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  454. // Set ACL_PUBLIC_RW
  455. err = client.SetBucketACL(bucketNameTest, ACLPrivate)
  456. c.Assert(err, IsNil)
  457. time.Sleep(timeoutInOperation)
  458. res, err = client.GetBucketACL(bucketNameTest)
  459. c.Assert(err, IsNil)
  460. c.Assert(res.ACL, Equals, string(ACLPrivate))
  461. err = client.DeleteBucket(bucketNameTest)
  462. c.Assert(err, IsNil)
  463. }
  464. // TestSetBucketAclNegative
  465. func (s *OssClientSuite) TestBucketAclNegative(c *C) {
  466. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  467. client, err := New(endpoint, accessID, accessKey)
  468. c.Assert(err, IsNil)
  469. err = client.CreateBucket(bucketNameTest)
  470. c.Assert(err, IsNil)
  471. err = client.SetBucketACL(bucketNameTest, "InvalidACL")
  472. c.Assert(err, NotNil)
  473. testLogger.Println(err)
  474. err = client.DeleteBucket(bucketNameTest)
  475. c.Assert(err, IsNil)
  476. }
  477. // TestGetBucketAcl
  478. func (s *OssClientSuite) TestGetBucketAcl(c *C) {
  479. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  480. client, err := New(endpoint, accessID, accessKey)
  481. c.Assert(err, IsNil)
  482. // Private
  483. err = client.CreateBucket(bucketNameTest)
  484. c.Assert(err, IsNil)
  485. time.Sleep(timeoutInOperation)
  486. res, err := client.GetBucketACL(bucketNameTest)
  487. c.Assert(err, IsNil)
  488. c.Assert(res.ACL, Equals, string(ACLPrivate))
  489. err = client.DeleteBucket(bucketNameTest)
  490. c.Assert(err, IsNil)
  491. time.Sleep(timeoutInOperation)
  492. // PublicRead
  493. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicRead))
  494. c.Assert(err, IsNil)
  495. time.Sleep(timeoutInOperation)
  496. res, err = client.GetBucketACL(bucketNameTest)
  497. c.Assert(err, IsNil)
  498. c.Assert(res.ACL, Equals, string(ACLPublicRead))
  499. err = client.DeleteBucket(bucketNameTest)
  500. c.Assert(err, IsNil)
  501. time.Sleep(timeoutInOperation)
  502. // PublicReadWrite
  503. err = client.CreateBucket(bucketNameTest, ACL(ACLPublicReadWrite))
  504. c.Assert(err, IsNil)
  505. time.Sleep(timeoutInOperation)
  506. res, err = client.GetBucketACL(bucketNameTest)
  507. c.Assert(err, IsNil)
  508. c.Assert(res.ACL, Equals, string(ACLPublicReadWrite))
  509. err = client.DeleteBucket(bucketNameTest)
  510. c.Assert(err, IsNil)
  511. }
  512. // TestGetBucketAcl
  513. func (s *OssClientSuite) TestGetBucketLocation(c *C) {
  514. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  515. client, err := New(endpoint, accessID, accessKey)
  516. c.Assert(err, IsNil)
  517. // Private
  518. err = client.CreateBucket(bucketNameTest)
  519. c.Assert(err, IsNil)
  520. loc, err := client.GetBucketLocation(bucketNameTest)
  521. c.Assert(strings.HasPrefix(loc, "oss-"), Equals, true)
  522. err = client.DeleteBucket(bucketNameTest)
  523. c.Assert(err, IsNil)
  524. }
  525. // TestGetBucketLocationNegative
  526. func (s *OssClientSuite) TestGetBucketLocationNegative(c *C) {
  527. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  528. client, err := New(endpoint, accessID, accessKey)
  529. c.Assert(err, IsNil)
  530. // Not exist
  531. _, err = client.GetBucketLocation(bucketNameTest)
  532. c.Assert(err, NotNil)
  533. // Not exist
  534. _, err = client.GetBucketLocation("InvalidBucketName_")
  535. c.Assert(err, NotNil)
  536. }
  537. // TestSetBucketLifecycle
  538. func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
  539. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  540. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  541. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  542. client, err := New(endpoint, accessID, accessKey)
  543. c.Assert(err, IsNil)
  544. err = client.CreateBucket(bucketNameTest)
  545. c.Assert(err, IsNil)
  546. // Set single rule
  547. var rules = []LifecycleRule{rule1}
  548. err = client.SetBucketLifecycle(bucketNameTest, rules)
  549. c.Assert(err, IsNil)
  550. // Double set rule
  551. err = client.SetBucketLifecycle(bucketNameTest, rules)
  552. c.Assert(err, IsNil)
  553. res, err := client.GetBucketLifecycle(bucketNameTest)
  554. c.Assert(err, IsNil)
  555. c.Assert(len(res.Rules), Equals, 1)
  556. c.Assert(res.Rules[0].ID, Equals, "rule1")
  557. err = client.DeleteBucketLifecycle(bucketNameTest)
  558. c.Assert(err, IsNil)
  559. // Set two rules
  560. rules = []LifecycleRule{rule1, rule2}
  561. err = client.SetBucketLifecycle(bucketNameTest, rules)
  562. c.Assert(err, IsNil)
  563. // Eliminate effect of cache
  564. time.Sleep(timeoutInOperation)
  565. res, err = client.GetBucketLifecycle(bucketNameTest)
  566. c.Assert(err, IsNil)
  567. c.Assert(len(res.Rules), Equals, 2)
  568. c.Assert(res.Rules[0].ID, Equals, "rule1")
  569. c.Assert(res.Rules[1].ID, Equals, "rule2")
  570. err = client.DeleteBucket(bucketNameTest)
  571. c.Assert(err, IsNil)
  572. }
  573. // TestSetBucketLifecycleNew
  574. func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
  575. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  576. client, err := New(endpoint, accessID, accessKey)
  577. c.Assert(err, IsNil)
  578. err = client.CreateBucket(bucketNameTest)
  579. c.Assert(err, IsNil)
  580. //invalid status of lifecyclerule
  581. expiration := LifecycleExpiration{
  582. Days: 30,
  583. }
  584. rule := LifecycleRule{
  585. ID: "rule1",
  586. Prefix: "one",
  587. Status: "Invalid",
  588. Expiration: &expiration,
  589. }
  590. rules := []LifecycleRule{rule}
  591. err = client.SetBucketLifecycle(bucketNameTest, rules)
  592. c.Assert(err, NotNil)
  593. //invalid value of CreatedBeforeDate
  594. expiration = LifecycleExpiration{
  595. CreatedBeforeDate: RandStr(10),
  596. }
  597. rule = LifecycleRule{
  598. ID: "rule1",
  599. Prefix: "one",
  600. Status: "Enabled",
  601. Expiration: &expiration,
  602. }
  603. rules = []LifecycleRule{rule}
  604. err = client.SetBucketLifecycle(bucketNameTest, rules)
  605. c.Assert(err, NotNil)
  606. //invalid value of Days
  607. abortMPU := LifecycleAbortMultipartUpload{
  608. Days: -30,
  609. }
  610. rule = LifecycleRule{
  611. ID: "rule1",
  612. Prefix: "one",
  613. Status: "Enabled",
  614. AbortMultipartUpload: &abortMPU,
  615. }
  616. rules = []LifecycleRule{rule}
  617. err = client.SetBucketLifecycle(bucketNameTest, rules)
  618. c.Assert(err, NotNil)
  619. expiration = LifecycleExpiration{
  620. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  621. }
  622. rule1 := LifecycleRule{
  623. ID: "rule1",
  624. Prefix: "one",
  625. Status: "Enabled",
  626. Expiration: &expiration,
  627. }
  628. abortMPU = LifecycleAbortMultipartUpload{
  629. Days: 30,
  630. }
  631. rule2 := LifecycleRule{
  632. ID: "rule2",
  633. Prefix: "two",
  634. Status: "Enabled",
  635. Expiration: &expiration,
  636. AbortMultipartUpload: &abortMPU,
  637. }
  638. transition1 := LifecycleTransition{
  639. Days: 3,
  640. StorageClass: StorageIA,
  641. }
  642. transition2 := LifecycleTransition{
  643. Days: 30,
  644. StorageClass: StorageArchive,
  645. }
  646. transitions := []LifecycleTransition{transition1, transition2}
  647. rule3 := LifecycleRule{
  648. ID: "rule3",
  649. Prefix: "three",
  650. Status: "Enabled",
  651. AbortMultipartUpload: &abortMPU,
  652. Transitions: transitions,
  653. }
  654. // Set single rule
  655. rules = []LifecycleRule{rule1}
  656. err = client.SetBucketLifecycle(bucketNameTest, rules)
  657. c.Assert(err, IsNil)
  658. res, err := client.GetBucketLifecycle(bucketNameTest)
  659. c.Assert(err, IsNil)
  660. c.Assert(len(res.Rules), Equals, 1)
  661. c.Assert(res.Rules[0].ID, Equals, "rule1")
  662. c.Assert(res.Rules[0].Expiration, NotNil)
  663. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  664. err = client.DeleteBucketLifecycle(bucketNameTest)
  665. c.Assert(err, IsNil)
  666. // Set two rule: rule1 and rule2
  667. rules = []LifecycleRule{rule1, rule2}
  668. err = client.SetBucketLifecycle(bucketNameTest, rules)
  669. c.Assert(err, IsNil)
  670. res, err = client.GetBucketLifecycle(bucketNameTest)
  671. c.Assert(err, IsNil)
  672. c.Assert(len(res.Rules), Equals, 2)
  673. c.Assert(res.Rules[0].ID, Equals, "rule1")
  674. c.Assert(res.Rules[0].Expiration, NotNil)
  675. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  676. c.Assert(res.Rules[1].ID, Equals, "rule2")
  677. c.Assert(res.Rules[1].Expiration, NotNil)
  678. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  679. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  680. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  681. err = client.DeleteBucketLifecycle(bucketNameTest)
  682. c.Assert(err, IsNil)
  683. // Set two rule: rule2 and rule3
  684. rules = []LifecycleRule{rule2, rule3}
  685. err = client.SetBucketLifecycle(bucketNameTest, rules)
  686. c.Assert(err, IsNil)
  687. res, err = client.GetBucketLifecycle(bucketNameTest)
  688. c.Assert(err, IsNil)
  689. c.Assert(len(res.Rules), Equals, 2)
  690. c.Assert(res.Rules[0].ID, Equals, "rule2")
  691. c.Assert(res.Rules[0].Expiration, NotNil)
  692. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  693. c.Assert(res.Rules[0].AbortMultipartUpload, NotNil)
  694. c.Assert(res.Rules[0].AbortMultipartUpload.Days, Equals, 30)
  695. c.Assert(res.Rules[1].ID, Equals, "rule3")
  696. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  697. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  698. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  699. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  700. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  701. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  702. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  703. err = client.DeleteBucketLifecycle(bucketNameTest)
  704. c.Assert(err, IsNil)
  705. // Set two rule: rule1 and rule3
  706. rules = []LifecycleRule{rule1, rule3}
  707. err = client.SetBucketLifecycle(bucketNameTest, rules)
  708. c.Assert(err, IsNil)
  709. res, err = client.GetBucketLifecycle(bucketNameTest)
  710. c.Assert(err, IsNil)
  711. c.Assert(len(res.Rules), Equals, 2)
  712. c.Assert(res.Rules[0].ID, Equals, "rule1")
  713. c.Assert(res.Rules[0].Expiration, NotNil)
  714. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  715. c.Assert(res.Rules[1].ID, Equals, "rule3")
  716. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  717. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  718. c.Assert(len(res.Rules[1].Transitions), Equals, 2)
  719. c.Assert(res.Rules[1].Transitions[0].StorageClass, Equals, StorageIA)
  720. c.Assert(res.Rules[1].Transitions[0].Days, Equals, 3)
  721. c.Assert(res.Rules[1].Transitions[1].StorageClass, Equals, StorageArchive)
  722. c.Assert(res.Rules[1].Transitions[1].Days, Equals, 30)
  723. err = client.DeleteBucketLifecycle(bucketNameTest)
  724. c.Assert(err, IsNil)
  725. // Set three rules
  726. rules = []LifecycleRule{rule1, rule2, rule3}
  727. err = client.SetBucketLifecycle(bucketNameTest, rules)
  728. c.Assert(err, IsNil)
  729. res, err = client.GetBucketLifecycle(bucketNameTest)
  730. c.Assert(err, IsNil)
  731. c.Assert(len(res.Rules), Equals, 3)
  732. c.Assert(res.Rules[0].ID, Equals, "rule1")
  733. c.Assert(res.Rules[0].Expiration, NotNil)
  734. c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  735. c.Assert(res.Rules[1].ID, Equals, "rule2")
  736. c.Assert(res.Rules[1].Expiration, NotNil)
  737. c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
  738. c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
  739. c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
  740. c.Assert(res.Rules[2].ID, Equals, "rule3")
  741. c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
  742. c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
  743. c.Assert(len(res.Rules[2].Transitions), Equals, 2)
  744. c.Assert(res.Rules[2].Transitions[0].StorageClass, Equals, StorageIA)
  745. c.Assert(res.Rules[2].Transitions[0].Days, Equals, 3)
  746. c.Assert(res.Rules[2].Transitions[1].StorageClass, Equals, StorageArchive)
  747. c.Assert(res.Rules[2].Transitions[1].Days, Equals, 30)
  748. err = client.DeleteBucket(bucketNameTest)
  749. c.Assert(err, IsNil)
  750. }
  751. // TestDeleteBucketLifecycle
  752. func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
  753. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  754. var rule1 = BuildLifecycleRuleByDate("rule1", "one", true, 2015, 11, 11)
  755. var rule2 = BuildLifecycleRuleByDays("rule2", "two", true, 3)
  756. var rules = []LifecycleRule{rule1, rule2}
  757. client, err := New(endpoint, accessID, accessKey)
  758. c.Assert(err, IsNil)
  759. err = client.CreateBucket(bucketNameTest)
  760. c.Assert(err, IsNil)
  761. //time.Sleep(timeoutInOperation)
  762. err = client.DeleteBucketLifecycle(bucketNameTest)
  763. c.Assert(err, IsNil)
  764. err = client.SetBucketLifecycle(bucketNameTest, rules)
  765. c.Assert(err, IsNil)
  766. //time.Sleep(timeoutInOperation)
  767. res, err := client.GetBucketLifecycle(bucketNameTest)
  768. c.Assert(err, IsNil)
  769. c.Assert(len(res.Rules), Equals, 2)
  770. // Delete
  771. err = client.DeleteBucketLifecycle(bucketNameTest)
  772. c.Assert(err, IsNil)
  773. //time.Sleep(timeoutInOperation)
  774. res, err = client.GetBucketLifecycle(bucketNameTest)
  775. c.Assert(err, NotNil)
  776. // Eliminate effect of cache
  777. //time.Sleep(timeoutInOperation)
  778. // Delete when not set
  779. err = client.DeleteBucketLifecycle(bucketNameTest)
  780. c.Assert(err, IsNil)
  781. err = client.DeleteBucket(bucketNameTest)
  782. c.Assert(err, IsNil)
  783. }
  784. // TestSetBucketLifecycleNegative
  785. func (s *OssClientSuite) TestBucketLifecycleNegative(c *C) {
  786. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  787. var rules = []LifecycleRule{}
  788. client, err := New(endpoint, accessID, accessKey)
  789. c.Assert(err, IsNil)
  790. err = client.CreateBucket(bucketNameTest)
  791. c.Assert(err, IsNil)
  792. // Set with no rule
  793. err = client.SetBucketLifecycle(bucketNameTest, rules)
  794. c.Assert(err, NotNil)
  795. err = client.DeleteBucket(bucketNameTest)
  796. c.Assert(err, IsNil)
  797. // Not exist
  798. err = client.SetBucketLifecycle(bucketNameTest, rules)
  799. c.Assert(err, NotNil)
  800. // Not exist
  801. _, err = client.GetBucketLifecycle(bucketNameTest)
  802. c.Assert(err, NotNil)
  803. // Not exist
  804. err = client.DeleteBucketLifecycle(bucketNameTest)
  805. c.Assert(err, NotNil)
  806. }
  807. // TestSetBucketReferer
  808. func (s *OssClientSuite) TestSetBucketReferer(c *C) {
  809. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  810. var referers = []string{"http://www.aliyun.com", "https://www.aliyun.com"}
  811. client, err := New(endpoint, accessID, accessKey)
  812. c.Assert(err, IsNil)
  813. err = client.CreateBucket(bucketNameTest)
  814. c.Assert(err, IsNil)
  815. time.Sleep(timeoutInOperation)
  816. res, err := client.GetBucketReferer(bucketNameTest)
  817. c.Assert(res.AllowEmptyReferer, Equals, true)
  818. c.Assert(len(res.RefererList), Equals, 0)
  819. // Set referers
  820. err = client.SetBucketReferer(bucketNameTest, referers, false)
  821. c.Assert(err, IsNil)
  822. time.Sleep(timeoutInOperation)
  823. res, err = client.GetBucketReferer(bucketNameTest)
  824. c.Assert(res.AllowEmptyReferer, Equals, false)
  825. c.Assert(len(res.RefererList), Equals, 2)
  826. c.Assert(res.RefererList[0], Equals, "http://www.aliyun.com")
  827. c.Assert(res.RefererList[1], Equals, "https://www.aliyun.com")
  828. // Reset referer, referers empty
  829. referers = []string{""}
  830. err = client.SetBucketReferer(bucketNameTest, referers, true)
  831. c.Assert(err, IsNil)
  832. referers = []string{}
  833. err = client.SetBucketReferer(bucketNameTest, referers, true)
  834. c.Assert(err, IsNil)
  835. res, err = client.GetBucketReferer(bucketNameTest)
  836. c.Assert(res.AllowEmptyReferer, Equals, true)
  837. c.Assert(len(res.RefererList), Equals, 0)
  838. err = client.DeleteBucket(bucketNameTest)
  839. c.Assert(err, IsNil)
  840. }
  841. // TestSetBucketRefererNegative
  842. func (s *OssClientSuite) TestBucketRefererNegative(c *C) {
  843. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  844. var referers = []string{""}
  845. client, err := New(endpoint, accessID, accessKey)
  846. c.Assert(err, IsNil)
  847. // Not exist
  848. _, err = client.GetBucketReferer(bucketNameTest)
  849. c.Assert(err, NotNil)
  850. testLogger.Println(err)
  851. // Not exist
  852. err = client.SetBucketReferer(bucketNameTest, referers, true)
  853. c.Assert(err, NotNil)
  854. testLogger.Println(err)
  855. }
  856. // TestSetBucketLogging
  857. func (s *OssClientSuite) TestSetBucketLogging(c *C) {
  858. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  859. var bucketNameTarget = bucketNameTest + "-target"
  860. client, err := New(endpoint, accessID, accessKey)
  861. c.Assert(err, IsNil)
  862. err = client.CreateBucket(bucketNameTest)
  863. c.Assert(err, IsNil)
  864. err = client.CreateBucket(bucketNameTarget)
  865. c.Assert(err, IsNil)
  866. time.Sleep(timeoutInOperation)
  867. // Set logging
  868. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  869. c.Assert(err, IsNil)
  870. // Reset
  871. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  872. c.Assert(err, IsNil)
  873. time.Sleep(timeoutInOperation)
  874. res, err := client.GetBucketLogging(bucketNameTest)
  875. c.Assert(err, IsNil)
  876. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  877. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  878. err = client.DeleteBucketLogging(bucketNameTest)
  879. c.Assert(err, IsNil)
  880. // Set to self
  881. err = client.SetBucketLogging(bucketNameTest, bucketNameTest, "prefix", true)
  882. c.Assert(err, IsNil)
  883. err = client.DeleteBucket(bucketNameTest)
  884. c.Assert(err, IsNil)
  885. err = client.DeleteBucket(bucketNameTarget)
  886. c.Assert(err, IsNil)
  887. }
  888. // TestDeleteBucketLogging
  889. func (s *OssClientSuite) TestDeleteBucketLogging(c *C) {
  890. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  891. var bucketNameTarget = bucketNameTest + "-target"
  892. client, err := New(endpoint, accessID, accessKey)
  893. c.Assert(err, IsNil)
  894. err = client.CreateBucket(bucketNameTest)
  895. c.Assert(err, IsNil)
  896. err = client.CreateBucket(bucketNameTarget)
  897. c.Assert(err, IsNil)
  898. time.Sleep(timeoutInOperation)
  899. // Get when not set
  900. res, err := client.GetBucketLogging(bucketNameTest)
  901. c.Assert(err, IsNil)
  902. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  903. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  904. // Set
  905. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  906. c.Assert(err, IsNil)
  907. // Get
  908. time.Sleep(timeoutInOperation)
  909. res, err = client.GetBucketLogging(bucketNameTest)
  910. c.Assert(err, IsNil)
  911. c.Assert(res.LoggingEnabled.TargetBucket, Equals, bucketNameTarget)
  912. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "prefix")
  913. // Set
  914. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", false)
  915. c.Assert(err, IsNil)
  916. // Get
  917. time.Sleep(timeoutInOperation)
  918. res, err = client.GetBucketLogging(bucketNameTest)
  919. c.Assert(err, IsNil)
  920. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  921. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  922. // Delete
  923. err = client.DeleteBucketLogging(bucketNameTest)
  924. c.Assert(err, IsNil)
  925. // Get after delete
  926. time.Sleep(timeoutInOperation)
  927. res, err = client.GetBucketLogging(bucketNameTest)
  928. c.Assert(err, IsNil)
  929. c.Assert(res.LoggingEnabled.TargetBucket, Equals, "")
  930. c.Assert(res.LoggingEnabled.TargetPrefix, Equals, "")
  931. err = client.DeleteBucket(bucketNameTest)
  932. c.Assert(err, IsNil)
  933. err = client.DeleteBucket(bucketNameTarget)
  934. c.Assert(err, IsNil)
  935. }
  936. // TestSetBucketLoggingNegative
  937. func (s *OssClientSuite) TestSetBucketLoggingNegative(c *C) {
  938. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  939. var bucketNameTarget = bucketNameTest + "-target"
  940. client, err := New(endpoint, accessID, accessKey)
  941. c.Assert(err, IsNil)
  942. // Not exist
  943. _, err = client.GetBucketLogging(bucketNameTest)
  944. c.Assert(err, NotNil)
  945. // Not exist
  946. err = client.SetBucketLogging(bucketNameTest, "targetbucket", "prefix", true)
  947. c.Assert(err, NotNil)
  948. // Not exist
  949. err = client.DeleteBucketLogging(bucketNameTest)
  950. c.Assert(err, NotNil)
  951. err = client.CreateBucket(bucketNameTest)
  952. c.Assert(err, IsNil)
  953. time.Sleep(timeoutInOperation)
  954. // Target bucket not exist
  955. err = client.SetBucketLogging(bucketNameTest, bucketNameTarget, "prefix", true)
  956. c.Assert(err, NotNil)
  957. // Parameter invalid
  958. err = client.SetBucketLogging(bucketNameTest, "XXXX", "prefix", true)
  959. c.Assert(err, NotNil)
  960. err = client.SetBucketLogging(bucketNameTest, "xx", "prefix", true)
  961. c.Assert(err, NotNil)
  962. err = client.DeleteBucket(bucketNameTest)
  963. c.Assert(err, IsNil)
  964. }
  965. // TestSetBucketWebsite
  966. func (s *OssClientSuite) TestSetBucketWebsite(c *C) {
  967. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  968. var indexWebsite = "myindex.html"
  969. var errorWebsite = "myerror.html"
  970. client, err := New(endpoint, accessID, accessKey)
  971. c.Assert(err, IsNil)
  972. time.Sleep(timeoutInOperation)
  973. err = client.CreateBucket(bucketNameTest)
  974. c.Assert(err, IsNil)
  975. // Set
  976. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  977. c.Assert(err, IsNil)
  978. // Double set
  979. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  980. c.Assert(err, IsNil)
  981. res, err := client.GetBucketWebsite(bucketNameTest)
  982. c.Assert(err, IsNil)
  983. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  984. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  985. // Reset
  986. err = client.SetBucketWebsite(bucketNameTest, "your"+indexWebsite, "your"+errorWebsite)
  987. c.Assert(err, IsNil)
  988. time.Sleep(timeoutInOperation)
  989. res, err = client.GetBucketWebsite(bucketNameTest)
  990. c.Assert(err, IsNil)
  991. c.Assert(res.IndexDocument.Suffix, Equals, "your"+indexWebsite)
  992. c.Assert(res.ErrorDocument.Key, Equals, "your"+errorWebsite)
  993. err = client.DeleteBucketWebsite(bucketNameTest)
  994. c.Assert(err, IsNil)
  995. // Set after delete
  996. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  997. c.Assert(err, IsNil)
  998. // Eliminate effect of cache
  999. time.Sleep(timeoutInOperation)
  1000. res, err = client.GetBucketWebsite(bucketNameTest)
  1001. c.Assert(err, IsNil)
  1002. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1003. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1004. err = client.DeleteBucket(bucketNameTest)
  1005. c.Assert(err, IsNil)
  1006. }
  1007. // TestDeleteBucketWebsite
  1008. func (s *OssClientSuite) TestDeleteBucketWebsite(c *C) {
  1009. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1010. var indexWebsite = "myindex.html"
  1011. var errorWebsite = "myerror.html"
  1012. client, err := New(endpoint, accessID, accessKey)
  1013. c.Assert(err, IsNil)
  1014. err = client.CreateBucket(bucketNameTest)
  1015. c.Assert(err, IsNil)
  1016. time.Sleep(timeoutInOperation)
  1017. // Get
  1018. res, err := client.GetBucketWebsite(bucketNameTest)
  1019. c.Assert(err, NotNil)
  1020. // Detele without set
  1021. err = client.DeleteBucketWebsite(bucketNameTest)
  1022. c.Assert(err, IsNil)
  1023. // Set
  1024. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1025. c.Assert(err, IsNil)
  1026. time.Sleep(timeoutInOperation)
  1027. res, err = client.GetBucketWebsite(bucketNameTest)
  1028. c.Assert(err, IsNil)
  1029. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1030. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1031. // Detele
  1032. time.Sleep(timeoutInOperation)
  1033. err = client.DeleteBucketWebsite(bucketNameTest)
  1034. c.Assert(err, IsNil)
  1035. time.Sleep(timeoutInOperation)
  1036. res, err = client.GetBucketWebsite(bucketNameTest)
  1037. c.Assert(err, NotNil)
  1038. // Detele after delete
  1039. err = client.DeleteBucketWebsite(bucketNameTest)
  1040. c.Assert(err, IsNil)
  1041. err = client.DeleteBucket(bucketNameTest)
  1042. c.Assert(err, IsNil)
  1043. }
  1044. // TestSetBucketWebsiteNegative
  1045. func (s *OssClientSuite) TestSetBucketWebsiteNegative(c *C) {
  1046. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1047. var indexWebsite = "myindex.html"
  1048. var errorWebsite = "myerror.html"
  1049. client, err := New(endpoint, accessID, accessKey)
  1050. c.Assert(err, IsNil)
  1051. err = client.DeleteBucket(bucketNameTest)
  1052. // Not exist
  1053. _, err = client.GetBucketWebsite(bucketNameTest)
  1054. c.Assert(err, NotNil)
  1055. err = client.DeleteBucketWebsite(bucketNameTest)
  1056. c.Assert(err, NotNil)
  1057. err = client.SetBucketWebsite(bucketNameTest, indexWebsite, errorWebsite)
  1058. c.Assert(err, NotNil)
  1059. err = client.CreateBucket(bucketNameTest)
  1060. c.Assert(err, IsNil)
  1061. // Set
  1062. time.Sleep(timeoutInOperation)
  1063. err = client.SetBucketWebsite(bucketNameTest, "myindex", "myerror")
  1064. c.Assert(err, IsNil)
  1065. res, err := client.GetBucketWebsite(bucketNameTest)
  1066. c.Assert(err, IsNil)
  1067. c.Assert(res.IndexDocument.Suffix, Equals, "myindex")
  1068. c.Assert(res.ErrorDocument.Key, Equals, "myerror")
  1069. // Detele
  1070. err = client.DeleteBucketWebsite(bucketNameTest)
  1071. c.Assert(err, IsNil)
  1072. time.Sleep(timeoutInOperation)
  1073. _, err = client.GetBucketWebsite(bucketNameTest)
  1074. c.Assert(err, NotNil)
  1075. // Detele after delete
  1076. err = client.DeleteBucketWebsite(bucketNameTest)
  1077. c.Assert(err, IsNil)
  1078. err = client.DeleteBucket(bucketNameTest)
  1079. c.Assert(err, IsNil)
  1080. }
  1081. // TestSetBucketWebsiteDetail
  1082. func (s *OssClientSuite) TestSetBucketWebsiteDetail(c *C) {
  1083. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1084. var indexWebsite = "myindex.html"
  1085. var errorWebsite = "myerror.html"
  1086. client, err := New(endpoint, accessID, accessKey)
  1087. c.Assert(err, IsNil)
  1088. err = client.CreateBucket(bucketNameTest)
  1089. c.Assert(err, IsNil)
  1090. time.Sleep(timeoutInOperation)
  1091. btrue := true
  1092. bfalse := false
  1093. // Define one routing rule
  1094. ruleOk := RoutingRule{
  1095. RuleNumber: 1,
  1096. Condition: Condition{
  1097. KeyPrefixEquals: "",
  1098. HTTPErrorCodeReturnedEquals: 404,
  1099. },
  1100. Redirect: Redirect{
  1101. RedirectType: "Mirror",
  1102. // PassQueryString: &btrue, // set default value
  1103. MirrorURL: "http://www.test.com/",
  1104. // MirrorPassQueryString:&btrue, // set default value
  1105. // MirrorFollowRedirect:&bfalse, // set default value
  1106. // MirrorCheckMd5:&bfalse, // set default value
  1107. MirrorHeaders: MirrorHeaders{
  1108. // PassAll:&bfalse, // set default value
  1109. Pass: []string{"myheader-key1", "myheader-key2"},
  1110. Remove: []string{"myheader-key3", "myheader-key4"},
  1111. Set: []MirrorHeaderSet{
  1112. MirrorHeaderSet{
  1113. Key: "myheader-key5",
  1114. Value: "myheader-value5",
  1115. },
  1116. },
  1117. },
  1118. },
  1119. }
  1120. // Define array routing rule
  1121. ruleArrOk := []RoutingRule{
  1122. RoutingRule{
  1123. RuleNumber: 2,
  1124. Condition: Condition{
  1125. KeyPrefixEquals: "abc/",
  1126. HTTPErrorCodeReturnedEquals: 404,
  1127. IncludeHeader: []IncludeHeader{
  1128. IncludeHeader{
  1129. Key: "host",
  1130. Equals: "test.oss-cn-beijing-internal.aliyuncs.com",
  1131. },
  1132. },
  1133. },
  1134. Redirect: Redirect{
  1135. RedirectType: "AliCDN",
  1136. Protocol: "http",
  1137. HostName: "www.test.com",
  1138. PassQueryString: &bfalse,
  1139. ReplaceKeyWith: "prefix/${key}.suffix",
  1140. HttpRedirectCode: 301,
  1141. },
  1142. },
  1143. RoutingRule{
  1144. RuleNumber: 3,
  1145. Condition: Condition{
  1146. KeyPrefixEquals: "",
  1147. HTTPErrorCodeReturnedEquals: 404,
  1148. },
  1149. Redirect: Redirect{
  1150. RedirectType: "Mirror",
  1151. PassQueryString: &btrue,
  1152. MirrorURL: "http://www.test.com/",
  1153. MirrorPassQueryString: &btrue,
  1154. MirrorFollowRedirect: &bfalse,
  1155. MirrorCheckMd5: &bfalse,
  1156. MirrorHeaders: MirrorHeaders{
  1157. PassAll: &btrue,
  1158. Pass: []string{"myheader-key1", "myheader-key2"},
  1159. Remove: []string{"myheader-key3", "myheader-key4"},
  1160. Set: []MirrorHeaderSet{
  1161. MirrorHeaderSet{
  1162. Key: "myheader-key5",
  1163. Value: "myheader-value5",
  1164. },
  1165. },
  1166. },
  1167. },
  1168. },
  1169. }
  1170. // Set one routing rule
  1171. wxmlOne := WebsiteXML{}
  1172. wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk)
  1173. var responseHeader http.Header
  1174. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlOne, GetResponseHeader(&responseHeader))
  1175. c.Assert(err, IsNil)
  1176. requestId := GetRequestId(responseHeader)
  1177. c.Assert(len(requestId) > 0, Equals, true)
  1178. res, err := client.GetBucketWebsite(bucketNameTest)
  1179. c.Assert(err, IsNil)
  1180. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1181. c.Assert(*res.RoutingRules[0].Redirect.PassQueryString, Equals, false)
  1182. c.Assert(*res.RoutingRules[0].Redirect.MirrorPassQueryString, Equals, false)
  1183. c.Assert(*res.RoutingRules[0].Redirect.MirrorFollowRedirect, Equals, true)
  1184. c.Assert(*res.RoutingRules[0].Redirect.MirrorCheckMd5, Equals, false)
  1185. c.Assert(*res.RoutingRules[0].Redirect.MirrorHeaders.PassAll, Equals, false)
  1186. // Set one routing rule and IndexDocument, IndexDocument
  1187. wxml := WebsiteXML{
  1188. IndexDocument: IndexDocument{Suffix: indexWebsite},
  1189. ErrorDocument: ErrorDocument{Key: errorWebsite},
  1190. }
  1191. wxml.RoutingRules = append(wxml.RoutingRules, ruleOk)
  1192. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1193. c.Assert(err, IsNil)
  1194. requestId = GetRequestId(responseHeader)
  1195. c.Assert(len(requestId) > 0, Equals, true)
  1196. res, err = client.GetBucketWebsite(bucketNameTest)
  1197. c.Assert(err, IsNil)
  1198. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1199. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1200. c.Assert(res.RoutingRules[0].Redirect.RedirectType, Equals, "Mirror")
  1201. // Set array routing rule
  1202. wxml.RoutingRules = append(wxml.RoutingRules, ruleArrOk...)
  1203. err = client.SetBucketWebsiteDetail(bucketNameTest, wxml, GetResponseHeader(&responseHeader))
  1204. c.Assert(err, IsNil)
  1205. requestId = GetRequestId(responseHeader)
  1206. c.Assert(len(requestId) > 0, Equals, true)
  1207. res, err = client.GetBucketWebsite(bucketNameTest)
  1208. c.Assert(err, IsNil)
  1209. c.Assert(res.IndexDocument.Suffix, Equals, indexWebsite)
  1210. c.Assert(res.ErrorDocument.Key, Equals, errorWebsite)
  1211. c.Assert(len(res.RoutingRules), Equals, 3)
  1212. c.Assert(res.RoutingRules[1].Redirect.RedirectType, Equals, "AliCDN")
  1213. c.Assert(*res.RoutingRules[2].Redirect.MirrorPassQueryString, Equals, true)
  1214. c.Assert(*res.RoutingRules[2].Redirect.MirrorFollowRedirect, Equals, false)
  1215. // Define one error routing rule
  1216. ruleErr := RoutingRule{
  1217. RuleNumber: 1,
  1218. Redirect: Redirect{
  1219. RedirectType: "Mirror",
  1220. PassQueryString: &btrue,
  1221. },
  1222. }
  1223. // Define array error routing rule
  1224. rulesErrArr := []RoutingRule{
  1225. RoutingRule{
  1226. RuleNumber: 1,
  1227. Redirect: Redirect{
  1228. RedirectType: "Mirror",
  1229. PassQueryString: &btrue,
  1230. },
  1231. },
  1232. RoutingRule{
  1233. RuleNumber: 2,
  1234. Redirect: Redirect{
  1235. RedirectType: "Mirror",
  1236. PassQueryString: &btrue,
  1237. },
  1238. },
  1239. }
  1240. ruleIntErr := RoutingRule{
  1241. // RuleNumber:0, // set NULL value
  1242. Condition: Condition{
  1243. KeyPrefixEquals: "",
  1244. HTTPErrorCodeReturnedEquals: 404,
  1245. },
  1246. Redirect: Redirect{
  1247. RedirectType: "Mirror",
  1248. // PassQueryString: &btrue, // set default value
  1249. MirrorURL: "http://www.test.com/",
  1250. // MirrorPassQueryString:&btrue, // set default value
  1251. // MirrorFollowRedirect:&bfalse, // set default value
  1252. // MirrorCheckMd5:&bfalse, // set default value
  1253. MirrorHeaders: MirrorHeaders{
  1254. // PassAll:&bfalse, // set default value
  1255. Pass: []string{"myheader-key1", "myheader-key2"},
  1256. Remove: []string{"myheader-key3", "myheader-key4"},
  1257. Set: []MirrorHeaderSet{
  1258. MirrorHeaderSet{
  1259. Key: "myheader-key5",
  1260. Value: "myheader-value5",
  1261. },
  1262. },
  1263. },
  1264. },
  1265. }
  1266. // Set one int type error rule
  1267. wxmlIntErr := WebsiteXML{}
  1268. wxmlIntErr.RoutingRules = append(wxmlIntErr.RoutingRules, ruleIntErr)
  1269. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlIntErr)
  1270. c.Assert(err, NotNil)
  1271. // Set one error rule
  1272. wxmlErr := WebsiteXML{}
  1273. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleErr)
  1274. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1275. c.Assert(err, NotNil)
  1276. // Set one error rule and one correct rule
  1277. wxmlErr.RoutingRules = append(wxmlErr.RoutingRules, ruleOk)
  1278. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErr)
  1279. c.Assert(err, NotNil)
  1280. wxmlErrRuleArr := WebsiteXML{}
  1281. wxmlErrRuleArr.RoutingRules = append(wxmlErrRuleArr.RoutingRules, rulesErrArr...)
  1282. // Set array error routing rule
  1283. err = client.SetBucketWebsiteDetail(bucketNameTest, wxmlErrRuleArr)
  1284. c.Assert(err, NotNil)
  1285. err = client.DeleteBucketWebsite(bucketNameTest)
  1286. c.Assert(err, IsNil)
  1287. err = client.DeleteBucket(bucketNameTest)
  1288. c.Assert(err, IsNil)
  1289. }
  1290. // TestSetBucketCORS
  1291. func (s *OssClientSuite) TestSetBucketCORS(c *C) {
  1292. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1293. var rule1 = CORSRule{
  1294. AllowedOrigin: []string{"*"},
  1295. AllowedMethod: []string{"PUT", "GET", "POST"},
  1296. AllowedHeader: []string{},
  1297. ExposeHeader: []string{},
  1298. MaxAgeSeconds: 100,
  1299. }
  1300. var rule2 = CORSRule{
  1301. AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
  1302. AllowedMethod: []string{"GET"},
  1303. AllowedHeader: []string{"Authorization"},
  1304. ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
  1305. MaxAgeSeconds: 200,
  1306. }
  1307. client, err := New(endpoint, accessID, accessKey)
  1308. c.Assert(err, IsNil)
  1309. err = client.CreateBucket(bucketNameTest)
  1310. c.Assert(err, IsNil)
  1311. time.Sleep(timeoutInOperation)
  1312. // Set
  1313. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1314. c.Assert(err, IsNil)
  1315. gbcr, err := client.GetBucketCORS(bucketNameTest)
  1316. c.Assert(err, IsNil)
  1317. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1318. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1319. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1320. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1321. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1322. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1323. // Double set
  1324. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1})
  1325. c.Assert(err, IsNil)
  1326. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1327. c.Assert(err, IsNil)
  1328. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1329. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 1)
  1330. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 3)
  1331. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 0)
  1332. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 0)
  1333. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 100)
  1334. // Set rule2
  1335. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule2})
  1336. c.Assert(err, IsNil)
  1337. time.Sleep(timeoutInOperation)
  1338. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1339. c.Assert(err, IsNil)
  1340. c.Assert(len(gbcr.CORSRules), Equals, 1)
  1341. c.Assert(len(gbcr.CORSRules[0].AllowedOrigin), Equals, 2)
  1342. c.Assert(len(gbcr.CORSRules[0].AllowedMethod), Equals, 1)
  1343. c.Assert(len(gbcr.CORSRules[0].AllowedHeader), Equals, 1)
  1344. c.Assert(len(gbcr.CORSRules[0].ExposeHeader), Equals, 2)
  1345. c.Assert(gbcr.CORSRules[0].MaxAgeSeconds, Equals, 200)
  1346. // Reset
  1347. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1348. c.Assert(err, IsNil)
  1349. time.Sleep(timeoutInOperation)
  1350. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1351. c.Assert(err, IsNil)
  1352. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1353. // Set after delete
  1354. err = client.DeleteBucketCORS(bucketNameTest)
  1355. c.Assert(err, IsNil)
  1356. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule1, rule2})
  1357. c.Assert(err, IsNil)
  1358. time.Sleep(timeoutInOperation)
  1359. gbcr, err = client.GetBucketCORS(bucketNameTest)
  1360. c.Assert(err, IsNil)
  1361. c.Assert(len(gbcr.CORSRules), Equals, 2)
  1362. err = client.DeleteBucketCORS(bucketNameTest)
  1363. c.Assert(err, IsNil)
  1364. err = client.DeleteBucket(bucketNameTest)
  1365. c.Assert(err, IsNil)
  1366. }
  1367. // TestDeleteBucketCORS
  1368. func (s *OssClientSuite) TestDeleteBucketCORS(c *C) {
  1369. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1370. var rule = CORSRule{
  1371. AllowedOrigin: []string{"*"},
  1372. AllowedMethod: []string{"PUT", "GET", "POST"},
  1373. AllowedHeader: []string{},
  1374. ExposeHeader: []string{},
  1375. MaxAgeSeconds: 100,
  1376. }
  1377. client, err := New(endpoint, accessID, accessKey)
  1378. c.Assert(err, IsNil)
  1379. err = client.CreateBucket(bucketNameTest)
  1380. c.Assert(err, IsNil)
  1381. time.Sleep(timeoutInOperation)
  1382. // Delete not set
  1383. err = client.DeleteBucketCORS(bucketNameTest)
  1384. c.Assert(err, IsNil)
  1385. // Set
  1386. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1387. c.Assert(err, IsNil)
  1388. time.Sleep(timeoutInOperation)
  1389. _, err = client.GetBucketCORS(bucketNameTest)
  1390. c.Assert(err, IsNil)
  1391. // Detele
  1392. err = client.DeleteBucketCORS(bucketNameTest)
  1393. c.Assert(err, IsNil)
  1394. time.Sleep(timeoutInOperation)
  1395. _, err = client.GetBucketCORS(bucketNameTest)
  1396. c.Assert(err, NotNil)
  1397. // Detele after deleting
  1398. err = client.DeleteBucketCORS(bucketNameTest)
  1399. c.Assert(err, IsNil)
  1400. err = client.DeleteBucket(bucketNameTest)
  1401. c.Assert(err, IsNil)
  1402. }
  1403. // TestSetBucketCORSNegative
  1404. func (s *OssClientSuite) TestSetBucketCORSNegative(c *C) {
  1405. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1406. var rule = CORSRule{
  1407. AllowedOrigin: []string{"*"},
  1408. AllowedMethod: []string{"PUT", "GET", "POST"},
  1409. AllowedHeader: []string{},
  1410. ExposeHeader: []string{},
  1411. MaxAgeSeconds: 100,
  1412. }
  1413. client, err := New(endpoint, accessID, accessKey)
  1414. c.Assert(err, IsNil)
  1415. err = client.DeleteBucket(bucketNameTest)
  1416. // Not exist
  1417. _, err = client.GetBucketCORS(bucketNameTest)
  1418. c.Assert(err, NotNil)
  1419. err = client.DeleteBucketCORS(bucketNameTest)
  1420. c.Assert(err, NotNil)
  1421. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1422. c.Assert(err, NotNil)
  1423. err = client.CreateBucket(bucketNameTest)
  1424. c.Assert(err, IsNil)
  1425. time.Sleep(timeoutInOperation)
  1426. _, err = client.GetBucketCORS(bucketNameTest)
  1427. c.Assert(err, NotNil)
  1428. // Set
  1429. err = client.SetBucketCORS(bucketNameTest, []CORSRule{rule})
  1430. c.Assert(err, IsNil)
  1431. time.Sleep(timeoutInOperation)
  1432. _, err = client.GetBucketCORS(bucketNameTest)
  1433. c.Assert(err, IsNil)
  1434. // Delete
  1435. err = client.DeleteBucketCORS(bucketNameTest)
  1436. c.Assert(err, IsNil)
  1437. time.Sleep(timeoutInOperation)
  1438. _, err = client.GetBucketCORS(bucketNameTest)
  1439. c.Assert(err, NotNil)
  1440. // Delete after deleting
  1441. err = client.DeleteBucketCORS(bucketNameTest)
  1442. c.Assert(err, IsNil)
  1443. err = client.DeleteBucket(bucketNameTest)
  1444. c.Assert(err, IsNil)
  1445. }
  1446. // TestGetBucketInfo
  1447. func (s *OssClientSuite) TestGetBucketInfo(c *C) {
  1448. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1449. client, err := New(endpoint, accessID, accessKey)
  1450. c.Assert(err, IsNil)
  1451. err = client.CreateBucket(bucketNameTest)
  1452. c.Assert(err, IsNil)
  1453. res, err := client.GetBucketInfo(bucketNameTest)
  1454. c.Assert(err, IsNil)
  1455. c.Assert(res.BucketInfo.Name, Equals, bucketNameTest)
  1456. c.Assert(strings.HasPrefix(res.BucketInfo.Location, "oss-"), Equals, true)
  1457. c.Assert(res.BucketInfo.ACL, Equals, "private")
  1458. c.Assert(strings.HasSuffix(res.BucketInfo.ExtranetEndpoint, ".com"), Equals, true)
  1459. c.Assert(strings.HasSuffix(res.BucketInfo.IntranetEndpoint, ".com"), Equals, true)
  1460. c.Assert(res.BucketInfo.CreationDate, NotNil)
  1461. err = client.DeleteBucket(bucketNameTest)
  1462. c.Assert(err, IsNil)
  1463. }
  1464. // TestGetBucketInfoNegative
  1465. func (s *OssClientSuite) TestGetBucketInfoNegative(c *C) {
  1466. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1467. client, err := New(endpoint, accessID, accessKey)
  1468. c.Assert(err, IsNil)
  1469. // Not exist
  1470. _, err = client.GetBucketInfo(bucketNameTest)
  1471. c.Assert(err, NotNil)
  1472. // Bucket name invalid
  1473. _, err = client.GetBucketInfo("InvalidBucketName_")
  1474. c.Assert(err, NotNil)
  1475. }
  1476. // TestEndpointFormat
  1477. func (s *OssClientSuite) TestEndpointFormat(c *C) {
  1478. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1479. // http://host
  1480. client, err := New(endpoint, accessID, accessKey)
  1481. c.Assert(err, IsNil)
  1482. err = client.CreateBucket(bucketNameTest)
  1483. c.Assert(err, IsNil)
  1484. time.Sleep(timeoutInOperation)
  1485. res, err := client.GetBucketACL(bucketNameTest)
  1486. c.Assert(err, IsNil)
  1487. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1488. err = client.DeleteBucket(bucketNameTest)
  1489. c.Assert(err, IsNil)
  1490. time.Sleep(timeoutInOperation)
  1491. // http://host:port
  1492. client, err = New(endpoint+":80", accessID, accessKey)
  1493. c.Assert(err, IsNil)
  1494. err = client.CreateBucket(bucketNameTest)
  1495. c.Assert(err, IsNil)
  1496. time.Sleep(timeoutInOperation)
  1497. res, err = client.GetBucketACL(bucketNameTest)
  1498. c.Assert(err, IsNil)
  1499. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1500. err = client.DeleteBucket(bucketNameTest)
  1501. c.Assert(err, IsNil)
  1502. }
  1503. // TestCname
  1504. func (s *OssClientSuite) _TestCname(c *C) {
  1505. var bucketNameTest = "<my-bucket-cname>"
  1506. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1507. c.Assert(err, IsNil)
  1508. err = client.CreateBucket(bucketNameTest)
  1509. c.Assert(err, IsNil)
  1510. _, err = client.ListBuckets()
  1511. c.Assert(err, NotNil)
  1512. res, err := client.GetBucketACL(bucketNameTest)
  1513. c.Assert(err, IsNil)
  1514. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1515. }
  1516. // TestCnameNegative
  1517. func (s *OssClientSuite) _TestCnameNegative(c *C) {
  1518. var bucketNameTest = "<my-bucket-cname>"
  1519. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>", UseCname(true))
  1520. c.Assert(err, IsNil)
  1521. err = client.CreateBucket(bucketNameTest)
  1522. c.Assert(err, NotNil)
  1523. _, err = client.ListBuckets()
  1524. c.Assert(err, NotNil)
  1525. _, err = client.GetBucketACL(bucketNameTest)
  1526. c.Assert(err, NotNil)
  1527. }
  1528. // _TestHTTPS
  1529. func (s *OssClientSuite) _TestHTTPS(c *C) {
  1530. var bucketNameTest = "<my-bucket-https>"
  1531. client, err := New("<endpoint>", "<accessKeyId>", "<accessKeySecret>")
  1532. c.Assert(err, IsNil)
  1533. err = client.CreateBucket(bucketNameTest)
  1534. c.Assert(err, IsNil)
  1535. res, err := client.GetBucketACL(bucketNameTest)
  1536. c.Assert(err, IsNil)
  1537. c.Assert(res.ACL, Equals, string(ACLPrivate))
  1538. err = client.DeleteBucket(bucketNameTest)
  1539. c.Assert(err, IsNil)
  1540. }
  1541. // TestClientOption
  1542. func (s *OssClientSuite) TestClientOption(c *C) {
  1543. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  1544. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1545. Timeout(11, 12), SecurityToken("token"), Proxy(proxyHost))
  1546. c.Assert(err, IsNil)
  1547. // CreateBucket timeout
  1548. err = client.CreateBucket(bucketNameTest)
  1549. c.Assert(err, NotNil)
  1550. c.Assert(client.Conn.config.HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1551. c.Assert(client.Conn.config.HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1552. c.Assert(client.Conn.config.HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1553. c.Assert(client.Conn.config.HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1554. c.Assert(client.Conn.config.HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1555. c.Assert(client.Conn.config.SecurityToken, Equals, "token")
  1556. c.Assert(client.Conn.config.IsCname, Equals, true)
  1557. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1558. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1559. client, err = New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1560. c.Assert(client.Conn.config.IsUseProxy, Equals, true)
  1561. c.Assert(client.Config.ProxyHost, Equals, proxyHost)
  1562. c.Assert(client.Conn.config.IsAuthProxy, Equals, true)
  1563. c.Assert(client.Conn.config.ProxyUser, Equals, proxyUser)
  1564. c.Assert(client.Conn.config.ProxyPassword, Equals, proxyPasswd)
  1565. client, err = New(endpoint, accessID, accessKey, UserAgent("go sdk user agent"))
  1566. c.Assert(client.Conn.config.UserAgent, Equals, "go sdk user agent")
  1567. // Check we can overide the http.Client
  1568. httpClient := new(http.Client)
  1569. client, err = New(endpoint, accessID, accessKey, HTTPClient(httpClient))
  1570. c.Assert(client.HTTPClient, Equals, httpClient)
  1571. c.Assert(client.Conn.client, Equals, httpClient)
  1572. client, err = New(endpoint, accessID, accessKey)
  1573. c.Assert(client.HTTPClient, IsNil)
  1574. }
  1575. // TestProxy
  1576. func (s *OssClientSuite) TestProxy(c *C) {
  1577. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  1578. objectName := "体育/奥运/首金"
  1579. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。"
  1580. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1581. // Create bucket
  1582. err = client.CreateBucket(bucketNameTest)
  1583. c.Assert(err, IsNil)
  1584. // Get bucket info
  1585. _, err = client.GetBucketInfo(bucketNameTest)
  1586. c.Assert(err, IsNil)
  1587. bucket, err := client.Bucket(bucketNameTest)
  1588. // Sign URL
  1589. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1590. c.Assert(err, IsNil)
  1591. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1592. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1593. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1594. // Put object with URL
  1595. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  1596. c.Assert(err, IsNil)
  1597. // Sign URL for get object
  1598. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  1599. c.Assert(err, IsNil)
  1600. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1601. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1602. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1603. // Get object with URL
  1604. body, err := bucket.GetObjectWithURL(str)
  1605. c.Assert(err, IsNil)
  1606. str, err = readBody(body)
  1607. c.Assert(err, IsNil)
  1608. c.Assert(str, Equals, objectValue)
  1609. // Put object
  1610. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1611. c.Assert(err, IsNil)
  1612. // Get object
  1613. _, err = bucket.GetObject(objectName)
  1614. c.Assert(err, IsNil)
  1615. // List objects
  1616. _, err = bucket.ListObjects()
  1617. c.Assert(err, IsNil)
  1618. // Delete object
  1619. err = bucket.DeleteObject(objectName)
  1620. c.Assert(err, IsNil)
  1621. // Delete bucket
  1622. err = client.DeleteBucket(bucketNameTest)
  1623. c.Assert(err, IsNil)
  1624. }
  1625. // TestProxy for https endpoint
  1626. func (s *OssClientSuite) TestHttpsEndpointProxy(c *C) {
  1627. bucketNameTest := bucketNamePrefix + RandLowStr(6)
  1628. objectName := objectNamePrefix + RandLowStr(6)
  1629. objectValue := RandLowStr(100)
  1630. httpsEndPoint := ""
  1631. if strings.HasPrefix(endpoint, "http://") {
  1632. httpsEndPoint = strings.Replace(endpoint, "http://", "https://", 1)
  1633. } else if !strings.HasPrefix(endpoint, "https://") {
  1634. httpsEndPoint = "https://" + endpoint
  1635. } else {
  1636. httpsEndPoint = endpoint
  1637. }
  1638. client, err := New(httpsEndPoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  1639. // Create bucket
  1640. err = client.CreateBucket(bucketNameTest)
  1641. c.Assert(err, IsNil)
  1642. bucket, err := client.Bucket(bucketNameTest)
  1643. // Put object
  1644. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1645. c.Assert(err, IsNil)
  1646. // Get object
  1647. _, err = bucket.GetObject(objectName)
  1648. c.Assert(err, IsNil)
  1649. // List objects
  1650. _, err = bucket.ListObjects()
  1651. c.Assert(err, IsNil)
  1652. // Delete object
  1653. err = bucket.DeleteObject(objectName)
  1654. c.Assert(err, IsNil)
  1655. // Delete bucket
  1656. err = client.DeleteBucket(bucketNameTest)
  1657. c.Assert(err, IsNil)
  1658. }
  1659. // Private
  1660. func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
  1661. for _, v := range buckets {
  1662. if v.Name == bucket {
  1663. return true
  1664. }
  1665. }
  1666. return false
  1667. }
  1668. func (s *OssClientSuite) getBucket(buckets []BucketProperties, bucket string) (bool, BucketProperties) {
  1669. for _, v := range buckets {
  1670. if v.Name == bucket {
  1671. return true, v
  1672. }
  1673. }
  1674. return false, BucketProperties{}
  1675. }
  1676. func (s *OssClientSuite) TestHttpLogNotSignUrl(c *C) {
  1677. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug.log" + RandStr(5)
  1678. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1679. c.Assert(err, IsNil)
  1680. client, err := New(endpoint, accessID, accessKey)
  1681. client.Config.LogLevel = Debug
  1682. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1683. var testBucketName = bucketNamePrefix + RandLowStr(6)
  1684. // CreateBucket
  1685. err = client.CreateBucket(testBucketName)
  1686. f.Close()
  1687. // read log file,get http info
  1688. contents, err := ioutil.ReadFile(logName)
  1689. c.Assert(err, IsNil)
  1690. httpContent := string(contents)
  1691. //fmt.Println(httpContent)
  1692. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1693. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1694. // delete test bucket and log
  1695. os.Remove(logName)
  1696. client.DeleteBucket(testBucketName)
  1697. }
  1698. func (s *OssClientSuite) TestHttpLogSignUrl(c *C) {
  1699. logName := "." + string(os.PathSeparator) + "test-go-sdk-httpdebug-signurl.log" + RandStr(5)
  1700. f, err := os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1701. c.Assert(err, IsNil)
  1702. client, err := New(endpoint, accessID, accessKey)
  1703. client.Config.LogLevel = Debug
  1704. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1705. var testBucketName = bucketNamePrefix + RandLowStr(6)
  1706. // CreateBucket
  1707. err = client.CreateBucket(testBucketName)
  1708. f.Close()
  1709. // clear log
  1710. f, err = os.OpenFile(logName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  1711. client.Config.Logger = log.New(f, "", log.LstdFlags)
  1712. bucket, _ := client.Bucket(testBucketName)
  1713. objectName := objectNamePrefix + RandStr(8)
  1714. objectValue := RandStr(20)
  1715. // Sign URL for put
  1716. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  1717. c.Assert(err, IsNil)
  1718. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  1719. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  1720. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  1721. // Error put object with URL
  1722. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  1723. f.Close()
  1724. // read log file,get http info
  1725. contents, err := ioutil.ReadFile(logName)
  1726. c.Assert(err, IsNil)
  1727. httpContent := string(contents)
  1728. //fmt.Println(httpContent)
  1729. c.Assert(strings.Contains(httpContent, "signStr"), Equals, true)
  1730. c.Assert(strings.Contains(httpContent, "Method:"), Equals, true)
  1731. // delete test bucket and log
  1732. os.Remove(logName)
  1733. client.DeleteBucket(testBucketName)
  1734. }
  1735. func (s *OssClientSuite) TestSetLimitUploadSpeed(c *C) {
  1736. client, err := New(endpoint, accessID, accessKey)
  1737. c.Assert(err, IsNil)
  1738. err = client.LimitUploadSpeed(100)
  1739. goVersion := runtime.Version()
  1740. pSlice := strings.Split(strings.ToLower(goVersion), ".")
  1741. // compare with go1.7
  1742. if len(pSlice) >= 2 {
  1743. if pSlice[0] > "go1" {
  1744. c.Assert(err, IsNil)
  1745. } else if pSlice[0] == "go1" {
  1746. subVersion, _ := strconv.Atoi(pSlice[1])
  1747. if subVersion >= 7 {
  1748. c.Assert(err, IsNil)
  1749. } else {
  1750. c.Assert(err, NotNil)
  1751. }
  1752. } else {
  1753. c.Assert(err, NotNil)
  1754. }
  1755. }
  1756. }
  1757. func (s *OssClientSuite) TestBucketEncyptionError(c *C) {
  1758. client, err := New(endpoint, accessID, accessKey)
  1759. c.Assert(err, IsNil)
  1760. bucketName := bucketNamePrefix + RandLowStr(5)
  1761. err = client.CreateBucket(bucketName)
  1762. c.Assert(err, IsNil)
  1763. // SetBucketEncryption:AES256 ,"123"
  1764. encryptionRule := ServerEncryptionRule{}
  1765. encryptionRule.SSEDefault.SSEAlgorithm = string(AESAlgorithm)
  1766. encryptionRule.SSEDefault.KMSMasterKeyID = "123"
  1767. var responseHeader http.Header
  1768. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1769. c.Assert(err, NotNil)
  1770. requestId := GetRequestId(responseHeader)
  1771. c.Assert(len(requestId) > 0, Equals, true)
  1772. // GetBucketEncryption
  1773. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1774. c.Assert(err, NotNil)
  1775. requestId = GetRequestId(responseHeader)
  1776. c.Assert(len(requestId) > 0, Equals, true)
  1777. // Get default bucket info
  1778. bucketResult, err := client.GetBucketInfo(bucketName)
  1779. c.Assert(err, IsNil)
  1780. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1781. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1782. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1783. err = client.DeleteBucket(bucketName)
  1784. c.Assert(err, IsNil)
  1785. }
  1786. func (s *OssClientSuite) TestBucketEncyptionPutAndGetAndDelete(c *C) {
  1787. client, err := New(endpoint, accessID, accessKey)
  1788. c.Assert(err, IsNil)
  1789. bucketName := bucketNamePrefix + RandLowStr(5)
  1790. err = client.CreateBucket(bucketName)
  1791. c.Assert(err, IsNil)
  1792. // SetBucketEncryption:KMS ,""
  1793. encryptionRule := ServerEncryptionRule{}
  1794. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1795. var responseHeader http.Header
  1796. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1797. c.Assert(err, IsNil)
  1798. requestId := GetRequestId(responseHeader)
  1799. c.Assert(len(requestId) > 0, Equals, true)
  1800. // GetBucketEncryption
  1801. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1802. c.Assert(err, IsNil)
  1803. requestId = GetRequestId(responseHeader)
  1804. c.Assert(len(requestId) > 0, Equals, true)
  1805. // check encryption value
  1806. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1807. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1808. // delete bucket encyption
  1809. err = client.DeleteBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1810. c.Assert(err, IsNil)
  1811. requestId = GetRequestId(responseHeader)
  1812. c.Assert(len(requestId) > 0, Equals, true)
  1813. // GetBucketEncryption failure
  1814. _, err = client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1815. c.Assert(err, NotNil)
  1816. requestId = GetRequestId(responseHeader)
  1817. c.Assert(len(requestId) > 0, Equals, true)
  1818. // Get default bucket info
  1819. bucketResult, err := client.GetBucketInfo(bucketName)
  1820. c.Assert(err, IsNil)
  1821. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "")
  1822. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1823. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1824. err = client.DeleteBucket(bucketName)
  1825. c.Assert(err, IsNil)
  1826. }
  1827. func (s *OssClientSuite) TestBucketEncyptionPutObjectSuccess(c *C) {
  1828. client, err := New(endpoint, accessID, accessKey)
  1829. c.Assert(err, IsNil)
  1830. bucketName := bucketNamePrefix + RandLowStr(5)
  1831. err = client.CreateBucket(bucketName)
  1832. c.Assert(err, IsNil)
  1833. // SetBucketEncryption:KMS ,""
  1834. encryptionRule := ServerEncryptionRule{}
  1835. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1836. var responseHeader http.Header
  1837. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1838. c.Assert(err, IsNil)
  1839. requestId := GetRequestId(responseHeader)
  1840. c.Assert(len(requestId) > 0, Equals, true)
  1841. // GetBucketEncryption
  1842. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1843. c.Assert(err, IsNil)
  1844. requestId = GetRequestId(responseHeader)
  1845. c.Assert(len(requestId) > 0, Equals, true)
  1846. // check encryption value
  1847. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1848. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1849. // Get default bucket info
  1850. bucketResult, err := client.GetBucketInfo(bucketName)
  1851. c.Assert(err, IsNil)
  1852. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1853. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1854. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1855. // put and get object success
  1856. //bucket, err := client.Bucket(bucketName)
  1857. //c.Assert(err, IsNil)
  1858. // put object success
  1859. //objectName := objectNamePrefix + RandStr(8)
  1860. //context := RandStr(100)
  1861. //err = bucket.PutObject(objectName, strings.NewReader(context))
  1862. //c.Assert(err, IsNil)
  1863. // get object success
  1864. //body, err := bucket.GetObject(objectName)
  1865. //c.Assert(err, IsNil)
  1866. //str, err := readBody(body)
  1867. //c.Assert(err, IsNil)
  1868. //body.Close()
  1869. //c.Assert(str, Equals, context)
  1870. //bucket.DeleteObject(objectName)
  1871. err = client.DeleteBucket(bucketName)
  1872. c.Assert(err, IsNil)
  1873. }
  1874. func (s *OssClientSuite) TestBucketEncyptionPutObjectError(c *C) {
  1875. client, err := New(endpoint, accessID, accessKey)
  1876. c.Assert(err, IsNil)
  1877. bucketName := bucketNamePrefix + RandLowStr(5)
  1878. err = client.CreateBucket(bucketName)
  1879. c.Assert(err, IsNil)
  1880. // SetBucketEncryption:KMS ,""
  1881. encryptionRule := ServerEncryptionRule{}
  1882. encryptionRule.SSEDefault.SSEAlgorithm = string(KMSAlgorithm)
  1883. var responseHeader http.Header
  1884. err = client.SetBucketEncryption(bucketName, encryptionRule, GetResponseHeader(&responseHeader))
  1885. c.Assert(err, IsNil)
  1886. requestId := GetRequestId(responseHeader)
  1887. c.Assert(len(requestId) > 0, Equals, true)
  1888. // GetBucketEncryption
  1889. getResult, err := client.GetBucketEncryption(bucketName, GetResponseHeader(&responseHeader))
  1890. c.Assert(err, IsNil)
  1891. requestId = GetRequestId(responseHeader)
  1892. c.Assert(len(requestId) > 0, Equals, true)
  1893. // check encryption value
  1894. c.Assert(encryptionRule.SSEDefault.SSEAlgorithm, Equals, getResult.SSEDefault.SSEAlgorithm)
  1895. c.Assert(encryptionRule.SSEDefault.KMSMasterKeyID, Equals, getResult.SSEDefault.KMSMasterKeyID)
  1896. // Get default bucket info
  1897. bucketResult, err := client.GetBucketInfo(bucketName)
  1898. c.Assert(err, IsNil)
  1899. c.Assert(bucketResult.BucketInfo.SseRule.SSEAlgorithm, Equals, "KMS")
  1900. c.Assert(bucketResult.BucketInfo.SseRule.KMSMasterKeyID, Equals, "")
  1901. c.Assert(bucketResult.BucketInfo.Versioning, Equals, "")
  1902. // put and get object failure
  1903. bucket, err := client.Bucket(bucketName)
  1904. c.Assert(err, IsNil)
  1905. // put object failure
  1906. objectName := objectNamePrefix + RandStr(8)
  1907. context := RandStr(100)
  1908. err = bucket.PutObject(objectName, strings.NewReader(context))
  1909. c.Assert(err, NotNil)
  1910. err = client.DeleteBucket(bucketName)
  1911. c.Assert(err, IsNil)
  1912. }
  1913. func (s *OssClientSuite) TestBucketTaggingOperation(c *C) {
  1914. client, err := New(endpoint, accessID, accessKey)
  1915. c.Assert(err, IsNil)
  1916. bucketName := bucketNamePrefix + RandLowStr(5)
  1917. err = client.CreateBucket(bucketName)
  1918. c.Assert(err, IsNil)
  1919. var respHeader http.Header
  1920. // Bucket Tagging
  1921. var tagging Tagging
  1922. tagging.Tags = []Tag{Tag{Key: "testkey2", Value: "testvalue2"}}
  1923. err = client.SetBucketTagging(bucketName, tagging, GetResponseHeader(&respHeader))
  1924. c.Assert(err, IsNil)
  1925. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1926. getResult, err := client.GetBucketTagging(bucketName)
  1927. c.Assert(err, IsNil)
  1928. c.Assert(getResult.Tags[0].Key, Equals, tagging.Tags[0].Key)
  1929. c.Assert(getResult.Tags[0].Value, Equals, tagging.Tags[0].Value)
  1930. // delete BucketTagging
  1931. err = client.DeleteBucketTagging(bucketName, GetResponseHeader(&respHeader))
  1932. c.Assert(err, IsNil)
  1933. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1934. getResult, err = client.GetBucketTagging(bucketName, GetResponseHeader(&respHeader))
  1935. c.Assert(err, IsNil)
  1936. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1937. c.Assert(len(getResult.Tags), Equals, 0)
  1938. err = client.DeleteBucket(bucketName)
  1939. c.Assert(err, IsNil)
  1940. }
  1941. func (s *OssClientSuite) TestListBucketsTagging(c *C) {
  1942. client, err := New(endpoint, accessID, accessKey)
  1943. c.Assert(err, IsNil)
  1944. bucketName1 := bucketNamePrefix + RandLowStr(5)
  1945. err = client.CreateBucket(bucketName1)
  1946. c.Assert(err, IsNil)
  1947. bucketName2 := bucketNamePrefix + RandLowStr(5)
  1948. err = client.CreateBucket(bucketName2)
  1949. c.Assert(err, IsNil)
  1950. // Bucket Tagging
  1951. var tagging Tagging
  1952. tagging.Tags = []Tag{Tag{Key: "testkey", Value: "testvalue"}}
  1953. err = client.SetBucketTagging(bucketName1, tagging)
  1954. c.Assert(err, IsNil)
  1955. // list bucket
  1956. listResult, err := client.ListBuckets(TagKey("testkey"))
  1957. c.Assert(err, IsNil)
  1958. c.Assert(len(listResult.Buckets), Equals, 1)
  1959. c.Assert(listResult.Buckets[0].Name, Equals, bucketName1)
  1960. client.DeleteBucket(bucketName1)
  1961. client.DeleteBucket(bucketName2)
  1962. }
  1963. func (s *OssClientSuite) TestGetBucketStat(c *C) {
  1964. client, err := New(endpoint, accessID, accessKey)
  1965. c.Assert(err, IsNil)
  1966. bucketName := bucketNamePrefix + RandLowStr(5)
  1967. err = client.CreateBucket(bucketName)
  1968. c.Assert(err, IsNil)
  1969. bucket, err := client.Bucket(bucketName)
  1970. c.Assert(err, IsNil)
  1971. // put object
  1972. objectName := objectNamePrefix + RandLowStr(5)
  1973. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  1974. c.Assert(err, IsNil)
  1975. bucket.DeleteObject(objectName)
  1976. err = bucket.PutObject(objectName, strings.NewReader(RandStr(10)))
  1977. c.Assert(err, IsNil)
  1978. bucket.DeleteObject(objectName)
  1979. _, err = client.GetBucketStat(bucketName)
  1980. c.Assert(err, IsNil)
  1981. client.DeleteBucket(bucketName)
  1982. }
  1983. func (s *OssBucketSuite) TestGetBucketVersioning(c *C) {
  1984. // create a bucket with default proprety
  1985. client, err := New(endpoint, accessID, accessKey)
  1986. c.Assert(err, IsNil)
  1987. bucketName := bucketNamePrefix + RandLowStr(6)
  1988. var respHeader http.Header
  1989. err = client.CreateBucket(bucketName, GetResponseHeader(&respHeader))
  1990. c.Assert(err, IsNil)
  1991. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  1992. // put bucket version:enabled
  1993. var versioningConfig VersioningConfig
  1994. versioningConfig.Status = string(VersionEnabled)
  1995. err = client.SetBucketVersioning(bucketName, versioningConfig)
  1996. c.Assert(err, IsNil)
  1997. // get bucket version success
  1998. versioningResult, err := client.GetBucketVersioning(bucketName, GetResponseHeader(&respHeader))
  1999. c.Assert(versioningResult.Status, Equals, "Enabled")
  2000. c.Assert(GetRequestId(respHeader) != "", Equals, true)
  2001. ForceDeleteBucket(client, bucketName, c)
  2002. }
  2003. func (s *OssClientSuite) TestBucketPolicy(c *C) {
  2004. client, err := New(endpoint, accessID, accessKey)
  2005. c.Assert(err, IsNil)
  2006. bucketName := bucketNamePrefix + RandLowStr(5)
  2007. err = client.CreateBucket(bucketName)
  2008. c.Assert(err, IsNil)
  2009. var responseHeader http.Header
  2010. ret, err := client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2011. c.Assert(err, NotNil)
  2012. requestId := GetRequestId(responseHeader)
  2013. c.Assert(len(requestId) > 0, Equals, true)
  2014. policyInfo := `
  2015. {
  2016. "Version":"1",
  2017. "Statement":[
  2018. {
  2019. "Action":[
  2020. "oss:GetObject",
  2021. "oss:PutObject"
  2022. ],
  2023. "Effect":"Deny",
  2024. "Principal":"[123456790]",
  2025. "Resource":["acs:oss:*:1234567890:*/*"]
  2026. }
  2027. ]
  2028. }`
  2029. err = client.SetBucketPolicy(bucketName, policyInfo, GetResponseHeader(&responseHeader))
  2030. c.Assert(err, IsNil)
  2031. requestId = GetRequestId(responseHeader)
  2032. c.Assert(len(requestId) > 0, Equals, true)
  2033. ret, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2034. c.Assert(err, IsNil)
  2035. testLogger.Println("policy:", ret)
  2036. c.Assert(ret, Equals, policyInfo)
  2037. requestId = GetRequestId(responseHeader)
  2038. c.Assert(len(requestId) > 0, Equals, true)
  2039. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2040. c.Assert(err, IsNil)
  2041. requestId = GetRequestId(responseHeader)
  2042. c.Assert(len(requestId) > 0, Equals, true)
  2043. client.DeleteBucket(bucketName)
  2044. }
  2045. func (s *OssClientSuite) TestBucketPolicyNegative(c *C) {
  2046. client, err := New(endpoint, accessID, accessKey)
  2047. c.Assert(err, IsNil)
  2048. bucketName := bucketNamePrefix + RandLowStr(5)
  2049. err = client.CreateBucket(bucketName)
  2050. c.Assert(err, IsNil)
  2051. var responseHeader http.Header
  2052. _, err = client.GetBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2053. c.Assert(err, NotNil)
  2054. requestId := GetRequestId(responseHeader)
  2055. c.Assert(len(requestId) > 0, Equals, true)
  2056. // Setting the Version is 2, this is error policy
  2057. errPolicy := `
  2058. {
  2059. "Version":"2",
  2060. "Statement":[
  2061. {
  2062. "Action":[
  2063. "oss:GetObject",
  2064. "oss:PutObject"
  2065. ],
  2066. "Effect":"Deny",
  2067. "Principal":"[123456790]",
  2068. "Resource":["acs:oss:*:1234567890:*/*"]
  2069. }
  2070. ]
  2071. }`
  2072. err = client.SetBucketPolicy(bucketName, errPolicy, GetResponseHeader(&responseHeader))
  2073. c.Assert(err, NotNil)
  2074. testLogger.Println("err:", err)
  2075. requestId = GetRequestId(responseHeader)
  2076. c.Assert(len(requestId) > 0, Equals, true)
  2077. err = client.DeleteBucketPolicy(bucketName, GetResponseHeader(&responseHeader))
  2078. c.Assert(err, IsNil)
  2079. bucketNameEmpty := bucketNamePrefix + RandLowStr(5)
  2080. client.DeleteBucket(bucketNameEmpty)
  2081. err = client.DeleteBucketPolicy(bucketNameEmpty, GetResponseHeader(&responseHeader))
  2082. c.Assert(err, NotNil)
  2083. requestId = GetRequestId(responseHeader)
  2084. c.Assert(len(requestId) > 0, Equals, true)
  2085. client.DeleteBucket(bucketName)
  2086. }
  2087. func (s *OssClientSuite) TestSetBucketRequestPayment(c *C) {
  2088. client, err := New(endpoint, accessID, accessKey)
  2089. c.Assert(err, IsNil)
  2090. bucketName := bucketNamePrefix + RandLowStr(5)
  2091. err = client.CreateBucket(bucketName)
  2092. c.Assert(err, IsNil)
  2093. reqPayConf := RequestPaymentConfiguration{
  2094. Payer: "Requester",
  2095. }
  2096. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2097. c.Assert(err, IsNil)
  2098. ret, err := client.GetBucketRequestPayment(bucketName)
  2099. c.Assert(err, IsNil)
  2100. c.Assert(ret.Payer, Equals, "Requester")
  2101. client.DeleteBucket(bucketName)
  2102. c.Assert(err, IsNil)
  2103. }
  2104. func (s *OssClientSuite) TestSetBucketRequestPaymentNegative(c *C) {
  2105. client, err := New(endpoint, accessID, accessKey)
  2106. c.Assert(err, IsNil)
  2107. bucketName := bucketNamePrefix + RandLowStr(5)
  2108. err = client.CreateBucket(bucketName)
  2109. c.Assert(err, IsNil)
  2110. reqPayConf := RequestPaymentConfiguration{
  2111. Payer: "Requesterttttt", // this is a error configuration
  2112. }
  2113. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  2114. c.Assert(err, NotNil)
  2115. ret, err := client.GetBucketRequestPayment(bucketName)
  2116. c.Assert(err, IsNil)
  2117. c.Assert(ret.Payer, Equals, "BucketOwner")
  2118. client.DeleteBucket(bucketName)
  2119. c.Assert(err, IsNil)
  2120. }
  2121. func (s *OssClientSuite) TestBucketQos(c *C) {
  2122. client, err := New(endpoint, accessID, accessKey)
  2123. c.Assert(err, IsNil)
  2124. ret, err := client.GetUserQoSInfo()
  2125. c.Assert(err, IsNil)
  2126. testLogger.Println("QosInfo:", ret)
  2127. bucketName := bucketNamePrefix + RandLowStr(5)
  2128. _ = client.DeleteBucket(bucketName)
  2129. err = client.CreateBucket(bucketName)
  2130. c.Assert(err, IsNil)
  2131. _, err = client.GetBucketQosInfo(bucketName)
  2132. c.Assert(err, NotNil)
  2133. // case 1 set BucketQoSConfiguration every member
  2134. five := 5
  2135. four := 4
  2136. totalQps := 200
  2137. qosConf := BucketQoSConfiguration{
  2138. TotalUploadBandwidth: &five,
  2139. IntranetUploadBandwidth: &four,
  2140. ExtranetUploadBandwidth: &four,
  2141. TotalDownloadBandwidth: &four,
  2142. IntranetDownloadBandwidth: &four,
  2143. ExtranetDownloadBandwidth: &four,
  2144. TotalQPS: &totalQps,
  2145. IntranetQPS: &totalQps,
  2146. ExtranetQPS: &totalQps,
  2147. }
  2148. var responseHeader http.Header
  2149. err = client.SetBucketQoSInfo(bucketName, qosConf, GetResponseHeader(&responseHeader))
  2150. c.Assert(err, IsNil)
  2151. requestId := GetRequestId(responseHeader)
  2152. c.Assert(len(requestId) > 0, Equals, true)
  2153. // wait a moment for configuration effect
  2154. time.Sleep(time.Second)
  2155. retQos, err := client.GetBucketQosInfo(bucketName)
  2156. c.Assert(err, IsNil)
  2157. // set qosConf default value
  2158. qosConf.XMLName.Local = "QoSConfiguration"
  2159. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConf, c))
  2160. // case 2 set BucketQoSConfiguration not every member
  2161. qosConfNo := BucketQoSConfiguration{
  2162. TotalUploadBandwidth: &five,
  2163. IntranetUploadBandwidth: &four,
  2164. ExtranetUploadBandwidth: &four,
  2165. TotalDownloadBandwidth: &four,
  2166. IntranetDownloadBandwidth: &four,
  2167. ExtranetDownloadBandwidth: &four,
  2168. TotalQPS: &totalQps,
  2169. }
  2170. err = client.SetBucketQoSInfo(bucketName, qosConfNo)
  2171. c.Assert(err, IsNil)
  2172. // wait a moment for configuration effect
  2173. time.Sleep(time.Second)
  2174. retQos, err = client.GetBucketQosInfo(bucketName)
  2175. c.Assert(err, IsNil)
  2176. // set qosConfNo default value
  2177. qosConfNo.XMLName.Local = "QoSConfiguration"
  2178. defNum := -1
  2179. qosConfNo.IntranetQPS = &defNum
  2180. qosConfNo.ExtranetQPS = &defNum
  2181. c.Assert(struct2string(retQos, c), Equals, struct2string(qosConfNo, c))
  2182. err = client.DeleteBucketQosInfo(bucketName)
  2183. c.Assert(err, IsNil)
  2184. // wait a moment for configuration effect
  2185. time.Sleep(time.Second)
  2186. _, err = client.GetBucketQosInfo(bucketName)
  2187. c.Assert(err, NotNil)
  2188. // this is a error qos configuration
  2189. to := *ret.TotalUploadBandwidth + 2
  2190. qosErrConf := BucketQoSConfiguration{
  2191. TotalUploadBandwidth: &to, // this exceed user TotalUploadBandwidth
  2192. IntranetUploadBandwidth: &four,
  2193. ExtranetUploadBandwidth: &four,
  2194. TotalDownloadBandwidth: &four,
  2195. IntranetDownloadBandwidth: &four,
  2196. ExtranetDownloadBandwidth: &four,
  2197. TotalQPS: &totalQps,
  2198. IntranetQPS: &totalQps,
  2199. ExtranetQPS: &totalQps,
  2200. }
  2201. err = client.SetBucketQoSInfo(bucketName, qosErrConf)
  2202. c.Assert(err, NotNil)
  2203. err = client.DeleteBucketQosInfo(bucketName)
  2204. c.Assert(err, IsNil)
  2205. err = client.DeleteBucket(bucketName)
  2206. c.Assert(err, IsNil)
  2207. }
  2208. // struct to string
  2209. func struct2string(obj interface{}, c *C) string {
  2210. t := reflect.TypeOf(obj)
  2211. v := reflect.ValueOf(obj)
  2212. var data = make(map[string]interface{})
  2213. for i := 0; i < t.NumField(); i++ {
  2214. data[t.Field(i).Name] = v.Field(i).Interface()
  2215. }
  2216. str, err := json.Marshal(data)
  2217. c.Assert(err, IsNil)
  2218. return string(str)
  2219. }
  2220. type TestCredentials struct {
  2221. }
  2222. func (testCreInf *TestCredentials) GetAccessKeyID() string {
  2223. return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
  2224. }
  2225. func (testCreInf *TestCredentials) GetAccessKeySecret() string {
  2226. return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
  2227. }
  2228. func (testCreInf *TestCredentials) GetSecurityToken() string {
  2229. return ""
  2230. }
  2231. type TestCredentialsProvider struct {
  2232. }
  2233. func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
  2234. return &TestCredentials{}
  2235. }
  2236. func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
  2237. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2238. var defaultBuild TestCredentialsProvider
  2239. client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
  2240. c.Assert(err, IsNil)
  2241. err = client.CreateBucket(bucketNameTest)
  2242. c.Assert(err, IsNil)
  2243. err = client.DeleteBucket(bucketNameTest)
  2244. c.Assert(err, IsNil)
  2245. }
  2246. func (s *OssClientSuite) TestClientSetLocalIpError(c *C) {
  2247. // create client and bucket
  2248. ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
  2249. c.Assert(err, IsNil)
  2250. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2251. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2252. c.Assert(err, IsNil)
  2253. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2254. err = client.CreateBucket(bucketNameTest)
  2255. c.Assert(err, NotNil)
  2256. }
  2257. func (s *OssClientSuite) TestClientSetLocalIpSuccess(c *C) {
  2258. //get local ip
  2259. conn, err := net.Dial("udp", "8.8.8.8:80")
  2260. c.Assert(err, IsNil)
  2261. localAddr := conn.LocalAddr().(*net.UDPAddr)
  2262. localIp := localAddr.IP.String()
  2263. conn.Close()
  2264. ipAddr, err := net.ResolveIPAddr("ip", localIp)
  2265. c.Assert(err, IsNil)
  2266. localTCPAddr := &(net.TCPAddr{IP: ipAddr.IP})
  2267. client, err := New(endpoint, accessID, accessKey, SetLocalAddr(localTCPAddr))
  2268. c.Assert(err, IsNil)
  2269. var bucketNameTest = bucketNamePrefix + RandLowStr(6)
  2270. err = client.CreateBucket(bucketNameTest)
  2271. c.Assert(err, IsNil)
  2272. err = client.DeleteBucket(bucketNameTest)
  2273. c.Assert(err, IsNil)
  2274. }