You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.1 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_GetLifecycle(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  14. testMethod(t, r, http.MethodGet)
  15. vs := values{
  16. "lifecycle": "",
  17. }
  18. testFormValues(t, r, vs)
  19. fmt.Fprint(w, `<LifecycleConfiguration>
  20. <Rule>
  21. <ID>1234</ID>
  22. <Filter>
  23. <And>
  24. <Prefix>test</Prefix>
  25. <Tag>
  26. <Key>key</Key>
  27. <Value>value</Value>
  28. </Tag>
  29. </And>
  30. </Filter>
  31. <Status>Enabled</Status>
  32. <Transition>
  33. <Days>10</Days>
  34. <StorageClass>Standard</StorageClass>
  35. </Transition>
  36. <Expiration>
  37. <Days>10</Days>
  38. </Expiration>
  39. <NoncurrentVersionTransition>
  40. <NoncurrentDays>90</NoncurrentDays>
  41. <StorageClass>ARCHIVE</StorageClass>
  42. </NoncurrentVersionTransition>
  43. <NoncurrentVersionTransition>
  44. <NoncurrentDays>180</NoncurrentDays>
  45. <StorageClass>DEEP_ARCHIVE</StorageClass>
  46. </NoncurrentVersionTransition>
  47. <NoncurrentVersionExpiration>
  48. <NoncurrentDays>360</NoncurrentDays>
  49. </NoncurrentVersionExpiration>
  50. </Rule>
  51. <Rule>
  52. <ID>123422</ID>
  53. <Filter>
  54. <Prefix>gg</Prefix>
  55. </Filter>
  56. <Status>Disabled</Status>
  57. <Expiration>
  58. <Days>10</Days>
  59. </Expiration>
  60. </Rule>
  61. </LifecycleConfiguration>`)
  62. })
  63. ref, _, err := client.Bucket.GetLifecycle(context.Background())
  64. if err != nil {
  65. t.Fatalf("Bucket.GetLifecycle returned error: %v", err)
  66. }
  67. want := &BucketGetLifecycleResult{
  68. XMLName: xml.Name{Local: "LifecycleConfiguration"},
  69. Rules: []BucketLifecycleRule{
  70. {
  71. ID: "1234",
  72. Filter: &BucketLifecycleFilter{
  73. And: &BucketLifecycleAndOperator{
  74. Prefix: "test",
  75. Tag: []BucketTaggingTag{
  76. {Key: "key", Value: "value"},
  77. },
  78. },
  79. },
  80. Status: "Enabled",
  81. Transition: []BucketLifecycleTransition{
  82. {Days: 10, StorageClass: "Standard"},
  83. },
  84. Expiration: &BucketLifecycleExpiration{Days: 10},
  85. NoncurrentVersionExpiration: &BucketLifecycleNoncurrentVersion{
  86. NoncurrentDays: 360,
  87. },
  88. NoncurrentVersionTransition: []BucketLifecycleNoncurrentVersion{
  89. {
  90. NoncurrentDays: 90,
  91. StorageClass: "ARCHIVE",
  92. },
  93. {
  94. NoncurrentDays: 180,
  95. StorageClass: "DEEP_ARCHIVE",
  96. },
  97. },
  98. },
  99. {
  100. ID: "123422",
  101. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  102. Status: "Disabled",
  103. Expiration: &BucketLifecycleExpiration{Days: 10},
  104. },
  105. },
  106. }
  107. if !reflect.DeepEqual(ref, want) {
  108. t.Errorf("Bucket.GetLifecycle returned %+v, want %+v", ref, want)
  109. }
  110. }
  111. func TestBucketService_PutLifecycle(t *testing.T) {
  112. setup()
  113. defer teardown()
  114. opt := &BucketPutLifecycleOptions{
  115. Rules: []BucketLifecycleRule{
  116. {
  117. ID: "1234",
  118. Filter: &BucketLifecycleFilter{Prefix: "test"},
  119. Status: "Enabled",
  120. Transition: []BucketLifecycleTransition{
  121. {Days: 10, StorageClass: "Standard"},
  122. },
  123. },
  124. {
  125. ID: "123422",
  126. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  127. Status: "Disabled",
  128. Expiration: &BucketLifecycleExpiration{Days: 10},
  129. },
  130. },
  131. }
  132. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  133. v := new(BucketPutLifecycleOptions)
  134. xml.NewDecoder(r.Body).Decode(v)
  135. testMethod(t, r, http.MethodPut)
  136. vs := values{
  137. "lifecycle": "",
  138. }
  139. testFormValues(t, r, vs)
  140. want := opt
  141. want.XMLName = xml.Name{Local: "LifecycleConfiguration"}
  142. if !reflect.DeepEqual(v, want) {
  143. t.Errorf("Bucket.PutLifecycle request body: %+v, want %+v", v, want)
  144. }
  145. })
  146. _, err := client.Bucket.PutLifecycle(context.Background(), opt)
  147. if err != nil {
  148. t.Fatalf("Bucket.PutLifecycle returned error: %v", err)
  149. }
  150. }
  151. func TestBucketService_DeleteLifecycle(t *testing.T) {
  152. setup()
  153. defer teardown()
  154. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  155. testMethod(t, r, http.MethodDelete)
  156. vs := values{
  157. "lifecycle": "",
  158. }
  159. testFormValues(t, r, vs)
  160. w.WriteHeader(http.StatusNoContent)
  161. })
  162. _, err := client.Bucket.DeleteLifecycle(context.Background())
  163. if err != nil {
  164. t.Fatalf("Bucket.DeleteLifecycle returned error: %v", err)
  165. }
  166. }