Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/dbtest/conn.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package dbtest
6
7
import (
8
"net"
9
"os"
10
"sync"
11
"testing"
12
13
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
14
"github.com/stretchr/testify/require"
15
"gorm.io/gorm"
16
)
17
18
var (
19
connLock = sync.Mutex{}
20
conn *gorm.DB
21
)
22
23
func ConnectForTests(t *testing.T) *gorm.DB {
24
t.Helper()
25
26
connLock.Lock()
27
defer connLock.Unlock()
28
29
if conn != nil {
30
return conn
31
}
32
33
// These are static connection details for tests, started by `leeway build components/gitpod-db/go:init-testdb`.
34
// We use the same static credentials for CI & local instance of MySQL Server.
35
var err error
36
conn, err = db.Connect(db.ConnectionParams{
37
User: "root",
38
Password: "test",
39
Host: net.JoinHostPort(os.Getenv("DB_HOST"), "23306"),
40
Database: "gitpod",
41
})
42
require.NoError(t, err, "Failed to establish connection to In a workspace, run `leeway run components/gitpod-db:init-testdb` once to bootstrap the db")
43
44
return conn
45
}
46
47