-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uneditable): Adds linting for editable dashboards
Today this linter tests a number of settings, but it does not validate that the dashboard cannot be edited after it is deployed in the UI. If this linter is to ensure the source of truth for linted dashboards are a json code object, we should ensure they are not editable by default after deployment. Signed-off-by: Jason Field <[email protected]>
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# template-uneditable-rule | ||
Checks that dashboard is not able to be edited in the ui. This is due to the fact dashboards are declared as code | ||
and therefore that code should be the accurate source of truth for the dashboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package lint | ||
|
||
func NewUneditableRule() *DashboardRuleFunc { | ||
return &DashboardRuleFunc{ | ||
name: "uneditable-dashboard", | ||
description: "Checks that the dashboard is not editable.", | ||
fn: func(d Dashboard) DashboardRuleResults { | ||
r := DashboardRuleResults{} | ||
if d.Editable { | ||
Check failure on line 9 in lint/rule_uneditable.go GitHub Actions / test (1.18.x, ubuntu-latest)
|
||
r.AddFixableError(d, "is editable, it should be set to 'editable: false'", FixUneditableRule) | ||
} | ||
return r | ||
}, | ||
} | ||
} | ||
|
||
func FixUneditableRule(d *Dashboard) { | ||
d.Editable = false | ||
Check failure on line 18 in lint/rule_uneditable.go GitHub Actions / test (1.18.x, ubuntu-latest)
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package lint | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewUneditableRule(t *testing.T) { | ||
linter := NewUneditableRule() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
result Result | ||
dashboard Dashboard | ||
fixed *Dashboard | ||
}{ | ||
{ | ||
name: "OK", | ||
result: ResultSuccess, | ||
dashboard: Dashboard{ | ||
Title: "test", | ||
Editable: false, | ||
}, | ||
}, | ||
{ | ||
name: "error", | ||
result: Result{ | ||
Severity: Error, | ||
Message: `Dashboard 'test' is editable, it should be set to 'editable: false'`, | ||
}, | ||
dashboard: Dashboard{ | ||
Title: "test", | ||
Editable: true, | ||
}, | ||
}, | ||
{ | ||
name: "autofix", | ||
result: Result{ | ||
Severity: Fixed, | ||
Message: `Dashboard 'test' is editable, it should be set to 'editable: false'`, | ||
}, | ||
dashboard: Dashboard{ | ||
Title: "test", | ||
Editable: true, | ||
}, | ||
fixed: &Dashboard{ | ||
Title: "test", | ||
Editable: false, | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
autofix := tc.fixed != nil | ||
testRuleWithAutofix(t, linter, &tc.dashboard, []Result{tc.result}, autofix) | ||
if autofix { | ||
expected, _ := json.Marshal(tc.fixed) | ||
actual, _ := json.Marshal(tc.dashboard) | ||
require.Equal(t, string(expected), string(actual)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"editable": true, | ||
"rows": [ | ||
{ | ||
"panels": [ | ||
|