Skip to content

Commit

Permalink
Add a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jan 6, 2022
1 parent 25794f6 commit ec21b84
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 10 deletions.
18 changes: 10 additions & 8 deletions decoder/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ func (panel DashboardLogs) toOption() (row.Option, error) {
if panel.Repeat != "" {
opts = append(opts, logs.Repeat(panel.Repeat))
}
if panel.Visualization != nil {
vizOpts, err := panel.Visualization.toOptions()
if err != nil {
return nil, err
}

opts = append(opts, vizOpts...)
}
for _, t := range panel.Targets {
opt, err := panel.target(t)
if err != nil {
Expand All @@ -64,6 +56,13 @@ func (panel DashboardLogs) toOption() (row.Option, error) {
opts = append(opts, opt)
}

vizOpts, err := panel.Visualization.toOptions()
if err != nil {
return nil, err
}

opts = append(opts, vizOpts...)

return row.WithLogs(panel.Title, opts...), nil
}

Expand Down Expand Up @@ -99,6 +98,9 @@ func (viz *LogsVisualization) toOptions() ([]logs.Option, error) {
if viz.UniqueLabels {
opts = append(opts, logs.UniqueLabels())
}
if viz.CommonLabels {
opts = append(opts, logs.CommonLabels())
}
if viz.WrapLines {
opts = append(opts, logs.WrapLines())
}
Expand Down
198 changes: 196 additions & 2 deletions decoder/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/K-Phoen/grabana/dashboard"
"github.com/K-Phoen/grabana/logs"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,6 +19,23 @@ func TestLogsPanelsCanBeDecoded(t *testing.T) {
Transparent: true,
Datasource: "some-loki",
Repeat: "ds",
Targets: []LogsTarget{
{
Loki: &LokiTarget{
Query: "{namespace=\"default\"}",
Legend: "logs",
Ref: "A",
},
},
{
Loki: &LokiTarget{
Query: "{namespace=\"other\"}",
Legend: "other",
Ref: "B",
Hidden: true,
},
},
},
}

rowOption, err := panel.toOption()
Expand All @@ -30,13 +48,189 @@ func TestLogsPanelsCanBeDecoded(t *testing.T) {
req.Len(panels, 1)

sdkPanel := panels[0]
tsPanel := panels[0].LogsPanel
logsPanel := panels[0].LogsPanel

req.NotNil(tsPanel)
req.NotNil(logsPanel)
req.Len(logsPanel.Targets, 2)
req.Equal(panel.Title, sdkPanel.Title)
req.Equal(panel.Description, *sdkPanel.Description)
req.Equal(panel.Span, sdkPanel.Span)
req.True(sdkPanel.Transparent)
req.Equal(panel.Datasource, *sdkPanel.Datasource)
req.Equal(panel.Repeat, *sdkPanel.Repeat)
}

func TestLogsPanelsWithValidSortOrder(t *testing.T) {
testCases := []struct {
input string
expected logs.SortOrder
}{
{
input: "asc",
expected: logs.Asc,
},
{
input: "desc",
expected: logs.Desc,
},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.input, func(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Visualization: &LogsVisualization{
Order: tc.input,
},
}

rowOption, err := panel.toOption()

req.NoError(err)

testBoard := dashboard.New("", dashboard.Row("", rowOption))
req.Len(testBoard.Internal().Rows, 1)
panels := testBoard.Internal().Rows[0].Panels
req.Len(panels, 1)

logsPanel := panels[0].LogsPanel

req.NotNil(logsPanel)

req.Equal(string(tc.expected), logsPanel.Options.SortOrder)
})
}
}

func TestLogsPanelsWithInvalidSortOrder(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Visualization: &LogsVisualization{
Order: "invalid",
},
}

_, err := panel.toOption()
req.Error(err)
req.Equal(ErrInvalidSortOrder, err)
}

func TestLogsPanelsWithValidDeduplicationStrategy(t *testing.T) {
testCases := []struct {
input string
expected logs.DedupStrategy
}{
{
input: "none",
expected: logs.None,
},
{
input: "exact",
expected: logs.Exact,
},
{
input: "signature",
expected: logs.Signature,
},
{
input: "numbers",
expected: logs.Numbers,
},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.input, func(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Visualization: &LogsVisualization{
Deduplication: tc.input,
},
}

rowOption, err := panel.toOption()

req.NoError(err)

testBoard := dashboard.New("", dashboard.Row("", rowOption))
req.Len(testBoard.Internal().Rows, 1)
panels := testBoard.Internal().Rows[0].Panels
req.Len(panels, 1)

logsPanel := panels[0].LogsPanel

req.NotNil(logsPanel)

req.Equal(string(tc.expected), logsPanel.Options.DedupStrategy)
})
}
}

func TestLogsPanelsWithInvalidDedupStrategy(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Visualization: &LogsVisualization{
Deduplication: "invalid",
},
}

_, err := panel.toOption()
req.Error(err)
req.Equal(ErrInvalidDeduplicationStrategy, err)
}

func TestLogsPanelsWithInvalidTarget(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Targets: []LogsTarget{
{
// empty!
},
},
}

_, err := panel.toOption()
req.Error(err)
req.Equal(ErrTargetNotConfigured, err)
}

func TestLogsPanelsVisualizationOptionsCanBeSet(t *testing.T) {
req := require.New(t)

panel := DashboardLogs{
Visualization: &LogsVisualization{
Time: true,
UniqueLabels: true,
CommonLabels: true,
WrapLines: true,
PrettifyJSON: true,
HideLogDetails: true,
},
}

rowOption, err := panel.toOption()

req.NoError(err)

testBoard := dashboard.New("", dashboard.Row("", rowOption))
req.Len(testBoard.Internal().Rows, 1)
panels := testBoard.Internal().Rows[0].Panels
req.Len(panels, 1)

logsPanel := panels[0].LogsPanel

req.False(logsPanel.Options.EnableLogDetails)
req.True(logsPanel.Options.PrettifyLogMessage)
req.True(logsPanel.Options.ShowLabels)
req.True(logsPanel.Options.WrapLogMessage)
req.True(logsPanel.Options.ShowCommonLabels)
req.True(logsPanel.Options.ShowTime)
}

0 comments on commit ec21b84

Please sign in to comment.