|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "os" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + mcpgrafana "github.com/grafana/mcp-grafana/v2" |
| 14 | +) |
| 15 | + |
| 16 | +var updateReadme = flag.Bool("update-readme", false, "rewrite the README tool table's permissions column") |
| 17 | + |
| 18 | +const readmePath = "../../README.md" |
| 19 | + |
| 20 | +// TestReadmeToolPermissions keeps the README tool table's "Required RBAC |
| 21 | +// Permissions" column in step with each tool's RequiresPermissions. Run |
| 22 | +// `go test ./cmd/mcp-grafana -run TestReadmeToolPermissions -update-readme` |
| 23 | +// to regenerate it. The other columns are written by hand. |
| 24 | +func TestReadmeToolPermissions(t *testing.T) { |
| 25 | + declared := make(map[string]string) |
| 26 | + readOnly := listAllCategoryTools(t, disabledTools{write: true}) |
| 27 | + for name, tool := range listAllCategoryTools(t, disabledTools{}) { |
| 28 | + perms, _ := mcpgrafana.ToolRequiredPermissions(tool) |
| 29 | + var roPerms []string |
| 30 | + if ro, ok := readOnly[name]; ok { |
| 31 | + roPerms, _ = mcpgrafana.ToolRequiredPermissions(ro) |
| 32 | + } |
| 33 | + declared[name] = formatPermissions(perms, roPerms) |
| 34 | + } |
| 35 | + for name, tool := range readOnly { |
| 36 | + if _, ok := declared[name]; !ok { |
| 37 | + perms, _ := mcpgrafana.ToolRequiredPermissions(tool) |
| 38 | + declared[name] = formatPermissions(perms, perms) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + raw, err := os.ReadFile(readmePath) |
| 43 | + require.NoError(t, err) |
| 44 | + lines := strings.Split(string(raw), "\n") |
| 45 | + |
| 46 | + start := slices.IndexFunc(lines, func(l string) bool { return strings.HasPrefix(l, "| Tool ") }) |
| 47 | + require.NotEqual(t, -1, start, "README tool table not found") |
| 48 | + end := start |
| 49 | + for end < len(lines) && strings.HasPrefix(lines[end], "|") { |
| 50 | + end++ |
| 51 | + } |
| 52 | + |
| 53 | + var rows [][]string |
| 54 | + listed := make(map[string]bool) |
| 55 | + for i, line := range lines[start:end] { |
| 56 | + cells := splitTableRow(line) |
| 57 | + if i > 0 && !assert.Len(t, cells, len(rows[0]), "README tool table row has the wrong number of cells: %s", line) { |
| 58 | + continue |
| 59 | + } |
| 60 | + if i > 1 { |
| 61 | + name := strings.Trim(cells[0], "`") |
| 62 | + perms, ok := declared[name] |
| 63 | + if !assert.True(t, ok, "README lists %q, which is not a registered tool", name) { |
| 64 | + continue |
| 65 | + } |
| 66 | + listed[name] = true |
| 67 | + cells[3] = perms |
| 68 | + } |
| 69 | + rows = append(rows, cells) |
| 70 | + } |
| 71 | + for name := range declared { |
| 72 | + assert.True(t, listed[name], "tool %q has no row in the README tool table", name) |
| 73 | + } |
| 74 | + |
| 75 | + want := strings.Join(slices.Concat(lines[:start], formatTable(rows), lines[end:]), "\n") |
| 76 | + if t.Failed() { |
| 77 | + return |
| 78 | + } |
| 79 | + if *updateReadme { |
| 80 | + require.NoError(t, os.WriteFile(readmePath, []byte(want), 0o644)) |
| 81 | + return |
| 82 | + } |
| 83 | + assert.Equal(t, string(raw), want, "README tool permissions are out of date; regenerate with "+ |
| 84 | + "`go test ./cmd/mcp-grafana -run TestReadmeToolPermissions -update-readme`") |
| 85 | +} |
| 86 | + |
| 87 | +// splitTableRow splits a markdown table row into trimmed cells, leaving |
| 88 | +// escaped pipes (\|) inside their cell. |
| 89 | +func splitTableRow(line string) []string { |
| 90 | + var cells []string |
| 91 | + var cell strings.Builder |
| 92 | + line = strings.TrimSpace(line) |
| 93 | + line = strings.TrimSuffix(strings.TrimPrefix(line, "|"), "|") |
| 94 | + for i := 0; i < len(line); i++ { |
| 95 | + switch { |
| 96 | + case line[i] == '\\' && i+1 < len(line) && line[i+1] == '|': |
| 97 | + cell.WriteString(`\|`) |
| 98 | + i++ |
| 99 | + case line[i] == '|': |
| 100 | + cells = append(cells, strings.TrimSpace(cell.String())) |
| 101 | + cell.Reset() |
| 102 | + default: |
| 103 | + cell.WriteByte(line[i]) |
| 104 | + } |
| 105 | + } |
| 106 | + return append(cells, strings.TrimSpace(cell.String())) |
| 107 | +} |
| 108 | + |
| 109 | +// formatPermissions renders a tool's actions. When the read-only variant |
| 110 | +// (--disable-write) needs fewer, the extra write actions are listed apart so |
| 111 | +// read-only deployments don't over-grant. |
| 112 | +func formatPermissions(readWrite, readOnly []string) string { |
| 113 | + writeOnly := slices.DeleteFunc(slices.Clone(readWrite), func(p string) bool { return slices.Contains(readOnly, p) }) |
| 114 | + if readOnly == nil || len(writeOnly) == 0 || len(readOnly)+len(writeOnly) != len(readWrite) { |
| 115 | + return quotePermissions(readWrite) |
| 116 | + } |
| 117 | + return quotePermissions(readOnly) + "; with writes enabled also " + quotePermissions(writeOnly) |
| 118 | +} |
| 119 | + |
| 120 | +func quotePermissions(perms []string) string { |
| 121 | + if len(perms) == 0 { |
| 122 | + return "None" |
| 123 | + } |
| 124 | + quoted := make([]string, len(perms)) |
| 125 | + for i, p := range perms { |
| 126 | + quoted[i] = "`" + p + "`" |
| 127 | + } |
| 128 | + return strings.Join(quoted, ", ") |
| 129 | +} |
| 130 | + |
| 131 | +// formatTable pads a markdown table so its columns line up. Row 1 is the |
| 132 | +// separator row. |
| 133 | +func formatTable(rows [][]string) []string { |
| 134 | + widths := make([]int, len(rows[0])) |
| 135 | + for i, row := range rows { |
| 136 | + for j, cell := range row { |
| 137 | + if i != 1 { |
| 138 | + widths[j] = max(widths[j], len([]rune(cell))) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + out := make([]string, len(rows)) |
| 143 | + for i, row := range rows { |
| 144 | + var b strings.Builder |
| 145 | + b.WriteString("|") |
| 146 | + for j, cell := range row { |
| 147 | + if i == 1 { |
| 148 | + cell = strings.Repeat("-", widths[j]) |
| 149 | + } |
| 150 | + b.WriteString(" " + cell + strings.Repeat(" ", widths[j]-len([]rune(cell))) + " |") |
| 151 | + } |
| 152 | + out[i] = b.String() |
| 153 | + } |
| 154 | + return out |
| 155 | +} |
| 156 | + |
| 157 | +func TestReadmeTableHelpers(t *testing.T) { |
| 158 | + assert.Equal(t, []string{"`a`", `x \| y`, ""}, splitTableRow("| `a` | x \\| y | |")) |
| 159 | + assert.Equal(t, "None", formatPermissions(nil, nil)) |
| 160 | + assert.Equal(t, "`a`, `b`", formatPermissions([]string{"a", "b"}, []string{"a", "b"})) |
| 161 | + assert.Equal(t, "`a`; with writes enabled also `b`", formatPermissions([]string{"a", "b"}, []string{"a"})) |
| 162 | +} |
0 commit comments