Skip to content

Commit bc8e6e7

Browse files
committed
fix the fake inprocess database, do various cleanups
the main thing needed was to set MaxOpenConns to 1 for the fake DB, because it doesn't support parallel queries: dolthub/go-mysql-server#1306 (comment) the only thing still not working in the fake DB is striking/unstriking of report entries, because of this issue I logged: dolthub/dolt#9268 but a fix is incoming: dolthub/go-mysql-server#3000
1 parent a8b2723 commit bc8e6e7

File tree

16 files changed

+200
-96
lines changed

16 files changed

+200
-96
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RUN go run ./bin/fetchbuilddeps/fetchbuilddeps.go
2727
COPY ./ ./
2828

2929
# Build the server
30-
RUN CGO_ENABLED=0 GOOS=linux go build -tags noinprocessdb -o /app/ranger-ims-go
30+
RUN CGO_ENABLED=0 GOOS=linux go build -tags nofakedb -o /app/ranger-ims-go
3131

3232
# Allow IMS to bind to privileged port numbers
3333
RUN setcap "cap_net_bind_service=+ep" /app/ranger-ims-go
@@ -42,6 +42,7 @@ COPY --from=build /app/ranger-ims-go /opt/ims/bin/ims
4242
# Docker-specific default configuration
4343
ENV IMS_HOSTNAME="0.0.0.0"
4444
ENV IMS_PORT="80"
45+
ENV IMS_DB_STORE_TYPE="MariaDB"
4546
ENV IMS_DIRECTORY="ClubhouseDB"
4647

4748
# Use a non-root user to run the server

