2019-01-25 15:56:19 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2023-05-29 09:45:08 -05:00
|
|
|
"context"
|
2021-05-10 10:03:10 -05:00
|
|
|
"errors"
|
|
|
|
"net/url"
|
2019-01-25 15:56:19 -06:00
|
|
|
"testing"
|
2023-05-29 09:45:08 -05:00
|
|
|
"time"
|
2019-01-25 15:56:19 -06:00
|
|
|
|
2023-05-29 09:45:08 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-10 10:03:10 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-30 02:21:27 -06:00
|
|
|
|
2023-07-11 08:47:35 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2023-05-29 09:45:08 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2023-01-30 02:21:27 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2019-01-25 15:56:19 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type sqlStoreTest struct {
|
2023-07-11 08:47:35 -05:00
|
|
|
name string
|
|
|
|
dbType string
|
|
|
|
dbHost string
|
|
|
|
dbURL string
|
|
|
|
dbUser string
|
|
|
|
dbPwd string
|
|
|
|
expConnStr string
|
|
|
|
features []string
|
|
|
|
err error
|
2019-01-25 15:56:19 -06:00
|
|
|
}
|
|
|
|
|
2019-01-27 10:28:02 -06:00
|
|
|
var sqlStoreTestCases = []sqlStoreTest{
|
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "MySQL IPv4",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "1.2.3.4:5678",
|
|
|
|
expConnStr: ":@tcp(1.2.3.4:5678)/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres IPv4",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "1.2.3.4:5678",
|
|
|
|
expConnStr: "user='' host=1.2.3.4 port=5678 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert=''",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres IPv4 (Default Port)",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "1.2.3.4",
|
|
|
|
expConnStr: "user='' host=1.2.3.4 port=5432 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert=''",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2023-07-07 04:17:35 -05:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres username and password",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "1.2.3.4",
|
|
|
|
dbUser: "grafana",
|
|
|
|
dbPwd: "password",
|
|
|
|
expConnStr: "user=grafana host=1.2.3.4 port=5432 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert='' password=password",
|
2023-07-07 04:17:35 -05:00
|
|
|
},
|
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres username no password",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "1.2.3.4",
|
|
|
|
dbUser: "grafana",
|
|
|
|
dbPwd: "",
|
|
|
|
expConnStr: "user=grafana host=1.2.3.4 port=5432 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert=''",
|
2023-07-07 04:17:35 -05:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "MySQL IPv4 (Default Port)",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "1.2.3.4",
|
|
|
|
expConnStr: ":@tcp(1.2.3.4)/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "MySQL IPv6",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "[fe80::24e8:31b2:91df:b177]:1234",
|
|
|
|
expConnStr: ":@tcp([fe80::24e8:31b2:91df:b177]:1234)/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres IPv6",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "[fe80::24e8:31b2:91df:b177]:1234",
|
|
|
|
expConnStr: "user='' host=fe80::24e8:31b2:91df:b177 port=1234 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert=''",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "MySQL IPv6 (Default Port)",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "[::1]",
|
|
|
|
expConnStr: ":@tcp([::1])/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2019-01-27 10:28:02 -06:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "Postgres IPv6 (Default Port)",
|
|
|
|
dbType: "postgres",
|
|
|
|
dbHost: "[::1]",
|
|
|
|
expConnStr: "user='' host=::1 port=5432 dbname=test_db sslmode='' sslcert='' sslkey='' sslrootcert=''",
|
2019-01-25 15:56:19 -06:00
|
|
|
},
|
2021-05-10 10:03:10 -05:00
|
|
|
{
|
|
|
|
name: "Invalid database URL",
|
|
|
|
dbURL: "://invalid.com/",
|
|
|
|
err: &url.Error{Op: "parse", URL: "://invalid.com/", Err: errors.New("missing protocol scheme")},
|
|
|
|
},
|
2022-08-08 11:14:17 -05:00
|
|
|
{
|
2023-07-11 08:47:35 -05:00
|
|
|
name: "MySQL with ANSI_QUOTES mode",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "[::1]",
|
|
|
|
features: []string{featuremgmt.FlagMysqlAnsiQuotes},
|
|
|
|
expConnStr: ":@tcp([::1])/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true&sql_mode='ANSI_QUOTES'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New DB library",
|
|
|
|
dbType: "mysql",
|
|
|
|
dbHost: "[::1]",
|
|
|
|
features: []string{featuremgmt.FlagNewDBLibrary},
|
|
|
|
expConnStr: ":@tcp([::1])/test_db?collation=utf8mb4_unicode_ci&allowNativePasswords=true&clientFoundRows=true&sql_mode='ANSI_QUOTES'&parseTime=true",
|
2022-08-08 11:14:17 -05:00
|
|
|
},
|
2019-01-25 15:56:19 -06:00
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationSQLConnectionString(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2021-05-10 10:03:10 -05:00
|
|
|
for _, testCase := range sqlStoreTestCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
sqlstore := &SQLStore{}
|
2023-07-11 08:47:35 -05:00
|
|
|
sqlstore.Cfg = makeSQLStoreTestConfig(t, testCase)
|
2021-05-10 10:03:10 -05:00
|
|
|
connStr, err := sqlstore.buildConnectionString()
|
|
|
|
require.Equal(t, testCase.err, err)
|
2019-01-25 15:56:19 -06:00
|
|
|
|
2023-07-11 08:47:35 -05:00
|
|
|
assert.Equal(t, testCase.expConnStr, connStr)
|
2021-05-10 10:03:10 -05:00
|
|
|
})
|
|
|
|
}
|
2019-01-25 15:56:19 -06:00
|
|
|
}
|
|
|
|
|
2023-05-29 09:45:08 -05:00
|
|
|
func TestIntegrationIsUniqueConstraintViolation(t *testing.T) {
|
|
|
|
store := InitTestDB(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
f func(*testing.T, *DBSession) error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "successfully detect primary key violations",
|
|
|
|
f: func(t *testing.T, sess *DBSession) error {
|
|
|
|
// Attempt to insert org with provided ID (primary key) twice
|
|
|
|
now := time.Now()
|
|
|
|
org := org.Org{Name: "test org primary key violation", Created: now, Updated: now, ID: 42}
|
|
|
|
err := sess.InsertId(&org, store.Dialect)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Provide a different name to avoid unique constraint violation
|
|
|
|
org.Name = "test org 2"
|
|
|
|
return sess.InsertId(&org, store.Dialect)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "successfully detect unique constrain violations",
|
|
|
|
f: func(t *testing.T, sess *DBSession) error {
|
|
|
|
// Attempt to insert org with reserved name
|
|
|
|
now := time.Now()
|
|
|
|
org := org.Org{Name: "test org unique constrain violation", Created: now, Updated: now, ID: 43}
|
|
|
|
err := sess.InsertId(&org, store.Dialect)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Provide a different ID to avoid primary key violation
|
|
|
|
org.ID = 44
|
|
|
|
return sess.InsertId(&org, store.Dialect)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
err := store.WithDbSession(context.Background(), func(sess *DBSession) error {
|
|
|
|
return tc.f(t, sess)
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.True(t, store.Dialect.IsUniqueConstraintViolation(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 08:47:35 -05:00
|
|
|
func makeSQLStoreTestConfig(t *testing.T, tc sqlStoreTest) *setting.Cfg {
|
2021-05-10 10:03:10 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2019-01-25 15:56:19 -06:00
|
|
|
cfg := setting.NewCfg()
|
|
|
|
|
2019-10-22 07:08:18 -05:00
|
|
|
sec, err := cfg.Raw.NewSection("database")
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2023-07-11 08:47:35 -05:00
|
|
|
_, err = sec.NewKey("type", tc.dbType)
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2023-07-11 08:47:35 -05:00
|
|
|
_, err = sec.NewKey("host", tc.dbHost)
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2023-07-11 08:47:35 -05:00
|
|
|
_, err = sec.NewKey("url", tc.dbURL)
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2023-07-11 08:47:35 -05:00
|
|
|
_, err = sec.NewKey("user", tc.dbUser)
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2019-10-22 07:08:18 -05:00
|
|
|
_, err = sec.NewKey("name", "test_db")
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2023-07-11 08:47:35 -05:00
|
|
|
_, err = sec.NewKey("password", tc.dbPwd)
|
2021-05-10 10:03:10 -05:00
|
|
|
require.NoError(t, err)
|
2019-01-25 15:56:19 -06:00
|
|
|
|
2023-07-11 08:47:35 -05:00
|
|
|
cfg.IsFeatureToggleEnabled = func(key string) bool {
|
|
|
|
for _, f := range tc.features {
|
|
|
|
if f == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-08-08 11:14:17 -05:00
|
|
|
|
2019-01-27 10:28:02 -06:00
|
|
|
return cfg
|
2019-01-25 15:56:19 -06:00
|
|
|
}
|