|
| 1 | +package cos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBucketService_PutInventory(t *testing.T) { |
| 13 | + setup() |
| 14 | + defer teardown() |
| 15 | + opt := &BucketPutInventoryOptions{ |
| 16 | + XMLName: xml.Name{Local: "InventoryConfiguration"}, |
| 17 | + ID: "list1", |
| 18 | + IsEnabled: "True", |
| 19 | + IncludedObjectVersions: "All", |
| 20 | + Filter: &BucketInventoryFilter{"myPrefix"}, |
| 21 | + Schedule: &BucketInventorySchedule{"Daily"}, |
| 22 | + Destination: &BucketInventoryDestination{ |
| 23 | + Bucket: "qcs::cos:ap-guangzhou::examplebucket-1250000000", |
| 24 | + AccountId: "100000000001", |
| 25 | + Prefix: "list1", |
| 26 | + Format: "CSV", |
| 27 | + Encryption: &BucketInventoryEncryption{}, |
| 28 | + }, |
| 29 | + OptionalFields: &BucketInventoryOptionalFields{ |
| 30 | + BucketInventoryFields: []string{ |
| 31 | + "Size", |
| 32 | + "LastModifiedDate", |
| 33 | + "ETag", |
| 34 | + "StorageClass", |
| 35 | + "IsMultipartUploaded", |
| 36 | + "ReplicationStatus", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 41 | + testMethod(t, r, http.MethodPut) |
| 42 | + vs := values{ |
| 43 | + "inventory": "", |
| 44 | + "id": "list1", |
| 45 | + } |
| 46 | + testFormValues(t, r, vs) |
| 47 | + |
| 48 | + body := &BucketPutInventoryOptions{} |
| 49 | + xml.NewDecoder(r.Body).Decode(body) |
| 50 | + want := opt |
| 51 | + if !reflect.DeepEqual(want, body) { |
| 52 | + t.Fatalf("Bucket.PutInventory request\n body: %+v\n, want %+v\n", body, want) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + _, err := client.Bucket.PutInventory(context.Background(), "list1", opt) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("Bucket.PutInventory failed, error: %v", err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestBucketService_GetInventory(t *testing.T) { |
| 63 | + setup() |
| 64 | + defer teardown() |
| 65 | + |
| 66 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 67 | + testMethod(t, r, http.MethodGet) |
| 68 | + vs := values{ |
| 69 | + "inventory": "", |
| 70 | + "id": "list1", |
| 71 | + } |
| 72 | + testFormValues(t, r, vs) |
| 73 | + |
| 74 | + fmt.Fprint(w, `<InventoryConfiguration> |
| 75 | + <Id>list1</Id> |
| 76 | + <IsEnabled>True</IsEnabled> |
| 77 | + <Destination> |
| 78 | + <COSBucketDestination> |
| 79 | + <Format>CSV</Format> |
| 80 | + <Bucket>qcs::cos:ap-guangzhou::examplebucket-1250000000</Bucket> |
| 81 | + <Prefix>list1</Prefix> |
| 82 | + <AccountId>100000000001</AccountId> |
| 83 | + </COSBucketDestination> |
| 84 | + </Destination> |
| 85 | + <Schedule> |
| 86 | + <Frequency>Daily</Frequency> |
| 87 | + </Schedule> |
| 88 | + <Filter> |
| 89 | + <Prefix>myPrefix</Prefix> |
| 90 | + </Filter> |
| 91 | + <IncludedObjectVersions>All</IncludedObjectVersions> |
| 92 | + <OptionalFields> |
| 93 | + <Field>Size</Field> |
| 94 | + <Field>LastModifiedDate</Field> |
| 95 | + <Field>ETag</Field> |
| 96 | + <Field>StorageClass</Field> |
| 97 | + <Field>IsMultipartUploaded</Field> |
| 98 | + <Field>ReplicationStatus</Field> |
| 99 | + </OptionalFields> |
| 100 | +</InventoryConfiguration>`) |
| 101 | + }) |
| 102 | + res, _, err := client.Bucket.GetInventory(context.Background(), "list1") |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Bucket.GetInventory failed, error: %v", err) |
| 105 | + } |
| 106 | + want := &BucketGetInventoryResult{ |
| 107 | + XMLName: xml.Name{Local: "InventoryConfiguration"}, |
| 108 | + ID: "list1", |
| 109 | + IsEnabled: "True", |
| 110 | + IncludedObjectVersions: "All", |
| 111 | + Filter: &BucketInventoryFilter{"myPrefix"}, |
| 112 | + Schedule: &BucketInventorySchedule{"Daily"}, |
| 113 | + Destination: &BucketInventoryDestination{ |
| 114 | + Bucket: "qcs::cos:ap-guangzhou::examplebucket-1250000000", |
| 115 | + AccountId: "100000000001", |
| 116 | + Prefix: "list1", |
| 117 | + Format: "CSV", |
| 118 | + }, |
| 119 | + OptionalFields: &BucketInventoryOptionalFields{ |
| 120 | + BucketInventoryFields: []string{ |
| 121 | + "Size", |
| 122 | + "LastModifiedDate", |
| 123 | + "ETag", |
| 124 | + "StorageClass", |
| 125 | + "IsMultipartUploaded", |
| 126 | + "ReplicationStatus", |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + if !reflect.DeepEqual(res, want) { |
| 132 | + t.Errorf("Bucket.GetInventory returned\n%+v, want\n%+v", res, want) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestBucketService_ListInventory(t *testing.T) { |
| 137 | + setup() |
| 138 | + defer teardown() |
| 139 | + |
| 140 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 141 | + testMethod(t, r, http.MethodGet) |
| 142 | + vs := values{ |
| 143 | + "inventory": "", |
| 144 | + } |
| 145 | + testFormValues(t, r, vs) |
| 146 | + fmt.Fprint(w, `<ListInventoryConfigurationResult> |
| 147 | + <InventoryConfiguration> |
| 148 | + <Id>list1</Id> |
| 149 | + <IsEnabled>True</IsEnabled> |
| 150 | + <Destination> |
| 151 | + <COSBucketDestination> |
| 152 | + <Format>CSV</Format> |
| 153 | + <AccountId>1250000000</AccountId> |
| 154 | + <Bucket>qcs::cos:ap-beijing::examplebucket-1250000000</Bucket> |
| 155 | + <Prefix>list1</Prefix> |
| 156 | + <Encryption> |
| 157 | + <SSE-COS/> |
| 158 | + </Encryption> |
| 159 | + </COSBucketDestination> |
| 160 | + </Destination> |
| 161 | + <Schedule> |
| 162 | + <Frequency>Daily</Frequency> |
| 163 | + </Schedule> |
| 164 | + <Filter> |
| 165 | + <Prefix>myPrefix</Prefix> |
| 166 | + </Filter> |
| 167 | + <IncludedObjectVersions>All</IncludedObjectVersions> |
| 168 | + <OptionalFields> |
| 169 | + <Field>Size</Field> |
| 170 | + <Field>LastModifiedDate</Field> |
| 171 | + <Field>ETag</Field> |
| 172 | + <Field>StorageClass</Field> |
| 173 | + <Field>IsMultipartUpload</Field> |
| 174 | + <Field>ReplicationStatus</Field> |
| 175 | + </OptionalFields> |
| 176 | + </InventoryConfiguration> |
| 177 | + <InventoryConfiguration> |
| 178 | + <Id>list2</Id> |
| 179 | + <IsEnabled>True</IsEnabled> |
| 180 | + <Destination> |
| 181 | + <COSBucketDestination> |
| 182 | + <Format>CSV</Format> |
| 183 | + <AccountId>1250000000</AccountId> |
| 184 | + <Bucket>qcs::cos:ap-beijing::examplebucket-1250000000</Bucket> |
| 185 | + <Prefix>list2</Prefix> |
| 186 | + </COSBucketDestination> |
| 187 | + </Destination> |
| 188 | + <Schedule> |
| 189 | + <Frequency>Weekly</Frequency> |
| 190 | + </Schedule> |
| 191 | + <Filter> |
| 192 | + <Prefix>myPrefix2</Prefix> |
| 193 | + </Filter> |
| 194 | + <IncludedObjectVersions>All</IncludedObjectVersions> |
| 195 | + <OptionalFields> |
| 196 | + <Field>Size</Field> |
| 197 | + <Field>LastModifiedDate</Field> |
| 198 | + <Field>ETag</Field> |
| 199 | + <Field>StorageClass</Field> |
| 200 | + </OptionalFields> |
| 201 | + </InventoryConfiguration> |
| 202 | + <IsTruncated>false</IsTruncated> |
| 203 | + <ContinuationToken>...</ContinuationToken> |
| 204 | + <IsTruncated>true</IsTruncated> |
| 205 | + <NextContinuationToken>1ueSDFASDF1Tr/XDAFdadEADadf2J/wm36Hy4vbOwM=</NextContinuationToken> |
| 206 | +</ListInventoryConfigurationResult>`) |
| 207 | + }) |
| 208 | + |
| 209 | + res, _, err := client.Bucket.ListInventoryConfigurations(context.Background(), "") |
| 210 | + if err != nil { |
| 211 | + t.Fatalf("Bucket.ListInventory failed, error: %v", err) |
| 212 | + } |
| 213 | + want := &ListBucketInventoryConfigResult{ |
| 214 | + XMLName: xml.Name{Local: "ListInventoryConfigurationResult"}, |
| 215 | + IsTruncated: true, |
| 216 | + ContinuationToken: "...", |
| 217 | + NextContinuationToken: "1ueSDFASDF1Tr/XDAFdadEADadf2J/wm36Hy4vbOwM=", |
| 218 | + InventoryConfigurations: []BucketListInventoryConfiguartion{ |
| 219 | + BucketListInventoryConfiguartion{ |
| 220 | + XMLName: xml.Name{Local: "InventoryConfiguration"}, |
| 221 | + ID: "list1", |
| 222 | + IsEnabled: "True", |
| 223 | + IncludedObjectVersions: "All", |
| 224 | + Filter: &BucketInventoryFilter{"myPrefix"}, |
| 225 | + Schedule: &BucketInventorySchedule{"Daily"}, |
| 226 | + Destination: &BucketInventoryDestination{ |
| 227 | + Bucket: "qcs::cos:ap-beijing::examplebucket-1250000000", |
| 228 | + AccountId: "1250000000", |
| 229 | + Prefix: "list1", |
| 230 | + Format: "CSV", |
| 231 | + Encryption: &BucketInventoryEncryption{}, |
| 232 | + }, |
| 233 | + OptionalFields: &BucketInventoryOptionalFields{ |
| 234 | + BucketInventoryFields: []string{ |
| 235 | + "Size", |
| 236 | + "LastModifiedDate", |
| 237 | + "ETag", |
| 238 | + "StorageClass", |
| 239 | + "IsMultipartUpload", |
| 240 | + "ReplicationStatus", |
| 241 | + }, |
| 242 | + }, |
| 243 | + }, |
| 244 | + BucketListInventoryConfiguartion{ |
| 245 | + XMLName: xml.Name{Local: "InventoryConfiguration"}, |
| 246 | + ID: "list2", |
| 247 | + IsEnabled: "True", |
| 248 | + IncludedObjectVersions: "All", |
| 249 | + Filter: &BucketInventoryFilter{"myPrefix2"}, |
| 250 | + Schedule: &BucketInventorySchedule{"Weekly"}, |
| 251 | + Destination: &BucketInventoryDestination{ |
| 252 | + Bucket: "qcs::cos:ap-beijing::examplebucket-1250000000", |
| 253 | + AccountId: "1250000000", |
| 254 | + Prefix: "list2", |
| 255 | + Format: "CSV", |
| 256 | + }, |
| 257 | + OptionalFields: &BucketInventoryOptionalFields{ |
| 258 | + BucketInventoryFields: []string{ |
| 259 | + "Size", |
| 260 | + "LastModifiedDate", |
| 261 | + "ETag", |
| 262 | + "StorageClass", |
| 263 | + }, |
| 264 | + }, |
| 265 | + }, |
| 266 | + }, |
| 267 | + } |
| 268 | + if !reflect.DeepEqual(res, want) { |
| 269 | + t.Fatalf("Bucket.ListInventory failed, \nwant: %+v\nres: %+v", want, res) |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +func TestBucketService_DeleteInventory(t *testing.T) { |
| 274 | + setup() |
| 275 | + defer teardown() |
| 276 | + |
| 277 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 278 | + testMethod(t, r, http.MethodDelete) |
| 279 | + vs := values{ |
| 280 | + "inventory": "", |
| 281 | + "id": "list1", |
| 282 | + } |
| 283 | + testFormValues(t, r, vs) |
| 284 | + |
| 285 | + w.WriteHeader(http.StatusNoContent) |
| 286 | + }) |
| 287 | + |
| 288 | + _, err := client.Bucket.DeleteInventory(context.Background(), "list1") |
| 289 | + if err != nil { |
| 290 | + t.Fatalf("Bucket.DeleteInventory returned error: %v", err) |
| 291 | + } |
| 292 | +} |
0 commit comments