File: boards_test.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,612 kB
  • sloc: makefile: 28; sh: 13
file content (208 lines) | stat: -rw-r--r-- 6,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package integration

import (
	"testing"

	"github.com/zorkian/go-datadog-api"
)

func TestOrderedBoardCreateAndDelete(t *testing.T) {
	expected := getTestBoard()

	// Create the dashboard and compare it
	actual, err := client.CreateBoard(expected)
	if err != nil {
		t.Fatalf("Creating a dashboard failed when it shouldn't. (%s)", err)
	}
	defer cleanUpBoard(t, *actual.Id)
	assertBoardEquals(t, actual, expected)

	// Now try to fetch it freshly and compare it again
	actual, err = client.GetBoard(*actual.Id)
	if err != nil {
		t.Fatalf("Retrieving a dashboard failed when it shouldn't. (%s)", err)
	}
	assertBoardEquals(t, actual, expected)
}

func TestBoardWithTemplateVarsAndPresets(t *testing.T) {
	expected := getTestBoardWithTemplateVars()

	// Create the dashboard and compare it
	actual, err := client.CreateBoard(expected)
	if err != nil {
		t.Fatalf("Creating a dashboard failed when it shouldn't. (%s)", err)
	}
	defer cleanUpBoard(t, *actual.Id)
	assertBoardEquals(t, actual, expected)

	// Now try to fetch it freshly and compare it again
	actual, err = client.GetBoard(*actual.Id)
	if err != nil {
		t.Fatalf("Retrieving a dashboard failed when it shouldn't. (%s)", err)
	}
	assertBoardEquals(t, actual, expected)
}

// Helpers for the tests

func getTestBoard() *datadog.Board {
	return &datadog.Board{
		Title:       datadog.String("Test Dashboard"),
		Widgets:     createWidgets(),
		LayoutType:  datadog.String("ordered"),
		Description: datadog.String("Test Dashboard description"),
		IsReadOnly:  datadog.Bool(false),
	}
}

func getTestBoardWithTemplateVars() *datadog.Board {
	return &datadog.Board{
		Title:                   datadog.String("Test Dashboard"),
		Widgets:                 createWidgets(),
		LayoutType:              datadog.String("ordered"),
		Description:             datadog.String("Test Dashboard description"),
		IsReadOnly:              datadog.Bool(false),
		TemplateVariables:       getTestTemplateVars(),
		TemplateVariablePresets: getTestTemplateVarPresets(),
	}
}

func getTestTemplateVars() []datadog.TemplateVariable {
	return []datadog.TemplateVariable{
		{
			Name:    datadog.String("Template Var 1"),
			Prefix:  datadog.String("var1"),
			Default: datadog.String("value1"),
		},
		{
			Name:   datadog.String("Template Var 2"),
			Prefix: datadog.String("var2"),
		},
	}
}

func getTestTemplateVarPresets() []datadog.TemplateVariablePreset {
	return []datadog.TemplateVariablePreset{
		{
			Name: datadog.String("Preset 1"),
			TemplateVariables: []datadog.TemplateVariablePresetValue{
				{
					Name:  datadog.String("Template Var 1"),
					Value: datadog.String("Preset 1 Var 1"),
				},
				{
					Name:  datadog.String("Template Var 2"),
					Value: datadog.String("Preset 1 Var 2"),
				},
			},
		},
		{
			Name: datadog.String("Preset 2"),
			TemplateVariables: []datadog.TemplateVariablePresetValue{
				{
					Name:  datadog.String("Template Var 1"),
					Value: datadog.String("Preset 2 Var 1"),
				},
			},
		},
	}
}

func createWidgets() []datadog.BoardWidget {
	widgets := []datadog.BoardWidget{}
	widgets = append(widgets, createAlertGraph())
	widgets = append(widgets, createTimeseriesWidget())
	widgets = append(widgets, createGroupWidget())
	return widgets
}

func createAlertGraph() datadog.BoardWidget {
	return datadog.BoardWidget{
		Definition: &datadog.AlertGraphDefinition{
			Type:    datadog.String("alert_graph"),
			AlertId: datadog.String("123456"),
			VizType: datadog.String("timeseries"),
			Title:   datadog.String("Test Alert Graph widget"),
		},
	}
}

func createGroupWidget() datadog.BoardWidget {
	widgets := []datadog.BoardWidget{
		createTimeseriesWidget(),
		createAlertGraph(),
	}
	return datadog.BoardWidget{
		Definition: &datadog.GroupDefinition{
			Type:       datadog.String("group"),
			LayoutType: datadog.String("ordered"),
			Widgets:    widgets,
			Title:      datadog.String("Group widget"),
		},
	}
}

func createTimeseriesWidget() datadog.BoardWidget {
	request := datadog.TimeseriesRequest{
		MetricQuery: datadog.String("avg:system.cpu.user{*}"),
		Style: &datadog.TimeseriesRequestStyle{
			LineType: datadog.String("dotted"),
		},
		DisplayType: datadog.String("area"),
	}
	return datadog.BoardWidget{
		Definition: &datadog.TimeseriesDefinition{
			Type:     datadog.String("timeseries"),
			Requests: []datadog.TimeseriesRequest{request},
			Yaxis: &datadog.WidgetAxis{
				Label:       datadog.String("y-axis label"),
				Max:         datadog.String("3000"),
				IncludeZero: datadog.Bool(true),
			},
			Title: datadog.String("Test Timeseries widget"),
		},
	}
}

func cleanUpBoard(t *testing.T, id string) {
	if err := client.DeleteBoard(id); err != nil {
		t.Fatalf("Deleting a dashboard failed when it shouldn't. Manual cleanup needed. (%s)", err)
	}

	deletedBoard, err := client.GetBoard(id)
	if deletedBoard != nil {
		t.Fatal("Dashboard hasn't been deleted when it should have been. Manual cleanup needed.")
	}

	if err == nil {
		t.Fatal("Fetching deleted dashboard didn't lead to an error. Manual cleanup needed.")
	}
}

func assertBoardEquals(t *testing.T, actual, expected *datadog.Board) {
	if *actual.Title != *expected.Title {
		t.Errorf("Dashboard title does not match: %s != %s", *actual.Title, *expected.Title)
	}
	if len(actual.Widgets) != len(expected.Widgets) {
		t.Errorf("Number of Dashboard widgets does not match: %d != %d", len(actual.Widgets), len(expected.Widgets))
	}
	if *actual.LayoutType != *expected.LayoutType {
		t.Errorf("Dashboard layout type does not match: %s != %s", *actual.LayoutType, *expected.LayoutType)
	}
	if *actual.Description != *expected.Description {
		t.Errorf("Dashboard description does not match: %s != %s", *actual.Description, *expected.Description)
	}
	if len(actual.TemplateVariables) != len(expected.TemplateVariables) {
		t.Errorf("Number of template variables does not match: %d != %d", len(actual.TemplateVariables), len(expected.TemplateVariables))
	}
	if len(actual.TemplateVariablePresets) != len(expected.TemplateVariablePresets) {
		t.Errorf("Number of template variables presets does not match: %d != %d", len(actual.TemplateVariablePresets), len(expected.TemplateVariablePresets))
	}
	if *actual.IsReadOnly != *expected.IsReadOnly {
		t.Errorf("Dashboard description does not match: %v != %v", *actual.IsReadOnly, *expected.IsReadOnly)
	}
	if len(actual.NotifyList) != len(expected.NotifyList) {
		t.Errorf("Number of users to notify does not match: %d != %d", len(actual.NotifyList), len(expected.NotifyList))
	}
}