|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/anhostfr/hangar/internal/service/bucket" |
| 10 | +) |
| 11 | + |
| 12 | +func TestS3BucketVersioningXML(t *testing.T) { |
| 13 | + s := newS3TestServer(t) |
| 14 | + if _, err := bucket.CreateBucket(&bucket.CreateBucketRequest{Name: "vxb"}); err != nil { |
| 15 | + t.Fatalf("bucket: %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + resp := s.do(t, http.MethodGet, "/vxb", "versioning=", nil) |
| 19 | + body, _ := io.ReadAll(resp.Body) |
| 20 | + resp.Body.Close() |
| 21 | + if resp.StatusCode != http.StatusOK { |
| 22 | + t.Fatalf("get initial: %d", resp.StatusCode) |
| 23 | + } |
| 24 | + var got VersioningConfigurationXML |
| 25 | + if err := xml.Unmarshal(body, &got); err != nil { |
| 26 | + t.Fatalf("decode: %v", err) |
| 27 | + } |
| 28 | + if got.Status != "" { |
| 29 | + t.Fatalf("expected empty status before enabling, got %q", got.Status) |
| 30 | + } |
| 31 | + |
| 32 | + putBody := []byte(`<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>Enabled</Status></VersioningConfiguration>`) |
| 33 | + resp = s.do(t, http.MethodPut, "/vxb", "versioning=", putBody) |
| 34 | + resp.Body.Close() |
| 35 | + if resp.StatusCode != http.StatusOK { |
| 36 | + t.Fatalf("put enable: %d", resp.StatusCode) |
| 37 | + } |
| 38 | + |
| 39 | + info, err := bucket.GetBucket("vxb") |
| 40 | + if err != nil || !info.VersioningEnabled { |
| 41 | + t.Fatalf("expected versioning enabled in storage, info=%+v err=%v", info, err) |
| 42 | + } |
| 43 | + |
| 44 | + resp = s.do(t, http.MethodGet, "/vxb", "versioning=", nil) |
| 45 | + body, _ = io.ReadAll(resp.Body) |
| 46 | + resp.Body.Close() |
| 47 | + if err := xml.Unmarshal(body, &got); err != nil { |
| 48 | + t.Fatalf("decode after enable: %v", err) |
| 49 | + } |
| 50 | + if got.Status != "Enabled" { |
| 51 | + t.Fatalf("status=%q want Enabled", got.Status) |
| 52 | + } |
| 53 | + |
| 54 | + putBody = []byte(`<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>Suspended</Status></VersioningConfiguration>`) |
| 55 | + resp = s.do(t, http.MethodPut, "/vxb", "versioning=", putBody) |
| 56 | + resp.Body.Close() |
| 57 | + if resp.StatusCode != http.StatusOK { |
| 58 | + t.Fatalf("put suspend: %d", resp.StatusCode) |
| 59 | + } |
| 60 | + info, _ = bucket.GetBucket("vxb") |
| 61 | + if info.VersioningEnabled { |
| 62 | + t.Fatalf("expected versioning disabled after Suspended") |
| 63 | + } |
| 64 | + |
| 65 | + putBody = []byte(`<VersioningConfiguration><Status>Bogus</Status></VersioningConfiguration>`) |
| 66 | + resp = s.do(t, http.MethodPut, "/vxb", "versioning=", putBody) |
| 67 | + resp.Body.Close() |
| 68 | + if resp.StatusCode != http.StatusBadRequest { |
| 69 | + t.Fatalf("expected 400 for bogus status, got %d", resp.StatusCode) |
| 70 | + } |
| 71 | +} |
0 commit comments