Skip to content

Commit

Permalink
Add CSV tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpreese authored May 19, 2020
1 parent d764544 commit c128181
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 17 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ Feedback, feature requests, and contributions are welcome!

## Usage

Run the following command in the root of your project to create a markdown file named `alerts.md` that contains your Prometheus alerts.
Promdoc will generate the output in the format that matches the output file.

For example, to generate markdown, run the following command in the root folder where you want `promdoc` to search for rules.

```console
$ promdoc generate alerts.md
```

### Flags
To generate the output in `CSV`, you can run the same command, but rather than `.md`, use `.csv`

#### --output
```console
$ promdoc generate alerts.csv
```

Outputs the PrometheusRules in different formats. Default is `markdown`.
Supported output formats:

*Currently supported formats: markdown, csv*
- Markdown (.md)
- CSV (.csv)

## Example

Given the following `PrometheusRule` definitions:

*controlplane-alerts.yaml*

```yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
Expand All @@ -58,6 +64,7 @@ spec:
```
*alertmanager-alerts.yaml*
```yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
Expand All @@ -76,7 +83,13 @@ spec:
severity: critical
```
The generated documentation would be
Running the generate command with a `.md` extension:

```console
$ promdoc generate alerts.md
```

Generates the following markdown:

# Alerts

Expand Down
2 changes: 2 additions & 0 deletions examples/csv/expected.csv
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name,RuleGroup,Summary,Message,Severity,Runbook
TestAlert,TestGroup,TestSummary,,TestSeverity,TestRunbookURL
19 changes: 10 additions & 9 deletions internal/commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"

"github.com/spf13/cobra"

Expand All @@ -19,30 +20,30 @@ func NewGenerateCommand() *cobra.Command {
Args: cobra.ExactArgs(1),

RunE: func(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("invalid argument: %w", err)
}

if err := runGenerateCommand(args[0], output); err != nil {
if err := runGenerateCommand(args[0]); err != nil {
return fmt.Errorf("generate: %w", err)
}

return nil
},
}
cmd.Flags().String("output", "markdown", "Output format: markdown, csv")

return &cmd
}

func runGenerateCommand(outputFile string, outputType string) error {
func runGenerateCommand(outputFile string) error {
workingDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("get working dir: %w", err)
}

output, err := rendering.Render(workingDir, outputType)
fileTokens := strings.Split(outputFile, ".")
if len(fileTokens) == 0 {
return fmt.Errorf("get file extension: %w", err)
}

fileExtension := fileTokens[len(fileTokens)-1]
output, err := rendering.Render(workingDir, fileExtension)
if err != nil {
return fmt.Errorf("rendering: %w", err)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/rendering/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rendering

import (
"io/ioutil"
"testing"
)

func TestCSV(t *testing.T) {
expected, err := ioutil.ReadFile("../../examples/csv/expected.csv")
if err != nil {
t.Fatal("read expected file")
}

actual, err := Render("../../examples/rule.yaml", "csv")
if err != nil {
t.Fatal("getting rule groups:", err)
}

if string(expected) != actual {
t.Error("rendered markdown did not match expected output")
}
}
2 changes: 1 addition & 1 deletion internal/rendering/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestMarkdown(t *testing.T) {
t.Fatal("read expected file")
}

actual, err := Render("../../examples/rule.yaml", "markdown")
actual, err := Render("../../examples/rule.yaml", "md")
if err != nil {
t.Fatal("getting rule groups:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/rendering/rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Render(path string, outputType string) (string, error) {
}

switch outputType {
case "markdown":
case "md":
return RenderMarkdown(ruleGroups), nil
case "csv":
return RenderCSV(ruleGroups), nil
Expand Down
Binary file modified promdoc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c128181

Please sign in to comment.