|
| 1 | +// Copyright 2025-Present Couchbase, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included |
| 4 | +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 5 | +// in that file, in accordance with the Business Source License, use of this |
| 6 | +// software will be governed by the Apache License, Version 2.0, included in |
| 7 | +// the file licenses/APL2.txt. |
| 8 | + |
| 9 | +package rest |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "net/http" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/couchbase/sync_gateway/base" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +// TestGetOldRevisionBodyByRevTreeID ensures that fetching an older revision body using a legacy RevTree ID after flushing the revision cache works with store_legacy_revtree_data set. |
| 21 | +func TestGetOldRevisionBodyByRevTreeID(t *testing.T) { |
| 22 | + base.SetUpTestLogging(t, base.LevelDebug, base.KeyCRUD) |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + storeLegacyRevTreeData bool |
| 27 | + deltaSyncEnabled bool |
| 28 | + expectedToFindOldRev bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "store_legacy_revtree_data=true, delta_sync=true", |
| 32 | + storeLegacyRevTreeData: true, |
| 33 | + deltaSyncEnabled: true, |
| 34 | + expectedToFindOldRev: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "store_legacy_revtree_data=true, delta_sync=false", |
| 38 | + storeLegacyRevTreeData: true, |
| 39 | + deltaSyncEnabled: false, |
| 40 | + expectedToFindOldRev: false, // TODO: CBG-4840 - Should be true - Requires restoration of non-delta sync rev storage |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "store_legacy_revtree_data=false, delta_sync=true", |
| 44 | + storeLegacyRevTreeData: false, |
| 45 | + deltaSyncEnabled: true, |
| 46 | + expectedToFindOldRev: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "store_legacy_revtree_data=false, delta_sync=false", |
| 50 | + storeLegacyRevTreeData: false, |
| 51 | + deltaSyncEnabled: false, |
| 52 | + expectedToFindOldRev: false, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tc := range tests { |
| 57 | + t.Run(tc.name, func(t *testing.T) { |
| 58 | + rt := NewRestTester(t, &RestTesterConfig{ |
| 59 | + DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{ |
| 60 | + DeltaSync: &DeltaSyncConfig{Enabled: base.Ptr(tc.deltaSyncEnabled)}, |
| 61 | + StoreLegacyRevTreeData: base.Ptr(tc.storeLegacyRevTreeData), |
| 62 | + }}, |
| 63 | + }) |
| 64 | + defer rt.Close() |
| 65 | + |
| 66 | + // Create and update a document to generate an old revision. |
| 67 | + const docID = "doc1" |
| 68 | + v1 := rt.PutDoc(docID, `{"v":1}`) |
| 69 | + v2 := rt.UpdateDoc(docID, v1, `{"v":2}`) |
| 70 | + _ = v2 |
| 71 | + |
| 72 | + // Flush the revision cache to force retrieval from bucket-stored old revision docs. |
| 73 | + rt.GetDatabase().FlushRevisionCacheForTest() |
| 74 | + |
| 75 | + resp := rt.SendAdminRequest(http.MethodGet, fmt.Sprintf("/{{.keyspace}}/%s?rev=%s", docID, v1.RevTreeID), "") |
| 76 | + // Attempt to fetch the old revision body using its RevTree ID. |
| 77 | + if tc.expectedToFindOldRev { |
| 78 | + RequireStatus(t, resp, http.StatusOK) |
| 79 | + assert.Contains(t, resp.BodyString(), `"v":1`) |
| 80 | + } else { |
| 81 | + RequireStatus(t, resp, http.StatusNotFound) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments