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:
Arve Knudsen
2020-06-17 11:17:11 +02:00
committed by GitHub
parent e6a5e88054
commit d352c213b3
5 changed files with 133 additions and 26 deletions

View File

@ -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