mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 15:32:49 +08:00
API: Recognize MSSQL data source URLs (#25629)
* API: Recognize MSSQL URLs Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Move MSSQL URL validation into mssql package Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/datasource"
|
||||
"github.com/grafana/grafana/pkg/components/securejsondata"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -583,11 +584,62 @@ func TestNewDataSourceProxy_ProtocolLessURL(t *testing.T) {
|
||||
}
|
||||
cfg := setting.Cfg{}
|
||||
plugin := plugins.DataSourcePlugin{}
|
||||
|
||||
_, err := NewDataSourceProxy(&ds, &plugin, &ctx, "api/method", &cfg)
|
||||
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Test wth MSSQL type data sources.
|
||||
func TestNewDataSourceProxy_MSSQL(t *testing.T) {
|
||||
ctx := models.ReqContext{
|
||||
Context: &macaron.Context{
|
||||
Req: macaron.Request{},
|
||||
},
|
||||
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR},
|
||||
}
|
||||
tcs := []struct {
|
||||
description string
|
||||
url string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
description: "Valid ODBC URL",
|
||||
url: `localhost\instance:1433`,
|
||||
},
|
||||
{
|
||||
description: "Invalid ODBC URL",
|
||||
url: `localhost\instance::1433`,
|
||||
err: datasource.URLValidationError{
|
||||
Err: fmt.Errorf(`unrecognized MSSQL URL format: "localhost\\instance::1433"`),
|
||||
URL: `localhost\instance::1433`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
cfg := setting.Cfg{}
|
||||
plugin := plugins.DataSourcePlugin{}
|
||||
ds := models.DataSource{
|
||||
Type: "mssql",
|
||||
Url: tc.url,
|
||||
}
|
||||
|
||||
p, err := NewDataSourceProxy(&ds, &plugin, &ctx, "api/method", &cfg)
|
||||
if tc.err == nil {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &url.URL{
|
||||
Scheme: "sqlserver",
|
||||
Host: ds.Url,
|
||||
}, p.targetUrl)
|
||||
} else {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, tc.err, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type CloseNotifierResponseRecorder struct {
|
||||
*httptest.ResponseRecorder
|
||||
closeChan chan bool
|
||||
|
Reference in New Issue
Block a user