Skip to content

Commit bb201b5

Browse files
committed
Stamp several default values into DefaultDbConfig for visibility in include_runtime - add test to catch future missed fields
1 parent 4b1645e commit bb201b5

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

rest/config_database.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package rest
1111
import (
1212
"context"
1313

14+
"github.com/couchbase/sync_gateway/auth"
1415
"github.com/couchbase/sync_gateway/base"
1516
"github.com/couchbase/sync_gateway/channels"
1617
"github.com/couchbase/sync_gateway/db"
@@ -160,7 +161,7 @@ func DefaultDbConfig(sc *StartupConfig, useXattrs bool) *DbConfig {
160161
LocalDocExpirySecs: base.Ptr(base.DefaultLocalDocExpirySecs),
161162
EnableXattrs: base.Ptr(base.DefaultUseXattrs),
162163
SecureCookieOverride: base.Ptr(sc.API.HTTPS.TLSCertPath != ""),
163-
SessionCookieName: "",
164+
SessionCookieName: auth.DefaultCookieName,
164165
SessionCookieHTTPOnly: base.Ptr(false),
165166
AllowConflicts: base.Ptr(base.DefaultAllowConflicts),
166167
Index: &IndexConfig{
@@ -169,7 +170,7 @@ func DefaultDbConfig(sc *StartupConfig, useXattrs bool) *DbConfig {
169170
UseViews: base.Ptr(false),
170171
SendWWWAuthenticateHeader: base.Ptr(true),
171172
DisablePasswordAuth: base.Ptr(false),
172-
BucketOpTimeoutMs: nil,
173+
BucketOpTimeoutMs: base.Ptr(uint32(base.DefaultGocbV2OperationTimeout.Milliseconds())),
173174
SlowQueryWarningThresholdMs: base.Ptr(kDefaultSlowQueryWarningThreshold),
174175
DeltaSync: &DeltaSyncConfig{
175176
Enabled: base.Ptr(db.DefaultDeltaSyncEnabled),
@@ -184,6 +185,7 @@ func DefaultDbConfig(sc *StartupConfig, useXattrs bool) *DbConfig {
184185
QueryPaginationLimit: base.Ptr(db.DefaultQueryPaginationLimit),
185186
UserXattrKey: nil,
186187
ClientPartitionWindowSecs: base.Ptr(int(base.DefaultClientPartitionWindow.Seconds())),
188+
Guest: &auth.PrincipalConfig{Disabled: base.Ptr(true)},
187189
JavascriptTimeoutSecs: base.Ptr(base.DefaultJavascriptTimeoutSecs),
188190
Suspendable: base.Ptr(sc.IsServerless()),
189191
ChangesRequestPlus: base.Ptr(false),
@@ -195,6 +197,8 @@ func DefaultDbConfig(sc *StartupConfig, useXattrs bool) *DbConfig {
195197
dbConfig.AutoImport = base.Ptr(base.DefaultAutoImport)
196198
if base.IsEnterpriseEdition() {
197199
dbConfig.ImportPartitions = base.Ptr(base.GetDefaultImportPartitions(sc.IsServerless()))
200+
} else {
201+
dbConfig.ImportPartitions = base.Ptr(uint16(0))
198202
}
199203
dbConfig.Index.NumPartitions = base.Ptr(db.DefaultNumIndexPartitions)
200204
} else {

rest/config_database_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"encoding/json"
1313
"fmt"
1414
"net/http"
15+
"reflect"
1516
"testing"
1617
"time"
1718

@@ -29,6 +30,70 @@ func TestDefaultDbConfig(t *testing.T) {
2930
require.Equal(t, db.DefaultCompactInterval, time.Duration(compactIntervalDays)*time.Hour*24)
3031
}
3132

33+
// TestDefaultDbConfigFieldCoverage ensures that all fields in DbConfig are either:
34+
// 1. Set to a non-zero value in DefaultDbConfig(), or
35+
// 2. Explicitly listed in the allowlist as intentionally unset
36+
//
37+
// This prevents new fields from being added to DbConfig without considering whether
38+
// they should have a default value exposed in include_runtime=true config output.
39+
func TestDefaultDbConfigFieldCoverage(t *testing.T) {
40+
sc := DefaultStartupConfig("/default/log/file/path")
41+
defaultConfig := DefaultDbConfig(&sc, true)
42+
43+
// Fields that are intentionally left unset in DefaultDbConfig.
44+
// When adding a field here, add a comment explaining why it doesn't need a default.
45+
intentionallyUnsetFields := map[string]string{
46+
// Embedded struct - fields checked separately
47+
"BucketConfig": "embedded struct, fields checked separately",
48+
"Scopes": "opt-in with no default",
49+
"Replications": "opt-in with no default",
50+
"UserFunctions": "opt-in with no default",
51+
"OIDCConfig": "opt-in with no default",
52+
"LocalJWTConfig": "opt-in with no default",
53+
"CORS": "opt-in with no default",
54+
"EventHandlers": "opt-in with no default",
55+
"ImportFilter": "opt-in with no default",
56+
"UserXattrKey": "opt-in with no default",
57+
"Name": "stamped at runtime",
58+
"UpdatedAt": "persisted timestamp",
59+
"CreatedAt": "persisted timestamp",
60+
"Unsupported": "unsupported",
61+
"DeprecatedRevCacheSize": "deprecated",
62+
"NumIndexReplicas": "deprecated",
63+
"FeedType": "deprecated",
64+
"Users": "legacy config",
65+
"Roles": "legacy config",
66+
}
67+
68+
configType := reflect.TypeFor[DbConfig]()
69+
configValue := reflect.ValueOf(*defaultConfig)
70+
71+
for i := 0; i < configType.NumField(); i++ {
72+
field := configType.Field(i)
73+
fieldName := field.Name
74+
fieldValue := configValue.Field(i)
75+
76+
// Skip unexported fields
77+
if !field.IsExported() {
78+
t.Logf("%q: Skipping unexported field", fieldName)
79+
continue
80+
}
81+
82+
// Check if field is in the allowlist
83+
if reason, ok := intentionallyUnsetFields[fieldName]; ok {
84+
if testing.Verbose() {
85+
t.Logf("%q: intentionally unset: %s", fieldName, reason)
86+
}
87+
continue
88+
}
89+
90+
// Check if the field has a non-zero value
91+
if fieldValue.IsZero() {
92+
assert.Failf(t, "Field not present in DefaultDbConfig", "%q: missing - either add a default value in DefaultDbConfig() or add it to the allowlist with a reason", fieldName)
93+
}
94+
}
95+
}
96+
3297
func TestDbConfigUpdatedAtField(t *testing.T) {
3398
b := base.GetTestBucket(t)
3499
rt := NewRestTester(t, &RestTesterConfig{

0 commit comments

Comments
 (0)