master
go 152 lines 3.36 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 //go:build linux || freebsd || openbsd || netbsd || dragonfly
4
5 package litespeed
6
7 import (
8 "context"
9 "os"
10 "testing"
11
12 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
13
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 )
17
18 var (
19 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
20 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
21 )
22
23 func Test_testDataIsValid(t *testing.T) {
24 for name, data := range map[string][]byte{
25 "dataConfigJSON": dataConfigJSON,
26 "dataConfigYAML": dataConfigYAML,
27 } {
28 require.NotNil(t, data, name)
29 }
30 }
31
32 func TestCollector_ConfigurationSerialize(t *testing.T) {
33 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
34 }
35
36 func TestCollector_Init(t *testing.T) {
37 tests := map[string]struct {
38 wantFail bool
39 config Config
40 }{
41 "success with default": {
42 wantFail: false,
43 config: New().Config,
44 },
45 "fails if reports_dir not set": {
46 wantFail: true,
47 config: Config{
48 ReportsDir: "",
49 },
50 },
51 }
52
53 for name, test := range tests {
54 t.Run(name, func(t *testing.T) {
55 collr := New()
56 collr.Config = test.config
57
58 if test.wantFail {
59 assert.Error(t, collr.Init(context.Background()))
60 } else {
61 assert.NoError(t, collr.Init(context.Background()))
62 }
63 })
64 }
65 }
66
67 func TestCollector_Charts(t *testing.T) {
68 assert.NotNil(t, New().Charts())
69 }
70
71 func TestCollector_Check(t *testing.T) {
72 tests := map[string]struct {
73 prepareLitespeed func() *Collector
74 wantFail bool
75 }{
76 "success": {
77 wantFail: false,
78 prepareLitespeed: prepareLitespeedOk,
79 },
80 "fails if reports dir not exist": {
81 wantFail: true,
82 prepareLitespeed: prepareLitespeedDirNotExist,
83 },
84 }
85
86 for name, test := range tests {
87 t.Run(name, func(t *testing.T) {
88 collr := test.prepareLitespeed()
89
90 if test.wantFail {
91 assert.Error(t, collr.Check(context.Background()))
92 } else {
93 assert.NoError(t, collr.Check(context.Background()))
94 }
95 })
96 }
97 }
98
99 func TestCollector_Collect(t *testing.T) {
100 tests := map[string]struct {
101 prepareLitespeed func() *Collector
102 wantMetrics map[string]int64
103 }{
104 "success": {
105 prepareLitespeed: prepareLitespeedOk,
106 wantMetrics: map[string]int64{
107 "availconn": 3804,
108 "availssl": 3814,
109 "bps_in": 0,
110 "bps_out": 240,
111 "plainconn": 10,
112 "private_cache_hits_per_sec": 0,
113 "pub_cache_hits_per_sec": 0,
114 "req_per_sec": 1560,
115 "req_processing": 168,
116 "ssl_bps_in": 16,
117 "ssl_bps_out": 3120,
118 "sslconn": 186,
119 "static_hits_per_sec": 760,
120 },
121 },
122 "fails if reports dir not exist": {
123 prepareLitespeed: prepareLitespeedDirNotExist,
124 },
125 }
126
127 for name, test := range tests {
128 t.Run(name, func(t *testing.T) {
129 collr := test.prepareLitespeed()
130
131 mx := collr.Collect(context.Background())
132
133 assert.Equal(t, test.wantMetrics, mx)
134
135 if len(test.wantMetrics) > 0 {
136 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
137 }
138 })
139 }
140 }
141
142 func prepareLitespeedOk() *Collector {
143 collr := New()
144 collr.ReportsDir = "testdata"
145 return collr
146 }
147
148 func prepareLitespeedDirNotExist() *Collector {
149 collr := prepareLitespeedOk()
150 collr.ReportsDir += "!"
151 return collr
152 }