| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package netdataapi |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestNew(t *testing.T) { |
| 13 | t.Run("valid writer", func(t *testing.T) { |
| 14 | require.NotNil(t, New(&bytes.Buffer{})) |
| 15 | }) |
| 16 | |
| 17 | t.Run("nil writer", func(t *testing.T) { |
| 18 | require.Panics(t, func() { New(nil) }) |
| 19 | }) |
| 20 | } |
| 21 | |
| 22 | func TestChart(t *testing.T) { |
| 23 | w := &bytes.Buffer{} |
| 24 | api := New(w) |
| 25 | |
| 26 | opts := ChartOpts{ |
| 27 | TypeID: "system", |
| 28 | ID: "cpu", |
| 29 | Name: "cpu_system", |
| 30 | Title: "CPU Usage", |
| 31 | Units: "percentage", |
| 32 | Family: "cpu", |
| 33 | Context: "system.cpu", |
| 34 | ChartType: "line", |
| 35 | Priority: 1000, |
| 36 | UpdateEvery: 1, |
| 37 | Options: "", |
| 38 | Plugin: "system", |
| 39 | Module: "cpu", |
| 40 | } |
| 41 | |
| 42 | api.CHART(opts) |
| 43 | |
| 44 | expected := "CHART 'system.cpu' 'cpu_system' 'CPU Usage' 'percentage' 'cpu' 'system.cpu' " + |
| 45 | "'line' '1000' '1' '' 'system' 'cpu'\n" |
| 46 | |
| 47 | require.Equal(t, expected, w.String()) |
| 48 | } |
| 49 | |
| 50 | func TestDimension(t *testing.T) { |
| 51 | w := &bytes.Buffer{} |
| 52 | api := New(w) |
| 53 | |
| 54 | opts := DimensionOpts{ |
| 55 | ID: "user", |
| 56 | Name: "user", |
| 57 | Algorithm: "absolute", |
| 58 | Multiplier: 1, |
| 59 | Divisor: 1, |
| 60 | Options: "", |
| 61 | } |
| 62 | |
| 63 | api.DIMENSION(opts) |
| 64 | |
| 65 | expected := "DIMENSION 'user' 'user' 'absolute' '1' '1' ''\n" |
| 66 | |
| 67 | require.Equal(t, expected, w.String()) |
| 68 | } |
| 69 | |
| 70 | func TestCLABEL(t *testing.T) { |
| 71 | w := &bytes.Buffer{} |
| 72 | api := New(w) |
| 73 | |
| 74 | api.CLABEL("key1", "value1", 1) |
| 75 | |
| 76 | expected := "CLABEL 'key1' 'value1' '1'\n" |
| 77 | |
| 78 | require.Equal(t, expected, w.String()) |
| 79 | } |
| 80 | |
| 81 | func TestCLABELCOMMIT(t *testing.T) { |
| 82 | w := &bytes.Buffer{} |
| 83 | api := New(w) |
| 84 | |
| 85 | api.CLABELCOMMIT() |
| 86 | |
| 87 | expected := "CLABEL_COMMIT\n" |
| 88 | |
| 89 | require.Equal(t, expected, w.String()) |
| 90 | } |
| 91 | |
| 92 | func TestBEGIN(t *testing.T) { |
| 93 | |
| 94 | tests := map[string]struct { |
| 95 | name string |
| 96 | typeID string |
| 97 | ID string |
| 98 | msSince int |
| 99 | expected string |
| 100 | }{ |
| 101 | "without msSince": { |
| 102 | typeID: "system", |
| 103 | ID: "cpu", |
| 104 | msSince: 0, |
| 105 | expected: "BEGIN 'system.cpu'\n", |
| 106 | }, |
| 107 | "with msSince": { |
| 108 | typeID: "system", |
| 109 | ID: "cpu", |
| 110 | msSince: 1000, |
| 111 | expected: "BEGIN 'system.cpu' 1000\n", |
| 112 | }, |
| 113 | } |
| 114 | |
| 115 | for name, test := range tests { |
| 116 | t.Run(name, func(t *testing.T) { |
| 117 | w := &bytes.Buffer{} |
| 118 | api := New(w) |
| 119 | |
| 120 | api.BEGIN(test.typeID, test.ID, test.msSince) |
| 121 | |
| 122 | require.Equal(t, test.expected, w.String()) |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestSET(t *testing.T) { |
| 128 | w := &bytes.Buffer{} |
| 129 | api := New(w) |
| 130 | |
| 131 | api.SET("cpu_user", 42) |
| 132 | |
| 133 | expected := "SET 'cpu_user' = 42\n" |
| 134 | |
| 135 | require.Equal(t, expected, w.String()) |
| 136 | } |
| 137 | |
| 138 | func TestSETFLOAT(t *testing.T) { |
| 139 | w := &bytes.Buffer{} |
| 140 | api := New(w) |
| 141 | |
| 142 | api.SETFLOAT("cpu_user", 42.42) |
| 143 | |
| 144 | expected := "SET 'cpu_user' = 42.42\n" |
| 145 | |
| 146 | require.Equal(t, expected, w.String()) |
| 147 | } |
| 148 | |
| 149 | func TestSETEMPTY(t *testing.T) { |
| 150 | w := &bytes.Buffer{} |
| 151 | api := New(w) |
| 152 | |
| 153 | api.SETEMPTY("cpu_user") |
| 154 | |
| 155 | expected := "SET 'cpu_user' = \n" |
| 156 | |
| 157 | require.Equal(t, expected, w.String()) |
| 158 | } |
| 159 | |
| 160 | func TestVARIABLE(t *testing.T) { |
| 161 | w := &bytes.Buffer{} |
| 162 | api := New(w) |
| 163 | |
| 164 | api.VARIABLE("var1", 100) |
| 165 | api.VARIABLE("var2", 100.1) |
| 166 | |
| 167 | expected := "VARIABLE CHART 'var1' = 100\nVARIABLE CHART 'var2' = 100.1\n" |
| 168 | |
| 169 | require.Equal(t, expected, w.String()) |
| 170 | } |
| 171 | |
| 172 | func TestEND(t *testing.T) { |
| 173 | w := &bytes.Buffer{} |
| 174 | api := New(w) |
| 175 | |
| 176 | api.END() |
| 177 | |
| 178 | expected := "END\n\n" |
| 179 | |
| 180 | require.Equal(t, expected, w.String()) |
| 181 | } |
| 182 | |
| 183 | func TestDISABLE(t *testing.T) { |
| 184 | w := &bytes.Buffer{} |
| 185 | api := New(w) |
| 186 | |
| 187 | api.DISABLE() |
| 188 | |
| 189 | expected := "DISABLE\n" |
| 190 | |
| 191 | require.Equal(t, expected, w.String()) |
| 192 | } |
| 193 | |
| 194 | func TestEMPTYLINE(t *testing.T) { |
| 195 | w := &bytes.Buffer{} |
| 196 | api := New(w) |
| 197 | |
| 198 | require.NoError(t, api.EMPTYLINE()) |
| 199 | |
| 200 | expected := "\n" |
| 201 | |
| 202 | require.Equal(t, expected, w.String()) |
| 203 | } |
| 204 | |
| 205 | func TestHOSTINFO(t *testing.T) { |
| 206 | w := &bytes.Buffer{} |
| 207 | api := New(w) |
| 208 | |
| 209 | info := HostInfo{ |
| 210 | GUID: "test-guid", |
| 211 | Hostname: "test-host", |
| 212 | Labels: map[string]string{ |
| 213 | "label2": "value2", |
| 214 | "label1": "value1", |
| 215 | }, |
| 216 | } |
| 217 | |
| 218 | api.HOSTINFO(info) |
| 219 | |
| 220 | expected := ` |
| 221 | HOST_DEFINE 'test-guid' 'test-host' |
| 222 | HOST_LABEL 'label1' 'value1' |
| 223 | HOST_LABEL 'label2' 'value2' |
| 224 | HOST_DEFINE_END |
| 225 | |
| 226 | `[1:] |
| 227 | |
| 228 | require.Equal(t, expected, w.String()) |
| 229 | } |
| 230 | |
| 231 | func TestHOST(t *testing.T) { |
| 232 | w := &bytes.Buffer{} |
| 233 | api := New(w) |
| 234 | |
| 235 | api.HOST("test-guid") |
| 236 | |
| 237 | expected := "HOST 'test-guid'\n\n" |
| 238 | |
| 239 | require.Equal(t, expected, w.String()) |
| 240 | } |
| 241 | |
| 242 | func TestFUNCRESULT(t *testing.T) { |
| 243 | w := &bytes.Buffer{} |
| 244 | api := New(w) |
| 245 | |
| 246 | result := FunctionResult{ |
| 247 | UID: "test-uid", |
| 248 | ContentType: "text/plain", |
| 249 | Payload: "test payload", |
| 250 | Code: "200", |
| 251 | ExpireTimestamp: "1234567890", |
| 252 | } |
| 253 | |
| 254 | api.FUNCRESULT(result) |
| 255 | |
| 256 | expected := "FUNCTION_RESULT_BEGIN test-uid 200 text/plain 1234567890\ntest payload\nFUNCTION_RESULT_END\n\n" |
| 257 | |
| 258 | require.Equal(t, expected, w.String()) |
| 259 | } |
| 260 | |
| 261 | func TestCONFIGCREATE(t *testing.T) { |
| 262 | w := &bytes.Buffer{} |
| 263 | api := New(w) |
| 264 | |
| 265 | opts := ConfigOpts{ |
| 266 | ID: "test-config", |
| 267 | Status: "active", |
| 268 | ConfigType: "test", |
| 269 | Path: "/test/path", |
| 270 | SourceType: "file", |
| 271 | Source: "test.conf", |
| 272 | SupportedCommands: "read,write", |
| 273 | } |
| 274 | |
| 275 | api.CONFIGCREATE(opts) |
| 276 | |
| 277 | expected := "CONFIG test-config create active test /test/path file 'test.conf' 'read,write' 0x0000 0x0000\n\n" |
| 278 | |
| 279 | require.Equal(t, expected, w.String()) |
| 280 | } |
| 281 | |
| 282 | func TestCONFIGDELETE(t *testing.T) { |
| 283 | w := &bytes.Buffer{} |
| 284 | api := New(w) |
| 285 | |
| 286 | api.CONFIGDELETE("test-config") |
| 287 | |
| 288 | expected := "CONFIG test-config delete\n\n" |
| 289 | |
| 290 | require.Equal(t, expected, w.String()) |
| 291 | } |
| 292 | |
| 293 | func TestCONFIGSTATUS(t *testing.T) { |
| 294 | w := &bytes.Buffer{} |
| 295 | api := New(w) |
| 296 | |
| 297 | api.CONFIGSTATUS("test-config", "inactive") |
| 298 | |
| 299 | expected := "CONFIG test-config status inactive\n\n" |
| 300 | |
| 301 | require.Equal(t, expected, w.String()) |
| 302 | } |