zt_test.go 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499
  1. package azblob_test
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "net/url"
  10. "os"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "testing"
  15. "time"
  16. chk "gopkg.in/check.v1"
  17. "github.com/Azure/azure-pipeline-go/pipeline"
  18. "github.com/Azure/azure-storage-blob-go/2016-05-31/azblob"
  19. )
  20. /*func finalizer(c azblob.ContainerURL) {
  21. c.Delete(context.Background(), azblob.ContainerAccessConditions{})
  22. }
  23. // Put in Test Main
  24. var defaultContainerURL *azblob.ContainerURL = createDefaultContainerURL()
  25. func createDefaultContainerURL() *azblob.ContainerURL {
  26. u, _ := url.Parse("")
  27. su := azblob.NewServiceURL(*u, azblob.NewPipeline())
  28. c := &su.NewContainerURL(generateContainerName())
  29. runtime.SetFinalizer(c, finalizer)
  30. return c
  31. }
  32. func getDefaultContainerURL() azblob.ContainerURL {
  33. // Call in all tests. No need to defer. Generally eliminates need for getBSU()
  34. return *c
  35. }
  36. func createContainer(c *chk.C, su *azblob.ServiceURL) (azblob.ContainerURL, func()){
  37. // Create container and
  38. // If service url not nil, use it. If nil, use default
  39. return containerURL, func(){ containerURL.Delete() }
  40. }*/
  41. // Hookup to the testing framework
  42. func Test(t *testing.T) { chk.TestingT(t) }
  43. type aztestsSuite struct{}
  44. var _ = chk.Suite(&aztestsSuite{})
  45. const (
  46. containerPrefix = "go"
  47. blobPrefix = "gotestblob"
  48. blockBlobDefaultData = "GoBlockBlobData"
  49. validationErrorSubstring = "validation failed"
  50. )
  51. var ctx = context.Background()
  52. var basicHeaders = azblob.BlobHTTPHeaders{ContentType: "my_type", ContentDisposition: "my_disposition",
  53. CacheControl: "control", ContentMD5: md5.Sum([]byte("")), ContentLanguage: "my_language", ContentEncoding: "my_encoding"}
  54. var basicMetadata = azblob.Metadata{"foo": "bar"}
  55. type testPipeline struct{}
  56. const testPipelineMessage string = "Test factory invoked"
  57. func (tm testPipeline) Do(ctx context.Context, methodFactory pipeline.Factory, request pipeline.Request) (pipeline.Response, error) {
  58. return nil, errors.New(testPipelineMessage)
  59. }
  60. // This function generates an entity name by concatenating the passed prefix,
  61. // the name of the test requesting the entity name, and the minute, second, and nanoseconds of the call.
  62. // This should make it easy to associate the entities with their test, uniquely identify
  63. // them, and determine the order in which they were created.
  64. // Note that this imposes a restriction on the length of test names
  65. func generateName(prefix string) string {
  66. // These next lines up through the for loop are obtaining and walking up the stack
  67. // trace to extrat the test name, which is stored in name
  68. pc := make([]uintptr, 10)
  69. runtime.Callers(0, pc)
  70. f := runtime.FuncForPC(pc[0])
  71. name := f.Name()
  72. for i := 0; !strings.Contains(name, "Suite"); i++ { // The tests are all scoped to the suite, so this ensures getting the actual test name
  73. f = runtime.FuncForPC(pc[i])
  74. name = f.Name()
  75. }
  76. funcNameStart := strings.Index(name, "Test")
  77. name = name[funcNameStart+len("Test"):] // Just get the name of the test and not any of the garbage at the beginning
  78. name = strings.ToLower(name) // Ensure it is a valid resource name
  79. currentTime := time.Now()
  80. name = fmt.Sprintf("%s%s%d%d%d", prefix, strings.ToLower(name), currentTime.Minute(), currentTime.Second(), currentTime.Nanosecond())
  81. return name
  82. }
  83. func generateContainerName() string {
  84. return generateName(containerPrefix)
  85. }
  86. func generateBlobName() string {
  87. return generateName(blobPrefix)
  88. }
  89. func getContainerURL(c *chk.C, bsu azblob.ServiceURL) (container azblob.ContainerURL, name string) {
  90. name = generateContainerName()
  91. container = bsu.NewContainerURL(name)
  92. return container, name
  93. }
  94. func getBlockBlobURL(c *chk.C, container azblob.ContainerURL) (blob azblob.BlockBlobURL, name string) {
  95. name = generateBlobName()
  96. blob = container.NewBlockBlobURL(name)
  97. return blob, name
  98. }
  99. func getAppendBlobURL(c *chk.C, container azblob.ContainerURL) (blob azblob.AppendBlobURL, name string) {
  100. name = generateBlobName()
  101. blob = container.NewAppendBlobURL(name)
  102. return blob, name
  103. }
  104. func getPageBlobURL(c *chk.C, container azblob.ContainerURL) (blob azblob.PageBlobURL, name string) {
  105. name = generateBlobName()
  106. blob = container.NewPageBlobURL(name)
  107. return
  108. }
  109. func createNewContainer(c *chk.C, bsu azblob.ServiceURL) (container azblob.ContainerURL, name string) {
  110. container, name = getContainerURL(c, bsu)
  111. cResp, err := container.Create(ctx, nil, azblob.PublicAccessBlob)
  112. c.Assert(err, chk.IsNil)
  113. c.Assert(cResp.StatusCode(), chk.Equals, 201)
  114. return container, name
  115. }
  116. func createNewContainerWithSuffix(c *chk.C, bsu azblob.ServiceURL, suffix string) (container azblob.ContainerURL, name string) {
  117. // The goal of adding the suffix is to be able to predetermine what order the containers will be in when listed.
  118. // We still need the container prefix to come first, though, to ensure only containers as a part of this test
  119. // are listed at all.
  120. name = generateName(containerPrefix + suffix)
  121. container = bsu.NewContainerURL(name)
  122. cResp, err := container.Create(ctx, nil, azblob.PublicAccessBlob)
  123. c.Assert(err, chk.IsNil)
  124. c.Assert(cResp.StatusCode(), chk.Equals, 201)
  125. return container, name
  126. }
  127. func createNewBlockBlob(c *chk.C, container azblob.ContainerURL) (blob azblob.BlockBlobURL, name string) {
  128. blob, name = getBlockBlobURL(c, container)
  129. cResp, err := blob.PutBlob(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobHTTPHeaders{},
  130. nil, azblob.BlobAccessConditions{})
  131. c.Assert(err, chk.IsNil)
  132. c.Assert(cResp.StatusCode(), chk.Equals, 201)
  133. return
  134. }
  135. func createNewAppendBlob(c *chk.C, container azblob.ContainerURL) (blob azblob.AppendBlobURL, name string) {
  136. blob, name = getAppendBlobURL(c, container)
  137. resp, err := blob.Create(ctx, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  138. c.Assert(err, chk.IsNil)
  139. c.Assert(resp.StatusCode(), chk.Equals, 201)
  140. return
  141. }
  142. func createNewPageBlob(c *chk.C, container azblob.ContainerURL) (blob azblob.PageBlobURL, name string) {
  143. blob, name = getPageBlobURL(c, container)
  144. resp, err := blob.Create(ctx, azblob.PageBlobPageBytes*10, 0, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  145. c.Assert(err, chk.IsNil)
  146. c.Assert(resp.StatusCode(), chk.Equals, 201)
  147. return
  148. }
  149. func createBlockBlobWithPrefix(c *chk.C, container azblob.ContainerURL, prefix string) (blob azblob.BlockBlobURL, name string) {
  150. name = prefix + generateName(blobPrefix)
  151. blob = container.NewBlockBlobURL(name)
  152. cResp, err := blob.PutBlob(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobHTTPHeaders{},
  153. nil, azblob.BlobAccessConditions{})
  154. c.Assert(err, chk.IsNil)
  155. c.Assert(cResp.StatusCode(), chk.Equals, 201)
  156. return
  157. }
  158. func deleteContainer(c *chk.C, container azblob.ContainerURL) {
  159. resp, err := container.Delete(ctx, azblob.ContainerAccessConditions{})
  160. c.Assert(err, chk.IsNil)
  161. c.Assert(resp.StatusCode(), chk.Equals, 202)
  162. }
  163. func getBSU() azblob.ServiceURL {
  164. name := os.Getenv("ACCOUNT_NAME")
  165. key := os.Getenv("ACCOUNT_KEY")
  166. if name == "" || key == "" {
  167. panic("ACCOUNT_NAME and ACCOUNT_KEY environment vars must be set before running tests")
  168. }
  169. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/", name))
  170. credential := azblob.NewSharedKeyCredential(name, key)
  171. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  172. return azblob.NewServiceURL(*u, pipeline)
  173. }
  174. func getAlternateBSU() (azblob.ServiceURL, error) {
  175. secondaryAccountName, secondaryAccountKey := os.Getenv("SECONDARY_ACCOUNT_NAME"), os.Getenv("SECONDARY_ACCOUNT_KEY")
  176. if secondaryAccountName == "" || secondaryAccountKey == "" {
  177. return azblob.ServiceURL{}, errors.New("SECONDARY_ACCOUNT_NAME and/or SECONDARY_ACCOUNT_KEY environment variables not specified.")
  178. }
  179. credentials := azblob.NewSharedKeyCredential(secondaryAccountName, secondaryAccountKey)
  180. pipeline := azblob.NewPipeline(credentials, azblob.PipelineOptions{ /*Log: pipeline.NewLogWrapper(pipeline.LogInfo, log.New(os.Stderr, "", log.LstdFlags))*/ })
  181. blobPrimaryURL, _ := url.Parse("https://" + secondaryAccountName + ".blob.core.windows.net/")
  182. return azblob.NewServiceURL(*blobPrimaryURL, pipeline), nil
  183. }
  184. func validateStorageError(c *chk.C, err error, code azblob.ServiceCodeType) {
  185. serr, _ := err.(azblob.StorageError)
  186. c.Assert(serr.ServiceCode(), chk.Equals, code)
  187. }
  188. func getRelativeTimeGMT(amount time.Duration) time.Time {
  189. currentTime := time.Now().In(time.FixedZone("GMT", 0))
  190. currentTime = currentTime.Add(amount * time.Second)
  191. return currentTime
  192. }
  193. func (s *aztestsSuite) TestAccountListContainersEmptyPrefix(c *chk.C) {
  194. bsu := getBSU()
  195. containerURL1, _ := createNewContainer(c, bsu)
  196. defer deleteContainer(c, containerURL1)
  197. containerURL2, _ := createNewContainer(c, bsu)
  198. defer deleteContainer(c, containerURL2)
  199. response, err := bsu.ListContainers(ctx, azblob.Marker{}, azblob.ListContainersOptions{})
  200. c.Assert(err, chk.IsNil)
  201. c.Assert(len(response.Containers) >= 2, chk.Equals, true) // The response should contain at least the two created containers. Probably many more
  202. }
  203. func (s *aztestsSuite) TestAccountListContainersIncludeTypeMetadata(c *chk.C) {
  204. bsu := getBSU()
  205. containerURLNoMetadata, nameNoMetadata := createNewContainerWithSuffix(c, bsu, "a")
  206. defer deleteContainer(c, containerURLNoMetadata)
  207. containerURLMetadata, nameMetadata := createNewContainerWithSuffix(c, bsu, "b")
  208. defer deleteContainer(c, containerURLMetadata)
  209. // Test on containers with and without metadata
  210. _, err := containerURLMetadata.SetMetadata(ctx, basicMetadata, azblob.ContainerAccessConditions{})
  211. c.Assert(err, chk.IsNil)
  212. // Also validates not specifying MaxResults
  213. response, err := bsu.ListContainers(ctx, azblob.Marker{},
  214. azblob.ListContainersOptions{Prefix: containerPrefix, Detail: azblob.ListContainersDetail{Metadata: true}})
  215. c.Assert(err, chk.IsNil)
  216. c.Assert(response.Containers[0].Name, chk.Equals, nameNoMetadata)
  217. c.Assert(response.Containers[0].Metadata, chk.HasLen, 0)
  218. c.Assert(response.Containers[1].Name, chk.Equals, nameMetadata)
  219. c.Assert(response.Containers[1].Metadata, chk.DeepEquals, basicMetadata)
  220. }
  221. func (s *aztestsSuite) TestAccountListContainersMaxResultsNegative(c *chk.C) {
  222. bsu := getBSU()
  223. containerURL, _ := createNewContainer(c, bsu)
  224. defer deleteContainer(c, containerURL)
  225. // The library should panic if MaxResults < -1
  226. defer func() {
  227. recover()
  228. }()
  229. bsu.ListContainers(ctx,
  230. azblob.Marker{}, *(&azblob.ListContainersOptions{Prefix: containerPrefix, MaxResults: -2}))
  231. c.Fail() // If the list call doesn't panic, we fail
  232. }
  233. func (s *aztestsSuite) TestAccountListContainersMaxResultsZero(c *chk.C) {
  234. bsu := getBSU()
  235. containerURL, _ := createNewContainer(c, bsu)
  236. defer deleteContainer(c, containerURL)
  237. // Max Results = 0 means the value will be ignored, the header not set, and the server default used
  238. resp, err := bsu.ListContainers(ctx,
  239. azblob.Marker{}, *(&azblob.ListContainersOptions{Prefix: containerPrefix, MaxResults: 0}))
  240. c.Assert(err, chk.IsNil)
  241. c.Assert(resp.Containers, chk.HasLen, 1)
  242. }
  243. func (s *aztestsSuite) TestAccountListContainersMaxResultsExact(c *chk.C) {
  244. // If this test fails, ensure there are no extra containers prefixed with go in the account. These may be left over if a test is interrupted.
  245. bsu := getBSU()
  246. containerURL1, containerName1 := createNewContainerWithSuffix(c, bsu, "a")
  247. defer deleteContainer(c, containerURL1)
  248. containerURL2, containerName2 := createNewContainerWithSuffix(c, bsu, "b")
  249. defer deleteContainer(c, containerURL2)
  250. response, err := bsu.ListContainers(ctx,
  251. azblob.Marker{}, *(&azblob.ListContainersOptions{Prefix: containerPrefix, MaxResults: 2}))
  252. c.Assert(err, chk.IsNil)
  253. c.Assert(response.Containers, chk.HasLen, 2)
  254. c.Assert(response.Containers[0].Name, chk.Equals, containerName1)
  255. c.Assert(response.Containers[1].Name, chk.Equals, containerName2)
  256. }
  257. func (s *aztestsSuite) TestAccountListContainersMaxResultsInsufficient(c *chk.C) {
  258. bsu := getBSU()
  259. containerURL1, _ := createNewContainer(c, bsu)
  260. defer deleteContainer(c, containerURL1)
  261. containerURL2, _ := createNewContainer(c, bsu)
  262. defer deleteContainer(c, containerURL2)
  263. response, err := bsu.ListContainers(ctx, azblob.Marker{},
  264. *(&azblob.ListContainersOptions{Prefix: containerPrefix, MaxResults: 1}))
  265. c.Assert(err, chk.IsNil)
  266. c.Assert(len(response.Containers), chk.Equals, 1)
  267. }
  268. func (s *aztestsSuite) TestAccountListContainersMaxResultsSufficient(c *chk.C) {
  269. bsu := getBSU()
  270. containerURL1, _ := createNewContainer(c, bsu)
  271. defer deleteContainer(c, containerURL1)
  272. containerURL2, _ := createNewContainer(c, bsu)
  273. defer deleteContainer(c, containerURL2)
  274. response, err := bsu.ListContainers(ctx, azblob.Marker{},
  275. *(&azblob.ListContainersOptions{Prefix: containerPrefix, MaxResults: 3}))
  276. c.Assert(err, chk.IsNil)
  277. c.Assert(len(response.Containers), chk.Equals, 2)
  278. }
  279. func (s *aztestsSuite) TestNewContainerURLValidName(c *chk.C) {
  280. bsu := getBSU()
  281. testURL := bsu.NewContainerURL(containerPrefix)
  282. correctURL := "https://" + os.Getenv("ACCOUNT_NAME") + ".blob.core.windows.net/" + containerPrefix
  283. temp := testURL.URL()
  284. c.Assert(temp.String(), chk.Equals, correctURL)
  285. }
  286. func (s *aztestsSuite) TestCreateRootContainerURL(c *chk.C) {
  287. bsu := getBSU()
  288. testURL := bsu.NewRootContainerURL()
  289. correctURL := "https://" + os.Getenv("ACCOUNT_NAME") + ".blob.core.windows.net/$root"
  290. temp := testURL.URL()
  291. c.Assert(temp.String(), chk.Equals, correctURL)
  292. }
  293. func (s *aztestsSuite) TestCreateBlobURL(c *chk.C) {
  294. bsu := getBSU()
  295. containerURL, containerName := getContainerURL(c, bsu)
  296. testURL, testName := getBlockBlobURL(c, containerURL)
  297. parts := azblob.NewBlobURLParts(testURL.URL())
  298. c.Assert(parts.BlobName, chk.Equals, testName)
  299. c.Assert(parts.ContainerName, chk.Equals, containerName)
  300. correctURL := "https://" + os.Getenv("ACCOUNT_NAME") + ".blob.core.windows.net/" + containerName + "/" + testName
  301. temp := testURL.URL()
  302. c.Assert(temp.String(), chk.Equals, correctURL)
  303. }
  304. func (s *aztestsSuite) TestCreateBlobURLWithSnapshotAndSAS(c *chk.C) {
  305. bsu := getBSU()
  306. containerURL, containerName := getContainerURL(c, bsu)
  307. blobURL, blobName := getBlockBlobURL(c, containerURL)
  308. currentTime := time.Now().UTC()
  309. credential := azblob.NewSharedKeyCredential(os.Getenv("ACCOUNT_NAME"), os.Getenv("ACCOUNT_KEY"))
  310. sasQueryParams := azblob.AccountSASSignatureValues{
  311. Protocol: azblob.SASProtocolHTTPS,
  312. ExpiryTime: currentTime.Add(48 * time.Hour),
  313. Permissions: azblob.AccountSASPermissions{Read: true, List: true}.String(),
  314. Services: azblob.AccountSASServices{Blob: true}.String(),
  315. ResourceTypes: azblob.AccountSASResourceTypes{Container: true, Object: true}.String(),
  316. }.NewSASQueryParameters(credential)
  317. parts := azblob.NewBlobURLParts(blobURL.URL())
  318. parts.SAS = sasQueryParams
  319. parts.Snapshot = currentTime
  320. testURL := parts.URL()
  321. // The snapshot format string is taken from the snapshotTimeFormat value in parsing_urls.go. The field is not public, so
  322. // it is copied here
  323. correctURL := "https://" + os.Getenv("ACCOUNT_NAME") + ".blob.core.windows.net/" + containerName + "/" + blobName +
  324. "?" + "snapshot=" + currentTime.Format("2006-01-02T15:04:05.0000000Z07:00") + "&" + sasQueryParams.Encode()
  325. c.Assert(testURL.String(), chk.Equals, correctURL)
  326. }
  327. func (s *aztestsSuite) TestAccountWithPipeline(c *chk.C) {
  328. bsu := getBSU()
  329. bsu = bsu.WithPipeline(testPipeline{}) // testPipeline returns an identifying message as an error
  330. containerURL := bsu.NewContainerURL("name")
  331. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  332. c.Assert(err.Error(), chk.Equals, testPipelineMessage)
  333. }
  334. func (s *aztestsSuite) TestContainerCreateInvalidName(c *chk.C) {
  335. bsu := getBSU()
  336. containerURL := bsu.NewContainerURL("foo bar")
  337. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  338. validateStorageError(c, err, azblob.ServiceCodeInvalidResourceName)
  339. }
  340. func (s *aztestsSuite) TestContainerCreateEmptyName(c *chk.C) {
  341. bsu := getBSU()
  342. containerURL := bsu.NewContainerURL("")
  343. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  344. validateStorageError(c, err, azblob.ServiceCodeInvalidQueryParameterValue)
  345. }
  346. func (s *aztestsSuite) TestContainerCreateNameCollision(c *chk.C) {
  347. bsu := getBSU()
  348. containerURL, containerName := createNewContainer(c, bsu)
  349. defer deleteContainer(c, containerURL)
  350. containerURL = bsu.NewContainerURL(containerName)
  351. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  352. validateStorageError(c, err, azblob.ServiceCodeContainerAlreadyExists)
  353. }
  354. func (s *aztestsSuite) TestContainerCreateInvalidMetadata(c *chk.C) {
  355. bsu := getBSU()
  356. containerURL, _ := getContainerURL(c, bsu)
  357. _, err := containerURL.Create(ctx, azblob.Metadata{"1 foo": "bar"}, azblob.PublicAccessBlob)
  358. c.Assert(err, chk.NotNil)
  359. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  360. }
  361. func (s *aztestsSuite) TestContainerCreateNilMetadata(c *chk.C) {
  362. bsu := getBSU()
  363. containerURL, _ := getContainerURL(c, bsu)
  364. _, err := containerURL.Create(ctx, nil, azblob.PublicAccessBlob)
  365. defer deleteContainer(c, containerURL)
  366. c.Assert(err, chk.IsNil)
  367. response, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  368. c.Assert(err, chk.IsNil)
  369. c.Assert(response.NewMetadata(), chk.HasLen, 0)
  370. }
  371. func (s *aztestsSuite) TestContainerCreateEmptyMetadata(c *chk.C) {
  372. bsu := getBSU()
  373. containerURL, _ := getContainerURL(c, bsu)
  374. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  375. defer deleteContainer(c, containerURL)
  376. c.Assert(err, chk.IsNil)
  377. response, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  378. c.Assert(err, chk.IsNil)
  379. c.Assert(response.NewMetadata(), chk.HasLen, 0)
  380. }
  381. // Note that for all tests that create blobs, deleting the container also deletes any blobs within that container, thus we
  382. // simply delete the whole container after the test
  383. func (s *aztestsSuite) TestContainerCreateAccessContainer(c *chk.C) {
  384. bsu := getBSU()
  385. containerURL, _ := getContainerURL(c, bsu)
  386. _, err := containerURL.Create(ctx, nil, azblob.PublicAccessContainer)
  387. defer deleteContainer(c, containerURL)
  388. c.Assert(err, chk.IsNil)
  389. blobURL := containerURL.NewBlockBlobURL(blobPrefix)
  390. blobURL.PutBlob(ctx, bytes.NewReader([]byte("Content")), azblob.BlobHTTPHeaders{},
  391. basicMetadata, azblob.BlobAccessConditions{})
  392. // Anonymous enumeration should be valid with container access
  393. containerURL2 := azblob.NewContainerURL(containerURL.URL(), azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
  394. response, err := containerURL2.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  395. c.Assert(err, chk.IsNil)
  396. c.Assert(response.Blobs.Blob[0].Name, chk.Equals, blobPrefix)
  397. // Getting blob data anonymously should still be valid with container access
  398. blobURL2 := containerURL2.NewBlockBlobURL(blobPrefix)
  399. resp, err := blobURL2.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  400. c.Assert(err, chk.IsNil)
  401. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  402. }
  403. func (s *aztestsSuite) TestContainerCreateAccessBlob(c *chk.C) {
  404. bsu := getBSU()
  405. containerURL, _ := getContainerURL(c, bsu)
  406. _, err := containerURL.Create(ctx, nil, azblob.PublicAccessBlob)
  407. defer deleteContainer(c, containerURL)
  408. c.Assert(err, chk.IsNil)
  409. blobURL := containerURL.NewBlockBlobURL(blobPrefix)
  410. blobURL.PutBlob(ctx, bytes.NewReader([]byte("Content")), azblob.BlobHTTPHeaders{},
  411. basicMetadata, azblob.BlobAccessConditions{})
  412. // Reference the same container URL but with anonymous credentials
  413. containerURL2 := azblob.NewContainerURL(containerURL.URL(), azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
  414. _, err = containerURL2.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  415. validateStorageError(c, err, azblob.ServiceCodeResourceNotFound) // Listing blobs is not publicly accessible
  416. // Accessing blob specific data should be public
  417. blobURL2 := containerURL2.NewBlockBlobURL(blobPrefix)
  418. resp, err := blobURL2.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  419. c.Assert(err, chk.IsNil)
  420. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  421. }
  422. func (s *aztestsSuite) TestContainerCreateAccessNone(c *chk.C) {
  423. bsu := getBSU()
  424. containerURL, _ := getContainerURL(c, bsu)
  425. _, err := containerURL.Create(ctx, nil, azblob.PublicAccessNone)
  426. defer deleteContainer(c, containerURL)
  427. blobURL := containerURL.NewBlockBlobURL(blobPrefix)
  428. blobURL.PutBlob(ctx, bytes.NewReader([]byte("Content")), azblob.BlobHTTPHeaders{},
  429. basicMetadata, azblob.BlobAccessConditions{})
  430. // Reference the same container URL but with anonymous credentials
  431. containerURL2 := azblob.NewContainerURL(containerURL.URL(), azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
  432. // Listing blobs is not public
  433. _, err = containerURL2.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  434. validateStorageError(c, err, azblob.ServiceCodeResourceNotFound)
  435. // Blob data is not public
  436. blobURL2 := containerURL2.NewBlockBlobURL(blobPrefix)
  437. _, err = blobURL2.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  438. c.Assert(err, chk.NotNil)
  439. serr := err.(azblob.StorageError)
  440. c.Assert(serr.Response().StatusCode, chk.Equals, 404) // HEAD request does not return a status code
  441. }
  442. func validateContainerDeleted(c *chk.C, containerURL azblob.ContainerURL) {
  443. _, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  444. validateStorageError(c, err, azblob.ServiceCodeContainerNotFound)
  445. }
  446. func (s *aztestsSuite) TestContainerDelete(c *chk.C) {
  447. bsu := getBSU()
  448. containerURL, _ := createNewContainer(c, bsu)
  449. _, err := containerURL.Delete(ctx, azblob.ContainerAccessConditions{})
  450. c.Assert(err, chk.IsNil)
  451. validateContainerDeleted(c, containerURL)
  452. }
  453. func (s *aztestsSuite) TestContainerDeleteNonExistant(c *chk.C) {
  454. bsu := getBSU()
  455. containerURL, _ := getContainerURL(c, bsu)
  456. _, err := containerURL.Delete(ctx, azblob.ContainerAccessConditions{})
  457. validateStorageError(c, err, azblob.ServiceCodeContainerNotFound)
  458. }
  459. func (s *aztestsSuite) TestContainerDeleteIfModifiedSinceTrue(c *chk.C) {
  460. currentTime := getRelativeTimeGMT(-10) // Ensure the requests occur at different times
  461. bsu := getBSU()
  462. containerURL, _ := createNewContainer(c, bsu)
  463. defer deleteContainer(c, containerURL)
  464. _, err := containerURL.Delete(ctx,
  465. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  466. c.Assert(err, chk.IsNil)
  467. validateContainerDeleted(c, containerURL)
  468. }
  469. func (s *aztestsSuite) TestContainerDeleteIfModifiedSinceFalse(c *chk.C) {
  470. bsu := getBSU()
  471. containerURL, _ := createNewContainer(c, bsu)
  472. defer deleteContainer(c, containerURL)
  473. currentTime := getRelativeTimeGMT(10)
  474. _, err := containerURL.Delete(ctx,
  475. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  476. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  477. }
  478. func (s *aztestsSuite) TestContainerDeleteIfUnModifiedSinceTrue(c *chk.C) {
  479. bsu := getBSU()
  480. containerURL, _ := createNewContainer(c, bsu)
  481. defer deleteContainer(c, containerURL)
  482. currentTime := getRelativeTimeGMT(10)
  483. _, err := containerURL.Delete(ctx,
  484. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  485. c.Assert(err, chk.IsNil)
  486. validateContainerDeleted(c, containerURL)
  487. }
  488. func (s *aztestsSuite) TestContainerDeleteIfUnModifiedSinceFalse(c *chk.C) {
  489. currentTime := getRelativeTimeGMT(-10) // Ensure the requests occur at different times
  490. bsu := getBSU()
  491. containerURL, _ := createNewContainer(c, bsu)
  492. defer deleteContainer(c, containerURL)
  493. _, err := containerURL.Delete(ctx,
  494. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  495. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  496. }
  497. func (s *aztestsSuite) TestContainerAccessConditionsUnsupportedConditions(c *chk.C) {
  498. // This test defines that the library will not stop you from adding conditional headers
  499. // that will be ignored by the service
  500. bsu := getBSU()
  501. containerURL, _ := createNewContainer(c, bsu)
  502. defer deleteContainer(c, containerURL)
  503. defer func() { // The library should panic if it sees If(None)Match ETAG access condition
  504. recover()
  505. }()
  506. invalidEtag := azblob.ETag("invalid")
  507. containerURL.SetMetadata(ctx, basicMetadata,
  508. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: invalidEtag}})
  509. c.Fail() // If the method panics (as expected), then we shouldn't get here
  510. /*
  511. c.Assert(err, chk.IsNil)
  512. resp, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  513. c.Assert(err, chk.IsNil)
  514. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  515. */
  516. }
  517. func (s *aztestsSuite) TestContainerListBlobsNonexistantPrefix(c *chk.C) {
  518. bsu := getBSU()
  519. containerURL, _ := createNewContainer(c, bsu)
  520. defer deleteContainer(c, containerURL)
  521. createNewBlockBlob(c, containerURL)
  522. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{Prefix: blobPrefix + blobPrefix})
  523. c.Assert(err, chk.IsNil)
  524. c.Assert(resp.Blobs.Blob, chk.HasLen, 0)
  525. }
  526. func (s *aztestsSuite) TestContainerListBlobsSpecificValidPrefix(c *chk.C) {
  527. bsu := getBSU()
  528. containerURL, _ := createNewContainer(c, bsu)
  529. defer deleteContainer(c, containerURL)
  530. _, blobName := createNewBlockBlob(c, containerURL)
  531. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{Prefix: blobPrefix})
  532. c.Assert(err, chk.IsNil)
  533. c.Assert(resp.Blobs.Blob, chk.HasLen, 1)
  534. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  535. }
  536. func (s *aztestsSuite) TestContainerListBlobsValidDelimiter(c *chk.C) {
  537. bsu := getBSU()
  538. containerURL, _ := createNewContainer(c, bsu)
  539. defer deleteContainer(c, containerURL)
  540. createBlockBlobWithPrefix(c, containerURL, "a/1")
  541. createBlockBlobWithPrefix(c, containerURL, "a/2")
  542. createBlockBlobWithPrefix(c, containerURL, "b/1")
  543. _, blobName := createBlockBlobWithPrefix(c, containerURL, "blob")
  544. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{Delimiter: "/"})
  545. c.Assert(err, chk.IsNil)
  546. c.Assert(len(resp.Blobs.Blob), chk.Equals, 1)
  547. c.Assert(len(resp.Blobs.BlobPrefix), chk.Equals, 2)
  548. c.Assert(resp.Blobs.BlobPrefix[0].Name, chk.Equals, "a/")
  549. c.Assert(resp.Blobs.BlobPrefix[1].Name, chk.Equals, "b/")
  550. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  551. }
  552. func (s *aztestsSuite) TestContainerListBlobsInvalidDelimiter(c *chk.C) {
  553. bsu := getBSU()
  554. containerURL, _ := createNewContainer(c, bsu)
  555. defer deleteContainer(c, containerURL)
  556. createBlockBlobWithPrefix(c, containerURL, "a/1")
  557. createBlockBlobWithPrefix(c, containerURL, "a/2")
  558. createBlockBlobWithPrefix(c, containerURL, "b/1")
  559. createBlockBlobWithPrefix(c, containerURL, "blob")
  560. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{Delimiter: "^"})
  561. c.Assert(err, chk.IsNil)
  562. c.Assert(resp.Blobs.Blob, chk.HasLen, 4)
  563. }
  564. func (s *aztestsSuite) TestContainerListBlobsIncludeTypeMetadata(c *chk.C) {
  565. bsu := getBSU()
  566. container, _ := createNewContainer(c, bsu)
  567. defer deleteContainer(c, container)
  568. _, blobNameNoMetadata := createBlockBlobWithPrefix(c, container, "a")
  569. blobMetadata, blobNameMetadata := createBlockBlobWithPrefix(c, container, "b")
  570. _, err := blobMetadata.SetMetadata(ctx, azblob.Metadata{"field": "value"}, azblob.BlobAccessConditions{})
  571. c.Assert(err, chk.IsNil)
  572. resp, err := container.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Metadata: true}})
  573. c.Assert(err, chk.IsNil)
  574. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobNameNoMetadata)
  575. c.Assert(resp.Blobs.Blob[0].Metadata, chk.HasLen, 0)
  576. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobNameMetadata)
  577. c.Assert(resp.Blobs.Blob[1].Metadata["field"], chk.Equals, "value")
  578. }
  579. func (s *aztestsSuite) TestContainerListBlobsIncludeTypeSnapshots(c *chk.C) {
  580. bsu := getBSU()
  581. containerURL, _ := createNewContainer(c, bsu)
  582. defer deleteContainer(c, containerURL)
  583. blob, blobName := createNewBlockBlob(c, containerURL)
  584. _, err := blob.CreateSnapshot(ctx, azblob.Metadata{}, azblob.BlobAccessConditions{})
  585. c.Assert(err, chk.IsNil)
  586. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{},
  587. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Snapshots: true}})
  588. c.Assert(err, chk.IsNil)
  589. c.Assert(resp.Blobs.Blob, chk.HasLen, 2)
  590. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  591. c.Assert(resp.Blobs.Blob[0].Snapshot, chk.NotNil)
  592. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobName)
  593. c.Assert(resp.Blobs.Blob[1].Snapshot.IsZero(), chk.Equals, true)
  594. }
  595. func (s *aztestsSuite) TestContainerListBlobsIncludeTypeCopy(c *chk.C) {
  596. bsu := getBSU()
  597. containerURL, _ := createNewContainer(c, bsu)
  598. defer deleteContainer(c, containerURL)
  599. blobURL, blobName := createNewBlockBlob(c, containerURL)
  600. blobCopyURL, blobCopyName := createBlockBlobWithPrefix(c, containerURL, "copy")
  601. _, err := blobCopyURL.StartCopy(ctx, blobURL.URL(), azblob.Metadata{}, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  602. c.Assert(err, chk.IsNil)
  603. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{},
  604. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Copy: true}})
  605. // These are sufficient to show that the blob copy was in fact included
  606. c.Assert(err, chk.IsNil)
  607. c.Assert(resp.Blobs.Blob, chk.HasLen, 2)
  608. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobName)
  609. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobCopyName)
  610. c.Assert(*resp.Blobs.Blob[0].Properties.ContentLength, chk.Equals, int64(len(blockBlobDefaultData)))
  611. temp := blobURL.URL()
  612. c.Assert(*resp.Blobs.Blob[0].Properties.CopySource, chk.Equals, temp.String())
  613. c.Assert(resp.Blobs.Blob[0].Properties.CopyStatus, chk.Equals, azblob.CopyStatusSuccess)
  614. }
  615. func (s *aztestsSuite) TestContainerListBlobsIncludeTypeUncommitted(c *chk.C) {
  616. bsu := getBSU()
  617. containerURL, _ := createNewContainer(c, bsu)
  618. defer deleteContainer(c, containerURL)
  619. blobURL, blobName := getBlockBlobURL(c, containerURL)
  620. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  621. c.Assert(err, chk.IsNil)
  622. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{},
  623. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{UncommittedBlobs: true}})
  624. c.Assert(err, chk.IsNil)
  625. c.Assert(resp.Blobs.Blob, chk.HasLen, 1)
  626. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  627. }
  628. func (s *aztestsSuite) TestContainerListBlobsIncludeMultiple(c *chk.C) {
  629. bsu := getBSU()
  630. containerURL, _ := createNewContainer(c, bsu)
  631. defer deleteContainer(c, containerURL)
  632. blobURL, blobName := createNewBlockBlob(c, containerURL)
  633. _, err := blobURL.CreateSnapshot(ctx, azblob.Metadata{}, azblob.BlobAccessConditions{})
  634. c.Assert(err, chk.IsNil)
  635. blobURL2, blobName2 := createBlockBlobWithPrefix(c, containerURL, "copy")
  636. _, err = blobURL2.StartCopy(ctx, blobURL.URL(), azblob.Metadata{}, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  637. c.Assert(err, chk.IsNil)
  638. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{},
  639. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Snapshots: true, Copy: true}})
  640. c.Assert(err, chk.IsNil)
  641. c.Assert(resp.Blobs.Blob, chk.HasLen, 3)
  642. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName2)
  643. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobName)
  644. c.Assert(resp.Blobs.Blob[1].Snapshot, chk.NotNil)
  645. c.Assert(resp.Blobs.Blob[2].Name, chk.Equals, blobName)
  646. }
  647. func (s *aztestsSuite) TestContainerListBlobsMaxResultsNegative(c *chk.C) {
  648. bsu := getBSU()
  649. containerURL, _ := createNewContainer(c, bsu)
  650. defer deleteContainer(c, containerURL)
  651. // If ListBlobs panics, as it should, this function will be called and recover from the panic, allowing the test to pass
  652. defer func() {
  653. recover()
  654. }()
  655. containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{MaxResults: -2})
  656. // We will only reach this if we did not panic
  657. c.Fail()
  658. }
  659. func (s *aztestsSuite) TestContainerListBlobsMaxResultsZero(c *chk.C) {
  660. bsu := getBSU()
  661. containerURL, _ := createNewContainer(c, bsu)
  662. defer deleteContainer(c, containerURL)
  663. createNewBlockBlob(c, containerURL)
  664. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{MaxResults: 0})
  665. c.Assert(err, chk.IsNil)
  666. c.Assert(resp.Blobs.Blob, chk.HasLen, 1)
  667. }
  668. func (s *aztestsSuite) TestContainerListBlobsMaxResultsInsufficient(c *chk.C) {
  669. bsu := getBSU()
  670. containerURL, _ := createNewContainer(c, bsu)
  671. defer deleteContainer(c, containerURL)
  672. _, blobName := createBlockBlobWithPrefix(c, containerURL, "a")
  673. createBlockBlobWithPrefix(c, containerURL, "b")
  674. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{MaxResults: 1})
  675. c.Assert(err, chk.IsNil)
  676. c.Assert(resp.Blobs.Blob, chk.HasLen, 1)
  677. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  678. }
  679. func (s *aztestsSuite) TestContainerListBlobsMaxResultsExact(c *chk.C) {
  680. bsu := getBSU()
  681. containerURL, _ := createNewContainer(c, bsu)
  682. defer deleteContainer(c, containerURL)
  683. _, blobName := createBlockBlobWithPrefix(c, containerURL, "a")
  684. _, blobName2 := createBlockBlobWithPrefix(c, containerURL, "b")
  685. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{MaxResults: 2})
  686. c.Assert(err, chk.IsNil)
  687. c.Assert(resp.Blobs.Blob, chk.HasLen, 2)
  688. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  689. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobName2)
  690. }
  691. func (s *aztestsSuite) TestContainerListBlobsMaxResultsSufficient(c *chk.C) {
  692. bsu := getBSU()
  693. containerURL, _ := createNewContainer(c, bsu)
  694. defer deleteContainer(c, containerURL)
  695. _, blobName := createBlockBlobWithPrefix(c, containerURL, "a")
  696. _, blobName2 := createBlockBlobWithPrefix(c, containerURL, "b")
  697. resp, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{MaxResults: 3})
  698. c.Assert(err, chk.IsNil)
  699. c.Assert(resp.Blobs.Blob, chk.HasLen, 2)
  700. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  701. c.Assert(resp.Blobs.Blob[1].Name, chk.Equals, blobName2)
  702. }
  703. func (s *aztestsSuite) TestContainerListBlobsNonExistentContainer(c *chk.C) {
  704. bsu := getBSU()
  705. containerURL, _ := getContainerURL(c, bsu)
  706. _, err := containerURL.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  707. c.Assert(err, chk.NotNil)
  708. }
  709. func (s *aztestsSuite) TestContainerWithNewPipeline(c *chk.C) {
  710. bsu := getBSU()
  711. pipeline := testPipeline{}
  712. containerURL, _ := getContainerURL(c, bsu)
  713. containerURL = containerURL.WithPipeline(pipeline)
  714. _, err := containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessBlob)
  715. c.Assert(err, chk.NotNil)
  716. c.Assert(err.Error(), chk.Equals, testPipelineMessage)
  717. }
  718. func (s *aztestsSuite) TestContainerGetSetPermissionsMultiplePolicies(c *chk.C) {
  719. bsu := getBSU()
  720. containerURL, _ := createNewContainer(c, bsu)
  721. defer deleteContainer(c, containerURL)
  722. // Define the policies
  723. start := time.Now().UTC()
  724. expiry := start.Add(5 * time.Minute).UTC()
  725. expiry2 := start.Add(time.Minute).UTC()
  726. permissions := []azblob.SignedIdentifier{
  727. {ID: "0000",
  728. AccessPolicy: azblob.AccessPolicy{
  729. Start: start,
  730. Expiry: expiry,
  731. Permission: azblob.AccessPolicyPermission{Read: true, Write: true}.String(),
  732. },
  733. },
  734. {ID: "0001",
  735. AccessPolicy: azblob.AccessPolicy{
  736. Start: start,
  737. Expiry: expiry2,
  738. Permission: azblob.AccessPolicyPermission{Read: true}.String(),
  739. },
  740. },
  741. }
  742. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, permissions,
  743. azblob.ContainerAccessConditions{})
  744. c.Assert(err, chk.IsNil)
  745. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  746. c.Assert(err, chk.IsNil)
  747. c.Assert(resp.Value, chk.DeepEquals, permissions)
  748. }
  749. func (s *aztestsSuite) TestContainerGetPermissionsPublicAccessNotNone(c *chk.C) {
  750. bsu := getBSU()
  751. containerURL, _ := getContainerURL(c, bsu)
  752. containerURL.Create(ctx, nil, azblob.PublicAccessBlob) // We create the container explicitly so we can be sure the access policy is not empty
  753. defer deleteContainer(c, containerURL)
  754. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  755. c.Assert(err, chk.IsNil)
  756. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessBlob)
  757. }
  758. func (s *aztestsSuite) TestContainerSetPermissionsPublicAccessNone(c *chk.C) {
  759. // Test the basic one by making an anonymous request to ensure it's actually doing it and also with GetPermissions
  760. // For all the others, can just use GetPermissions since we've validated that it at least registers on the server correctly
  761. bsu := getBSU()
  762. containerURL, containerName := createNewContainer(c, bsu)
  763. defer deleteContainer(c, containerURL)
  764. _, blobName := createNewBlockBlob(c, containerURL)
  765. // Container is created with PublicAccessBlob, so setting it to None will actually test that it is changed through this method
  766. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil, azblob.ContainerAccessConditions{})
  767. c.Assert(err, chk.IsNil)
  768. pipeline := azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{})
  769. bsu2 := azblob.NewServiceURL(bsu.URL(), pipeline)
  770. containerURL2 := bsu2.NewContainerURL(containerName)
  771. blobURL2 := containerURL2.NewBlockBlobURL(blobName)
  772. _, err = blobURL2.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  773. // Get permissions via the original container URL so the request succeeds
  774. resp, _ := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  775. // If we cannot access a blob's data, we will also not be able to enumerate blobs
  776. validateStorageError(c, err, azblob.ServiceCodeResourceNotFound)
  777. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessNone)
  778. }
  779. func (s *aztestsSuite) TestContainerSetPermissionsPublicAccessBlob(c *chk.C) {
  780. bsu := getBSU()
  781. containerURL, _ := createNewContainer(c, bsu)
  782. defer deleteContainer(c, containerURL)
  783. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, nil, azblob.ContainerAccessConditions{})
  784. c.Assert(err, chk.IsNil)
  785. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  786. c.Assert(err, chk.IsNil)
  787. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessBlob)
  788. }
  789. func (s *aztestsSuite) TestContainerSetPermissionsPublicAccessContainer(c *chk.C) {
  790. bsu := getBSU()
  791. containerURL, _ := createNewContainer(c, bsu)
  792. defer deleteContainer(c, containerURL)
  793. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessContainer, nil, azblob.ContainerAccessConditions{})
  794. c.Assert(err, chk.IsNil)
  795. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  796. c.Assert(err, chk.IsNil)
  797. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessContainer)
  798. }
  799. func (s *aztestsSuite) TestContainerSetPermissionsACLSinglePolicy(c *chk.C) {
  800. bsu := getBSU()
  801. credentials := azblob.NewSharedKeyCredential(os.Getenv("ACCOUNT_NAME"), os.Getenv("ACCOUNT_KEY"))
  802. containerURL, containerName := createNewContainer(c, bsu)
  803. defer deleteContainer(c, containerURL)
  804. _, blobName := createNewBlockBlob(c, containerURL)
  805. start := time.Now().UTC().Add(-15 * time.Second)
  806. expiry := start.Add(5 * time.Minute).UTC()
  807. permissions := []azblob.SignedIdentifier{{
  808. ID: "0000",
  809. AccessPolicy: azblob.AccessPolicy{
  810. Start: start,
  811. Expiry: expiry,
  812. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  813. },
  814. }}
  815. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, permissions, azblob.ContainerAccessConditions{})
  816. c.Assert(err, chk.IsNil)
  817. serviceSASValues := azblob.BlobSASSignatureValues{Version: "2015-04-05",
  818. Identifier: "0000", ContainerName: containerName}
  819. queryParams := serviceSASValues.NewSASQueryParameters(credentials)
  820. sasURL := bsu.URL()
  821. sasURL.RawQuery = queryParams.Encode()
  822. sasPipeline := azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{})
  823. sasBlobServiceURL := azblob.NewServiceURL(sasURL, sasPipeline)
  824. // Verifies that the SAS can access the resource
  825. sasContainer := sasBlobServiceURL.NewContainerURL(containerName)
  826. resp, err := sasContainer.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  827. c.Assert(err, chk.IsNil)
  828. c.Assert(resp.Blobs.Blob[0].Name, chk.Equals, blobName)
  829. // Verifies that successful sas access is not just because it's public
  830. anonymousBlobService := azblob.NewServiceURL(bsu.URL(), sasPipeline)
  831. anonymousContainer := anonymousBlobService.NewContainerURL(containerName)
  832. _, err = anonymousContainer.ListBlobs(ctx, azblob.Marker{}, azblob.ListBlobsOptions{})
  833. validateStorageError(c, err, azblob.ServiceCodeResourceNotFound)
  834. }
  835. func (s *aztestsSuite) TestContainerSetPermissionsACLMoreThanFive(c *chk.C) {
  836. bsu := getBSU()
  837. containerURL, _ := createNewContainer(c, bsu)
  838. defer deleteContainer(c, containerURL)
  839. start := time.Now().UTC()
  840. expiry := start.Add(5 * time.Minute).UTC()
  841. permissions := make([]azblob.SignedIdentifier, 6, 6)
  842. for i := 0; i < 6; i++ {
  843. permissions[i] = azblob.SignedIdentifier{
  844. ID: "000" + strconv.Itoa(i),
  845. AccessPolicy: azblob.AccessPolicy{
  846. Start: start,
  847. Expiry: expiry,
  848. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  849. },
  850. }
  851. }
  852. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  853. validateStorageError(c, err, azblob.ServiceCodeInvalidXMLDocument)
  854. }
  855. func (s *aztestsSuite) TestContainerSetPermissionsDeleteAndModifyACL(c *chk.C) {
  856. bsu := getBSU()
  857. containerURL, _ := createNewContainer(c, bsu)
  858. defer deleteContainer(c, containerURL)
  859. start := time.Now().UTC()
  860. expiry := start.Add(5 * time.Minute).UTC()
  861. permissions := make([]azblob.SignedIdentifier, 2, 2)
  862. for i := 0; i < 2; i++ {
  863. permissions[i] = azblob.SignedIdentifier{
  864. ID: "000" + strconv.Itoa(i),
  865. AccessPolicy: azblob.AccessPolicy{
  866. Start: start,
  867. Expiry: expiry,
  868. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  869. },
  870. }
  871. }
  872. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  873. c.Assert(err, chk.IsNil)
  874. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  875. c.Assert(err, chk.IsNil)
  876. c.Assert(resp.Value, chk.DeepEquals, permissions)
  877. permissions = resp.Value[:1] // Delete the first policy by removing it from the slice
  878. permissions[0].ID = "0004" // Modify the remaining policy which is at index 0 in the new slice
  879. _, err = containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  880. resp, err = containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  881. c.Assert(err, chk.IsNil)
  882. c.Assert(resp.Value, chk.HasLen, 1)
  883. c.Assert(resp.Value, chk.DeepEquals, permissions)
  884. }
  885. func (s *aztestsSuite) TestContainerSetPermissionsDeleteAllPolicies(c *chk.C) {
  886. bsu := getBSU()
  887. containerURL, _ := createNewContainer(c, bsu)
  888. defer deleteContainer(c, containerURL)
  889. start := time.Now().UTC()
  890. expiry := start.Add(5 * time.Minute).UTC()
  891. permissions := make([]azblob.SignedIdentifier, 2, 2)
  892. for i := 0; i < 2; i++ {
  893. permissions[i] = azblob.SignedIdentifier{
  894. ID: "000" + strconv.Itoa(i),
  895. AccessPolicy: azblob.AccessPolicy{
  896. Start: start,
  897. Expiry: expiry,
  898. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  899. },
  900. }
  901. }
  902. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  903. c.Assert(err, chk.IsNil)
  904. _, err = containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, []azblob.SignedIdentifier{}, azblob.ContainerAccessConditions{})
  905. c.Assert(err, chk.IsNil)
  906. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  907. c.Assert(err, chk.IsNil)
  908. c.Assert(resp.Value, chk.HasLen, 0)
  909. }
  910. func (s *aztestsSuite) TestContainerSetPermissionsInvalidPolicyTimes(c *chk.C) {
  911. bsu := getBSU()
  912. containerURL, _ := createNewContainer(c, bsu)
  913. defer deleteContainer(c, containerURL)
  914. // Swap start and expiry
  915. expiry := time.Now().UTC()
  916. start := expiry.Add(5 * time.Minute).UTC()
  917. permissions := make([]azblob.SignedIdentifier, 2, 2)
  918. for i := 0; i < 2; i++ {
  919. permissions[i] = azblob.SignedIdentifier{
  920. ID: "000" + strconv.Itoa(i),
  921. AccessPolicy: azblob.AccessPolicy{
  922. Start: start,
  923. Expiry: expiry,
  924. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  925. },
  926. }
  927. }
  928. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  929. c.Assert(err, chk.IsNil)
  930. }
  931. func (s *aztestsSuite) TestContainerSetPermissionsNilPolicySlice(c *chk.C) {
  932. bsu := getBSU()
  933. containerURL, _ := createNewContainer(c, bsu)
  934. defer deleteContainer(c, containerURL)
  935. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, nil, azblob.ContainerAccessConditions{})
  936. c.Assert(err, chk.IsNil)
  937. }
  938. func (s *aztestsSuite) TestContainerSetPermissionsSignedIdentifierTooLong(c *chk.C) {
  939. bsu := getBSU()
  940. containerURL, _ := createNewContainer(c, bsu)
  941. defer deleteContainer(c, containerURL)
  942. id := ""
  943. for i := 0; i < 65; i++ {
  944. id += "a"
  945. }
  946. expiry := time.Now().UTC()
  947. start := expiry.Add(5 * time.Minute).UTC()
  948. permissions := make([]azblob.SignedIdentifier, 2, 2)
  949. for i := 0; i < 2; i++ {
  950. permissions[i] = azblob.SignedIdentifier{
  951. ID: id,
  952. AccessPolicy: azblob.AccessPolicy{
  953. Start: start,
  954. Expiry: expiry,
  955. Permission: azblob.AccessPolicyPermission{List: true}.String(),
  956. },
  957. }
  958. }
  959. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, permissions, azblob.ContainerAccessConditions{})
  960. validateStorageError(c, err, azblob.ServiceCodeInvalidXMLDocument)
  961. }
  962. func (s *aztestsSuite) TestContainerSetPermissionsIfModifiedSinceTrue(c *chk.C) {
  963. currentTime := getRelativeTimeGMT(-10)
  964. bsu := getBSU()
  965. container, _ := createNewContainer(c, bsu)
  966. defer deleteContainer(c, container)
  967. _, err := container.SetPermissions(ctx, azblob.PublicAccessNone, nil,
  968. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  969. c.Assert(err, chk.IsNil)
  970. resp, err := container.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  971. c.Assert(err, chk.IsNil)
  972. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessNone)
  973. }
  974. func (s *aztestsSuite) TestContainerSetPermissionsIfModifiedSinceFalse(c *chk.C) {
  975. bsu := getBSU()
  976. containerURL, _ := createNewContainer(c, bsu)
  977. defer deleteContainer(c, containerURL)
  978. currentTime := getRelativeTimeGMT(10)
  979. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil,
  980. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  981. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  982. }
  983. func (s *aztestsSuite) TestContainerSetPermissionsIfUnModifiedSinceTrue(c *chk.C) {
  984. bsu := getBSU()
  985. containerURL, _ := createNewContainer(c, bsu)
  986. defer deleteContainer(c, containerURL)
  987. currentTime := getRelativeTimeGMT(10)
  988. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil,
  989. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  990. c.Assert(err, chk.IsNil)
  991. resp, err := containerURL.GetPermissions(ctx, azblob.LeaseAccessConditions{})
  992. c.Assert(err, chk.IsNil)
  993. c.Assert(resp.BlobPublicAccess(), chk.Equals, azblob.PublicAccessNone)
  994. }
  995. func (s *aztestsSuite) TestContainerSetPermissionsIfUnModifiedSinceFalse(c *chk.C) {
  996. currentTime := getRelativeTimeGMT(-10)
  997. bsu := getBSU()
  998. containerURL, _ := createNewContainer(c, bsu)
  999. defer deleteContainer(c, containerURL)
  1000. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil,
  1001. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  1002. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1003. }
  1004. func (s *aztestsSuite) TestContainerGetPropertiesAndMetadataNoMetadata(c *chk.C) {
  1005. bsu := getBSU()
  1006. containerURL, _ := createNewContainer(c, bsu)
  1007. defer deleteContainer(c, containerURL)
  1008. resp, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  1009. c.Assert(err, chk.IsNil)
  1010. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  1011. }
  1012. func (s *aztestsSuite) TestContainerGetPropsAndMetaNonExistantContainer(c *chk.C) {
  1013. bsu := getBSU()
  1014. containerURL, _ := getContainerURL(c, bsu)
  1015. _, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  1016. validateStorageError(c, err, azblob.ServiceCodeContainerNotFound)
  1017. }
  1018. func (s *aztestsSuite) TestContainerSetMetadataEmpty(c *chk.C) {
  1019. bsu := getBSU()
  1020. containerURL, _ := getContainerURL(c, bsu)
  1021. _, err := containerURL.Create(ctx, basicMetadata, azblob.PublicAccessBlob)
  1022. defer deleteContainer(c, containerURL)
  1023. _, err = containerURL.SetMetadata(ctx, azblob.Metadata{}, azblob.ContainerAccessConditions{})
  1024. c.Assert(err, chk.IsNil)
  1025. resp, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  1026. c.Assert(err, chk.IsNil)
  1027. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  1028. }
  1029. func (*aztestsSuite) TestContainerSetMetadataNil(c *chk.C) {
  1030. bsu := getBSU()
  1031. containerURL, _ := getContainerURL(c, bsu)
  1032. _, err := containerURL.Create(ctx, basicMetadata, azblob.PublicAccessBlob)
  1033. defer deleteContainer(c, containerURL)
  1034. _, err = containerURL.SetMetadata(ctx, nil, azblob.ContainerAccessConditions{})
  1035. c.Assert(err, chk.IsNil)
  1036. resp, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  1037. c.Assert(err, chk.IsNil)
  1038. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  1039. }
  1040. func (*aztestsSuite) TestContainerSetMetadataInvalidField(c *chk.C) {
  1041. bsu := getBSU()
  1042. containerURL, _ := createNewContainer(c, bsu)
  1043. defer deleteContainer(c, containerURL)
  1044. _, err := containerURL.SetMetadata(ctx, azblob.Metadata{"!nval!d Field!@#%": "value"}, azblob.ContainerAccessConditions{})
  1045. c.Assert(err, chk.NotNil)
  1046. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  1047. }
  1048. func (*aztestsSuite) TestContainerSetMetadataNonExistant(c *chk.C) {
  1049. bsu := getBSU()
  1050. containerURL, _ := getContainerURL(c, bsu)
  1051. _, err := containerURL.SetMetadata(ctx, nil, azblob.ContainerAccessConditions{})
  1052. validateStorageError(c, err, azblob.ServiceCodeContainerNotFound)
  1053. }
  1054. func (s *aztestsSuite) TestContainerSetMetadataIfModifiedSinceTrue(c *chk.C) {
  1055. currentTime := getRelativeTimeGMT(-10)
  1056. bsu := getBSU()
  1057. containerURL, _ := createNewContainer(c, bsu)
  1058. defer deleteContainer(c, containerURL)
  1059. _, err := containerURL.SetMetadata(ctx, basicMetadata,
  1060. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1061. c.Assert(err, chk.IsNil)
  1062. resp, err := containerURL.GetPropertiesAndMetadata(ctx, azblob.LeaseAccessConditions{})
  1063. c.Assert(err, chk.IsNil)
  1064. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1065. }
  1066. func (s *aztestsSuite) TestContainerSetMetadataIfModifiedSinceFalse(c *chk.C) {
  1067. bsu := getBSU()
  1068. containerURL, _ := createNewContainer(c, bsu)
  1069. defer deleteContainer(c, containerURL)
  1070. currentTime := getRelativeTimeGMT(10)
  1071. _, err := containerURL.SetMetadata(ctx, basicMetadata,
  1072. azblob.ContainerAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1073. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1074. }
  1075. func (s *aztestsSuite) TestContainerNewBlobURL(c *chk.C) {
  1076. bsu := getBSU()
  1077. containerURL, _ := getContainerURL(c, bsu)
  1078. blobURL := containerURL.NewBlobURL(blobPrefix)
  1079. tempBlob := blobURL.URL()
  1080. tempContainer := containerURL.URL()
  1081. c.Assert(tempBlob.String(), chk.Equals, tempContainer.String()+"/"+blobPrefix)
  1082. c.Assert(blobURL, chk.FitsTypeOf, azblob.BlobURL{})
  1083. }
  1084. func (s *aztestsSuite) TestContainerNewBlockBlobURL(c *chk.C) {
  1085. bsu := getBSU()
  1086. containerURL, _ := getContainerURL(c, bsu)
  1087. blobURL := containerURL.NewBlockBlobURL(blobPrefix)
  1088. tempBlob := blobURL.URL()
  1089. tempContainer := containerURL.URL()
  1090. c.Assert(tempBlob.String(), chk.Equals, tempContainer.String()+"/"+blobPrefix)
  1091. c.Assert(blobURL, chk.FitsTypeOf, azblob.BlockBlobURL{})
  1092. }
  1093. func (s *aztestsSuite) TestBlobWithNewPipeline(c *chk.C) {
  1094. bsu := getBSU()
  1095. containerURL, _ := getContainerURL(c, bsu)
  1096. blobURL := containerURL.NewBlockBlobURL(blobPrefix)
  1097. newBlobURL := blobURL.WithPipeline(testPipeline{})
  1098. _, err := newBlobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  1099. c.Assert(err, chk.NotNil)
  1100. c.Assert(err.Error(), chk.Equals, testPipelineMessage)
  1101. }
  1102. func waitForCopy(c *chk.C, copyBlobURL azblob.BlockBlobURL, blobCopyResponse *azblob.BlobsCopyResponse) {
  1103. status := blobCopyResponse.CopyStatus()
  1104. // Wait for the copy to finish. If the copy takes longer than a minute, we will fail
  1105. start := time.Now()
  1106. for status != azblob.CopyStatusSuccess {
  1107. GetPropertiesAndMetadataResult, _ := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1108. status = GetPropertiesAndMetadataResult.CopyStatus()
  1109. currentTime := time.Now()
  1110. if currentTime.Sub(start) >= time.Minute {
  1111. c.Fail()
  1112. }
  1113. }
  1114. }
  1115. func (s *aztestsSuite) TestBlobStartCopyDestEmpty(c *chk.C) {
  1116. bsu := getBSU()
  1117. containerURL, _ := createNewContainer(c, bsu)
  1118. defer deleteContainer(c, containerURL)
  1119. blobURL, _ := createNewBlockBlob(c, containerURL)
  1120. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1121. blobCopyResponse, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1122. c.Assert(err, chk.IsNil)
  1123. waitForCopy(c, copyBlobURL, blobCopyResponse)
  1124. resp, err := copyBlobURL.GetBlob(ctx, azblob.BlobRange{Offset: 0, Count: 20}, azblob.BlobAccessConditions{}, false)
  1125. c.Assert(err, chk.IsNil)
  1126. // Read the blob data to verify the copy
  1127. data, _ := ioutil.ReadAll(resp.Response().Body)
  1128. c.Assert(resp.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1129. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1130. resp.Body().Close()
  1131. }
  1132. func (s *aztestsSuite) TestBlobStartCopyMetadata(c *chk.C) {
  1133. bsu := getBSU()
  1134. containerURL, _ := createNewContainer(c, bsu)
  1135. defer deleteContainer(c, containerURL)
  1136. blobURL, _ := createNewBlockBlob(c, containerURL)
  1137. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1138. resp, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1139. c.Assert(err, chk.IsNil)
  1140. waitForCopy(c, copyBlobURL, resp)
  1141. resp2, err := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1142. c.Assert(err, chk.IsNil)
  1143. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1144. }
  1145. func (s *aztestsSuite) TestBlobStartCopyMetadataNil(c *chk.C) {
  1146. bsu := getBSU()
  1147. containerURL, _ := createNewContainer(c, bsu)
  1148. defer deleteContainer(c, containerURL)
  1149. blobURL, _ := createNewBlockBlob(c, containerURL)
  1150. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1151. // Have the destination start with metadata so we ensure the nil metadata passed later takes effect
  1152. _, err := copyBlobURL.PutBlob(ctx, bytes.NewReader([]byte("data")), azblob.BlobHTTPHeaders{},
  1153. basicMetadata, azblob.BlobAccessConditions{})
  1154. c.Assert(err, chk.IsNil)
  1155. resp, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1156. c.Assert(err, chk.IsNil)
  1157. waitForCopy(c, copyBlobURL, resp)
  1158. resp2, err := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1159. c.Assert(err, chk.IsNil)
  1160. c.Assert(resp2.NewMetadata(), chk.HasLen, 0)
  1161. }
  1162. func (s *aztestsSuite) TestBlobStartCopyMetadataEmpty(c *chk.C) {
  1163. bsu := getBSU()
  1164. containerURL, _ := createNewContainer(c, bsu)
  1165. defer deleteContainer(c, containerURL)
  1166. blobURL, _ := createNewBlockBlob(c, containerURL)
  1167. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1168. // Have the destination start with metadata so we ensure the empty metadata passed later takes effect
  1169. _, err := copyBlobURL.PutBlob(ctx, bytes.NewReader([]byte("data")), azblob.BlobHTTPHeaders{},
  1170. basicMetadata, azblob.BlobAccessConditions{})
  1171. c.Assert(err, chk.IsNil)
  1172. resp, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), azblob.Metadata{}, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1173. c.Assert(err, chk.IsNil)
  1174. waitForCopy(c, copyBlobURL, resp)
  1175. resp2, err := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1176. c.Assert(err, chk.IsNil)
  1177. c.Assert(resp2.NewMetadata(), chk.HasLen, 0)
  1178. }
  1179. func (s *aztestsSuite) TestBlobStartCopyMetadataInvalidField(c *chk.C) {
  1180. bsu := getBSU()
  1181. containerURL, _ := createNewContainer(c, bsu)
  1182. defer deleteContainer(c, containerURL)
  1183. blobURL, _ := createNewBlockBlob(c, containerURL)
  1184. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1185. _, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), azblob.Metadata{"I nvalid.": "bar"}, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1186. c.Assert(err, chk.NotNil)
  1187. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  1188. }
  1189. func (s *aztestsSuite) TestBlobStartCopySourceNonExistant(c *chk.C) {
  1190. bsu := getBSU()
  1191. containerURL, _ := createNewContainer(c, bsu)
  1192. defer deleteContainer(c, containerURL)
  1193. blobURL, _ := getBlockBlobURL(c, containerURL)
  1194. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1195. _, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1196. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  1197. }
  1198. func (s *aztestsSuite) TestBlobStartCopySourcePrivate(c *chk.C) {
  1199. bsu := getBSU()
  1200. containerURL, _ := createNewContainer(c, bsu)
  1201. defer deleteContainer(c, containerURL)
  1202. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil, azblob.ContainerAccessConditions{})
  1203. c.Assert(err, chk.IsNil)
  1204. blobURL, _ := createNewBlockBlob(c, containerURL)
  1205. bsu2, err := getAlternateBSU()
  1206. if err != nil {
  1207. c.Skip(err.Error())
  1208. return
  1209. }
  1210. copyContainerURL, _ := createNewContainer(c, bsu2)
  1211. defer deleteContainer(c, copyContainerURL)
  1212. copyBlobURL, _ := getBlockBlobURL(c, copyContainerURL)
  1213. if bsu.String() == bsu2.String() {
  1214. c.Skip("Test not valid because primary and secondary accounts are the same")
  1215. }
  1216. _, err = copyBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1217. validateStorageError(c, err, azblob.ServiceCodeCannotVerifyCopySource)
  1218. }
  1219. func (s *aztestsSuite) TestBlobStartCopyUsingSASSrc(c *chk.C) {
  1220. bsu := getBSU()
  1221. containerURL, containerName := createNewContainer(c, bsu)
  1222. defer deleteContainer(c, containerURL)
  1223. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil, azblob.ContainerAccessConditions{})
  1224. c.Assert(err, chk.IsNil)
  1225. blobURL, blobName := createNewBlockBlob(c, containerURL)
  1226. // Create sas values for the source blob
  1227. credentials := azblob.NewSharedKeyCredential(os.Getenv("ACCOUNT_NAME"), os.Getenv("ACCOUNT_KEY"))
  1228. serviceSASValues := azblob.BlobSASSignatureValues{Version: "2015-04-05", StartTime: time.Now().Add(-1 * time.Hour).UTC(),
  1229. ExpiryTime: time.Now().Add(time.Hour).UTC(), Permissions: azblob.BlobSASPermissions{Read: true, Write: true}.String(),
  1230. ContainerName: containerName, BlobName: blobName}
  1231. queryParams := serviceSASValues.NewSASQueryParameters(credentials)
  1232. // Create URLs to the destination blob with sas parameters
  1233. sasURL := blobURL.URL()
  1234. sasURL.RawQuery = queryParams.Encode()
  1235. // Create a new container for the destination
  1236. bsu2, err := getAlternateBSU()
  1237. if err != nil {
  1238. c.Skip(err.Error())
  1239. return
  1240. }
  1241. copyContainerURL, _ := createNewContainer(c, bsu2)
  1242. defer deleteContainer(c, copyContainerURL)
  1243. copyBlobURL, _ := getBlockBlobURL(c, copyContainerURL)
  1244. resp, err := copyBlobURL.StartCopy(ctx, sasURL, nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1245. c.Assert(err, chk.IsNil)
  1246. waitForCopy(c, copyBlobURL, resp)
  1247. resp2, err := copyBlobURL.GetBlob(ctx, azblob.BlobRange{Offset: 0, Count: int64(len(blockBlobDefaultData))}, azblob.BlobAccessConditions{}, false)
  1248. c.Assert(err, chk.IsNil)
  1249. data, err := ioutil.ReadAll(resp2.Response().Body)
  1250. c.Assert(resp2.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1251. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1252. resp2.Body().Close()
  1253. }
  1254. func (s *aztestsSuite) TestBlobStartCopyUsingSASDest(c *chk.C) {
  1255. bsu := getBSU()
  1256. containerURL, containerName := createNewContainer(c, bsu)
  1257. defer deleteContainer(c, containerURL)
  1258. _, err := containerURL.SetPermissions(ctx, azblob.PublicAccessNone, nil, azblob.ContainerAccessConditions{})
  1259. c.Assert(err, chk.IsNil)
  1260. blobURL, blobName := createNewBlockBlob(c, containerURL)
  1261. _ = blobURL
  1262. // Generate SAS on the source
  1263. serviceSASValues := azblob.BlobSASSignatureValues{ExpiryTime: time.Now().Add(time.Hour).UTC(),
  1264. Permissions: azblob.BlobSASPermissions{Read: true, Write: true, Create: true}.String(), ContainerName: containerName, BlobName: blobName}
  1265. credentials := azblob.NewSharedKeyCredential(os.Getenv("ACCOUNT_NAME"), os.Getenv("ACCOUNT_KEY"))
  1266. queryParams := serviceSASValues.NewSASQueryParameters(credentials)
  1267. // Create destination container
  1268. bsu2, err := getAlternateBSU()
  1269. if err != nil {
  1270. c.Skip(err.Error())
  1271. return
  1272. }
  1273. copyContainerURL, copyContainerName := createNewContainer(c, bsu2)
  1274. defer deleteContainer(c, copyContainerURL)
  1275. copyBlobURL, copyBlobName := getBlockBlobURL(c, copyContainerURL)
  1276. // Generate Sas for the destination
  1277. credentials = azblob.NewSharedKeyCredential(os.Getenv("SECONDARY_ACCOUNT_NAME"), os.Getenv("SECONDARY_ACCOUNT_KEY"))
  1278. copyServiceSASvalues := azblob.BlobSASSignatureValues{StartTime: time.Now().Add(-1 * time.Hour).UTC(),
  1279. ExpiryTime: time.Now().Add(time.Hour).UTC(), Permissions: azblob.BlobSASPermissions{Read: true, Write: true}.String(),
  1280. ContainerName: copyContainerName, BlobName: copyBlobName}
  1281. copyQueryParams := copyServiceSASvalues.NewSASQueryParameters(credentials)
  1282. // Generate anonymous URL to destination with SAS
  1283. anonURL := bsu2.URL()
  1284. anonURL.RawQuery = copyQueryParams.Encode()
  1285. anonPipeline := azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{})
  1286. anonBSU := azblob.NewServiceURL(anonURL, anonPipeline)
  1287. anonContainerURL := anonBSU.NewContainerURL(copyContainerName)
  1288. anonBlobURL := anonContainerURL.NewBlockBlobURL(copyBlobName)
  1289. // Apply sas to source
  1290. srcBlobWithSasURL := blobURL.URL()
  1291. srcBlobWithSasURL.RawQuery = queryParams.Encode()
  1292. resp, err := anonBlobURL.StartCopy(ctx, srcBlobWithSasURL, nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1293. c.Assert(err, chk.IsNil)
  1294. // Allow copy to happen
  1295. waitForCopy(c, anonBlobURL, resp)
  1296. resp2, err := copyBlobURL.GetBlob(ctx, azblob.BlobRange{Offset: 0, Count: int64(len(blockBlobDefaultData))}, azblob.BlobAccessConditions{}, false)
  1297. c.Assert(err, chk.IsNil)
  1298. data, err := ioutil.ReadAll(resp2.Response().Body)
  1299. _, err = resp2.Body().Read(data)
  1300. c.Assert(resp2.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1301. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1302. resp2.Body().Close()
  1303. }
  1304. func (s *aztestsSuite) TestBlobStartCopySourceIfModifiedSinceTrue(c *chk.C) {
  1305. currentTime := getRelativeTimeGMT(-10)
  1306. bsu := getBSU()
  1307. containerURL, _ := createNewContainer(c, bsu)
  1308. defer deleteContainer(c, containerURL)
  1309. blobURL, _ := createNewBlockBlob(c, containerURL)
  1310. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1311. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1312. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}},
  1313. azblob.BlobAccessConditions{})
  1314. c.Assert(err, chk.IsNil)
  1315. resp, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1316. c.Assert(err, chk.IsNil)
  1317. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1318. }
  1319. func (s *aztestsSuite) TestBlobStartCopySourceIfModifiedSinceFalse(c *chk.C) {
  1320. bsu := getBSU()
  1321. containerURL, _ := createNewContainer(c, bsu)
  1322. defer deleteContainer(c, containerURL)
  1323. blobURL, _ := createNewBlockBlob(c, containerURL)
  1324. currentTime := getRelativeTimeGMT(10)
  1325. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1326. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil,
  1327. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}},
  1328. azblob.BlobAccessConditions{})
  1329. validateStorageError(c, err, azblob.ServiceCodeSourceConditionNotMet)
  1330. }
  1331. func (s *aztestsSuite) TestBlobStartCopySourceIfUnmodifiedSinceTrue(c *chk.C) {
  1332. bsu := getBSU()
  1333. containerURL, _ := createNewContainer(c, bsu)
  1334. defer deleteContainer(c, containerURL)
  1335. blobURL, _ := createNewBlockBlob(c, containerURL)
  1336. currentTime := getRelativeTimeGMT(10)
  1337. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1338. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1339. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}},
  1340. azblob.BlobAccessConditions{})
  1341. c.Assert(err, chk.IsNil)
  1342. resp, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1343. c.Assert(err, chk.IsNil)
  1344. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1345. }
  1346. func (s *aztestsSuite) TestBlobStartCopySourceIfUnmodifiedSinceFalse(c *chk.C) {
  1347. bsu := getBSU()
  1348. containerURL, _ := createNewContainer(c, bsu)
  1349. defer deleteContainer(c, containerURL)
  1350. blobURL, _ := createNewBlockBlob(c, containerURL)
  1351. currentTime := getRelativeTimeGMT(-10)
  1352. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1353. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil,
  1354. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}},
  1355. azblob.BlobAccessConditions{})
  1356. validateStorageError(c, err, azblob.ServiceCodeSourceConditionNotMet)
  1357. }
  1358. func (s *aztestsSuite) TestBlobStartCopySourceIfMatchTrue(c *chk.C) {
  1359. bsu := getBSU()
  1360. containerURL, _ := createNewContainer(c, bsu)
  1361. defer deleteContainer(c, containerURL)
  1362. blobURL, _ := createNewBlockBlob(c, containerURL)
  1363. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1364. c.Assert(err, chk.IsNil)
  1365. etag := resp.ETag()
  1366. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1367. _, err = destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1368. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}},
  1369. azblob.BlobAccessConditions{})
  1370. c.Assert(err, chk.IsNil)
  1371. resp2, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1372. c.Assert(err, chk.IsNil)
  1373. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1374. }
  1375. func (s *aztestsSuite) TestBlobStartCopySourceIfMatchFalse(c *chk.C) {
  1376. bsu := getBSU()
  1377. containerURL, _ := createNewContainer(c, bsu)
  1378. defer deleteContainer(c, containerURL)
  1379. blobURL, _ := createNewBlockBlob(c, containerURL)
  1380. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1381. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1382. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: "a"}},
  1383. azblob.BlobAccessConditions{})
  1384. validateStorageError(c, err, azblob.ServiceCodeSourceConditionNotMet)
  1385. }
  1386. func (s *aztestsSuite) TestBlobStartCopySourceIfNoneMatchTrue(c *chk.C) {
  1387. bsu := getBSU()
  1388. containerURL, _ := createNewContainer(c, bsu)
  1389. defer deleteContainer(c, containerURL)
  1390. blobURL, _ := createNewBlockBlob(c, containerURL)
  1391. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1392. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1393. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: "a"}},
  1394. azblob.BlobAccessConditions{})
  1395. c.Assert(err, chk.IsNil)
  1396. resp2, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1397. c.Assert(err, chk.IsNil)
  1398. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1399. }
  1400. func (s *aztestsSuite) TestBlobStartCopySourceIfNoneMatchFalse(c *chk.C) {
  1401. bsu := getBSU()
  1402. containerURL, _ := createNewContainer(c, bsu)
  1403. defer deleteContainer(c, containerURL)
  1404. blobURL, _ := createNewBlockBlob(c, containerURL)
  1405. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1406. c.Assert(err, chk.IsNil)
  1407. etag := resp.ETag()
  1408. destBlobURL, _ := getBlockBlobURL(c, containerURL)
  1409. _, err = destBlobURL.StartCopy(ctx, blobURL.URL(), nil,
  1410. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}},
  1411. azblob.BlobAccessConditions{})
  1412. validateStorageError(c, err, azblob.ServiceCodeSourceConditionNotMet)
  1413. }
  1414. func (s *aztestsSuite) TestBlobStartCopyDestIfModifiedSinceTrue(c *chk.C) {
  1415. currentTime := getRelativeTimeGMT(-10)
  1416. bsu := getBSU()
  1417. containerURL, _ := createNewContainer(c, bsu)
  1418. defer deleteContainer(c, containerURL)
  1419. blobURL, _ := createNewBlockBlob(c, containerURL)
  1420. destBlobURL, _ := createNewBlockBlob(c, containerURL) // The blob must exist to have a last-modified time
  1421. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1422. azblob.BlobAccessConditions{},
  1423. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1424. c.Assert(err, chk.IsNil)
  1425. resp, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1426. c.Assert(err, chk.IsNil)
  1427. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1428. }
  1429. func (s *aztestsSuite) TestBlobStartCopyDestIfModifiedSinceFalse(c *chk.C) {
  1430. bsu := getBSU()
  1431. containerURL, _ := createNewContainer(c, bsu)
  1432. defer deleteContainer(c, containerURL)
  1433. blobURL, _ := createNewBlockBlob(c, containerURL)
  1434. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1435. currentTime := getRelativeTimeGMT(10)
  1436. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil,
  1437. azblob.BlobAccessConditions{},
  1438. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1439. validateStorageError(c, err, azblob.ServiceCodeTargetConditionNotMet)
  1440. }
  1441. func (s *aztestsSuite) TestBlobStartCopyDestIfUnmodifiedSinceTrue(c *chk.C) {
  1442. bsu := getBSU()
  1443. containerURL, _ := createNewContainer(c, bsu)
  1444. defer deleteContainer(c, containerURL)
  1445. blobURL, _ := createNewBlockBlob(c, containerURL)
  1446. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1447. currentTime := getRelativeTimeGMT(10)
  1448. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1449. azblob.BlobAccessConditions{},
  1450. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  1451. c.Assert(err, chk.IsNil)
  1452. resp, err := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1453. c.Assert(err, chk.IsNil)
  1454. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1455. }
  1456. func (s *aztestsSuite) TestBlobStartCopyDestIfUnmodifiedSinceFalse(c *chk.C) {
  1457. bsu := getBSU()
  1458. containerURL, _ := createNewContainer(c, bsu)
  1459. defer deleteContainer(c, containerURL)
  1460. blobURL, _ := createNewBlockBlob(c, containerURL)
  1461. currentTime := getRelativeTimeGMT(-10)
  1462. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1463. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil,
  1464. azblob.BlobAccessConditions{},
  1465. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  1466. validateStorageError(c, err, azblob.ServiceCodeTargetConditionNotMet)
  1467. }
  1468. func (s *aztestsSuite) TestBlobStartCopyDestIfMatchTrue(c *chk.C) {
  1469. bsu := getBSU()
  1470. containerURL, _ := createNewContainer(c, bsu)
  1471. defer deleteContainer(c, containerURL)
  1472. blobURL, _ := createNewBlockBlob(c, containerURL)
  1473. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1474. resp, _ := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1475. etag := resp.ETag()
  1476. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata,
  1477. azblob.BlobAccessConditions{},
  1478. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}})
  1479. c.Assert(err, chk.IsNil)
  1480. resp, err = destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1481. c.Assert(err, chk.IsNil)
  1482. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1483. }
  1484. func (s *aztestsSuite) TestBlobStartCopyDestIfMatchFalse(c *chk.C) {
  1485. bsu := getBSU()
  1486. containerURL, _ := createNewContainer(c, bsu)
  1487. defer deleteContainer(c, containerURL)
  1488. blobURL, _ := createNewBlockBlob(c, containerURL)
  1489. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1490. resp, _ := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1491. etag := resp.ETag()
  1492. destBlobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{}) // SetMetadata chances the blob's etag
  1493. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{},
  1494. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}})
  1495. validateStorageError(c, err, azblob.ServiceCodeTargetConditionNotMet)
  1496. }
  1497. func (s *aztestsSuite) TestBlobStartCopyDestIfNoneMatchTrue(c *chk.C) {
  1498. bsu := getBSU()
  1499. containerURL, _ := createNewContainer(c, bsu)
  1500. defer deleteContainer(c, containerURL)
  1501. blobURL, _ := createNewBlockBlob(c, containerURL)
  1502. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1503. resp, _ := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1504. etag := resp.ETag()
  1505. destBlobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{}) // SetMetadata chances the blob's etag
  1506. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), basicMetadata, azblob.BlobAccessConditions{},
  1507. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}})
  1508. c.Assert(err, chk.IsNil)
  1509. resp, err = destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1510. c.Assert(err, chk.IsNil)
  1511. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  1512. }
  1513. func (s *aztestsSuite) TestBlobStartCopyDestIfNoneMatchFalse(c *chk.C) {
  1514. bsu := getBSU()
  1515. containerURL, _ := createNewContainer(c, bsu)
  1516. defer deleteContainer(c, containerURL)
  1517. blobURL, _ := createNewBlockBlob(c, containerURL)
  1518. destBlobURL, _ := createNewBlockBlob(c, containerURL)
  1519. resp, _ := destBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1520. etag := resp.ETag()
  1521. _, err := destBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{},
  1522. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}})
  1523. validateStorageError(c, err, azblob.ServiceCodeTargetConditionNotMet)
  1524. }
  1525. func (s *aztestsSuite) TestBlobAbortCopyInProgress(c *chk.C) {
  1526. bsu := getBSU()
  1527. containerURL, _ := createNewContainer(c, bsu)
  1528. defer deleteContainer(c, containerURL)
  1529. blobURL, _ := getBlockBlobURL(c, containerURL)
  1530. // Create a large blob that takes time to copy
  1531. blobSize := 8 * 1024 * 1024
  1532. blobData := make([]byte, blobSize, blobSize)
  1533. for i := range blobData {
  1534. blobData[i] = byte('a' + i%26)
  1535. }
  1536. _, err := blobURL.PutBlob(ctx, bytes.NewReader(blobData), azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  1537. c.Assert(err, chk.IsNil)
  1538. containerURL.SetPermissions(ctx, azblob.PublicAccessBlob, nil, azblob.ContainerAccessConditions{}) // So that we don't have to create a SAS
  1539. // Must copy across accounts so it takes time to copy
  1540. bsu2, err := getAlternateBSU()
  1541. if err != nil {
  1542. c.Skip(err.Error())
  1543. return
  1544. }
  1545. copyContainerURL, _ := createNewContainer(c, bsu2)
  1546. copyBlobURL, _ := getBlockBlobURL(c, copyContainerURL)
  1547. defer deleteContainer(c, copyContainerURL)
  1548. resp, err := copyBlobURL.StartCopy(ctx, blobURL.URL(), nil, azblob.BlobAccessConditions{}, azblob.BlobAccessConditions{})
  1549. c.Assert(err, chk.IsNil)
  1550. c.Assert(resp.CopyStatus(), chk.Equals, azblob.CopyStatusPending)
  1551. _, err = copyBlobURL.AbortCopy(ctx, resp.CopyID(), azblob.LeaseAccessConditions{})
  1552. if err != nil {
  1553. // If the error is nil, the test continues as normal.
  1554. // If the error is not nil, we want to check if it's because the copy is finished and send a message indicating this.
  1555. validateStorageError(c, err, azblob.ServiceCodeNoPendingCopyOperation)
  1556. c.Error("The test failed because the copy completed because it was aborted")
  1557. }
  1558. resp2, _ := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1559. c.Assert(resp2.CopyStatus(), chk.Equals, azblob.CopyStatusAborted)
  1560. }
  1561. func (s *aztestsSuite) TestBlobAbortCopyNoCopyStarted(c *chk.C) {
  1562. bsu := getBSU()
  1563. containerURL, _ := createNewContainer(c, bsu)
  1564. defer deleteContainer(c, containerURL)
  1565. copyBlobURL, _ := getBlockBlobURL(c, containerURL)
  1566. _, err := copyBlobURL.AbortCopy(ctx, "copynotstarted", azblob.LeaseAccessConditions{})
  1567. validateStorageError(c, err, azblob.ServiceCodeInvalidQueryParameterValue)
  1568. }
  1569. func (s *aztestsSuite) TestBlobSnapshotMetadata(c *chk.C) {
  1570. bsu := getBSU()
  1571. containerURL, _ := createNewContainer(c, bsu)
  1572. defer deleteContainer(c, containerURL)
  1573. blobURL, _ := createNewBlockBlob(c, containerURL)
  1574. resp, err := blobURL.CreateSnapshot(ctx, basicMetadata, azblob.BlobAccessConditions{})
  1575. c.Assert(err, chk.IsNil)
  1576. // Since metadata is specified on the snapshot, the snapshot should have its own metadata different from the (empty) metadata on the source
  1577. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  1578. resp2, err := snapshotURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1579. c.Assert(err, chk.IsNil)
  1580. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1581. }
  1582. func (s *aztestsSuite) TestBlobSnapshotMetadataEmpty(c *chk.C) {
  1583. bsu := getBSU()
  1584. containerURL, _ := createNewContainer(c, bsu)
  1585. defer deleteContainer(c, containerURL)
  1586. blobURL, _ := createNewBlockBlob(c, containerURL)
  1587. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  1588. c.Assert(err, chk.IsNil)
  1589. resp, err := blobURL.CreateSnapshot(ctx, azblob.Metadata{}, azblob.BlobAccessConditions{})
  1590. c.Assert(err, chk.IsNil)
  1591. // In this case, because no metadata was specified, it should copy the basicMetadata from the source
  1592. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  1593. resp2, err := snapshotURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1594. c.Assert(err, chk.IsNil)
  1595. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1596. }
  1597. func (s *aztestsSuite) TestBlobSnapshotMetadataNil(c *chk.C) {
  1598. bsu := getBSU()
  1599. containerURL, _ := createNewContainer(c, bsu)
  1600. defer deleteContainer(c, containerURL)
  1601. blobURL, _ := createNewBlockBlob(c, containerURL)
  1602. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  1603. c.Assert(err, chk.IsNil)
  1604. resp, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1605. c.Assert(err, chk.IsNil)
  1606. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  1607. resp2, err := snapshotURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1608. c.Assert(err, chk.IsNil)
  1609. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  1610. }
  1611. func (s *aztestsSuite) TestBlobSnapshotMetadataInvalid(c *chk.C) {
  1612. bsu := getBSU()
  1613. containerURL, _ := createNewContainer(c, bsu)
  1614. defer deleteContainer(c, containerURL)
  1615. blobURL, _ := createNewBlockBlob(c, containerURL)
  1616. _, err := blobURL.CreateSnapshot(ctx, azblob.Metadata{"Invalid Field!": "value"}, azblob.BlobAccessConditions{})
  1617. c.Assert(err, chk.NotNil)
  1618. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  1619. }
  1620. func (s *aztestsSuite) TestBlobSnapshotBlobNotExist(c *chk.C) {
  1621. bsu := getBSU()
  1622. containerURL, _ := createNewContainer(c, bsu)
  1623. defer deleteContainer(c, containerURL)
  1624. blobURL, _ := getBlockBlobURL(c, containerURL)
  1625. _, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1626. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  1627. }
  1628. func (s *aztestsSuite) TestBlobSnapshotOfSnapshot(c *chk.C) {
  1629. bsu := getBSU()
  1630. containerURL, _ := createNewContainer(c, bsu)
  1631. defer deleteContainer(c, containerURL)
  1632. blobURL, _ := createNewBlockBlob(c, containerURL)
  1633. snapshotURL := blobURL.WithSnapshot(time.Now().UTC())
  1634. // The library allows the server to handle the snapshot of snapshot error
  1635. _, err := snapshotURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1636. validateStorageError(c, err, azblob.ServiceCodeInvalidQueryParameterValue)
  1637. }
  1638. func (s *aztestsSuite) TestBlobSnapshotIfModifiedSinceTrue(c *chk.C) {
  1639. currentTime := getRelativeTimeGMT(-10)
  1640. bsu := getBSU()
  1641. containerURL, _ := createNewContainer(c, bsu)
  1642. defer deleteContainer(c, containerURL)
  1643. blobURL, _ := createNewBlockBlob(c, containerURL)
  1644. resp, err := blobURL.CreateSnapshot(ctx, nil,
  1645. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1646. c.Assert(err, chk.IsNil)
  1647. c.Assert(resp.Snapshot().IsZero(), chk.Equals, false) // i.e. The snapshot time is not zero. If the service gives us back a snapshot time, it successfully created a snapshot
  1648. }
  1649. func (s *aztestsSuite) TestBlobSnapshotIfModifiedSinceFalse(c *chk.C) {
  1650. bsu := getBSU()
  1651. containerURL, _ := createNewContainer(c, bsu)
  1652. defer deleteContainer(c, containerURL)
  1653. blobURL, _ := createNewBlockBlob(c, containerURL)
  1654. currentTime := getRelativeTimeGMT(10)
  1655. _, err := blobURL.CreateSnapshot(ctx, nil,
  1656. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1657. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1658. }
  1659. func (s *aztestsSuite) TestBlobSnapshotIfUnmodifiedSinceTrue(c *chk.C) {
  1660. bsu := getBSU()
  1661. containerURL, _ := createNewContainer(c, bsu)
  1662. defer deleteContainer(c, containerURL)
  1663. blobURL, _ := createNewBlockBlob(c, containerURL)
  1664. currentTime := getRelativeTimeGMT(10)
  1665. resp, err := blobURL.CreateSnapshot(ctx, nil,
  1666. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  1667. c.Assert(err, chk.IsNil)
  1668. c.Assert(resp.Snapshot().IsZero(), chk.Equals, false)
  1669. }
  1670. func (s *aztestsSuite) TestBlobSnapshotIfUnmodifiedSinceFalse(c *chk.C) {
  1671. currentTime := getRelativeTimeGMT(-10)
  1672. bsu := getBSU()
  1673. containerURL, _ := createNewContainer(c, bsu)
  1674. defer deleteContainer(c, containerURL)
  1675. blobURL, _ := createNewBlockBlob(c, containerURL)
  1676. _, err := blobURL.CreateSnapshot(ctx, nil,
  1677. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  1678. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1679. }
  1680. func (s *aztestsSuite) TestBlobSnapshotIfMatchTrue(c *chk.C) {
  1681. bsu := getBSU()
  1682. containerURL, _ := createNewContainer(c, bsu)
  1683. defer deleteContainer(c, containerURL)
  1684. blobURL, _ := createNewBlockBlob(c, containerURL)
  1685. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1686. resp2, err := blobURL.CreateSnapshot(ctx, nil,
  1687. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  1688. c.Assert(err, chk.IsNil)
  1689. c.Assert(resp2.Snapshot().IsZero(), chk.Equals, false)
  1690. }
  1691. func (s *aztestsSuite) TestBlobSnapshotIfMatchFalse(c *chk.C) {
  1692. bsu := getBSU()
  1693. containerURL, _ := createNewContainer(c, bsu)
  1694. defer deleteContainer(c, containerURL)
  1695. blobURL, _ := createNewBlockBlob(c, containerURL)
  1696. _, err := blobURL.CreateSnapshot(ctx, nil,
  1697. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: "garbage"}})
  1698. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1699. }
  1700. func (s *aztestsSuite) TestBlobSnapshotIfNoneMatchTrue(c *chk.C) {
  1701. bsu := getBSU()
  1702. containerURL, _ := createNewContainer(c, bsu)
  1703. defer deleteContainer(c, containerURL)
  1704. blobURL, _ := createNewBlockBlob(c, containerURL)
  1705. resp, err := blobURL.CreateSnapshot(ctx, nil,
  1706. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: "garbage"}})
  1707. c.Assert(err, chk.IsNil)
  1708. c.Assert(resp.Snapshot().IsZero(), chk.Equals, false)
  1709. }
  1710. func (s *aztestsSuite) TestBlobSnapshotIfNoneMatchFalse(c *chk.C) {
  1711. bsu := getBSU()
  1712. containerURL, _ := createNewContainer(c, bsu)
  1713. defer deleteContainer(c, containerURL)
  1714. blobURL, _ := createNewBlockBlob(c, containerURL)
  1715. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1716. _, err = blobURL.CreateSnapshot(ctx, nil,
  1717. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  1718. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1719. }
  1720. func (s *aztestsSuite) TestBlobDownloadDataNonExistantBlob(c *chk.C) {
  1721. bsu := getBSU()
  1722. containerURL, _ := createNewContainer(c, bsu)
  1723. defer deleteContainer(c, containerURL)
  1724. blobURL, _ := getBlockBlobURL(c, containerURL)
  1725. _, err := blobURL.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  1726. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  1727. }
  1728. func (s *aztestsSuite) TestBlobDownloadDataNegativeOffset(c *chk.C) {
  1729. bsu := getBSU()
  1730. containerURL, _ := createNewContainer(c, bsu)
  1731. defer deleteContainer(c, containerURL)
  1732. blobURL, _ := createNewBlockBlob(c, containerURL)
  1733. defer func() { // The library should fail if it seems numeric parameters that are guaranteed invalid
  1734. recover()
  1735. }()
  1736. blobURL.GetBlob(ctx, azblob.BlobRange{Offset: -1}, azblob.BlobAccessConditions{}, false)
  1737. c.Fail()
  1738. }
  1739. func (s *aztestsSuite) TestBlobDownloadDataOffsetOutOfRange(c *chk.C) {
  1740. bsu := getBSU()
  1741. containerURL, _ := createNewContainer(c, bsu)
  1742. defer deleteContainer(c, containerURL)
  1743. blobURL, _ := createNewBlockBlob(c, containerURL)
  1744. _, err := blobURL.GetBlob(ctx, azblob.BlobRange{Offset: int64(len(blockBlobDefaultData))}, azblob.BlobAccessConditions{}, false)
  1745. validateStorageError(c, err, azblob.ServiceCodeInvalidRange)
  1746. }
  1747. func (s *aztestsSuite) TestBlobDownloadDataCountNegative(c *chk.C) {
  1748. bsu := getBSU()
  1749. containerURL, _ := createNewContainer(c, bsu)
  1750. defer deleteContainer(c, containerURL)
  1751. blobURL, _ := createNewBlockBlob(c, containerURL)
  1752. defer func() { // The library should panic if it sees numeric parameters that are guaranteed invalid
  1753. recover()
  1754. }()
  1755. blobURL.GetBlob(ctx, azblob.BlobRange{Count: -1}, azblob.BlobAccessConditions{}, false)
  1756. c.Fail()
  1757. }
  1758. func (s *aztestsSuite) TestBlobDownloadDataCountZero(c *chk.C) {
  1759. bsu := getBSU()
  1760. containerURL, _ := createNewContainer(c, bsu)
  1761. defer deleteContainer(c, containerURL)
  1762. blobURL, _ := createNewBlockBlob(c, containerURL)
  1763. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{Count: 0}, azblob.BlobAccessConditions{}, false)
  1764. c.Assert(err, chk.IsNil)
  1765. // Specifying a count of 0 results in the value being ignored
  1766. data, err := ioutil.ReadAll(resp.Response().Body)
  1767. c.Assert(err, chk.IsNil)
  1768. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1769. }
  1770. func (s *aztestsSuite) TestBlobDownloadDataCountExact(c *chk.C) {
  1771. bsu := getBSU()
  1772. containerURL, _ := createNewContainer(c, bsu)
  1773. defer deleteContainer(c, containerURL)
  1774. blobURL, _ := createNewBlockBlob(c, containerURL)
  1775. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{Count: int64(len(blockBlobDefaultData))}, azblob.BlobAccessConditions{}, false)
  1776. c.Assert(err, chk.IsNil)
  1777. data, err := ioutil.ReadAll(resp.Response().Body)
  1778. c.Assert(err, chk.IsNil)
  1779. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1780. }
  1781. func (s *aztestsSuite) TestBlobDownloadDataCountOutOfRange(c *chk.C) {
  1782. bsu := getBSU()
  1783. containerURL, _ := createNewContainer(c, bsu)
  1784. defer deleteContainer(c, containerURL)
  1785. blobURL, _ := createNewBlockBlob(c, containerURL)
  1786. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{Count: int64(len(blockBlobDefaultData)) * 2}, azblob.BlobAccessConditions{}, false)
  1787. c.Assert(err, chk.IsNil)
  1788. data, err := ioutil.ReadAll(resp.Response().Body)
  1789. c.Assert(err, chk.IsNil)
  1790. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1791. }
  1792. func (s *aztestsSuite) TestBlobDownloadDataEmptyRangeStruct(c *chk.C) {
  1793. bsu := getBSU()
  1794. containerURL, _ := createNewContainer(c, bsu)
  1795. defer deleteContainer(c, containerURL)
  1796. blobURL, _ := createNewBlockBlob(c, containerURL)
  1797. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  1798. c.Assert(err, chk.IsNil)
  1799. data, err := ioutil.ReadAll(resp.Response().Body)
  1800. c.Assert(err, chk.IsNil)
  1801. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  1802. }
  1803. func (s *aztestsSuite) TestBlobDownloadDataContentMD5(c *chk.C) {
  1804. bsu := getBSU()
  1805. containerURL, _ := createNewContainer(c, bsu)
  1806. defer deleteContainer(c, containerURL)
  1807. blobURL, _ := createNewBlockBlob(c, containerURL)
  1808. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{Offset: 10, Count: 3}, azblob.BlobAccessConditions{}, true)
  1809. c.Assert(err, chk.IsNil)
  1810. mdf := md5.Sum([]byte(blockBlobDefaultData)[10:13])
  1811. c.Assert(resp.ContentMD5(), chk.Equals, mdf)
  1812. }
  1813. func (s *aztestsSuite) TestBlobDownloadDataIfModifiedSinceTrue(c *chk.C) {
  1814. currentTime := getRelativeTimeGMT(-10)
  1815. bsu := getBSU()
  1816. containerURL, _ := createNewContainer(c, bsu)
  1817. defer deleteContainer(c, containerURL)
  1818. blobURL, _ := createNewBlockBlob(c, containerURL)
  1819. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1820. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}}, false)
  1821. c.Assert(err, chk.IsNil)
  1822. c.Assert(resp.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1823. }
  1824. func (s *aztestsSuite) TestBlobDownloadDataIfModifiedSinceFalse(c *chk.C) {
  1825. bsu := getBSU()
  1826. containerURL, _ := createNewContainer(c, bsu)
  1827. defer deleteContainer(c, containerURL)
  1828. blobURL, _ := createNewBlockBlob(c, containerURL)
  1829. currentTime := getRelativeTimeGMT(10)
  1830. _, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1831. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}}, false)
  1832. serr := err.(azblob.StorageError)
  1833. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // The server does not return the error in the body even though it is a GET
  1834. }
  1835. func (s *aztestsSuite) TestBlobDownloadDataIfUnmodifiedSinceTrue(c *chk.C) {
  1836. bsu := getBSU()
  1837. containerURL, _ := createNewContainer(c, bsu)
  1838. defer deleteContainer(c, containerURL)
  1839. blobURL, _ := createNewBlockBlob(c, containerURL)
  1840. currentTime := getRelativeTimeGMT(10)
  1841. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1842. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}}, false)
  1843. c.Assert(err, chk.IsNil)
  1844. c.Assert(resp.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1845. }
  1846. func (s *aztestsSuite) TestBlobDownloadDataIfUnmodifiedSinceFalse(c *chk.C) {
  1847. currentTime := getRelativeTimeGMT(-10)
  1848. bsu := getBSU()
  1849. containerURL, _ := createNewContainer(c, bsu)
  1850. defer deleteContainer(c, containerURL)
  1851. blobURL, _ := createNewBlockBlob(c, containerURL)
  1852. _, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1853. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}}, false)
  1854. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1855. }
  1856. func (s *aztestsSuite) TestBlobDownloadDataIfMatchTrue(c *chk.C) {
  1857. bsu := getBSU()
  1858. containerURL, _ := createNewContainer(c, bsu)
  1859. defer deleteContainer(c, containerURL)
  1860. blobURL, _ := createNewBlockBlob(c, containerURL)
  1861. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1862. c.Assert(err, chk.IsNil)
  1863. etag := resp.ETag()
  1864. resp2, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1865. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}}, false)
  1866. c.Assert(err, chk.IsNil)
  1867. c.Assert(resp2.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1868. }
  1869. func (s *aztestsSuite) TestBlobDownloadDataIfMatchFalse(c *chk.C) {
  1870. bsu := getBSU()
  1871. containerURL, _ := createNewContainer(c, bsu)
  1872. defer deleteContainer(c, containerURL)
  1873. blobURL, _ := createNewBlockBlob(c, containerURL)
  1874. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1875. c.Assert(err, chk.IsNil)
  1876. etag := resp.ETag()
  1877. blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  1878. _, err = blobURL.GetBlob(ctx, azblob.BlobRange{},
  1879. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}}, false)
  1880. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1881. }
  1882. func (s *aztestsSuite) TestBlobDownloadDataIfNoneMatchTrue(c *chk.C) {
  1883. bsu := getBSU()
  1884. containerURL, _ := createNewContainer(c, bsu)
  1885. defer deleteContainer(c, containerURL)
  1886. blobURL, _ := createNewBlockBlob(c, containerURL)
  1887. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1888. c.Assert(err, chk.IsNil)
  1889. etag := resp.ETag()
  1890. blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  1891. resp2, err := blobURL.GetBlob(ctx, azblob.BlobRange{},
  1892. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}}, false)
  1893. c.Assert(err, chk.IsNil)
  1894. c.Assert(resp2.ContentLength(), chk.Equals, int64(len(blockBlobDefaultData)))
  1895. }
  1896. func (s *aztestsSuite) TestBlobDownloadDataIfNoneMatchFalse(c *chk.C) {
  1897. bsu := getBSU()
  1898. containerURL, _ := createNewContainer(c, bsu)
  1899. defer deleteContainer(c, containerURL)
  1900. blobURL, _ := createNewBlockBlob(c, containerURL)
  1901. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1902. c.Assert(err, chk.IsNil)
  1903. etag := resp.ETag()
  1904. _, err = blobURL.GetBlob(ctx, azblob.BlobRange{},
  1905. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}}, false)
  1906. serr := err.(azblob.StorageError)
  1907. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // The server does not return the error in the body even though it is a GET
  1908. }
  1909. func (s *aztestsSuite) TestBlobDeleteNonExistant(c *chk.C) {
  1910. bsu := getBSU()
  1911. containerURL, _ := createNewContainer(c, bsu)
  1912. defer deleteContainer(c, containerURL)
  1913. blobURL, _ := getBlockBlobURL(c, containerURL)
  1914. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{})
  1915. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  1916. }
  1917. func (s *aztestsSuite) TestBlobDeleteSnapshot(c *chk.C) {
  1918. bsu := getBSU()
  1919. containerURL, _ := createNewContainer(c, bsu)
  1920. defer deleteContainer(c, containerURL)
  1921. blobURL, _ := createNewBlockBlob(c, containerURL)
  1922. resp, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1923. c.Assert(err, chk.IsNil)
  1924. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  1925. _, err = snapshotURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
  1926. c.Assert(err, chk.IsNil)
  1927. validateBlobDeleted(c, snapshotURL)
  1928. }
  1929. func (s *aztestsSuite) TestBlobDeleteSnapshotsInclude(c *chk.C) {
  1930. bsu := getBSU()
  1931. containerURL, _ := createNewContainer(c, bsu)
  1932. defer deleteContainer(c, containerURL)
  1933. blobURL, _ := createNewBlockBlob(c, containerURL)
  1934. _, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1935. c.Assert(err, chk.IsNil)
  1936. _, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{})
  1937. c.Assert(err, chk.IsNil)
  1938. resp, _ := containerURL.ListBlobs(ctx, azblob.Marker{},
  1939. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Snapshots: true}})
  1940. c.Assert(resp.Blobs.Blob, chk.HasLen, 0)
  1941. }
  1942. func (s *aztestsSuite) TestBlobDeleteSnapshotsOnly(c *chk.C) {
  1943. bsu := getBSU()
  1944. containerURL, _ := createNewContainer(c, bsu)
  1945. defer deleteContainer(c, containerURL)
  1946. blobURL, _ := createNewBlockBlob(c, containerURL)
  1947. _, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1948. c.Assert(err, chk.IsNil)
  1949. _, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionOnly, azblob.BlobAccessConditions{})
  1950. c.Assert(err, chk.IsNil)
  1951. resp, _ := containerURL.ListBlobs(ctx, azblob.Marker{},
  1952. azblob.ListBlobsOptions{Details: azblob.BlobListingDetails{Snapshots: true}})
  1953. c.Assert(resp.Blobs.Blob, chk.HasLen, 1)
  1954. c.Assert(resp.Blobs.Blob[0].Snapshot.IsZero(), chk.Equals, true)
  1955. }
  1956. func (s *aztestsSuite) TestBlobDeleteSnapshotsNoneWithSnapshots(c *chk.C) {
  1957. bsu := getBSU()
  1958. containerURL, _ := createNewContainer(c, bsu)
  1959. defer deleteContainer(c, containerURL)
  1960. blobURL, _ := createNewBlockBlob(c, containerURL)
  1961. _, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  1962. c.Assert(err, chk.IsNil)
  1963. _, err = blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
  1964. validateStorageError(c, err, azblob.ServiceCodeSnapshotsPresent)
  1965. }
  1966. func validateBlobDeleted(c *chk.C, blobURL azblob.BlockBlobURL) {
  1967. _, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  1968. c.Assert(err, chk.NotNil)
  1969. serr := err.(azblob.StorageError) // Delete blob is a HEAD request and does not return a ServiceCode in the body
  1970. c.Assert(serr.Response().StatusCode, chk.Equals, 404)
  1971. }
  1972. func (s *aztestsSuite) TestBlobDeleteIfModifiedSinceTrue(c *chk.C) {
  1973. currentTime := getRelativeTimeGMT(-10)
  1974. bsu := getBSU()
  1975. containerURL, _ := createNewContainer(c, bsu)
  1976. defer deleteContainer(c, containerURL)
  1977. blobURL, _ := createNewBlockBlob(c, containerURL)
  1978. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  1979. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1980. c.Assert(err, chk.IsNil)
  1981. validateBlobDeleted(c, blobURL)
  1982. }
  1983. func (s *aztestsSuite) TestBlobDeleteIfModifiedSinceFalse(c *chk.C) {
  1984. bsu := getBSU()
  1985. containerURL, _ := createNewContainer(c, bsu)
  1986. defer deleteContainer(c, containerURL)
  1987. blobURL, _ := createNewBlockBlob(c, containerURL)
  1988. currentTime := getRelativeTimeGMT(10)
  1989. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  1990. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  1991. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  1992. }
  1993. func (s *aztestsSuite) TestBlobDeleteIfUnmodifiedSinceTrue(c *chk.C) {
  1994. bsu := getBSU()
  1995. containerURL, _ := createNewContainer(c, bsu)
  1996. defer deleteContainer(c, containerURL)
  1997. blobURL, _ := createNewBlockBlob(c, containerURL)
  1998. currentTime := getRelativeTimeGMT(10)
  1999. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2000. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2001. c.Assert(err, chk.IsNil)
  2002. validateBlobDeleted(c, blobURL)
  2003. }
  2004. func (s *aztestsSuite) TestBlobDeleteIfUnmodifiedSinceFalse(c *chk.C) {
  2005. currentTime := getRelativeTimeGMT(-10)
  2006. bsu := getBSU()
  2007. containerURL, _ := createNewContainer(c, bsu)
  2008. defer deleteContainer(c, containerURL)
  2009. blobURL, _ := createNewBlockBlob(c, containerURL)
  2010. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2011. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2012. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2013. }
  2014. func (s *aztestsSuite) TestBlobDeleteIfMatchTrue(c *chk.C) {
  2015. bsu := getBSU()
  2016. containerURL, _ := createNewContainer(c, bsu)
  2017. defer deleteContainer(c, containerURL)
  2018. blobURL, _ := createNewBlockBlob(c, containerURL)
  2019. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2020. etag := resp.ETag()
  2021. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2022. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}})
  2023. c.Assert(err, chk.IsNil)
  2024. validateBlobDeleted(c, blobURL)
  2025. }
  2026. func (s *aztestsSuite) TestBlobDeleteIfMatchFalse(c *chk.C) {
  2027. bsu := getBSU()
  2028. containerURL, _ := createNewContainer(c, bsu)
  2029. defer deleteContainer(c, containerURL)
  2030. blobURL, _ := createNewBlockBlob(c, containerURL)
  2031. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2032. etag := resp.ETag()
  2033. blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  2034. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2035. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: etag}})
  2036. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2037. }
  2038. func (s *aztestsSuite) TestBlobDeleteIfNoneMatchTrue(c *chk.C) {
  2039. bsu := getBSU()
  2040. containerURL, _ := createNewContainer(c, bsu)
  2041. defer deleteContainer(c, containerURL)
  2042. blobURL, _ := createNewBlockBlob(c, containerURL)
  2043. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2044. etag := resp.ETag()
  2045. blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  2046. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2047. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}})
  2048. c.Assert(err, chk.IsNil)
  2049. validateBlobDeleted(c, blobURL)
  2050. }
  2051. func (s *aztestsSuite) TestBlobDeleteIfNoneMatchFalse(c *chk.C) {
  2052. bsu := getBSU()
  2053. containerURL, _ := createNewContainer(c, bsu)
  2054. defer deleteContainer(c, containerURL)
  2055. blobURL, _ := createNewBlockBlob(c, containerURL)
  2056. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2057. etag := resp.ETag()
  2058. _, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone,
  2059. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: etag}})
  2060. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2061. }
  2062. func (s *aztestsSuite) TestBlobPutBlobNonEmptyBody(c *chk.C) {
  2063. bsu := getBSU()
  2064. containerURL, _ := createNewContainer(c, bsu)
  2065. defer deleteContainer(c, containerURL)
  2066. blobURL, _ := getBlockBlobURL(c, containerURL)
  2067. _, err := blobURL.PutBlob(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2068. c.Assert(err, chk.IsNil)
  2069. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  2070. c.Assert(err, chk.IsNil)
  2071. data, _ := ioutil.ReadAll(resp.Response().Body)
  2072. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  2073. }
  2074. func (s *aztestsSuite) TestBlobPutBlobHTTPHeaders(c *chk.C) {
  2075. bsu := getBSU()
  2076. containerURL, _ := createNewContainer(c, bsu)
  2077. defer deleteContainer(c, containerURL)
  2078. blobURL, _ := getBlockBlobURL(c, containerURL)
  2079. _, err := blobURL.PutBlob(ctx, nil, basicHeaders, nil, azblob.BlobAccessConditions{})
  2080. c.Assert(err, chk.IsNil)
  2081. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2082. c.Assert(err, chk.IsNil)
  2083. h := resp.NewHTTPHeaders()
  2084. c.Assert(h, chk.DeepEquals, basicHeaders)
  2085. }
  2086. func (s *aztestsSuite) TestBlobPutBlobMetadataNotEmpty(c *chk.C) {
  2087. bsu := getBSU()
  2088. containerURL, _ := createNewContainer(c, bsu)
  2089. defer deleteContainer(c, containerURL)
  2090. blobURL, _ := getBlockBlobURL(c, containerURL)
  2091. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, basicMetadata, azblob.BlobAccessConditions{})
  2092. c.Assert(err, chk.IsNil)
  2093. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2094. c.Assert(err, chk.IsNil)
  2095. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2096. }
  2097. func (s *aztestsSuite) TestBlobPutBlobMetadataEmpty(c *chk.C) {
  2098. bsu := getBSU()
  2099. containerURL, _ := createNewContainer(c, bsu)
  2100. defer deleteContainer(c, containerURL)
  2101. blobURL, _ := getBlockBlobURL(c, containerURL)
  2102. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  2103. c.Assert(err, chk.IsNil)
  2104. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2105. c.Assert(err, chk.IsNil)
  2106. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  2107. }
  2108. func (s *aztestsSuite) TestBlobPutBlobMetadataInvalid(c *chk.C) {
  2109. bsu := getBSU()
  2110. containerURL, _ := createNewContainer(c, bsu)
  2111. defer deleteContainer(c, containerURL)
  2112. blobURL, _ := getBlockBlobURL(c, containerURL)
  2113. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, azblob.Metadata{"In valid!": "bar"}, azblob.BlobAccessConditions{})
  2114. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  2115. }
  2116. func validatePutBlob(c *chk.C, blobURL azblob.BlockBlobURL) {
  2117. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  2118. c.Assert(err, chk.IsNil)
  2119. data, _ := ioutil.ReadAll(resp.Response().Body)
  2120. c.Assert(data, chk.HasLen, 0)
  2121. }
  2122. func (s *aztestsSuite) TestBlobPutBlobIfModifiedSinceTrue(c *chk.C) {
  2123. bsu := getBSU()
  2124. containerURL, _ := createNewContainer(c, bsu)
  2125. defer deleteContainer(c, containerURL)
  2126. blobURL, _ := createNewBlockBlob(c, containerURL)
  2127. currentTime := getRelativeTimeGMT(-10)
  2128. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2129. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2130. c.Assert(err, chk.IsNil)
  2131. validatePutBlob(c, blobURL)
  2132. }
  2133. func (s *aztestsSuite) TestBlobPutBlobIfModifiedSinceFalse(c *chk.C) {
  2134. bsu := getBSU()
  2135. containerURL, _ := createNewContainer(c, bsu)
  2136. defer deleteContainer(c, containerURL)
  2137. blobURL, _ := createNewBlockBlob(c, containerURL)
  2138. currentTime := getRelativeTimeGMT(10)
  2139. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2140. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2141. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2142. }
  2143. func (s *aztestsSuite) TestBlobPutBlobIfUnmodifiedSinceTrue(c *chk.C) {
  2144. bsu := getBSU()
  2145. containerURL, _ := createNewContainer(c, bsu)
  2146. defer deleteContainer(c, containerURL)
  2147. blobURL, _ := createNewBlockBlob(c, containerURL)
  2148. currentTime := getRelativeTimeGMT(10)
  2149. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2150. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2151. c.Assert(err, chk.IsNil)
  2152. validatePutBlob(c, blobURL)
  2153. }
  2154. func (s *aztestsSuite) TestBlobPutBlobIfUnmodifiedSinceFalse(c *chk.C) {
  2155. bsu := getBSU()
  2156. containerURL, _ := createNewContainer(c, bsu)
  2157. defer deleteContainer(c, containerURL)
  2158. blobURL, _ := createNewBlockBlob(c, containerURL)
  2159. currentTime := getRelativeTimeGMT(-10)
  2160. _, err := blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2161. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2162. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2163. }
  2164. func (s *aztestsSuite) TestBlobPutBlobIfMatchTrue(c *chk.C) {
  2165. bsu := getBSU()
  2166. containerURL, _ := createNewContainer(c, bsu)
  2167. defer deleteContainer(c, containerURL)
  2168. blobURL, _ := createNewBlockBlob(c, containerURL)
  2169. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2170. c.Assert(err, chk.IsNil)
  2171. _, err = blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2172. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2173. c.Assert(err, chk.IsNil)
  2174. validatePutBlob(c, blobURL)
  2175. }
  2176. func (s *aztestsSuite) TestBlobPutBlobIfMatchFalse(c *chk.C) {
  2177. bsu := getBSU()
  2178. containerURL, _ := createNewContainer(c, bsu)
  2179. defer deleteContainer(c, containerURL)
  2180. blobURL, _ := createNewBlockBlob(c, containerURL)
  2181. _, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2182. c.Assert(err, chk.IsNil)
  2183. _, err = blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2184. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2185. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2186. }
  2187. func (s *aztestsSuite) TestBlobPutBlobIfNoneMatchTrue(c *chk.C) {
  2188. bsu := getBSU()
  2189. containerURL, _ := createNewContainer(c, bsu)
  2190. defer deleteContainer(c, containerURL)
  2191. blobURL, _ := createNewBlockBlob(c, containerURL)
  2192. _, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2193. c.Assert(err, chk.IsNil)
  2194. _, err = blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2195. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2196. c.Assert(err, chk.IsNil)
  2197. validatePutBlob(c, blobURL)
  2198. }
  2199. func (s *aztestsSuite) TestBlobPutBlobIfNoneMatchFalse(c *chk.C) {
  2200. bsu := getBSU()
  2201. containerURL, _ := createNewContainer(c, bsu)
  2202. defer deleteContainer(c, containerURL)
  2203. blobURL, _ := createNewBlockBlob(c, containerURL)
  2204. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2205. c.Assert(err, chk.IsNil)
  2206. _, err = blobURL.PutBlob(ctx, nil, azblob.BlobHTTPHeaders{}, nil,
  2207. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2208. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2209. }
  2210. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfModifiedSinceTrue(c *chk.C) {
  2211. currentTime := getRelativeTimeGMT(-10)
  2212. bsu := getBSU()
  2213. containerURL, _ := createNewContainer(c, bsu)
  2214. defer deleteContainer(c, containerURL)
  2215. blobURL, _ := createNewBlockBlob(c, containerURL)
  2216. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2217. c.Assert(err, chk.IsNil)
  2218. resp, err := blobURL.GetPropertiesAndMetadata(ctx,
  2219. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2220. c.Assert(err, chk.IsNil)
  2221. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2222. }
  2223. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfModifiedSinceFalse(c *chk.C) {
  2224. bsu := getBSU()
  2225. containerURL, _ := createNewContainer(c, bsu)
  2226. defer deleteContainer(c, containerURL)
  2227. blobURL, _ := createNewBlockBlob(c, containerURL)
  2228. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2229. c.Assert(err, chk.IsNil)
  2230. currentTime := getRelativeTimeGMT(10)
  2231. _, err = blobURL.GetPropertiesAndMetadata(ctx,
  2232. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2233. c.Assert(err, chk.NotNil)
  2234. serr := err.(azblob.StorageError)
  2235. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // No service code returned for a HEAD
  2236. }
  2237. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfUnmodifiedSinceTrue(c *chk.C) {
  2238. bsu := getBSU()
  2239. containerURL, _ := createNewContainer(c, bsu)
  2240. defer deleteContainer(c, containerURL)
  2241. blobURL, _ := createNewBlockBlob(c, containerURL)
  2242. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2243. c.Assert(err, chk.IsNil)
  2244. currentTime := getRelativeTimeGMT(10)
  2245. resp, err := blobURL.GetPropertiesAndMetadata(ctx,
  2246. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2247. c.Assert(err, chk.IsNil)
  2248. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2249. }
  2250. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfUnmodifiedSinceFalse(c *chk.C) {
  2251. bsu := getBSU()
  2252. containerURL, _ := createNewContainer(c, bsu)
  2253. defer deleteContainer(c, containerURL)
  2254. blobURL, _ := createNewBlockBlob(c, containerURL)
  2255. currentTime := getRelativeTimeGMT(-10)
  2256. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2257. c.Assert(err, chk.IsNil)
  2258. _, err = blobURL.GetPropertiesAndMetadata(ctx,
  2259. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2260. c.Assert(err, chk.NotNil)
  2261. serr := err.(azblob.StorageError)
  2262. c.Assert(serr.Response().StatusCode, chk.Equals, 412)
  2263. }
  2264. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfMatchTrue(c *chk.C) {
  2265. bsu := getBSU()
  2266. containerURL, _ := createNewContainer(c, bsu)
  2267. defer deleteContainer(c, containerURL)
  2268. blobURL, _ := createNewBlockBlob(c, containerURL)
  2269. resp, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2270. c.Assert(err, chk.IsNil)
  2271. resp2, err := blobURL.GetPropertiesAndMetadata(ctx,
  2272. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2273. c.Assert(err, chk.IsNil)
  2274. c.Assert(resp2.NewMetadata(), chk.DeepEquals, basicMetadata)
  2275. }
  2276. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfMatchFalse(c *chk.C) {
  2277. bsu := getBSU()
  2278. containerURL, _ := createNewContainer(c, bsu)
  2279. defer deleteContainer(c, containerURL)
  2280. blobURL, _ := createNewBlockBlob(c, containerURL)
  2281. _, err := blobURL.GetPropertiesAndMetadata(ctx,
  2282. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2283. c.Assert(err, chk.NotNil)
  2284. serr := err.(azblob.StorageError)
  2285. c.Assert(serr.Response().StatusCode, chk.Equals, 412)
  2286. }
  2287. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfNoneMatchTrue(c *chk.C) {
  2288. bsu := getBSU()
  2289. containerURL, _ := createNewContainer(c, bsu)
  2290. defer deleteContainer(c, containerURL)
  2291. blobURL, _ := createNewBlockBlob(c, containerURL)
  2292. _, err := blobURL.SetMetadata(ctx, basicMetadata, azblob.BlobAccessConditions{})
  2293. c.Assert(err, chk.IsNil)
  2294. resp, err := blobURL.GetPropertiesAndMetadata(ctx,
  2295. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2296. c.Assert(err, chk.IsNil)
  2297. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2298. }
  2299. func (s *aztestsSuite) TestBlobGetPropsAndMetadataIfNoneMatchFalse(c *chk.C) {
  2300. bsu := getBSU()
  2301. containerURL, _ := createNewContainer(c, bsu)
  2302. defer deleteContainer(c, containerURL)
  2303. blobURL, _ := createNewBlockBlob(c, containerURL)
  2304. resp, err := blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  2305. c.Assert(err, chk.IsNil)
  2306. _, err = blobURL.GetPropertiesAndMetadata(ctx,
  2307. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2308. c.Assert(err, chk.NotNil)
  2309. serr := err.(azblob.StorageError)
  2310. c.Assert(serr.Response().StatusCode, chk.Equals, 304)
  2311. }
  2312. func (s *aztestsSuite) TestBlobSetPropertiesBasic(c *chk.C) {
  2313. bsu := getBSU()
  2314. containerURL, _ := createNewContainer(c, bsu)
  2315. defer deleteContainer(c, containerURL)
  2316. blobURL, _ := createNewBlockBlob(c, containerURL)
  2317. _, err := blobURL.SetProperties(ctx, basicHeaders, azblob.BlobAccessConditions{})
  2318. c.Assert(err, chk.IsNil)
  2319. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2320. h := resp.NewHTTPHeaders()
  2321. c.Assert(h, chk.DeepEquals, basicHeaders)
  2322. }
  2323. func (s *aztestsSuite) TestBlobSetPropertiesEmptyValue(c *chk.C) {
  2324. bsu := getBSU()
  2325. containerURL, _ := createNewContainer(c, bsu)
  2326. defer deleteContainer(c, containerURL)
  2327. blobURL, _ := createNewBlockBlob(c, containerURL)
  2328. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentType: "my_type"}, azblob.BlobAccessConditions{})
  2329. c.Assert(err, chk.IsNil)
  2330. _, err = blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  2331. c.Assert(err, chk.IsNil)
  2332. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2333. c.Assert(err, chk.IsNil)
  2334. c.Assert(resp.ContentType(), chk.Equals, "")
  2335. }
  2336. func validatePropertiesSet(c *chk.C, blobURL azblob.BlockBlobURL, str string) {
  2337. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2338. c.Assert(err, chk.IsNil)
  2339. c.Assert(resp.ContentDisposition(), chk.Equals, "my_disposition")
  2340. }
  2341. func (s *aztestsSuite) TestBlobSetPropertiesIfModifiedSinceTrue(c *chk.C) {
  2342. currentTime := getRelativeTimeGMT(-10)
  2343. bsu := getBSU()
  2344. containerURL, _ := createNewContainer(c, bsu)
  2345. defer deleteContainer(c, containerURL)
  2346. blobURL, _ := createNewBlockBlob(c, containerURL)
  2347. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2348. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2349. c.Assert(err, chk.IsNil)
  2350. validatePropertiesSet(c, blobURL, "my_disposition")
  2351. }
  2352. func (s *aztestsSuite) TestBlobSetPropertiesIfModifiedSinceFalse(c *chk.C) {
  2353. bsu := getBSU()
  2354. containerURL, _ := createNewContainer(c, bsu)
  2355. defer deleteContainer(c, containerURL)
  2356. blobURL, _ := createNewBlockBlob(c, containerURL)
  2357. currentTime := getRelativeTimeGMT(10)
  2358. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2359. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2360. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2361. }
  2362. func (s *aztestsSuite) TestBlobSetPropertiesIfUnmodifiedSinceTrue(c *chk.C) {
  2363. bsu := getBSU()
  2364. containerURL, _ := createNewContainer(c, bsu)
  2365. defer deleteContainer(c, containerURL)
  2366. blobURL, _ := createNewBlockBlob(c, containerURL)
  2367. currentTime := getRelativeTimeGMT(10)
  2368. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2369. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2370. c.Assert(err, chk.IsNil)
  2371. validatePropertiesSet(c, blobURL, "my_disposition")
  2372. }
  2373. func (s *aztestsSuite) TestBlobSetPropertiesIfUnmodifiedSinceFalse(c *chk.C) {
  2374. currentTime := getRelativeTimeGMT(-10)
  2375. bsu := getBSU()
  2376. containerURL, _ := createNewContainer(c, bsu)
  2377. defer deleteContainer(c, containerURL)
  2378. blobURL, _ := createNewBlockBlob(c, containerURL)
  2379. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2380. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2381. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2382. }
  2383. func (s *aztestsSuite) TestBlobSetPropertiesIfMatchTrue(c *chk.C) {
  2384. bsu := getBSU()
  2385. containerURL, _ := createNewContainer(c, bsu)
  2386. defer deleteContainer(c, containerURL)
  2387. blobURL, _ := createNewBlockBlob(c, containerURL)
  2388. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2389. c.Assert(err, chk.IsNil)
  2390. _, err = blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2391. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2392. c.Assert(err, chk.IsNil)
  2393. validatePropertiesSet(c, blobURL, "my_disposition")
  2394. }
  2395. func (s *aztestsSuite) TestBlobSetPropertiesIfMatchFalse(c *chk.C) {
  2396. bsu := getBSU()
  2397. containerURL, _ := createNewContainer(c, bsu)
  2398. defer deleteContainer(c, containerURL)
  2399. blobURL, _ := createNewBlockBlob(c, containerURL)
  2400. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2401. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2402. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2403. }
  2404. func (s *aztestsSuite) TestBlobSetPropertiesIfNoneMatchTrue(c *chk.C) {
  2405. bsu := getBSU()
  2406. containerURL, _ := createNewContainer(c, bsu)
  2407. defer deleteContainer(c, containerURL)
  2408. blobURL, _ := createNewBlockBlob(c, containerURL)
  2409. _, err := blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2410. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2411. c.Assert(err, chk.IsNil)
  2412. validatePropertiesSet(c, blobURL, "my_disposition")
  2413. }
  2414. func (s *aztestsSuite) TestBlobSetPropertiesIfNoneMatchFalse(c *chk.C) {
  2415. bsu := getBSU()
  2416. containerURL, _ := createNewContainer(c, bsu)
  2417. defer deleteContainer(c, containerURL)
  2418. blobURL, _ := createNewBlockBlob(c, containerURL)
  2419. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2420. c.Assert(err, chk.IsNil)
  2421. _, err = blobURL.SetProperties(ctx, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"},
  2422. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2423. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2424. }
  2425. func (s *aztestsSuite) TestBlobSetMetadataNil(c *chk.C) {
  2426. bsu := getBSU()
  2427. containerURL, _ := createNewContainer(c, bsu)
  2428. defer deleteContainer(c, containerURL)
  2429. blobURL, _ := createNewBlockBlob(c, containerURL)
  2430. _, err := blobURL.SetMetadata(ctx, azblob.Metadata{"not": "nil"}, azblob.BlobAccessConditions{})
  2431. c.Assert(err, chk.IsNil)
  2432. _, err = blobURL.SetMetadata(ctx, nil, azblob.BlobAccessConditions{})
  2433. c.Assert(err, chk.IsNil)
  2434. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2435. c.Assert(err, chk.IsNil)
  2436. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  2437. }
  2438. func (s *aztestsSuite) TestBlobSetMetadataEmpty(c *chk.C) {
  2439. bsu := getBSU()
  2440. containerURL, _ := createNewContainer(c, bsu)
  2441. defer deleteContainer(c, containerURL)
  2442. blobURL, _ := createNewBlockBlob(c, containerURL)
  2443. _, err := blobURL.SetMetadata(ctx, azblob.Metadata{"not": "nil"}, azblob.BlobAccessConditions{})
  2444. c.Assert(err, chk.IsNil)
  2445. _, err = blobURL.SetMetadata(ctx, azblob.Metadata{}, azblob.BlobAccessConditions{})
  2446. c.Assert(err, chk.IsNil)
  2447. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2448. c.Assert(err, chk.IsNil)
  2449. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  2450. }
  2451. func (s *aztestsSuite) TestBlobSetMetadataInvalidField(c *chk.C) {
  2452. bsu := getBSU()
  2453. containerURL, _ := createNewContainer(c, bsu)
  2454. defer deleteContainer(c, containerURL)
  2455. blobURL, _ := createNewBlockBlob(c, containerURL)
  2456. _, err := blobURL.SetMetadata(ctx, azblob.Metadata{"Invalid field!": "value"}, azblob.BlobAccessConditions{})
  2457. c.Assert(err, chk.NotNil)
  2458. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  2459. }
  2460. func validateMetadataSet(c *chk.C, blobURL azblob.BlockBlobURL) {
  2461. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2462. c.Assert(err, chk.IsNil)
  2463. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2464. }
  2465. func (s *aztestsSuite) TestBlobSetMetadataIfModifiedSinceTrue(c *chk.C) {
  2466. currentTime := getRelativeTimeGMT(-10)
  2467. bsu := getBSU()
  2468. containerURL, _ := createNewContainer(c, bsu)
  2469. defer deleteContainer(c, containerURL)
  2470. blobURL, _ := createNewBlockBlob(c, containerURL)
  2471. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2472. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2473. c.Assert(err, chk.IsNil)
  2474. validateMetadataSet(c, blobURL)
  2475. }
  2476. func (s *aztestsSuite) TestBlobSetMetadataIfModifiedSinceFalse(c *chk.C) {
  2477. bsu := getBSU()
  2478. containerURL, _ := createNewContainer(c, bsu)
  2479. defer deleteContainer(c, containerURL)
  2480. blobURL, _ := createNewBlockBlob(c, containerURL)
  2481. currentTime := getRelativeTimeGMT(10)
  2482. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2483. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2484. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2485. }
  2486. func (s *aztestsSuite) TestBlobSetMetadataIfUnmodifiedSinceTrue(c *chk.C) {
  2487. bsu := getBSU()
  2488. containerURL, _ := createNewContainer(c, bsu)
  2489. defer deleteContainer(c, containerURL)
  2490. blobURL, _ := createNewBlockBlob(c, containerURL)
  2491. currentTime := getRelativeTimeGMT(10)
  2492. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2493. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2494. c.Assert(err, chk.IsNil)
  2495. validateMetadataSet(c, blobURL)
  2496. }
  2497. func (s *aztestsSuite) TestBlobSetMetadataIfUnmodifiedSinceFalse(c *chk.C) {
  2498. currentTime := getRelativeTimeGMT(-10)
  2499. bsu := getBSU()
  2500. containerURL, _ := createNewContainer(c, bsu)
  2501. defer deleteContainer(c, containerURL)
  2502. blobURL, _ := createNewBlockBlob(c, containerURL)
  2503. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2504. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2505. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2506. }
  2507. func (s *aztestsSuite) TestBlobSetMetadataIfMatchTrue(c *chk.C) {
  2508. bsu := getBSU()
  2509. containerURL, _ := createNewContainer(c, bsu)
  2510. defer deleteContainer(c, containerURL)
  2511. blobURL, _ := createNewBlockBlob(c, containerURL)
  2512. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2513. c.Assert(err, chk.IsNil)
  2514. _, err = blobURL.SetMetadata(ctx, basicMetadata,
  2515. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2516. c.Assert(err, chk.IsNil)
  2517. validateMetadataSet(c, blobURL)
  2518. }
  2519. func (s *aztestsSuite) TestBlobSetMetadataIfMatchFalse(c *chk.C) {
  2520. bsu := getBSU()
  2521. containerURL, _ := createNewContainer(c, bsu)
  2522. defer deleteContainer(c, containerURL)
  2523. blobURL, _ := createNewBlockBlob(c, containerURL)
  2524. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2525. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2526. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2527. }
  2528. func (s *aztestsSuite) TestBlobSetMetadataIfNoneMatchTrue(c *chk.C) {
  2529. bsu := getBSU()
  2530. containerURL, _ := createNewContainer(c, bsu)
  2531. defer deleteContainer(c, containerURL)
  2532. blobURL, _ := createNewBlockBlob(c, containerURL)
  2533. _, err := blobURL.SetMetadata(ctx, basicMetadata,
  2534. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2535. c.Assert(err, chk.IsNil)
  2536. validateMetadataSet(c, blobURL)
  2537. }
  2538. func (s *aztestsSuite) TestBlobSetMetadataIfNoneMatchFalse(c *chk.C) {
  2539. bsu := getBSU()
  2540. containerURL, _ := createNewContainer(c, bsu)
  2541. defer deleteContainer(c, containerURL)
  2542. blobURL, _ := createNewBlockBlob(c, containerURL)
  2543. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2544. c.Assert(err, chk.IsNil)
  2545. _, err = blobURL.SetMetadata(ctx, basicMetadata,
  2546. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2547. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2548. }
  2549. func (s *aztestsSuite) TestBlobGetBlockListNone(c *chk.C) {
  2550. bsu := getBSU()
  2551. containerURL, _ := createNewContainer(c, bsu)
  2552. defer deleteContainer(c, containerURL)
  2553. blobURL, _ := getBlockBlobURL(c, containerURL)
  2554. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2555. c.Assert(err, chk.IsNil)
  2556. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListNone, azblob.LeaseAccessConditions{})
  2557. c.Assert(err, chk.IsNil)
  2558. c.Assert(resp.CommittedBlocks, chk.HasLen, 0)
  2559. c.Assert(resp.UncommittedBlocks, chk.HasLen, 0) // Not specifying a block list type should default to only returning committed blocks
  2560. }
  2561. func (s *aztestsSuite) TestBlobGetBlockListUncommitted(c *chk.C) {
  2562. bsu := getBSU()
  2563. containerURL, _ := createNewContainer(c, bsu)
  2564. defer deleteContainer(c, containerURL)
  2565. blobURL, _ := getBlockBlobURL(c, containerURL)
  2566. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2567. c.Assert(err, chk.IsNil)
  2568. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListUncommitted, azblob.LeaseAccessConditions{})
  2569. c.Assert(err, chk.IsNil)
  2570. c.Assert(resp.CommittedBlocks, chk.HasLen, 0)
  2571. c.Assert(resp.UncommittedBlocks, chk.HasLen, 1)
  2572. }
  2573. func (s *aztestsSuite) TestBlobGetBlockListCommitted(c *chk.C) {
  2574. bsu := getBSU()
  2575. containerURL, _ := createNewContainer(c, bsu)
  2576. defer deleteContainer(c, containerURL)
  2577. blobURL, _ := getBlockBlobURL(c, containerURL)
  2578. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2579. c.Assert(err, chk.IsNil)
  2580. _, err = blobURL.PutBlockList(ctx, []string{"0000"}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2581. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListCommitted, azblob.LeaseAccessConditions{})
  2582. c.Assert(err, chk.IsNil)
  2583. c.Assert(resp.CommittedBlocks, chk.HasLen, 1)
  2584. c.Assert(resp.UncommittedBlocks, chk.HasLen, 0)
  2585. }
  2586. func (s *aztestsSuite) TestBlobGetBlockListCommittedEmpty(c *chk.C) {
  2587. bsu := getBSU()
  2588. containerURL, _ := createNewContainer(c, bsu)
  2589. defer deleteContainer(c, containerURL)
  2590. blobURL, _ := getBlockBlobURL(c, containerURL)
  2591. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2592. c.Assert(err, chk.IsNil)
  2593. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListCommitted, azblob.LeaseAccessConditions{})
  2594. c.Assert(err, chk.IsNil)
  2595. c.Assert(resp.CommittedBlocks, chk.HasLen, 0)
  2596. c.Assert(resp.UncommittedBlocks, chk.HasLen, 0)
  2597. }
  2598. func (s *aztestsSuite) TestBlobGetBlockListBothEmpty(c *chk.C) {
  2599. bsu := getBSU()
  2600. containerURL, _ := createNewContainer(c, bsu)
  2601. defer deleteContainer(c, containerURL)
  2602. blobURL, _ := getBlockBlobURL(c, containerURL)
  2603. _, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2604. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  2605. }
  2606. func (s *aztestsSuite) TestBlobGetBlockListBothNotEmpty(c *chk.C) {
  2607. bsu := getBSU()
  2608. containerURL, _ := createNewContainer(c, bsu)
  2609. defer deleteContainer(c, containerURL)
  2610. blobURL, _ := getBlockBlobURL(c, containerURL)
  2611. // Put and commit two blocks
  2612. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2613. c.Assert(err, chk.IsNil)
  2614. _, err = blobURL.PutBlock(ctx, "0001", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2615. c.Assert(err, chk.IsNil)
  2616. _, err = blobURL.PutBlockList(ctx, []string{"0001", "0000"}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2617. c.Assert(err, chk.IsNil)
  2618. // Put two uncommitted blocks
  2619. _, err = blobURL.PutBlock(ctx, "0003", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2620. c.Assert(err, chk.IsNil)
  2621. _, err = blobURL.PutBlock(ctx, "0002", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2622. c.Assert(err, chk.IsNil)
  2623. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2624. c.Assert(err, chk.IsNil)
  2625. c.Assert(resp.CommittedBlocks[0].Name, chk.Equals, "0001")
  2626. c.Assert(resp.CommittedBlocks[1].Name, chk.Equals, "0000") // Committed blocks are returned in the order they are committed (in the commit list)
  2627. c.Assert(resp.UncommittedBlocks[0].Name, chk.Equals, "0002") // Uncommitted blocks are returned in alphabetical order
  2628. c.Assert(resp.UncommittedBlocks[1].Name, chk.Equals, "0003")
  2629. }
  2630. func (s *aztestsSuite) TestBlobGetBlockListInvalidType(c *chk.C) {
  2631. bsu := getBSU()
  2632. containerURL, _ := createNewContainer(c, bsu)
  2633. defer deleteContainer(c, containerURL)
  2634. blobURL, _ := getBlockBlobURL(c, containerURL)
  2635. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2636. c.Assert(err, chk.IsNil)
  2637. _, err = blobURL.GetBlockList(ctx, azblob.BlockListType("garbage"), azblob.LeaseAccessConditions{})
  2638. validateStorageError(c, err, azblob.ServiceCodeInvalidQueryParameterValue)
  2639. }
  2640. func (s *aztestsSuite) TestBlobGetBlockListSnapshot(c *chk.C) {
  2641. bsu := getBSU()
  2642. containerURL, _ := createNewContainer(c, bsu)
  2643. defer deleteContainer(c, containerURL)
  2644. blobURL, _ := getBlockBlobURL(c, containerURL)
  2645. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2646. c.Assert(err, chk.IsNil)
  2647. _, err = blobURL.PutBlockList(ctx, []string{"0000"}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2648. c.Assert(err, chk.IsNil)
  2649. resp, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  2650. c.Assert(err, chk.IsNil)
  2651. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  2652. resp2, err := snapshotURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2653. c.Assert(err, chk.IsNil)
  2654. c.Assert(resp2.CommittedBlocks, chk.HasLen, 1)
  2655. }
  2656. func (s *aztestsSuite) TestBlobPutBlockIDInvalidCharacters(c *chk.C) {
  2657. bsu := getBSU()
  2658. containerURL, _ := createNewContainer(c, bsu)
  2659. defer deleteContainer(c, containerURL)
  2660. blobURL, _ := getBlockBlobURL(c, containerURL)
  2661. _, err := blobURL.PutBlock(ctx, "!!", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2662. validateStorageError(c, err, azblob.ServiceCodeInvalidQueryParameterValue)
  2663. }
  2664. func (s *aztestsSuite) TestBlobPutBlockIDInvalidLength(c *chk.C) {
  2665. bsu := getBSU()
  2666. containerURL, _ := createNewContainer(c, bsu)
  2667. defer deleteContainer(c, containerURL)
  2668. blobURL, _ := getBlockBlobURL(c, containerURL)
  2669. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2670. c.Assert(err, chk.IsNil)
  2671. _, err = blobURL.PutBlock(ctx, "00000000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2672. validateStorageError(c, err, azblob.ServiceCodeInvalidBlobOrBlock)
  2673. }
  2674. func (s *aztestsSuite) TestBlobPutBlockEmptyBody(c *chk.C) {
  2675. bsu := getBSU()
  2676. containerURL, _ := createNewContainer(c, bsu)
  2677. defer deleteContainer(c, containerURL)
  2678. blobURL, _ := getBlockBlobURL(c, containerURL)
  2679. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(""), azblob.LeaseAccessConditions{})
  2680. validateStorageError(c, err, azblob.ServiceCodeMissingContentLengthHeader) // The server does not accept getReaderToRandomBytes length = 0
  2681. }
  2682. func setupPutBlockListTest(c *chk.C) (containerURL azblob.ContainerURL, blobURL azblob.BlockBlobURL, id string) {
  2683. bsu := getBSU()
  2684. containerURL, _ = createNewContainer(c, bsu)
  2685. blobURL, _ = getBlockBlobURL(c, containerURL)
  2686. _, err := blobURL.PutBlock(ctx, "0000", strings.NewReader(blockBlobDefaultData), azblob.LeaseAccessConditions{})
  2687. c.Assert(err, chk.IsNil)
  2688. id = "0000"
  2689. return
  2690. }
  2691. func (s *aztestsSuite) TestBlobPutBlockListInvalidID(c *chk.C) {
  2692. containerURL, blobURL, id := setupPutBlockListTest(c)
  2693. defer deleteContainer(c, containerURL)
  2694. _, err := blobURL.PutBlockList(ctx, []string{id[:2]}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2695. validateStorageError(c, err, azblob.ServiceCodeInvalidBlockID)
  2696. }
  2697. func (s *aztestsSuite) TestBlobPutBlockListDuplicateBlocks(c *chk.C) {
  2698. containerURL, blobURL, id := setupPutBlockListTest(c)
  2699. defer deleteContainer(c, containerURL)
  2700. _, err := blobURL.PutBlockList(ctx, []string{id, id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2701. c.Assert(err, chk.IsNil)
  2702. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2703. c.Assert(err, chk.IsNil)
  2704. c.Assert(resp.CommittedBlocks, chk.HasLen, 2)
  2705. }
  2706. func (s *aztestsSuite) TestBlobPutBlockListEmptyList(c *chk.C) {
  2707. containerURL, blobURL, _ := setupPutBlockListTest(c)
  2708. defer deleteContainer(c, containerURL)
  2709. _, err := blobURL.PutBlockList(ctx, []string{}, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  2710. c.Assert(err, chk.IsNil)
  2711. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2712. c.Assert(err, chk.IsNil)
  2713. c.Assert(resp.CommittedBlocks, chk.HasLen, 0)
  2714. }
  2715. func (s *aztestsSuite) TestBlobPutBlockListMetadataEmpty(c *chk.C) {
  2716. containerURL, blobURL, id := setupPutBlockListTest(c)
  2717. defer deleteContainer(c, containerURL)
  2718. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  2719. c.Assert(err, chk.IsNil)
  2720. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2721. c.Assert(err, chk.IsNil)
  2722. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  2723. }
  2724. func (s *aztestsSuite) TestBlobPutBlockListMetadataNonEmpty(c *chk.C) {
  2725. containerURL, blobURL, id := setupPutBlockListTest(c)
  2726. defer deleteContainer(c, containerURL)
  2727. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, basicMetadata, azblob.BlobAccessConditions{})
  2728. c.Assert(err, chk.IsNil)
  2729. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2730. c.Assert(err, chk.IsNil)
  2731. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2732. }
  2733. func (s *aztestsSuite) TestBlobPutBlockListHTTPHeaders(c *chk.C) {
  2734. containerURL, blobURL, id := setupPutBlockListTest(c)
  2735. defer deleteContainer(c, containerURL)
  2736. _, err := blobURL.PutBlockList(ctx, []string{id}, basicHeaders, nil, azblob.BlobAccessConditions{})
  2737. c.Assert(err, chk.IsNil)
  2738. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2739. h := resp.NewHTTPHeaders()
  2740. c.Assert(h, chk.DeepEquals, basicHeaders)
  2741. }
  2742. func (s *aztestsSuite) TestBlobPutBlockListHTTPHeadersEmpty(c *chk.C) {
  2743. containerURL, blobURL, id := setupPutBlockListTest(c)
  2744. defer deleteContainer(c, containerURL)
  2745. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{ContentDisposition: "my_disposition"}, nil, azblob.BlobAccessConditions{})
  2746. c.Assert(err, chk.IsNil)
  2747. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  2748. c.Assert(err, chk.IsNil)
  2749. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2750. c.Assert(err, chk.IsNil)
  2751. c.Assert(resp.ContentDisposition(), chk.Equals, "")
  2752. }
  2753. func validateBlobCommitted(c *chk.C, blobURL azblob.BlockBlobURL) {
  2754. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2755. c.Assert(err, chk.IsNil)
  2756. c.Assert(resp.CommittedBlocks, chk.HasLen, 1)
  2757. }
  2758. func (s *aztestsSuite) TestBlobPutBlockListIfModifiedSinceTrue(c *chk.C) {
  2759. containerURL, blobURL, id := setupPutBlockListTest(c)
  2760. defer deleteContainer(c, containerURL)
  2761. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2762. c.Assert(err, chk.IsNil)
  2763. currentTime := getRelativeTimeGMT(-10)
  2764. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,
  2765. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2766. c.Assert(err, chk.IsNil)
  2767. validateBlobCommitted(c, blobURL)
  2768. }
  2769. func (s *aztestsSuite) TestBlobPutBlockListIfModifiedSinceFalse(c *chk.C) {
  2770. containerURL, blobURL, id := setupPutBlockListTest(c)
  2771. defer deleteContainer(c, containerURL)
  2772. currentTime := getRelativeTimeGMT(10)
  2773. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,
  2774. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2775. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2776. }
  2777. func (s *aztestsSuite) TestBlobPutBlockListIfUnmodifiedSinceTrue(c *chk.C) {
  2778. containerURL, blobURL, id := setupPutBlockListTest(c)
  2779. defer deleteContainer(c, containerURL)
  2780. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2781. c.Assert(err, chk.IsNil)
  2782. currentTime := getRelativeTimeGMT(10)
  2783. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,
  2784. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2785. c.Assert(err, chk.IsNil)
  2786. validateBlobCommitted(c, blobURL)
  2787. }
  2788. func (s *aztestsSuite) TestBlobPutBlockListIfUnmodifiedSinceFalse(c *chk.C) {
  2789. containerURL, blobURL, id := setupPutBlockListTest(c)
  2790. blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2791. defer deleteContainer(c, containerURL)
  2792. currentTime := getRelativeTimeGMT(-10)
  2793. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,
  2794. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2795. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2796. }
  2797. func (s *aztestsSuite) TestBlobPutBlockListIfMatchTrue(c *chk.C) {
  2798. containerURL, blobURL, id := setupPutBlockListTest(c)
  2799. defer deleteContainer(c, containerURL)
  2800. resp, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2801. c.Assert(err, chk.IsNil)
  2802. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{},nil,
  2803. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2804. c.Assert(err, chk.IsNil)
  2805. validateBlobCommitted(c, blobURL)
  2806. }
  2807. func (s *aztestsSuite) TestBlobPutBlockListIfMatchFalse(c *chk.C) {
  2808. containerURL, blobURL, id := setupPutBlockListTest(c)
  2809. defer deleteContainer(c, containerURL)
  2810. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2811. c.Assert(err, chk.IsNil)
  2812. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{},nil,
  2813. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2814. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2815. }
  2816. func (s *aztestsSuite) TestBlobPutBlockListIfNoneMatchTrue(c *chk.C) {
  2817. containerURL, blobURL, id := setupPutBlockListTest(c)
  2818. defer deleteContainer(c, containerURL)
  2819. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2820. c.Assert(err, chk.IsNil)
  2821. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{},nil,
  2822. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2823. c.Assert(err, chk.IsNil)
  2824. validateBlobCommitted(c, blobURL)
  2825. }
  2826. func (s *aztestsSuite) TestBlobPutBlockListIfNoneMatchFalse(c *chk.C) {
  2827. containerURL, blobURL, id := setupPutBlockListTest(c)
  2828. defer deleteContainer(c, containerURL)
  2829. resp, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{}) // The blob must actually exist to have a modifed time
  2830. c.Assert(err, chk.IsNil)
  2831. _, err = blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{},nil,
  2832. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2833. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2834. }
  2835. func (s *aztestsSuite) TestBlobPutBlockListValidateData(c *chk.C) {
  2836. containerURL, blobURL, id := setupPutBlockListTest(c)
  2837. defer deleteContainer(c, containerURL)
  2838. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{})
  2839. resp, err := blobURL.GetBlob(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{}, false)
  2840. c.Assert(err, chk.IsNil)
  2841. data, _ := ioutil.ReadAll(resp.Response().Body)
  2842. c.Assert(string(data), chk.Equals, blockBlobDefaultData)
  2843. }
  2844. func (s *aztestsSuite) TestBlobPutBlockListModifyBlob(c *chk.C) {
  2845. containerURL, blobURL, id := setupPutBlockListTest(c)
  2846. defer deleteContainer(c, containerURL)
  2847. _, err := blobURL.PutBlockList(ctx, []string{id}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{})
  2848. c.Assert(err, chk.IsNil)
  2849. _, err = blobURL.PutBlock(ctx, "0001", bytes.NewReader([]byte("new data")), azblob.LeaseAccessConditions{})
  2850. c.Assert(err, chk.IsNil)
  2851. _, err = blobURL.PutBlock(ctx, "0010", bytes.NewReader([]byte("new data")), azblob.LeaseAccessConditions{})
  2852. c.Assert(err, chk.IsNil)
  2853. _, err = blobURL.PutBlock(ctx, "0011", bytes.NewReader([]byte("new data")), azblob.LeaseAccessConditions{})
  2854. c.Assert(err, chk.IsNil)
  2855. _, err = blobURL.PutBlock(ctx, "0100", bytes.NewReader([]byte("new data")), azblob.LeaseAccessConditions{})
  2856. c.Assert(err, chk.IsNil)
  2857. _, err = blobURL.PutBlockList(ctx, []string{"0001", "0011"}, azblob.BlobHTTPHeaders{}, nil,azblob.BlobAccessConditions{})
  2858. c.Assert(err, chk.IsNil)
  2859. resp, err := blobURL.GetBlockList(ctx, azblob.BlockListAll, azblob.LeaseAccessConditions{})
  2860. c.Assert(err, chk.IsNil)
  2861. c.Assert(resp.CommittedBlocks, chk.HasLen, 2)
  2862. c.Assert(resp.CommittedBlocks[0].Name, chk.Equals, "0001")
  2863. c.Assert(resp.CommittedBlocks[1].Name, chk.Equals, "0011")
  2864. c.Assert(resp.UncommittedBlocks, chk.HasLen, 0)
  2865. }
  2866. func (s *aztestsSuite) TestBlobCreateAppendMetadataNonEmpty(c *chk.C) {
  2867. bsu := getBSU()
  2868. containerURL, _ := createNewContainer(c, bsu)
  2869. defer deleteContainer(c, containerURL)
  2870. blobURL, _ := getAppendBlobURL(c, containerURL)
  2871. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{}, basicMetadata,azblob.BlobAccessConditions{})
  2872. c.Assert(err, chk.IsNil)
  2873. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2874. c.Assert(err, chk.IsNil)
  2875. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2876. }
  2877. func (s *aztestsSuite) TestBlobCreateAppendMetadataEmpty(c *chk.C) {
  2878. bsu := getBSU()
  2879. containerURL, _ := createNewContainer(c, bsu)
  2880. defer deleteContainer(c, containerURL)
  2881. blobURL, _ := getAppendBlobURL(c, containerURL)
  2882. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{}, azblob.Metadata{},azblob.BlobAccessConditions{})
  2883. c.Assert(err, chk.IsNil)
  2884. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2885. c.Assert(err, chk.IsNil)
  2886. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  2887. }
  2888. func (s *aztestsSuite) TestBlobCreateAppendMetadataInvalid(c *chk.C) {
  2889. bsu := getBSU()
  2890. containerURL, _ := createNewContainer(c, bsu)
  2891. defer deleteContainer(c, containerURL)
  2892. blobURL, _ := getAppendBlobURL(c, containerURL)
  2893. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{}, azblob.Metadata{"In valid!": "bar"}, azblob.BlobAccessConditions{})
  2894. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  2895. }
  2896. func (s *aztestsSuite) TestBlobCreateAppendHTTPHeaders(c *chk.C) {
  2897. bsu := getBSU()
  2898. containerURL, _ := createNewContainer(c, bsu)
  2899. defer deleteContainer(c, containerURL)
  2900. blobURL, _ := getAppendBlobURL(c, containerURL)
  2901. _, err := blobURL.Create(ctx, basicHeaders, nil, azblob.BlobAccessConditions{})
  2902. c.Assert(err, chk.IsNil)
  2903. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2904. c.Assert(err, chk.IsNil)
  2905. h := resp.NewHTTPHeaders()
  2906. c.Assert(h, chk.DeepEquals, basicHeaders)
  2907. }
  2908. func validateAppendBlobPut(c *chk.C, blobURL azblob.AppendBlobURL) {
  2909. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2910. c.Assert(err, chk.IsNil)
  2911. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  2912. }
  2913. func (s *aztestsSuite) TestBlobCreateAppendIfModifiedSinceTrue(c *chk.C) {
  2914. bsu := getBSU()
  2915. containerURL, _ := createNewContainer(c, bsu)
  2916. defer deleteContainer(c, containerURL)
  2917. blobURL, _ := createNewAppendBlob(c, containerURL)
  2918. currentTime := getRelativeTimeGMT(-10)
  2919. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2920. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2921. c.Assert(err, chk.IsNil)
  2922. validateAppendBlobPut(c, blobURL)
  2923. }
  2924. func (s *aztestsSuite) TestBlobCreateAppendIfModifiedSinceFalse(c *chk.C) {
  2925. bsu := getBSU()
  2926. containerURL, _ := createNewContainer(c, bsu)
  2927. defer deleteContainer(c, containerURL)
  2928. blobURL, _ := createNewAppendBlob(c, containerURL)
  2929. currentTime := getRelativeTimeGMT(10)
  2930. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2931. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  2932. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2933. }
  2934. func (s *aztestsSuite) TestBlobCreateAppendIfUnmodifiedSinceTrue(c *chk.C) {
  2935. bsu := getBSU()
  2936. containerURL, _ := createNewContainer(c, bsu)
  2937. defer deleteContainer(c, containerURL)
  2938. blobURL, _ := createNewAppendBlob(c, containerURL)
  2939. currentTime := getRelativeTimeGMT(10)
  2940. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2941. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2942. c.Assert(err, chk.IsNil)
  2943. validateAppendBlobPut(c, blobURL)
  2944. }
  2945. func (s *aztestsSuite) TestBlobCreateAppendIfUnmodifiedSinceFalse(c *chk.C) {
  2946. bsu := getBSU()
  2947. containerURL, _ := createNewContainer(c, bsu)
  2948. defer deleteContainer(c, containerURL)
  2949. blobURL, _ := createNewAppendBlob(c, containerURL)
  2950. currentTime := getRelativeTimeGMT(-10)
  2951. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2952. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  2953. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2954. }
  2955. func (s *aztestsSuite) TestBlobCreateAppendIfMatchTrue(c *chk.C) {
  2956. bsu := getBSU()
  2957. containerURL, _ := createNewContainer(c, bsu)
  2958. defer deleteContainer(c, containerURL)
  2959. blobURL, _ := createNewAppendBlob(c, containerURL)
  2960. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2961. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2962. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  2963. c.Assert(err, chk.IsNil)
  2964. validateAppendBlobPut(c, blobURL)
  2965. }
  2966. func (s *aztestsSuite) TestBlobCreateAppendIfMatchFalse(c *chk.C) {
  2967. bsu := getBSU()
  2968. containerURL, _ := createNewContainer(c, bsu)
  2969. defer deleteContainer(c, containerURL)
  2970. blobURL, _ := createNewAppendBlob(c, containerURL)
  2971. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2972. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  2973. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2974. }
  2975. func (s *aztestsSuite) TestBlobCreateAppendIfNoneMatchTrue(c *chk.C) {
  2976. bsu := getBSU()
  2977. containerURL, _ := createNewContainer(c, bsu)
  2978. defer deleteContainer(c, containerURL)
  2979. blobURL, _ := createNewAppendBlob(c, containerURL)
  2980. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2981. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  2982. c.Assert(err, chk.IsNil)
  2983. validateAppendBlobPut(c, blobURL)
  2984. }
  2985. func (s *aztestsSuite) TestBlobCreateAppendIfNoneMatchFalse(c *chk.C) {
  2986. bsu := getBSU()
  2987. containerURL, _ := createNewContainer(c, bsu)
  2988. defer deleteContainer(c, containerURL)
  2989. blobURL, _ := createNewAppendBlob(c, containerURL)
  2990. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  2991. _, err := blobURL.Create(ctx, azblob.BlobHTTPHeaders{},basicMetadata,
  2992. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  2993. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  2994. }
  2995. func (s *aztestsSuite) TestBlobAppendBlockNilBody(c *chk.C) {
  2996. bsu := getBSU()
  2997. containerURL, _ := createNewContainer(c, bsu)
  2998. defer deleteContainer(c, containerURL)
  2999. blobURL, _ := createNewAppendBlob(c, containerURL)
  3000. _, err := blobURL.AppendBlock(ctx, nil, azblob.BlobAccessConditions{})
  3001. c.Assert(err, chk.NotNil)
  3002. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  3003. }
  3004. func (s *aztestsSuite) TestBlobAppendBlockEmptyBody(c *chk.C) {
  3005. bsu := getBSU()
  3006. containerURL, _ := createNewContainer(c, bsu)
  3007. defer deleteContainer(c, containerURL)
  3008. blobURL, _ := createNewAppendBlob(c, containerURL)
  3009. _, err := blobURL.AppendBlock(ctx, strings.NewReader(""), azblob.BlobAccessConditions{})
  3010. validateStorageError(c, err, azblob.ServiceCodeMissingContentLengthHeader) // The server does not accept getReaderToRandomBytes length = 0
  3011. }
  3012. func (s *aztestsSuite) TestBlobAppendBlockNonExistantBlob(c *chk.C) {
  3013. bsu := getBSU()
  3014. containerURL, _ := createNewContainer(c, bsu)
  3015. defer deleteContainer(c, containerURL)
  3016. blobURL, _ := getAppendBlobURL(c, containerURL)
  3017. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobAccessConditions{})
  3018. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  3019. }
  3020. func validateBlockAppended(c *chk.C, blobURL azblob.AppendBlobURL, expectedSize int) {
  3021. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3022. c.Assert(err, chk.IsNil)
  3023. c.Assert(resp.ContentLength(), chk.Equals, int64(expectedSize))
  3024. }
  3025. func (s *aztestsSuite) TestBlobAppendBlockIfModifiedSinceTrue(c *chk.C) {
  3026. bsu := getBSU()
  3027. containerURL, _ := createNewContainer(c, bsu)
  3028. defer deleteContainer(c, containerURL)
  3029. blobURL, _ := createNewAppendBlob(c, containerURL)
  3030. currentTime := getRelativeTimeGMT(-10)
  3031. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3032. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3033. c.Assert(err, chk.IsNil)
  3034. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3035. }
  3036. func (s *aztestsSuite) TestBlobAppendBlockIfModifiedSinceFalse(c *chk.C) {
  3037. bsu := getBSU()
  3038. containerURL, _ := createNewContainer(c, bsu)
  3039. defer deleteContainer(c, containerURL)
  3040. blobURL, _ := createNewAppendBlob(c, containerURL)
  3041. currentTime := getRelativeTimeGMT(10)
  3042. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3043. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3044. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3045. }
  3046. func (s *aztestsSuite) TestBlobAppendBlockIfUnmodifiedSinceTrue(c *chk.C) {
  3047. bsu := getBSU()
  3048. containerURL, _ := createNewContainer(c, bsu)
  3049. defer deleteContainer(c, containerURL)
  3050. blobURL, _ := createNewAppendBlob(c, containerURL)
  3051. currentTime := getRelativeTimeGMT(10)
  3052. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3053. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3054. c.Assert(err, chk.IsNil)
  3055. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3056. }
  3057. func (s *aztestsSuite) TestBlobAppendBlockIfUnmodifiedSinceFalse(c *chk.C) {
  3058. bsu := getBSU()
  3059. containerURL, _ := createNewContainer(c, bsu)
  3060. defer deleteContainer(c, containerURL)
  3061. blobURL, _ := createNewAppendBlob(c, containerURL)
  3062. currentTime := getRelativeTimeGMT(-10)
  3063. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3064. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3065. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3066. }
  3067. func (s *aztestsSuite) TestBlobAppendBlockIfMatchTrue(c *chk.C) {
  3068. bsu := getBSU()
  3069. containerURL, _ := createNewContainer(c, bsu)
  3070. defer deleteContainer(c, containerURL)
  3071. blobURL, _ := createNewAppendBlob(c, containerURL)
  3072. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3073. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3074. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3075. c.Assert(err, chk.IsNil)
  3076. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3077. }
  3078. func (s *aztestsSuite) TestBlobAppendBlockIfMatchFalse(c *chk.C) {
  3079. bsu := getBSU()
  3080. containerURL, _ := createNewContainer(c, bsu)
  3081. defer deleteContainer(c, containerURL)
  3082. blobURL, _ := createNewAppendBlob(c, containerURL)
  3083. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3084. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3085. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3086. }
  3087. func (s *aztestsSuite) TestBlobAppendBlockIfNoneMatchTrue(c *chk.C) {
  3088. bsu := getBSU()
  3089. containerURL, _ := createNewContainer(c, bsu)
  3090. defer deleteContainer(c, containerURL)
  3091. blobURL, _ := createNewAppendBlob(c, containerURL)
  3092. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3093. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3094. c.Assert(err, chk.IsNil)
  3095. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3096. }
  3097. func (s *aztestsSuite) TestBlobAppendBlockIfNoneMatchFalse(c *chk.C) {
  3098. bsu := getBSU()
  3099. containerURL, _ := createNewContainer(c, bsu)
  3100. defer deleteContainer(c, containerURL)
  3101. blobURL, _ := createNewAppendBlob(c, containerURL)
  3102. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3103. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3104. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3105. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3106. }
  3107. func (s *aztestsSuite) TestBlobAppendBlockIfAppendPositionMatchTrueNegOne(c *chk.C) {
  3108. bsu := getBSU()
  3109. containerURL, _ := createNewContainer(c, bsu)
  3110. defer deleteContainer(c, containerURL)
  3111. blobURL, _ := createNewAppendBlob(c, containerURL)
  3112. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3113. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfAppendPositionEqual: -1}}) // This will cause the library to set the value of the header to 0
  3114. c.Assert(err, chk.IsNil)
  3115. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3116. }
  3117. func (s *aztestsSuite) TestBlobAppendBlockIfAppendPositionMatchZero(c *chk.C) {
  3118. bsu := getBSU()
  3119. containerURL, _ := createNewContainer(c, bsu)
  3120. defer deleteContainer(c, containerURL)
  3121. blobURL, _ := createNewAppendBlob(c, containerURL)
  3122. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobAccessConditions{}) // The position will not match, but the condition should be ignored
  3123. c.Assert(err, chk.IsNil)
  3124. _, err = blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3125. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfAppendPositionEqual: 0}})
  3126. c.Assert(err, chk.IsNil)
  3127. validateBlockAppended(c, blobURL, 2*len(blockBlobDefaultData))
  3128. }
  3129. func (s *aztestsSuite) TestBlobAppendBlockIfAppendPositionMatchTrueNonZero(c *chk.C) {
  3130. bsu := getBSU()
  3131. containerURL, _ := createNewContainer(c, bsu)
  3132. defer deleteContainer(c, containerURL)
  3133. blobURL, _ := createNewAppendBlob(c, containerURL)
  3134. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobAccessConditions{})
  3135. c.Assert(err, chk.IsNil)
  3136. _, err = blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3137. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfAppendPositionEqual: int32(len(blockBlobDefaultData))}})
  3138. c.Assert(err, chk.IsNil)
  3139. validateBlockAppended(c, blobURL, len(blockBlobDefaultData)*2)
  3140. }
  3141. func (s *aztestsSuite) TestBlobAppendBlockIfAppendPositionMatchFalseNegOne(c *chk.C) {
  3142. bsu := getBSU()
  3143. containerURL, _ := createNewContainer(c, bsu)
  3144. defer deleteContainer(c, containerURL)
  3145. blobURL, _ := createNewAppendBlob(c, containerURL)
  3146. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData), azblob.BlobAccessConditions{})
  3147. c.Assert(err, chk.IsNil)
  3148. _, err = blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3149. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfAppendPositionEqual: -1}}) // This will cause the library to set the value of the header to 0
  3150. validateStorageError(c, err, azblob.ServiceCodeAppendPositionConditionNotMet)
  3151. }
  3152. func (s *aztestsSuite) TestBlobAppendBlockIfAppendPositionMatchFalseNonZero(c *chk.C) {
  3153. bsu := getBSU()
  3154. containerURL, _ := createNewContainer(c, bsu)
  3155. defer deleteContainer(c, containerURL)
  3156. blobURL, _ := createNewAppendBlob(c, containerURL)
  3157. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3158. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfAppendPositionEqual: 12}})
  3159. validateStorageError(c, err, azblob.ServiceCodeAppendPositionConditionNotMet)
  3160. }
  3161. func (s *aztestsSuite) TestBlobAppendBlockIfMaxSizeTrue(c *chk.C) {
  3162. bsu := getBSU()
  3163. containerURL, _ := createNewContainer(c, bsu)
  3164. defer deleteContainer(c, containerURL)
  3165. blobURL, _ := createNewAppendBlob(c, containerURL)
  3166. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3167. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfMaxSizeLessThanOrEqual: int32(len(blockBlobDefaultData) + 1)}})
  3168. c.Assert(err, chk.IsNil)
  3169. validateBlockAppended(c, blobURL, len(blockBlobDefaultData))
  3170. }
  3171. func (s *aztestsSuite) TestBlobAppendBlockIfMaxSizeFalse(c *chk.C) {
  3172. bsu := getBSU()
  3173. containerURL, _ := createNewContainer(c, bsu)
  3174. defer deleteContainer(c, containerURL)
  3175. blobURL, _ := createNewAppendBlob(c, containerURL)
  3176. _, err := blobURL.AppendBlock(ctx, strings.NewReader(blockBlobDefaultData),
  3177. azblob.BlobAccessConditions{AppendBlobAccessConditions: azblob.AppendBlobAccessConditions{IfMaxSizeLessThanOrEqual: int32(len(blockBlobDefaultData) - 1)}})
  3178. validateStorageError(c, err, azblob.ServiceCodeMaxBlobSizeConditionNotMet)
  3179. }
  3180. func (s *aztestsSuite) TestBlobCreatePageSizeInvalid(c *chk.C) {
  3181. bsu := getBSU()
  3182. containerURL, _ := createNewContainer(c, bsu)
  3183. defer deleteContainer(c, containerURL)
  3184. blobURL, _ := getPageBlobURL(c, containerURL)
  3185. _, err := blobURL.Create(ctx, 1, 0, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  3186. validateStorageError(c, err, azblob.ServiceCodeInvalidHeaderValue)
  3187. }
  3188. func (s *aztestsSuite) TestBlobCreatePageSequenceInvalid(c *chk.C) {
  3189. bsu := getBSU()
  3190. containerURL, _ := createNewContainer(c, bsu)
  3191. defer deleteContainer(c, containerURL)
  3192. blobURL, _ := getPageBlobURL(c, containerURL)
  3193. // Negative sequenceNumber should cause a panic
  3194. defer func() {
  3195. recover()
  3196. }()
  3197. blobURL.Create(ctx, azblob.PageBlobPageBytes, -1, azblob.BlobHTTPHeaders{}, nil, azblob.BlobAccessConditions{})
  3198. c.Fail()
  3199. }
  3200. func (s *aztestsSuite) TestBlobCreatePageMetadataNonEmpty(c *chk.C) {
  3201. bsu := getBSU()
  3202. containerURL, _ := createNewContainer(c, bsu)
  3203. defer deleteContainer(c, containerURL)
  3204. blobURL, _ := getPageBlobURL(c, containerURL)
  3205. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{}, basicMetadata,azblob.BlobAccessConditions{})
  3206. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3207. c.Assert(err, chk.IsNil)
  3208. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  3209. }
  3210. func (s *aztestsSuite) TestBlobCreatePageMetadataEmpty(c *chk.C) {
  3211. bsu := getBSU()
  3212. containerURL, _ := createNewContainer(c, bsu)
  3213. defer deleteContainer(c, containerURL)
  3214. blobURL, _ := getPageBlobURL(c, containerURL)
  3215. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  3216. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3217. c.Assert(err, chk.IsNil)
  3218. c.Assert(resp.NewMetadata(), chk.HasLen, 0)
  3219. }
  3220. func (s *aztestsSuite) TestBlobCreatePageMetadataInvalid(c *chk.C) {
  3221. bsu := getBSU()
  3222. containerURL, _ := createNewContainer(c, bsu)
  3223. defer deleteContainer(c, containerURL)
  3224. blobURL, _ := getPageBlobURL(c, containerURL)
  3225. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{}, azblob.Metadata{"In valid1": "bar"}, azblob.BlobAccessConditions{})
  3226. c.Assert(strings.Contains(err.Error(), validationErrorSubstring), chk.Equals, true)
  3227. }
  3228. func (s *aztestsSuite) TestBlobCreatePageHTTPHeaders(c *chk.C) {
  3229. bsu := getBSU()
  3230. containerURL, _ := createNewContainer(c, bsu)
  3231. defer deleteContainer(c, containerURL)
  3232. blobURL, _ := getPageBlobURL(c, containerURL)
  3233. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, basicHeaders, nil, azblob.BlobAccessConditions{})
  3234. c.Assert(err, chk.IsNil)
  3235. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3236. c.Assert(err, chk.IsNil)
  3237. h := resp.NewHTTPHeaders()
  3238. c.Assert(h, chk.DeepEquals, basicHeaders)
  3239. }
  3240. func validatePageBlobPut(c *chk.C, blobURL azblob.PageBlobURL) {
  3241. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3242. c.Assert(err, chk.IsNil)
  3243. c.Assert(resp.NewMetadata(), chk.DeepEquals, basicMetadata)
  3244. }
  3245. func (s *aztestsSuite) TestBlobCreatePageIfModifiedSinceTrue(c *chk.C) {
  3246. bsu := getBSU()
  3247. containerURL, _ := createNewContainer(c, bsu)
  3248. defer deleteContainer(c, containerURL)
  3249. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3250. currentTime := getRelativeTimeGMT(-10)
  3251. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{}, basicMetadata,
  3252. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3253. c.Assert(err, chk.IsNil)
  3254. validatePageBlobPut(c, blobURL)
  3255. }
  3256. func (s *aztestsSuite) TestBlobCreatePageIfModifiedSinceFalse(c *chk.C) {
  3257. bsu := getBSU()
  3258. containerURL, _ := createNewContainer(c, bsu)
  3259. defer deleteContainer(c, containerURL)
  3260. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3261. currentTime := getRelativeTimeGMT(10)
  3262. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3263. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3264. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3265. }
  3266. func (s *aztestsSuite) TestBlobCreatePageIfUnmodifiedSinceTrue(c *chk.C) {
  3267. bsu := getBSU()
  3268. containerURL, _ := createNewContainer(c, bsu)
  3269. defer deleteContainer(c, containerURL)
  3270. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3271. currentTime := getRelativeTimeGMT(10)
  3272. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3273. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3274. c.Assert(err, chk.IsNil)
  3275. validatePageBlobPut(c, blobURL)
  3276. }
  3277. func (s *aztestsSuite) TestBlobCreatePageIfUnmodifiedSinceFalse(c *chk.C) {
  3278. bsu := getBSU()
  3279. containerURL, _ := createNewContainer(c, bsu)
  3280. defer deleteContainer(c, containerURL)
  3281. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3282. currentTime := getRelativeTimeGMT(-10)
  3283. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3284. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3285. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3286. }
  3287. func (s *aztestsSuite) TestBlobCreatePageIfMatchTrue(c *chk.C) {
  3288. bsu := getBSU()
  3289. containerURL, _ := createNewContainer(c, bsu)
  3290. defer deleteContainer(c, containerURL)
  3291. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3292. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3293. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3294. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3295. c.Assert(err, chk.IsNil)
  3296. validatePageBlobPut(c, blobURL)
  3297. }
  3298. func (s *aztestsSuite) TestBlobCreatePageIfMatchFalse(c *chk.C) {
  3299. bsu := getBSU()
  3300. containerURL, _ := createNewContainer(c, bsu)
  3301. defer deleteContainer(c, containerURL)
  3302. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3303. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3304. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3305. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3306. }
  3307. func (s *aztestsSuite) TestBlobCreatePageIfNoneMatchTrue(c *chk.C) {
  3308. bsu := getBSU()
  3309. containerURL, _ := createNewContainer(c, bsu)
  3310. defer deleteContainer(c, containerURL)
  3311. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3312. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3313. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3314. c.Assert(err, chk.IsNil)
  3315. validatePageBlobPut(c, blobURL)
  3316. }
  3317. func (s *aztestsSuite) TestBlobCreatePageIfNoneMatchFalse(c *chk.C) {
  3318. bsu := getBSU()
  3319. containerURL, _ := createNewContainer(c, bsu)
  3320. defer deleteContainer(c, containerURL)
  3321. blobURL, _ := createNewPageBlob(c, containerURL) // Originally created without metadata
  3322. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3323. _, err := blobURL.Create(ctx, azblob.PageBlobPageBytes, 0, azblob.BlobHTTPHeaders{},basicMetadata,
  3324. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3325. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3326. }
  3327. func (s *aztestsSuite) TestBlobPutPagesInvalidRange(c *chk.C) {
  3328. bsu := getBSU()
  3329. containerURL, _ := createNewContainer(c, bsu)
  3330. defer deleteContainer(c, containerURL)
  3331. blobURL, _ := createNewPageBlob(c, containerURL)
  3332. defer func() { // The library should panic if the page range is invalid in any way
  3333. recover()
  3334. }()
  3335. blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: int32(len(blockBlobDefaultData))}, strings.NewReader(blockBlobDefaultData),
  3336. azblob.BlobAccessConditions{})
  3337. c.Fail()
  3338. }
  3339. func (s *aztestsSuite) TestBlobPutPagesNilBody(c *chk.C) {
  3340. bsu := getBSU()
  3341. containerURL, _ := createNewContainer(c, bsu)
  3342. defer deleteContainer(c, containerURL)
  3343. blobURL, _ := createNewPageBlob(c, containerURL)
  3344. // A page range that starts and ends at 0 should panic
  3345. defer func() {
  3346. recover()
  3347. }()
  3348. blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: 0}, nil, azblob.BlobAccessConditions{})
  3349. c.Fail()
  3350. }
  3351. func (s *aztestsSuite) TestBlobPutPagesEmptyBody(c *chk.C) {
  3352. bsu := getBSU()
  3353. containerURL, _ := createNewContainer(c, bsu)
  3354. defer deleteContainer(c, containerURL)
  3355. blobURL, _ := createNewPageBlob(c, containerURL)
  3356. // A page range that starts and ends at 0 should panic
  3357. defer func() {
  3358. recover()
  3359. }()
  3360. blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: 0}, bytes.NewReader([]byte{}), azblob.BlobAccessConditions{})
  3361. c.Fail()
  3362. }
  3363. func (s *aztestsSuite) TestBlobPutPagesNonExistantBlob(c *chk.C) {
  3364. bsu := getBSU()
  3365. containerURL, _ := createNewContainer(c, bsu)
  3366. defer deleteContainer(c, containerURL)
  3367. blobURL, _ := getPageBlobURL(c, containerURL)
  3368. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3369. validateStorageError(c, err, azblob.ServiceCodeBlobNotFound)
  3370. }
  3371. func validatePutPages(c *chk.C, blobURL azblob.PageBlobURL) {
  3372. // This will only validate a single put page at 0-azblob.PageBlobPageBytes-1
  3373. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3374. c.Assert(err, chk.IsNil)
  3375. c.Assert(resp.PageRange[0], chk.Equals, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1})
  3376. }
  3377. func (s *aztestsSuite) TestBlobPutPagesIfModifiedSinceTrue(c *chk.C) {
  3378. bsu := getBSU()
  3379. containerURL, _ := createNewContainer(c, bsu)
  3380. defer deleteContainer(c, containerURL)
  3381. blobURL, _ := createNewPageBlob(c, containerURL)
  3382. currentTime := getRelativeTimeGMT(-10)
  3383. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3384. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3385. c.Assert(err, chk.IsNil)
  3386. validatePutPages(c, blobURL)
  3387. }
  3388. func (s *aztestsSuite) TestBlobPutPagesIfModifiedSinceFalse(c *chk.C) {
  3389. bsu := getBSU()
  3390. containerURL, _ := createNewContainer(c, bsu)
  3391. defer deleteContainer(c, containerURL)
  3392. blobURL, _ := createNewPageBlob(c, containerURL)
  3393. currentTime := getRelativeTimeGMT(10)
  3394. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3395. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3396. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3397. }
  3398. func (s *aztestsSuite) TestBlobPutPagesIfUnmodifiedSinceTrue(c *chk.C) {
  3399. bsu := getBSU()
  3400. containerURL, _ := createNewContainer(c, bsu)
  3401. defer deleteContainer(c, containerURL)
  3402. blobURL, _ := createNewPageBlob(c, containerURL)
  3403. currentTime := getRelativeTimeGMT(10)
  3404. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3405. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3406. c.Assert(err, chk.IsNil)
  3407. validatePutPages(c, blobURL)
  3408. }
  3409. func (s *aztestsSuite) TestBlobPutPagesIfUnmodifiedSinceFalse(c *chk.C) {
  3410. bsu := getBSU()
  3411. containerURL, _ := createNewContainer(c, bsu)
  3412. defer deleteContainer(c, containerURL)
  3413. blobURL, _ := createNewPageBlob(c, containerURL)
  3414. currentTime := getRelativeTimeGMT(-10)
  3415. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3416. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3417. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3418. }
  3419. func (s *aztestsSuite) TestBlobPutPagesIfMatchTrue(c *chk.C) {
  3420. bsu := getBSU()
  3421. containerURL, _ := createNewContainer(c, bsu)
  3422. defer deleteContainer(c, containerURL)
  3423. blobURL, _ := createNewPageBlob(c, containerURL)
  3424. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3425. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3426. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3427. c.Assert(err, chk.IsNil)
  3428. validatePutPages(c, blobURL)
  3429. }
  3430. func (s *aztestsSuite) TestBlobPutPagesIfMatchFalse(c *chk.C) {
  3431. bsu := getBSU()
  3432. containerURL, _ := createNewContainer(c, bsu)
  3433. defer deleteContainer(c, containerURL)
  3434. blobURL, _ := createNewPageBlob(c, containerURL)
  3435. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3436. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3437. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3438. }
  3439. func (s *aztestsSuite) TestBlobPutPagesIfNoneMatchTrue(c *chk.C) {
  3440. bsu := getBSU()
  3441. containerURL, _ := createNewContainer(c, bsu)
  3442. defer deleteContainer(c, containerURL)
  3443. blobURL, _ := createNewPageBlob(c, containerURL)
  3444. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3445. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3446. c.Assert(err, chk.IsNil)
  3447. validatePutPages(c, blobURL)
  3448. }
  3449. func (s *aztestsSuite) TestBlobPutPagesIfNoneMatchFalse(c *chk.C) {
  3450. bsu := getBSU()
  3451. containerURL, _ := createNewContainer(c, bsu)
  3452. defer deleteContainer(c, containerURL)
  3453. blobURL, _ := createNewPageBlob(c, containerURL)
  3454. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3455. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3456. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3457. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3458. }
  3459. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLessThanTrue(c *chk.C) {
  3460. bsu := getBSU()
  3461. containerURL, _ := createNewContainer(c, bsu)
  3462. defer deleteContainer(c, containerURL)
  3463. blobURL, _ := createNewPageBlob(c, containerURL)
  3464. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3465. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: 10}})
  3466. c.Assert(err, chk.IsNil)
  3467. validatePutPages(c, blobURL)
  3468. }
  3469. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLessThanFalse(c *chk.C) {
  3470. bsu := getBSU()
  3471. containerURL, _ := createNewContainer(c, bsu)
  3472. defer deleteContainer(c, containerURL)
  3473. blobURL, _ := createNewPageBlob(c, containerURL)
  3474. blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3475. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3476. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: 1}})
  3477. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3478. }
  3479. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLessThanNegOne(c *chk.C) {
  3480. bsu := getBSU()
  3481. containerURL, _ := createNewContainer(c, bsu)
  3482. defer deleteContainer(c, containerURL)
  3483. blobURL, _ := createNewPageBlob(c, containerURL)
  3484. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3485. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: -1}}) // This will cause the library to set the value of the header to 0
  3486. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3487. }
  3488. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLTETrue(c *chk.C) {
  3489. bsu := getBSU()
  3490. containerURL, _ := createNewContainer(c, bsu)
  3491. defer deleteContainer(c, containerURL)
  3492. blobURL, _ := createNewPageBlob(c, containerURL)
  3493. blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 1, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3494. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3495. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: 1}})
  3496. c.Assert(err, chk.IsNil)
  3497. validatePutPages(c, blobURL)
  3498. }
  3499. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLTEqualFalse(c *chk.C) {
  3500. bsu := getBSU()
  3501. containerURL, _ := createNewContainer(c, bsu)
  3502. defer deleteContainer(c, containerURL)
  3503. blobURL, _ := createNewPageBlob(c, containerURL)
  3504. blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3505. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3506. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: 1}})
  3507. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3508. }
  3509. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberLTENegOne(c *chk.C) {
  3510. bsu := getBSU()
  3511. containerURL, _ := createNewContainer(c, bsu)
  3512. defer deleteContainer(c, containerURL)
  3513. blobURL, _ := createNewPageBlob(c, containerURL)
  3514. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3515. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: -1}}) // This will cause the library to set the value of the header to 0
  3516. c.Assert(err, chk.IsNil)
  3517. validatePutPages(c, blobURL)
  3518. }
  3519. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberEqualTrue(c *chk.C) {
  3520. bsu := getBSU()
  3521. containerURL, _ := createNewContainer(c, bsu)
  3522. defer deleteContainer(c, containerURL)
  3523. blobURL, _ := createNewPageBlob(c, containerURL)
  3524. blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 1, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3525. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3526. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: 1}})
  3527. c.Assert(err, chk.IsNil)
  3528. validatePutPages(c, blobURL)
  3529. }
  3530. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberEqualFalse(c *chk.C) {
  3531. bsu := getBSU()
  3532. containerURL, _ := createNewContainer(c, bsu)
  3533. defer deleteContainer(c, containerURL)
  3534. blobURL, _ := createNewPageBlob(c, containerURL)
  3535. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3536. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: 1}})
  3537. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3538. }
  3539. func (s *aztestsSuite) TestBlobPutPagesIfSequenceNumberEqualNegOne(c *chk.C) {
  3540. bsu := getBSU()
  3541. containerURL, _ := createNewContainer(c, bsu)
  3542. defer deleteContainer(c, containerURL)
  3543. blobURL, _ := createNewPageBlob(c, containerURL)
  3544. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes),
  3545. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: -1}}) // This will cause the library to set the value of the header to 0
  3546. c.Assert(err, chk.IsNil)
  3547. validatePutPages(c, blobURL)
  3548. }
  3549. func setupClearPagesTest(c *chk.C) (azblob.ContainerURL, azblob.PageBlobURL) {
  3550. bsu := getBSU()
  3551. containerURL, _ := createNewContainer(c, bsu)
  3552. blobURL, _ := createNewPageBlob(c, containerURL)
  3553. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3554. c.Assert(err, chk.IsNil)
  3555. return containerURL, blobURL
  3556. }
  3557. func validateClearPagesTest(c *chk.C, blobURL azblob.PageBlobURL) {
  3558. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3559. c.Assert(err, chk.IsNil)
  3560. c.Assert(resp.PageRange, chk.HasLen, 0)
  3561. }
  3562. func (s *aztestsSuite) TestBlobClearPagesInvalidRange(c *chk.C) {
  3563. containerURL, blobURL := setupClearPagesTest(c)
  3564. defer deleteContainer(c, containerURL)
  3565. // A misaligned page range will panic (End is set to n*512 instead of (n*512)-1 as is required)
  3566. defer func() {
  3567. recover()
  3568. }()
  3569. blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes}, azblob.BlobAccessConditions{})
  3570. c.Fail()
  3571. }
  3572. func (s *aztestsSuite) TestBlobClearPagesIfModifiedSinceTrue(c *chk.C) {
  3573. containerURL, blobURL := setupClearPagesTest(c)
  3574. defer deleteContainer(c, containerURL)
  3575. currentTime := getRelativeTimeGMT(-10)
  3576. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3577. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3578. c.Assert(err, chk.IsNil)
  3579. validateClearPagesTest(c, blobURL)
  3580. }
  3581. func (s *aztestsSuite) TestBlobClearPagesIfModifiedSinceFalse(c *chk.C) {
  3582. containerURL, blobURL := setupClearPagesTest(c)
  3583. defer deleteContainer(c, containerURL)
  3584. currentTime := getRelativeTimeGMT(10)
  3585. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3586. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3587. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3588. }
  3589. func (s *aztestsSuite) TestBlobClearPagesIfUnmodifiedSinceTrue(c *chk.C) {
  3590. containerURL, blobURL := setupClearPagesTest(c)
  3591. defer deleteContainer(c, containerURL)
  3592. currentTime := getRelativeTimeGMT(10)
  3593. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3594. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3595. c.Assert(err, chk.IsNil)
  3596. validateClearPagesTest(c, blobURL)
  3597. }
  3598. func (s *aztestsSuite) TestBlobClearPagesIfUnmodifiedSinceFalse(c *chk.C) {
  3599. containerURL, blobURL := setupClearPagesTest(c)
  3600. defer deleteContainer(c, containerURL)
  3601. currentTime := getRelativeTimeGMT(-10)
  3602. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3603. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3604. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3605. }
  3606. func (s *aztestsSuite) TestBlobClearPagesIfMatchTrue(c *chk.C) {
  3607. containerURL, blobURL := setupClearPagesTest(c)
  3608. defer deleteContainer(c, containerURL)
  3609. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3610. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3611. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3612. c.Assert(err, chk.IsNil)
  3613. validateClearPagesTest(c, blobURL)
  3614. }
  3615. func (s *aztestsSuite) TestBlobClearPagesIfMatchFalse(c *chk.C) {
  3616. containerURL, blobURL := setupClearPagesTest(c)
  3617. defer deleteContainer(c, containerURL)
  3618. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3619. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3620. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3621. }
  3622. func (s *aztestsSuite) TestBlobClearPagesIfNoneMatchTrue(c *chk.C) {
  3623. containerURL, blobURL := setupClearPagesTest(c)
  3624. defer deleteContainer(c, containerURL)
  3625. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3626. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3627. c.Assert(err, chk.IsNil)
  3628. validateClearPagesTest(c, blobURL)
  3629. }
  3630. func (s *aztestsSuite) TestBlobClearPagesIfNoneMatchFalse(c *chk.C) {
  3631. containerURL, blobURL := setupClearPagesTest(c)
  3632. defer deleteContainer(c, containerURL)
  3633. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3634. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3635. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3636. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3637. }
  3638. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLessThanTrue(c *chk.C) {
  3639. containerURL, blobURL := setupClearPagesTest(c)
  3640. defer deleteContainer(c, containerURL)
  3641. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3642. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: 10}})
  3643. c.Assert(err, chk.IsNil)
  3644. validateClearPagesTest(c, blobURL)
  3645. }
  3646. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLessThanFalse(c *chk.C) {
  3647. containerURL, blobURL := setupClearPagesTest(c)
  3648. defer deleteContainer(c, containerURL)
  3649. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3650. c.Assert(err, chk.IsNil)
  3651. _, err = blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3652. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: 1}})
  3653. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3654. }
  3655. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLessThanNegOne(c *chk.C) {
  3656. containerURL, blobURL := setupClearPagesTest(c)
  3657. defer deleteContainer(c, containerURL)
  3658. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3659. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThan: -1}}) // This will cause the library to set the value of the header to 0
  3660. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3661. }
  3662. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLTETrue(c *chk.C) {
  3663. containerURL, blobURL := setupClearPagesTest(c)
  3664. defer deleteContainer(c, containerURL)
  3665. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3666. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: 10}})
  3667. c.Assert(err, chk.IsNil)
  3668. validateClearPagesTest(c, blobURL)
  3669. }
  3670. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLTEFalse(c *chk.C) {
  3671. containerURL, blobURL := setupClearPagesTest(c)
  3672. defer deleteContainer(c, containerURL)
  3673. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3674. c.Assert(err, chk.IsNil)
  3675. _, err = blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3676. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: 1}})
  3677. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3678. }
  3679. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberLTENegOne(c *chk.C) {
  3680. containerURL, blobURL := setupClearPagesTest(c)
  3681. defer deleteContainer(c, containerURL)
  3682. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3683. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberLessThanOrEqual: -1}}) // This will cause the library to set the value of the header to 0
  3684. c.Assert(err, chk.IsNil)
  3685. validateClearPagesTest(c, blobURL)
  3686. }
  3687. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberEqualTrue(c *chk.C) {
  3688. containerURL, blobURL := setupClearPagesTest(c)
  3689. defer deleteContainer(c, containerURL)
  3690. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3691. c.Assert(err, chk.IsNil)
  3692. _, err = blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3693. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: 10}})
  3694. c.Assert(err, chk.IsNil)
  3695. validateClearPagesTest(c, blobURL)
  3696. }
  3697. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberEqualFalse(c *chk.C) {
  3698. containerURL, blobURL := setupClearPagesTest(c)
  3699. defer deleteContainer(c, containerURL)
  3700. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, 10, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  3701. c.Assert(err, chk.IsNil)
  3702. _, err = blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3703. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: 1}})
  3704. validateStorageError(c, err, azblob.ServiceCodeSequenceNumberConditionNotMet)
  3705. }
  3706. func (s *aztestsSuite) TestBlobClearPagesIfSequenceNumberEqualNegOne(c *chk.C) {
  3707. containerURL, blobURL := setupClearPagesTest(c)
  3708. defer deleteContainer(c, containerURL)
  3709. _, err := blobURL.ClearPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1},
  3710. azblob.BlobAccessConditions{PageBlobAccessConditions: azblob.PageBlobAccessConditions{IfSequenceNumberEqual: -1}}) // This will cause the library to set the value of the header to 0
  3711. c.Assert(err, chk.IsNil)
  3712. validateClearPagesTest(c, blobURL)
  3713. }
  3714. func setupGetPageRangesTest(c *chk.C) (containerURL azblob.ContainerURL, blobURL azblob.PageBlobURL) {
  3715. bsu := getBSU()
  3716. containerURL, _ = createNewContainer(c, bsu)
  3717. blobURL, _ = createNewPageBlob(c, containerURL)
  3718. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3719. c.Assert(err, chk.IsNil)
  3720. return
  3721. }
  3722. func validateBasicGetPageRanges(c *chk.C, resp *azblob.PageList, err error) {
  3723. c.Assert(err, chk.IsNil)
  3724. c.Assert(resp.PageRange, chk.HasLen, 1)
  3725. c.Assert(resp.PageRange[0], chk.Equals, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1})
  3726. }
  3727. func (s *aztestsSuite) TestBlobGetPageRangesEmptyBlob(c *chk.C) {
  3728. bsu := getBSU()
  3729. containerURL, _ := createNewContainer(c, bsu)
  3730. defer deleteContainer(c, containerURL)
  3731. blobURL, _ := createNewPageBlob(c, containerURL)
  3732. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3733. c.Assert(err, chk.IsNil)
  3734. c.Assert(resp.PageRange, chk.HasLen, 0)
  3735. }
  3736. func (s *aztestsSuite) TestBlobGetPageRangesEmptyRange(c *chk.C) {
  3737. containerURL, blobURL := setupGetPageRangesTest(c)
  3738. defer deleteContainer(c, containerURL)
  3739. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3740. validateBasicGetPageRanges(c, resp, err)
  3741. }
  3742. func (s *aztestsSuite) TestBlobGetPageRangesInvalidRange(c *chk.C) {
  3743. containerURL, blobURL := setupGetPageRangesTest(c)
  3744. defer deleteContainer(c, containerURL)
  3745. defer func() { // Invalid blob range should panic
  3746. recover()
  3747. }()
  3748. blobURL.GetPageRanges(ctx, azblob.BlobRange{Offset: -2, Count: 500}, azblob.BlobAccessConditions{})
  3749. c.Fail()
  3750. }
  3751. func (s *aztestsSuite) TestBlobGetPageRangesNonContiguousRanges(c *chk.C) {
  3752. containerURL, blobURL := setupGetPageRangesTest(c)
  3753. defer deleteContainer(c, containerURL)
  3754. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: azblob.PageBlobPageBytes * 2, End: (azblob.PageBlobPageBytes * 3) - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3755. c.Assert(err, chk.IsNil)
  3756. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3757. c.Assert(err, chk.IsNil)
  3758. c.Assert(resp.PageRange, chk.HasLen, 2)
  3759. c.Assert(resp.PageRange[0], chk.Equals, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1})
  3760. c.Assert(resp.PageRange[1], chk.Equals, azblob.PageRange{Start: azblob.PageBlobPageBytes * 2, End: (azblob.PageBlobPageBytes * 3) - 1})
  3761. }
  3762. func (s *aztestsSuite) TestblobGetPageRangesNotPageAligned(c *chk.C) {
  3763. containerURL, blobURL := setupGetPageRangesTest(c)
  3764. defer deleteContainer(c, containerURL)
  3765. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{Offset: 0, Count: 2000}, azblob.BlobAccessConditions{})
  3766. c.Assert(err, chk.IsNil)
  3767. validateBasicGetPageRanges(c, resp, err)
  3768. }
  3769. func (s *aztestsSuite) TestBlobGetPageRangesSnapshot(c *chk.C) {
  3770. containerURL, blobURL := setupGetPageRangesTest(c)
  3771. defer deleteContainer(c, containerURL)
  3772. resp, _ := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  3773. snapshotURL := blobURL.WithSnapshot(resp.Snapshot())
  3774. resp2, err := snapshotURL.GetPageRanges(ctx, azblob.BlobRange{}, azblob.BlobAccessConditions{})
  3775. c.Assert(err, chk.IsNil)
  3776. validateBasicGetPageRanges(c, resp2, err)
  3777. }
  3778. func (s *aztestsSuite) TestBlobGetPageRangesIfModifiedSinceTrue(c *chk.C) {
  3779. containerURL, blobURL := setupGetPageRangesTest(c)
  3780. defer deleteContainer(c, containerURL)
  3781. currentTime := getRelativeTimeGMT(-10)
  3782. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3783. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3784. validateBasicGetPageRanges(c, resp, err)
  3785. }
  3786. func (s *aztestsSuite) TestBlobGetPageRangesIfModifiedSinceFalse(c *chk.C) {
  3787. containerURL, blobURL := setupGetPageRangesTest(c)
  3788. defer deleteContainer(c, containerURL)
  3789. currentTime := getRelativeTimeGMT(10)
  3790. _, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3791. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3792. serr := err.(azblob.StorageError)
  3793. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // Service Code not returned in the body for a HEAD
  3794. }
  3795. func (s *aztestsSuite) TestBlobGetPageRangesIfUnmodifiedSinceTrue(c *chk.C) {
  3796. containerURL, blobURL := setupGetPageRangesTest(c)
  3797. defer deleteContainer(c, containerURL)
  3798. currentTime := getRelativeTimeGMT(10)
  3799. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3800. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3801. validateBasicGetPageRanges(c, resp, err)
  3802. }
  3803. func (s *aztestsSuite) TestBlobGetPageRangesIfUnmodifiedSinceFalse(c *chk.C) {
  3804. containerURL, blobURL := setupGetPageRangesTest(c)
  3805. defer deleteContainer(c, containerURL)
  3806. currentTime := getRelativeTimeGMT(-10)
  3807. _, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3808. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3809. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3810. }
  3811. func (s *aztestsSuite) TestBlobGetPageRangesIfMatchTrue(c *chk.C) {
  3812. containerURL, blobURL := setupGetPageRangesTest(c)
  3813. defer deleteContainer(c, containerURL)
  3814. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3815. resp2, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3816. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3817. validateBasicGetPageRanges(c, resp2, err)
  3818. }
  3819. func (s *aztestsSuite) TestBlobGetPageRangesIfMatchFalse(c *chk.C) {
  3820. containerURL, blobURL := setupGetPageRangesTest(c)
  3821. defer deleteContainer(c, containerURL)
  3822. _, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3823. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3824. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3825. }
  3826. func (s *aztestsSuite) TestBlobGetPageRangesIfNoneMatchTrue(c *chk.C) {
  3827. containerURL, blobURL := setupGetPageRangesTest(c)
  3828. defer deleteContainer(c, containerURL)
  3829. resp, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3830. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3831. validateBasicGetPageRanges(c, resp, err)
  3832. }
  3833. func (s *aztestsSuite) TestBlobGetPageRangesIfNoneMatchFalse(c *chk.C) {
  3834. containerURL, blobURL := setupGetPageRangesTest(c)
  3835. defer deleteContainer(c, containerURL)
  3836. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3837. _, err := blobURL.GetPageRanges(ctx, azblob.BlobRange{},
  3838. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3839. serr := err.(azblob.StorageError)
  3840. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // Service Code not returned in the body for a HEAD
  3841. }
  3842. func setupDiffPageRangesTest(c *chk.C) (containerURL azblob.ContainerURL, blobURL azblob.PageBlobURL, snapshot time.Time) {
  3843. bsu := getBSU()
  3844. containerURL, _ = createNewContainer(c, bsu)
  3845. blobURL, _ = createNewPageBlob(c, containerURL)
  3846. _, err := blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3847. c.Assert(err, chk.IsNil)
  3848. resp, err := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  3849. c.Assert(err, chk.IsNil)
  3850. snapshot = resp.Snapshot()
  3851. _, err = blobURL.PutPages(ctx, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1}, getReaderToRandomBytes(azblob.PageBlobPageBytes), azblob.BlobAccessConditions{})
  3852. c.Assert(err, chk.IsNil) // This ensures there is a diff on the first page
  3853. return
  3854. }
  3855. func validateDiffPageRanges(c *chk.C, resp *azblob.PageList, err error) {
  3856. c.Assert(err, chk.IsNil)
  3857. c.Assert(resp.PageRange, chk.HasLen, 1)
  3858. c.Assert(resp.PageRange[0], chk.Equals, azblob.PageRange{Start: 0, End: azblob.PageBlobPageBytes - 1})
  3859. }
  3860. func (s *aztestsSuite) TestBlobDiffPageRangesNonExistantSnapshot(c *chk.C) {
  3861. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3862. defer deleteContainer(c, containerURL)
  3863. _, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot.Add(time.Minute), azblob.BlobAccessConditions{})
  3864. validateStorageError(c, err, azblob.ServiceCodePreviousSnapshotNotFound)
  3865. }
  3866. func (s *aztestsSuite) TestBlobDiffPageRangeInvalidRange(c *chk.C) {
  3867. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3868. defer deleteContainer(c, containerURL)
  3869. defer func() { // Invalid page range should panic
  3870. recover()
  3871. }()
  3872. blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{Offset: -22, Count: 14}, snapshot, azblob.BlobAccessConditions{})
  3873. c.Fail()
  3874. }
  3875. func (s *aztestsSuite) TestBlobDiffPageRangeIfModifiedSinceTrue(c *chk.C) {
  3876. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3877. defer deleteContainer(c, containerURL)
  3878. currentTime := getRelativeTimeGMT(-10)
  3879. resp, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3880. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3881. validateDiffPageRanges(c, resp, err)
  3882. }
  3883. func (s *aztestsSuite) TestBlobDiffPageRangeIfModifiedSinceFalse(c *chk.C) {
  3884. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3885. defer deleteContainer(c, containerURL)
  3886. currentTime := getRelativeTimeGMT(10)
  3887. _, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3888. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3889. serr := err.(azblob.StorageError)
  3890. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // Service Code not returned in the body for a HEAD
  3891. }
  3892. func (s *aztestsSuite) TestBlobDiffPageRangeIfUnmodifiedSinceTrue(c *chk.C) {
  3893. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3894. defer deleteContainer(c, containerURL)
  3895. currentTime := getRelativeTimeGMT(10)
  3896. resp, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3897. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3898. validateDiffPageRanges(c, resp, err)
  3899. }
  3900. func (s *aztestsSuite) TestBlobDiffPageRangeIfUnmodifiedSinceFalse(c *chk.C) {
  3901. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3902. defer deleteContainer(c, containerURL)
  3903. currentTime := getRelativeTimeGMT(-10)
  3904. _, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3905. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  3906. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3907. }
  3908. func (s *aztestsSuite) TestBlobDiffPageRangeIfMatchTrue(c *chk.C) {
  3909. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3910. defer deleteContainer(c, containerURL)
  3911. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3912. resp2, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3913. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  3914. validateDiffPageRanges(c, resp2, err)
  3915. }
  3916. func (s *aztestsSuite) TestBlobDiffPageRangeIfMatchFalse(c *chk.C) {
  3917. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3918. defer deleteContainer(c, containerURL)
  3919. _, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3920. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  3921. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3922. }
  3923. func (s *aztestsSuite) TestBlobDiffPageRangeIfNoneMatchTrue(c *chk.C) {
  3924. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3925. defer deleteContainer(c, containerURL)
  3926. resp, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3927. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  3928. validateDiffPageRanges(c, resp, err)
  3929. }
  3930. func (s *aztestsSuite) TestBlobDiffPageRangeIfNoneMatchFalse(c *chk.C) {
  3931. containerURL, blobURL, snapshot := setupDiffPageRangesTest(c)
  3932. defer deleteContainer(c, containerURL)
  3933. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3934. _, err := blobURL.GetPageRangesDiff(ctx, azblob.BlobRange{}, snapshot,
  3935. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  3936. serr := err.(azblob.StorageError)
  3937. c.Assert(serr.Response().StatusCode, chk.Equals, 304) // Service Code not returned in the body for a HEAD
  3938. }
  3939. func (s *aztestsSuite) TestBlobResizeZero(c *chk.C) {
  3940. bsu := getBSU()
  3941. containerURL, _ := createNewContainer(c, bsu)
  3942. defer deleteContainer(c, containerURL)
  3943. blobURL, _ := createNewPageBlob(c, containerURL)
  3944. // The default blob is created with size > 0, so this should actually update
  3945. _, err := blobURL.Resize(ctx, 0, azblob.BlobAccessConditions{})
  3946. c.Assert(err, chk.IsNil)
  3947. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3948. c.Assert(err, chk.IsNil)
  3949. c.Assert(resp.ContentLength(), chk.Equals, int64(0))
  3950. }
  3951. func (s *aztestsSuite) TestBlobResizeInvalidSizeNegative(c *chk.C) {
  3952. bsu := getBSU()
  3953. containerURL, _ := createNewContainer(c, bsu)
  3954. defer deleteContainer(c, containerURL)
  3955. blobURL, _ := createNewPageBlob(c, containerURL)
  3956. defer func() { // Negative size should panic
  3957. recover()
  3958. }()
  3959. blobURL.Resize(ctx, -4, azblob.BlobAccessConditions{})
  3960. c.Fail()
  3961. }
  3962. func (s *aztestsSuite) TestBlobResizeInvalidSizeMisaligned(c *chk.C) {
  3963. bsu := getBSU()
  3964. containerURL, _ := createNewContainer(c, bsu)
  3965. defer deleteContainer(c, containerURL)
  3966. blobURL, _ := createNewPageBlob(c, containerURL)
  3967. defer func() { // Invalid size should panic
  3968. recover()
  3969. }()
  3970. blobURL.Resize(ctx, 12, azblob.BlobAccessConditions{})
  3971. c.Fail()
  3972. }
  3973. func validateResize(c *chk.C, blobURL azblob.PageBlobURL) {
  3974. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  3975. c.Assert(resp.ContentLength(), chk.Equals, int64(azblob.PageBlobPageBytes))
  3976. }
  3977. func (s *aztestsSuite) TestBlobResizeIfModifiedSinceTrue(c *chk.C) {
  3978. bsu := getBSU()
  3979. containerURL, _ := createNewContainer(c, bsu)
  3980. defer deleteContainer(c, containerURL)
  3981. blobURL, _ := createNewPageBlob(c, containerURL)
  3982. currentTime := getRelativeTimeGMT(-10)
  3983. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  3984. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3985. c.Assert(err, chk.IsNil)
  3986. validateResize(c, blobURL)
  3987. }
  3988. func (s *aztestsSuite) TestBlobResizeIfModifiedSinceFalse(c *chk.C) {
  3989. bsu := getBSU()
  3990. containerURL, _ := createNewContainer(c, bsu)
  3991. defer deleteContainer(c, containerURL)
  3992. blobURL, _ := createNewPageBlob(c, containerURL)
  3993. currentTime := getRelativeTimeGMT(10)
  3994. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  3995. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  3996. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  3997. }
  3998. func (s *aztestsSuite) TestBlobResizeIfUnmodifiedSinceTrue(c *chk.C) {
  3999. bsu := getBSU()
  4000. containerURL, _ := createNewContainer(c, bsu)
  4001. defer deleteContainer(c, containerURL)
  4002. blobURL, _ := createNewPageBlob(c, containerURL)
  4003. currentTime := getRelativeTimeGMT(10)
  4004. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4005. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4006. c.Assert(err, chk.IsNil)
  4007. validateResize(c, blobURL)
  4008. }
  4009. func (s *aztestsSuite) TestBlobResizeIfUnmodifiedSinceFalse(c *chk.C) {
  4010. bsu := getBSU()
  4011. containerURL, _ := createNewContainer(c, bsu)
  4012. defer deleteContainer(c, containerURL)
  4013. blobURL, _ := createNewPageBlob(c, containerURL)
  4014. currentTime := getRelativeTimeGMT(-10)
  4015. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4016. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4017. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4018. }
  4019. func (s *aztestsSuite) TestBlobResizeIfMatchTrue(c *chk.C) {
  4020. bsu := getBSU()
  4021. containerURL, _ := createNewContainer(c, bsu)
  4022. defer deleteContainer(c, containerURL)
  4023. blobURL, _ := createNewPageBlob(c, containerURL)
  4024. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4025. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4026. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  4027. c.Assert(err, chk.IsNil)
  4028. validateResize(c, blobURL)
  4029. }
  4030. func (s *aztestsSuite) TestBlobResizeIfMatchFalse(c *chk.C) {
  4031. bsu := getBSU()
  4032. containerURL, _ := createNewContainer(c, bsu)
  4033. defer deleteContainer(c, containerURL)
  4034. blobURL, _ := createNewPageBlob(c, containerURL)
  4035. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4036. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  4037. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4038. }
  4039. func (s *aztestsSuite) TestBlobResizeIfNoneMatchTrue(c *chk.C) {
  4040. bsu := getBSU()
  4041. containerURL, _ := createNewContainer(c, bsu)
  4042. defer deleteContainer(c, containerURL)
  4043. blobURL, _ := createNewPageBlob(c, containerURL)
  4044. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4045. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  4046. c.Assert(err, chk.IsNil)
  4047. validateResize(c, blobURL)
  4048. }
  4049. func (s *aztestsSuite) TestBlobResizeIfNoneMatchFalse(c *chk.C) {
  4050. bsu := getBSU()
  4051. containerURL, _ := createNewContainer(c, bsu)
  4052. defer deleteContainer(c, containerURL)
  4053. blobURL, _ := createNewPageBlob(c, containerURL)
  4054. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4055. _, err := blobURL.Resize(ctx, azblob.PageBlobPageBytes,
  4056. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  4057. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4058. }
  4059. func (s *aztestsSuite) TestBlobSetSequenceNumberActionTypeInvalid(c *chk.C) {
  4060. bsu := getBSU()
  4061. containerURL, _ := createNewContainer(c, bsu)
  4062. defer deleteContainer(c, containerURL)
  4063. blobURL, _ := createNewPageBlob(c, containerURL)
  4064. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionType("garbage"), 1, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  4065. validateStorageError(c, err, azblob.ServiceCodeInvalidHeaderValue)
  4066. }
  4067. func (s *aztestsSuite) TestBlobSetSequenceNumberSequenceNumberInvalid(c *chk.C) {
  4068. bsu := getBSU()
  4069. containerURL, _ := createNewContainer(c, bsu)
  4070. defer deleteContainer(c, containerURL)
  4071. blobURL, _ := createNewPageBlob(c, containerURL)
  4072. defer func() { // Invalid sequence number should panic
  4073. recover()
  4074. }()
  4075. blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionUpdate, -1, azblob.BlobHTTPHeaders{}, azblob.BlobAccessConditions{})
  4076. }
  4077. func (s *aztestsSuite) TestBlobSetSequenceNumberHTTPHeaders(c *chk.C) {
  4078. bsu := getBSU()
  4079. containerURL, _ := createNewContainer(c, bsu)
  4080. defer deleteContainer(c, containerURL)
  4081. blobURL, _ := createNewPageBlob(c, containerURL)
  4082. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, basicHeaders, azblob.BlobAccessConditions{})
  4083. c.Assert(err, chk.IsNil)
  4084. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4085. c.Assert(err, chk.IsNil)
  4086. c.Assert(resp.NewHTTPHeaders(), chk.DeepEquals, basicHeaders)
  4087. }
  4088. func validateSequenceNumberSet(c *chk.C, blobURL azblob.PageBlobURL) {
  4089. resp, err := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4090. c.Assert(err, chk.IsNil)
  4091. c.Assert(resp.BlobSequenceNumber(), chk.Equals, "1")
  4092. }
  4093. func (s *aztestsSuite) TestBlobSetSequenceNumberIfModifiedSinceTrue(c *chk.C) {
  4094. bsu := getBSU()
  4095. containerURL, _ := createNewContainer(c, bsu)
  4096. defer deleteContainer(c, containerURL)
  4097. blobURL, _ := createNewPageBlob(c, containerURL)
  4098. currentTime := getRelativeTimeGMT(-10)
  4099. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4100. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  4101. c.Assert(err, chk.IsNil)
  4102. validateSequenceNumberSet(c, blobURL)
  4103. }
  4104. func (s *aztestsSuite) TestBlobSetSequenceNumberIfModifiedSinceFalse(c *chk.C) {
  4105. bsu := getBSU()
  4106. containerURL, _ := createNewContainer(c, bsu)
  4107. defer deleteContainer(c, containerURL)
  4108. blobURL, _ := createNewPageBlob(c, containerURL)
  4109. currentTime := getRelativeTimeGMT(10)
  4110. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4111. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  4112. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4113. }
  4114. func (s *aztestsSuite) TestBlobSetSequenceNumberIfUnmodifiedSinceTrue(c *chk.C) {
  4115. bsu := getBSU()
  4116. containerURL, _ := createNewContainer(c, bsu)
  4117. defer deleteContainer(c, containerURL)
  4118. blobURL, _ := createNewPageBlob(c, containerURL)
  4119. currentTime := getRelativeTimeGMT(10)
  4120. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4121. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4122. c.Assert(err, chk.IsNil)
  4123. validateSequenceNumberSet(c, blobURL)
  4124. }
  4125. func (s *aztestsSuite) TestBlobSetSequenceNumberIfUnmodifiedSinceFalse(c *chk.C) {
  4126. bsu := getBSU()
  4127. containerURL, _ := createNewContainer(c, bsu)
  4128. defer deleteContainer(c, containerURL)
  4129. blobURL, _ := createNewPageBlob(c, containerURL)
  4130. currentTime := getRelativeTimeGMT(-10)
  4131. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4132. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4133. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4134. }
  4135. func (s *aztestsSuite) TestBlobSetSequenceNumberIfMatchTrue(c *chk.C) {
  4136. bsu := getBSU()
  4137. containerURL, _ := createNewContainer(c, bsu)
  4138. defer deleteContainer(c, containerURL)
  4139. blobURL, _ := createNewPageBlob(c, containerURL)
  4140. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4141. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4142. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  4143. c.Assert(err, chk.IsNil)
  4144. validateSequenceNumberSet(c, blobURL)
  4145. }
  4146. func (s *aztestsSuite) TestBlobSetSequenceNumberIfMatchFalse(c *chk.C) {
  4147. bsu := getBSU()
  4148. containerURL, _ := createNewContainer(c, bsu)
  4149. defer deleteContainer(c, containerURL)
  4150. blobURL, _ := createNewPageBlob(c, containerURL)
  4151. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4152. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  4153. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4154. }
  4155. func (s *aztestsSuite) TestBlobSetSequenceNumberIfNoneMatchTrue(c *chk.C) {
  4156. bsu := getBSU()
  4157. containerURL, _ := createNewContainer(c, bsu)
  4158. defer deleteContainer(c, containerURL)
  4159. blobURL, _ := createNewPageBlob(c, containerURL)
  4160. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4161. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  4162. c.Assert(err, chk.IsNil)
  4163. validateSequenceNumberSet(c, blobURL)
  4164. }
  4165. func (s *aztestsSuite) TestBlobSetSequenceNumberIfNoneMatchFalse(c *chk.C) {
  4166. bsu := getBSU()
  4167. containerURL, _ := createNewContainer(c, bsu)
  4168. defer deleteContainer(c, containerURL)
  4169. blobURL, _ := createNewPageBlob(c, containerURL)
  4170. resp, _ := blobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4171. _, err := blobURL.SetSequenceNumber(ctx, azblob.SequenceNumberActionIncrement, 0, azblob.BlobHTTPHeaders{},
  4172. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  4173. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4174. }
  4175. func waitForIncrementalCopy(c *chk.C, copyBlobURL azblob.PageBlobURL, blobCopyResponse *azblob.PageBlobsIncrementalCopyResponse) time.Time {
  4176. status := blobCopyResponse.CopyStatus()
  4177. var getPropertiesAndMetadataResult *azblob.BlobsGetPropertiesResponse
  4178. // Wait for the copy to finish
  4179. start := time.Now()
  4180. for status != azblob.CopyStatusSuccess {
  4181. getPropertiesAndMetadataResult, _ = copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4182. status = getPropertiesAndMetadataResult.CopyStatus()
  4183. currentTime := time.Now()
  4184. if currentTime.Sub(start) >= time.Minute {
  4185. c.Fail()
  4186. }
  4187. }
  4188. return getPropertiesAndMetadataResult.DestinationSnapshot()
  4189. }
  4190. func setupStartIncrementalCopyTest(c *chk.C) (containerURL azblob.ContainerURL, blobURL azblob.PageBlobURL, copyBlobURL azblob.PageBlobURL, snapshot time.Time) {
  4191. bsu := getBSU()
  4192. containerURL, _ = createNewContainer(c, bsu)
  4193. blobURL, _ = createNewPageBlob(c, containerURL)
  4194. resp, _ := blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{})
  4195. copyBlobURL, _ = getPageBlobURL(c, containerURL)
  4196. // Must create the incremental copy blob so that the access conditions work on it
  4197. resp2, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), resp.Snapshot(), azblob.BlobAccessConditions{})
  4198. c.Assert(err, chk.IsNil)
  4199. waitForIncrementalCopy(c, copyBlobURL, resp2)
  4200. resp, _ = blobURL.CreateSnapshot(ctx, nil, azblob.BlobAccessConditions{}) // Take a new snapshot so the next copy will succeed
  4201. snapshot = resp.Snapshot()
  4202. return
  4203. }
  4204. func validateIncrementalCopy(c *chk.C, copyBlobURL azblob.PageBlobURL, resp *azblob.PageBlobsIncrementalCopyResponse) {
  4205. t := waitForIncrementalCopy(c, copyBlobURL, resp)
  4206. // If we can access the snapshot without error, we are satisfied that it was created as a result of the copy
  4207. copySnapshotURL := copyBlobURL.WithSnapshot(t)
  4208. _, err := copySnapshotURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4209. c.Assert(err, chk.IsNil)
  4210. }
  4211. func (s *aztestsSuite) TestBlobStartIncrementalCopySnapshotNotExist(c *chk.C) {
  4212. bsu := getBSU()
  4213. containerURL, _ := createNewContainer(c, bsu)
  4214. defer deleteContainer(c, containerURL)
  4215. blobURL, _ := createNewPageBlob(c, containerURL)
  4216. copyBlobURL, _ := getPageBlobURL(c, containerURL)
  4217. _, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), time.Now(), azblob.BlobAccessConditions{})
  4218. validateStorageError(c, err, azblob.ServiceCodeInvalidHeaderValue)
  4219. }
  4220. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfModifiedSinceTrue(c *chk.C) {
  4221. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4222. defer deleteContainer(c, containerURL)
  4223. currentTime := getRelativeTimeGMT(-20)
  4224. resp, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4225. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  4226. c.Assert(err, chk.IsNil)
  4227. validateIncrementalCopy(c, copyBlobURL, resp)
  4228. }
  4229. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfModifiedSinceFalse(c *chk.C) {
  4230. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4231. defer deleteContainer(c, containerURL)
  4232. currentTime := getRelativeTimeGMT(20)
  4233. _, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4234. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfModifiedSince: currentTime}})
  4235. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4236. }
  4237. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfUnmodifiedSinceTrue(c *chk.C) {
  4238. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4239. defer deleteContainer(c, containerURL)
  4240. currentTime := getRelativeTimeGMT(20)
  4241. resp, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4242. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4243. c.Assert(err, chk.IsNil)
  4244. validateIncrementalCopy(c, copyBlobURL, resp)
  4245. }
  4246. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfUnmodifiedSinceFalse(c *chk.C) {
  4247. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4248. defer deleteContainer(c, containerURL)
  4249. currentTime := getRelativeTimeGMT(-20)
  4250. _, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4251. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfUnmodifiedSince: currentTime}})
  4252. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4253. }
  4254. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfMatchTrue(c *chk.C) {
  4255. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4256. defer deleteContainer(c, containerURL)
  4257. resp, _ := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4258. resp2, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4259. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: resp.ETag()}})
  4260. c.Assert(err, chk.IsNil)
  4261. validateIncrementalCopy(c, copyBlobURL, resp2)
  4262. }
  4263. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfMatchFalse(c *chk.C) {
  4264. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4265. defer deleteContainer(c, containerURL)
  4266. _, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4267. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfMatch: azblob.ETag("garbage")}})
  4268. validateStorageError(c, err, azblob.ServiceCodeTargetConditionNotMet)
  4269. }
  4270. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfNoneMatchTrue(c *chk.C) {
  4271. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4272. defer deleteContainer(c, containerURL)
  4273. resp, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4274. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: azblob.ETag("garbage")}})
  4275. c.Assert(err, chk.IsNil)
  4276. validateIncrementalCopy(c, copyBlobURL, resp)
  4277. }
  4278. func (s *aztestsSuite) TestBlobStartIncrementalCopyIfNoneMatchFalse(c *chk.C) {
  4279. containerURL, blobURL, copyBlobURL, snapshot := setupStartIncrementalCopyTest(c)
  4280. defer deleteContainer(c, containerURL)
  4281. resp, _ := copyBlobURL.GetPropertiesAndMetadata(ctx, azblob.BlobAccessConditions{})
  4282. _, err := copyBlobURL.StartIncrementalCopy(ctx, blobURL.URL(), snapshot,
  4283. azblob.BlobAccessConditions{HTTPAccessConditions: azblob.HTTPAccessConditions{IfNoneMatch: resp.ETag()}})
  4284. validateStorageError(c, err, azblob.ServiceCodeConditionNotMet)
  4285. }