forked from couchbase/sync_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test_bucket_pool_config.go
203 lines (173 loc) · 6.65 KB
/
main_test_bucket_pool_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package base
import (
"context"
"errors"
"os"
"strconv"
"time"
)
// Bucket names start with a fixed prefix and end with a sequential bucket number and a creation timestamp for uniqueness
const (
tbpBucketNamePrefix = "sg_int_"
tbpBucketNameFormat = "%s%d_%d"
tbpScopePrefix = "sg_test_"
tbpCollectionPrefix = "sg_test_"
)
const (
envTestClusterUsername = "SG_TEST_USERNAME"
DefaultTestClusterUsername = DefaultCouchbaseAdministrator
envTestClusterPassword = "SG_TEST_PASSWORD"
DefaultTestClusterPassword = DefaultCouchbasePassword
// Creates and prepares this many buckets in the backing store to be pooled for testing.
tbpDefaultBucketPoolSize = 3
tbpEnvBucketPoolSize = "SG_TEST_BUCKET_POOL_SIZE"
// Creates and prepares this many collections in each bucket in the backing store.
tbpDefaultCollectionPoolSize = 3 // (per bucket)
tbpEnvCollectionPoolSize = "SG_TEST_COLLECTION_POOL_SIZE"
// Allocate this much memory to each bucket.
defaultBucketQuotaMB = 200
tbpEnvBucketQuotaMB = "SG_TEST_BUCKET_QUOTA_MB"
// Prevents reuse and cleanup of buckets used in failed tests for later inspection.
// When all pooled buckets are in a preserved state, any remaining tests are skipped instead of blocking waiting for a bucket.
tbpEnvPreserve = "SG_TEST_BUCKET_POOL_PRESERVE"
// Prints detailed debug logs from the test pooling framework.
tbpEnvVerbose = "SG_TEST_BUCKET_POOL_DEBUG"
tbpEnvUseDefaultCollection = "SG_TEST_USE_DEFAULT_COLLECTION"
// wait this long when requesting a test bucket from the pool before giving up and failing the test.
waitForReadyBucketTimeout = time.Minute
// Creates buckets with a specific number of number of replicas
tbpEnvBucketNumReplicas = "SG_TEST_BUCKET_NUM_REPLICAS"
)
var tbpDefaultBucketSpec = BucketSpec{
Server: UnitTestUrl(),
Auth: TestAuthenticator{
Username: TestClusterUsername(),
Password: TestClusterPassword(),
},
UseXattrs: TestUseXattrs(),
}
// TestsUseNamedCollections returns true if the tests use named collections.
func TestsUseNamedCollections() bool {
ok, err := GTestBucketPool.canUseNamedCollections()
return err == nil && ok
}
// TestsUseNamedCollections returns true if the tests use named collections.
func TestsUseServerCE() bool {
ok, err := GTestBucketPool.cluster.isServerEnterprise()
return err == nil && ok
}
// canUseNamedCollections returns true if the cluster supports named collections, and they are also requested
func (tbp *TestBucketPool) canUseNamedCollections() (bool, error) {
// walrus supports collections, but we need to query the server's version for capability check
clusterSupport := true
if tbp.cluster != nil {
var err error
clusterSupport, err = tbp.cluster.supportsCollections()
if err != nil {
return false, err
}
}
// Walrus views work with collections - Server views do not - we need GSI when running with CB Server...
queryStoreSupportsCollections := true
if !UnitTestUrlIsWalrus() && TestsDisableGSI() {
queryStoreSupportsCollections = false
}
// if we've not explicitly set a use default collection flag - determine support based on other flags
useDefaultCollection, isSet := os.LookupEnv(tbpEnvUseDefaultCollection)
if !isSet {
if !queryStoreSupportsCollections {
tbp.Logf(context.TODO(), "GSI disabled - not using named collections")
return false, nil
}
tbp.Logf(context.TODO(), "Will use named collections if cluster supports them: %v", clusterSupport)
// use collections if running GSI and server >= 7
return clusterSupport, nil
}
requestDefaultCollection, _ := strconv.ParseBool(useDefaultCollection)
requestNamedCollection := !requestDefaultCollection
if requestNamedCollection {
if !clusterSupport {
return false, errors.New("Unable to use named collections - Cluster does not support collections")
}
if !queryStoreSupportsCollections {
return false, errors.New("Unable to use named collections - GSI disabled")
}
}
return requestNamedCollection, nil
}
// tbpNumBuckets returns the configured number of buckets to use in the pool.
func tbpNumBuckets() int {
numBuckets := tbpDefaultBucketPoolSize
if envPoolSize := os.Getenv(tbpEnvBucketPoolSize); envPoolSize != "" {
var err error
numBuckets, err = strconv.Atoi(envPoolSize)
if err != nil {
FatalfCtx(context.TODO(), "Couldn't parse %s: %v", tbpEnvBucketPoolSize, err)
}
}
return numBuckets
}
// tbpNumReplicasreturns the number of replicas to use in each bucket.
func tbpNumReplicas() uint32 {
numReplicas := os.Getenv(tbpEnvBucketNumReplicas)
if numReplicas == "" {
return 0
}
replicas, err := strconv.Atoi(numReplicas)
if err != nil {
FatalfCtx(context.TODO(), "Couldn't parse %s: %v", tbpEnvBucketPoolSize, err)
}
return uint32(replicas)
}
// tbpNumCollectionsPerBucket returns the configured number of collections prepared in a bucket.
func tbpNumCollectionsPerBucket() int {
numCollectionsPerBucket := tbpDefaultCollectionPoolSize
if envCollectionPoolSize := os.Getenv(tbpEnvCollectionPoolSize); envCollectionPoolSize != "" {
var err error
numCollectionsPerBucket, err = strconv.Atoi(envCollectionPoolSize)
if err != nil {
FatalfCtx(context.TODO(), "Couldn't parse %s: %v", tbpEnvCollectionPoolSize, err)
}
}
return numCollectionsPerBucket
}
// tbpBucketQuotaMB returns the configured bucket RAM quota.
func tbpBucketQuotaMB() int {
bucketQuota := defaultBucketQuotaMB
if envBucketQuotaMB := os.Getenv(tbpEnvBucketQuotaMB); envBucketQuotaMB != "" {
var err error
bucketQuota, err = strconv.Atoi(envBucketQuotaMB)
if err != nil {
FatalfCtx(context.TODO(), "Couldn't parse %s: %v", tbpEnvBucketQuotaMB, err)
}
}
return bucketQuota
}
// tbpVerbose returns the configured test bucket pool verbose flag.
func tbpVerbose() bool {
verbose, _ := strconv.ParseBool(os.Getenv(tbpEnvVerbose))
return verbose
}
// TestClusterUsername returns the configured cluster username.
func TestClusterUsername() string {
username := DefaultTestClusterUsername
if envClusterUsername := os.Getenv(envTestClusterUsername); envClusterUsername != "" {
username = envClusterUsername
}
return username
}
// TestClusterPassword returns the configured cluster password.
func TestClusterPassword() string {
password := DefaultTestClusterPassword
if envClusterPassword := os.Getenv(envTestClusterPassword); envClusterPassword != "" {
password = envClusterPassword
}
return password
}