// SPDX-License-Identifier: GPL-3.0-or-later
package windows
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
v0200Metrics, _ = os.ReadFile("testdata/v0.20.0/metrics.txt")
)
func Test_TestData(t *testing.T) {
for name, data := range map[string][]byte{
"v0200Metrics": v0200Metrics,
} {
assert.NotNilf(t, data, name)
}
}
func TestNew(t *testing.T) {
assert.IsType(t, (*Windows)(nil), New())
}
func TestWindows_Init(t *testing.T) {
tests := map[string]struct {
config Config
wantFail bool
}{
"success if 'url' is set": {
config: Config{
HTTP: web.HTTP{Request: web.Request{URL: "http://127.0.0.1:9182/metrics"}}},
},
"fails on default config": {
wantFail: true,
config: New().Config,
},
"fails if 'url' is unset": {
wantFail: true,
config: Config{HTTP: web.HTTP{Request: web.Request{URL: ""}}},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
win := New()
win.Config = test.config
if test.wantFail {
assert.False(t, win.Init())
} else {
assert.True(t, win.Init())
}
})
}
}
func TestWindows_Check(t *testing.T) {
tests := map[string]struct {
prepare func() (win *Windows, cleanup func())
wantFail bool
}{
"success on valid response v0.20.0": {
prepare: prepareWindowsV0200,
},
"fails if endpoint returns invalid data": {
wantFail: true,
prepare: prepareWindowsReturnsInvalidData,
},
"fails on connection refused": {
wantFail: true,
prepare: prepareWindowsConnectionRefused,
},
"fails on 404 response": {
wantFail: true,
prepare: prepareWindowsResponse404,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
win, cleanup := test.prepare()
defer cleanup()
require.True(t, win.Init())
if test.wantFail {
assert.False(t, win.Check())
} else {
assert.True(t, win.Check())
}
})
}
}