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.

153 lines
3.3 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. </Rule>
  37. <Rule>
  38. <ID>123422</ID>
  39. <Filter>
  40. <Prefix>gg</Prefix>
  41. </Filter>
  42. <Status>Disabled</Status>
  43. <Expiration>
  44. <Days>10</Days>
  45. </Expiration>
  46. </Rule>
  47. </LifecycleConfiguration>`)
  48. })
  49. ref, _, err := client.Bucket.GetLifecycle(context.Background())
  50. if err != nil {
  51. t.Fatalf("Bucket.GetLifecycle returned error: %v", err)
  52. }
  53. want := &BucketGetLifecycleResult{
  54. XMLName: xml.Name{Local: "LifecycleConfiguration"},
  55. Rules: []BucketLifecycleRule{
  56. {
  57. ID: "1234",
  58. Filter: &BucketLifecycleFilter{
  59. And: &BucketLifecycleAndOperator{
  60. Prefix: "test",
  61. Tag: []BucketTaggingTag{
  62. {Key: "key", Value: "value"},
  63. },
  64. },
  65. },
  66. Status: "Enabled",
  67. Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
  68. },
  69. {
  70. ID: "123422",
  71. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  72. Status: "Disabled",
  73. Expiration: &BucketLifecycleExpiration{Days: 10},
  74. },
  75. },
  76. }
  77. if !reflect.DeepEqual(ref, want) {
  78. t.Errorf("Bucket.GetLifecycle returned %+v, want %+v", ref, want)
  79. }
  80. }
  81. func TestBucketService_PutLifecycle(t *testing.T) {
  82. setup()
  83. defer teardown()
  84. opt := &BucketPutLifecycleOptions{
  85. Rules: []BucketLifecycleRule{
  86. {
  87. ID: "1234",
  88. Filter: &BucketLifecycleFilter{Prefix: "test"},
  89. Status: "Enabled",
  90. Transition: &BucketLifecycleTransition{Days: 10, StorageClass: "Standard"},
  91. },
  92. {
  93. ID: "123422",
  94. Filter: &BucketLifecycleFilter{Prefix: "gg"},
  95. Status: "Disabled",
  96. Expiration: &BucketLifecycleExpiration{Days: 10},
  97. },
  98. },
  99. }
  100. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  101. v := new(BucketPutLifecycleOptions)
  102. xml.NewDecoder(r.Body).Decode(v)
  103. testMethod(t, r, http.MethodPut)
  104. vs := values{
  105. "lifecycle": "",
  106. }
  107. testFormValues(t, r, vs)
  108. want := opt
  109. want.XMLName = xml.Name{Local: "LifecycleConfiguration"}
  110. if !reflect.DeepEqual(v, want) {
  111. t.Errorf("Bucket.PutLifecycle request body: %+v, want %+v", v, want)
  112. }
  113. })
  114. _, err := client.Bucket.PutLifecycle(context.Background(), opt)
  115. if err != nil {
  116. t.Fatalf("Bucket.PutLifecycle returned error: %v", err)
  117. }
  118. }
  119. func TestBucketService_DeleteLifecycle(t *testing.T) {
  120. setup()
  121. defer teardown()
  122. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  123. testMethod(t, r, http.MethodDelete)
  124. vs := values{
  125. "lifecycle": "",
  126. }
  127. testFormValues(t, r, vs)
  128. w.WriteHeader(http.StatusNoContent)
  129. })
  130. _, err := client.Bucket.DeleteLifecycle(context.Background())
  131. if err != nil {
  132. t.Fatalf("Bucket.DeleteLifecycle returned error: %v", err)
  133. }
  134. }