api/integration/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestGetAuthWithEvent(t *testing.T) {
125125
require.NoError(t, resp.Body.Close())
126126
resp = apisAdmin.editAccess(ctx, imsjson.EventsAccess{
127127
eventName: imsjson.EventAccess{
128-
Readers: []imsjson.AccessRule{imsjson.AccessRule{
128+
Readers: []imsjson.AccessRule{{
129129
Expression: "person:" + userAdminHandle,
130130
Validity: "always",
131131
}},

api/integration/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func setup(ctx context.Context, tempDir string) {
9595
shared.cfg.AttachmentsStore.Local = conf.LocalAttachments{
9696
Dir: tempRoot,
9797
}
98-
shared.cfg.Store.Type = conf.DBStoreTypeMaria
9998
shared.cfg.Store.MariaDB.Database = "ims-" + rand.NonCryptoText()
10099
shared.cfg.Store.MariaDB.Username = "rangers-" + rand.NonCryptoText()
101100
shared.cfg.Store.MariaDB.Password = "password-" + rand.NonCryptoText()

bin/test_docker

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ set -o errexit -o nounset -o pipefail
1111
cd "$(git rev-parse --show-toplevel)"
1212

1313
exit_code=0
14-
# `docker compose up` will only rebuild from the local Dockerfile if no image
15-
# exists for the given name, e.g. ranger-ims-go. To change that behavior, you'd
16-
# add a --build arg to the `docker compose up`.
17-
echo "Starting Docker containers. Note this may not pick up any local changes"
14+
image="ranger-ims-go:${IMAGE_TAG:-latest}"
15+
docker image inspect "${image}" > /dev/null 2>&1 || exit_code=$?
16+
if [ "${exit_code}" -ne "0" ]; then
17+
echo "No local version of ${image} was found, so docker compose will build it"
18+
else
19+
# If you want the image to be rebuilt, then add "--build" to the "docker compose up"
20+
# below. That is not desirable behavior for CI though.
21+
echo "A local version of ${image} was found, so docker compose will not rebuild it"
22+
fi
23+
24+
exit_code=0
1825
docker compose up --wait-timeout 30 --wait || exit_code=$?
1926

2027
echo "Stopping and deleting those Docker containers"
2128
docker compose rm -fsv
2229

2330
if [ "${exit_code}" -ne "0" ]; then
24-
echo "App failed health check"
31+
echo '!!! App failed health check !!!'
2532
exit ${exit_code}
2633
else
2734
echo "App started and passed health check"

cmd/serve.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func mustInitConfig(envFileName string) *conf.IMSConfig {
199199
must(err)
200200
}
201201
if v, ok := lookupEnv("IMS_DEPLOYMENT"); ok {
202-
newCfg.Core.Deployment = strings.ToLower(v)
202+
newCfg.Core.Deployment = conf.DeploymentType(strings.ToLower(v))
203203
}
204204
// This should really be called "IMS_REFRESH_TOKEN_LIFETIME". This name of
205205
// "IMS_TOKEN_LIFETIME" predates our use of refresh tokens, and what it tried
@@ -246,7 +246,7 @@ func mustInitConfig(envFileName string) *conf.IMSConfig {
246246
newCfg.Core.JWTSecret = v
247247
}
248248
if v, ok := lookupEnv("IMS_DB_STORE_TYPE"); ok {
249-
newCfg.Store.Type = conf.DBStoreType(v)
249+
newCfg.Store.Type = conf.DBStoreType(strings.ToLower(v))
250250
}
251251
if v, ok := lookupEnv("IMS_DB_HOST_NAME"); ok {
252252
newCfg.Store.MariaDB.HostName = v

cmd/serve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestMustInitConfig(t *testing.T) {
6767

6868
assert.Equal(t, "host", cfg.Core.Host)
6969
assert.Equal(t, int32(1234), cfg.Core.Port)
70-
assert.Equal(t, "dev", cfg.Core.Deployment)
70+
assert.Equal(t, conf.DeploymentTypeDev, cfg.Core.Deployment)
7171
assert.Equal(t, 1000*time.Second, cfg.Core.RefreshTokenLifetime)
7272
assert.Equal(t, 100*time.Second, cfg.Core.AccessTokenLifetime)
7373
assert.Equal(t, 3*time.Minute, cfg.Core.CacheControlShort)

conf/imsconfig.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ func DefaultIMS() *IMSConfig {
5050
HostName: "localhost",
5151
HostPort: 3306,
5252
Database: "ims",
53+
// Some arbitrary value. We'll get errors from MariaDB if the server
54+
// hits the DB with too many parallel requests.
55+
MaxOpenConns: 20,
5356
},
54-
InProcess: DBStoreMaria{
57+
Fake: DBStoreMaria{
5558
HostName: "localhost",
5659
// HostPort can be left as 0 for automatic port selection on startup
5760
HostPort: 0,
5861
Database: "ims-db",
5962
Username: "ims-db-user",
6063
Password: rand.Text(),
64+
// This needs to be 1 for the fake DB because of
65+
// https://github.com/dolthub/go-mysql-server/issues/1306
66+
MaxOpenConns: 1,
6167
},
6268
},
6369
Directory: Directory{
@@ -78,17 +84,37 @@ func DefaultIMS() *IMSConfig {
7884
// Validate should be called after an IMSConfig has been fully configured.
7985
func (c *IMSConfig) Validate() error {
8086
var errs []error
87+
88+
// IMS database
8189
errs = append(errs, c.Store.Type.Validate())
82-
if c.Store.Type == DBStoreTypeNoOp {
90+
if c.Store.Type != DBStoreTypeMaria {
8391
c.Store.MariaDB = DBStoreMaria{}
8492
}
93+
if c.Store.Type != DBStoreTypeFake {
94+
c.Store.Fake = DBStoreMaria{}
95+
}
96+
97+
// User directory
8598
errs = append(errs, c.Directory.Directory.Validate())
8699
if c.Directory.Directory != DirectoryTypeTestUsers {
87100
c.Directory.TestUsers = nil
88101
}
89102
if c.Directory.Directory != DirectoryTypeClubhouseDB {
90103
c.Directory.ClubhouseDB = ClubhouseDB{}
91104
}
105+
106+
// Deployment
107+
errs = append(errs, c.Core.Deployment.Validate())
108+
if c.Core.Deployment != DeploymentTypeDev {
109+
if c.Directory.Directory != DirectoryTypeClubhouseDB {
110+
errs = append(errs, errors.New("non-dev environments must use a ClubhouseDB directory"))
111+
}
112+
if c.Store.Type != DBStoreTypeMaria {
113+
errs = append(errs, errors.New("non-dev environments must use a MariaDB datastore"))
114+
}
115+
}
116+
117+
// Attachments store
92118
errs = append(errs, c.AttachmentsStore.Type.Validate())
93119
if c.AttachmentsStore.Type == AttachmentsStoreLocal {
94120
if c.AttachmentsStore.Local.Dir == nil {
@@ -106,9 +132,8 @@ func (c *IMSConfig) Validate() error {
106132
}
107133
c.AttachmentsStore.Local = LocalAttachments{}
108134
}
109-
if c.Core.Deployment != "dev" && c.Directory.Directory == DirectoryTypeTestUsers {
110-
errs = append(errs, errors.New("do not use TestUsers outside dev! A ClubhouseDB must be provided"))
111-
}
135+
136+
// Assorted other validations
112137
if c.Core.AccessTokenLifetime > c.Core.RefreshTokenLifetime {
113138
errs = append(errs, errors.New("access token lifetime should not be greater than refresh token lifetime"))
114139
}
@@ -137,6 +162,7 @@ type DeploymentType string
137162

138163
type DBStoreType string
139164

165+
// All these consts should have lowercase values to allow case-insensitive matching.
140166
const (
141167
DirectoryTypeClubhouseDB DirectoryType = "clubhousedb"
142168
DirectoryTypeTestUsers DirectoryType = "testusers"
@@ -148,12 +174,12 @@ const (
148174
DeploymentTypeProduction DeploymentType = "production"
149175
DBStoreTypeMaria DBStoreType = "mariadb"
150176
DBStoreTypeNoOp DBStoreType = "noop"
151-
DBStoreTypeInProcess DBStoreType = "inprocess"
177+
DBStoreTypeFake DBStoreType = "fake"
152178
)
153179

154180
func (d DBStoreType) Validate() error {
155181
switch d {
156-
case DBStoreTypeMaria, DBStoreTypeNoOp, DBStoreTypeInProcess:
182+
case DBStoreTypeMaria, DBStoreTypeNoOp, DBStoreTypeFake:
157183
return nil
158184
default:
159185
return fmt.Errorf("unknown DB store type %v", d)
@@ -195,7 +221,7 @@ type ConfigCore struct {
195221
Admins []string
196222
MasterKey string `redact:"true"`
197223
JWTSecret string `redact:"true"`
198-
Deployment string
224+
Deployment DeploymentType
199225

200226
// CacheControlShort is the duration we set in various responses' Cache-Control headers
201227
// for resources that aren't expected to change often, but still do change (e.g. the list of
@@ -216,17 +242,18 @@ type ConfigCore struct {
216242
}
217243

218244
type DBStore struct {
219-
Type DBStoreType
220-
MariaDB DBStoreMaria
221-
InProcess DBStoreMaria
245+
Type DBStoreType
246+
MariaDB DBStoreMaria
247+
Fake DBStoreMaria
222248
}
223249

224250
type DBStoreMaria struct {
225-
HostName string
226-
HostPort int32
227-
Database string
228-
Username string
229-
Password string `redact:"true"`
251+
HostName string
252+
HostPort int32
253+
Database string
254+
Username string
255+
Password string `redact:"true"`
256+
MaxOpenConns int32
230257
}
231258

232259
type TestUser struct {

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.24.3
55
require (
66
github.com/a-h/templ v0.3.865
77
github.com/aws/aws-sdk-go-v2/config v1.29.14
8-
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.4
8+
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.0
99
github.com/aws/smithy-go v1.22.3
10-
github.com/dolthub/go-mysql-server v0.20.0
10+
github.com/dolthub/go-mysql-server v0.20.1-0.20250523181431-8c883e3e574d
1111
github.com/go-sql-driver/mysql v1.9.2
1212
github.com/golang-jwt/jwt/v5 v5.2.2
1313
github.com/joho/godotenv v1.5.1
@@ -64,7 +64,7 @@ require (
6464
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
6565
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad // indirect
6666
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
67-
github.com/dolthub/vitess v0.0.0-20250512224608-8fb9c6ea092c // indirect
67+
github.com/dolthub/vitess v0.0.0-20250523011542-0f6cf9472d1c // indirect
6868
github.com/dustin/go-humanize v1.0.1 // indirect
6969
github.com/ebitengine/purego v0.8.4 // indirect
7070
github.com/fatih/color v1.18.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 h1:dM9/92u2
5555
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15/go.mod h1:SwFBy2vjtA0vZbjjaFtfN045boopadnoVPhu4Fv66vY=
5656
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15 h1:moLQUoVq91LiqT1nbvzDukyqAlCv89ZmwaHw/ZFlFZg=
5757
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15/go.mod h1:ZH34PJUc8ApjBIfgQCFvkWcUDBtl/WTD+uiYHjd8igA=
58-
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.4 h1:4yxno6bNHkekkfqG/a1nz/gC2gBwhJSojV1+oTE7K+4=
59-
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.4/go.mod h1:qbn305Je/IofWBJ4bJz/Q7pDEtnnoInw/dGt71v6rHE=
58+
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.0 h1:fV4XIU5sn/x8gjRouoJpDVHj+ExJaUk4prYF+eb6qTs=
59+
github.com/aws/aws-sdk-go-v2/service/s3 v1.80.0/go.mod h1:qbn305Je/IofWBJ4bJz/Q7pDEtnnoInw/dGt71v6rHE=
6060
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 h1:1Gw+9ajCV1jogloEv1RRnvfRFia2cL6c9cuKV2Ps+G8=
6161
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3/go.mod h1:qs4a9T5EMLl/Cajiw2TcbNt2UNo/Hqlyp+GiuG4CFDI=
6262
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1 h1:hXmVKytPfTy5axZ+fYbR5d0cFmC3JvwLm5kM83luako=
@@ -135,12 +135,12 @@ github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1G
135135
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2/go.mod h1:mIEZOHnFx4ZMQeawhw9rhsj+0zwQj7adVsnBX7t+eKY=
136136
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad h1:66ZPawHszNu37VPQckdhX1BPPVzREsGgNxQeefnlm3g=
137137
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA=
138-
github.com/dolthub/go-mysql-server v0.20.0 h1:oB1WXD5TwdjhdyJDbF6VgVxyEbCevDRok9yEXefpoyI=
139-
github.com/dolthub/go-mysql-server v0.20.0/go.mod h1:5ZdrW0fHZbz+8CngT9gksqSX4H3y+7v1pns7tJCEpu0=
138+
github.com/dolthub/go-mysql-server v0.20.1-0.20250523181431-8c883e3e574d h1:MyV/Er1lqEtFiBGMwiXe0yUHUXNffJMPpZGQ3IGxc5k=
139+
github.com/dolthub/go-mysql-server v0.20.1-0.20250523181431-8c883e3e574d/go.mod h1:Zn9XK5KLYwPbyMpwfeUP+TgnhlgyID2vXf1WcF0M6Fk=
140140
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTEtT5tOBsCuCrlYnLRKpbJVJkDbrTRhwQ=
141141
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
142-
github.com/dolthub/vitess v0.0.0-20250512224608-8fb9c6ea092c h1:imdag6PPCHAO2rZNsFoQoR4I/vIVTmO/czoOl5rUnbk=
143-
github.com/dolthub/vitess v0.0.0-20250512224608-8fb9c6ea092c/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
142+
github.com/dolthub/vitess v0.0.0-20250523011542-0f6cf9472d1c h1:23KvsBtJk2GmHpXwQ/RkwIkdNpWL8tWdHRCiidhnaUA=
143+
github.com/dolthub/vitess v0.0.0-20250523011542-0f6cf9472d1c/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
144144
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
145145
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
146146
github.com/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0omw=

store/inprocessdb/inprocessdb.go renamed to store/fakeimsdb/fakeimsdb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
// limitations under the License.
1515
//
1616

17-
//go:build !noinprocessdb
17+
//go:build !nofakedb
1818

19-
package inprocessdb
19+
package fakeimsdb
2020

2121
import (
2222
"context"

0 commit comments

Comments
 (0)