Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pkg/analysis/passes/coderules/coderules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,52 @@ func TestNoAPIDsQueryEndpointGood(t *testing.T) {
require.NoError(t, err)
require.Len(t, interceptor.Diagnostics, 0)
}

func TestBackendDatasourceShouldNotImplementTestDatasource(t *testing.T) {
if !isSemgrepInstalled() {
t.Skip("semgrep not installed, skipping test")
return
}
var interceptor testpassinterceptor.TestPassInterceptor
pass := &analysis.Pass{
RootDir: filepath.Join("./"),
ResultOf: map[*analysis.Analyzer]interface{}{
sourcecode.Analyzer: filepath.Join("testdata", "backend-datasource-testdatasource-bad"),
},
Report: interceptor.ReportInterceptor(),
}

_, err := Analyzer.Run(pass)
require.NoError(t, err)
require.Len(t, interceptor.Diagnostics, 1)
require.Equal(t, analysis.Error, interceptor.Diagnostics[0].Severity)
require.Equal(
t,
"Backend datasources (classes extending DataSourceWithBackend) must not implement testDatasource(). Remove this method and rely on backend health checks.",
interceptor.Diagnostics[0].Title,
)
require.Equal(
t,
"code-rules-backend-datasource-should-not-implement-testdatasource",
interceptor.Diagnostics[0].Name,
)
}

func TestBackendDatasourceWithoutTestDatasourceGood(t *testing.T) {
if !isSemgrepInstalled() {
t.Skip("semgrep not installed, skipping test")
return
}
var interceptor testpassinterceptor.TestPassInterceptor
pass := &analysis.Pass{
RootDir: filepath.Join("./"),
ResultOf: map[*analysis.Analyzer]interface{}{
sourcecode.Analyzer: filepath.Join("testdata", "backend-datasource-testdatasource-good"),
},
Report: interceptor.ReportInterceptor(),
}

_, err := Analyzer.Run(pass)
require.NoError(t, err)
require.Len(t, interceptor.Diagnostics, 0)
}
9 changes: 9 additions & 0 deletions pkg/analysis/passes/coderules/semgrep-rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,12 @@ rules:
message: "Direct frontend usage of '/api/ds/query' is not permitted. Use Grafana runtime APIs getDataSourceSrv().get(...) and then query(...) instead of calling this endpoint directly."
languages: [javascript, typescript]
severity: ERROR

- id: backend-datasource-should-not-implement-testdatasource
pattern-regex: (?s)class\s+\w+\s+extends\s+DataSourceWithBackend(?:<[^>]+>)?\s*\{.*?\btestDatasource\s*\(
paths:
include:
- "**/datasource.ts"
message: "Backend datasources (classes extending DataSourceWithBackend) must not implement testDatasource(). Remove this method and rely on backend health checks."
languages: [typescript]
severity: ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DataSourceWithBackend } from '@grafana/runtime';

export class DataSource extends DataSourceWithBackend {
async testDatasource() {
return {
status: 'success',
message: 'ok',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DataSourceWithBackend } from '@grafana/runtime';

export class DataSource extends DataSourceWithBackend {}
Loading