2023-02-17 12:54:40 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-02-20 15:34:35 +00:00
|
|
|
func TestHandleStatsConfig(t *testing.T) {
|
2023-02-17 12:54:40 +00:00
|
|
|
var (
|
|
|
|
halfHour = time.Hour / 2
|
|
|
|
hour = time.Hour
|
|
|
|
year = timeutil.Day * 365
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2023-02-20 15:34:35 +00:00
|
|
|
body getConfigResp
|
2023-02-17 12:54:40 +00:00
|
|
|
wantCode int
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr string
|
2023-02-17 12:54:40 +00:00
|
|
|
}{{
|
|
|
|
name: "set_ivl_1_hour",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(hour.Milliseconds()),
|
|
|
|
Ignored: nil,
|
|
|
|
},
|
|
|
|
wantCode: http.StatusOK,
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr: "",
|
2023-02-17 12:54:40 +00:00
|
|
|
}, {
|
|
|
|
name: "small_interval",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(halfHour.Milliseconds()),
|
|
|
|
Ignored: []string{},
|
|
|
|
},
|
2023-02-20 15:34:35 +00:00
|
|
|
wantCode: http.StatusUnprocessableEntity,
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr: "unsupported interval\n",
|
2023-02-17 12:54:40 +00:00
|
|
|
}, {
|
|
|
|
name: "big_interval",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(year.Milliseconds() + hour.Milliseconds()),
|
|
|
|
Ignored: []string{},
|
|
|
|
},
|
2023-02-20 15:34:35 +00:00
|
|
|
wantCode: http.StatusUnprocessableEntity,
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr: "unsupported interval\n",
|
2023-02-17 12:54:40 +00:00
|
|
|
}, {
|
|
|
|
name: "set_ignored_ivl_1_year",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(year.Milliseconds()),
|
|
|
|
Ignored: []string{
|
|
|
|
"ignor.ed",
|
|
|
|
},
|
|
|
|
},
|
2023-02-20 08:17:28 +00:00
|
|
|
wantCode: http.StatusOK,
|
|
|
|
wantErr: "",
|
2023-02-17 12:54:40 +00:00
|
|
|
}, {
|
|
|
|
name: "ignored_duplicate",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(hour.Milliseconds()),
|
|
|
|
Ignored: []string{
|
|
|
|
"ignor.ed",
|
|
|
|
"ignor.ed",
|
|
|
|
},
|
|
|
|
},
|
2023-02-20 15:34:35 +00:00
|
|
|
wantCode: http.StatusUnprocessableEntity,
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr: "ignored: duplicate host name \"ignor.ed\"\n",
|
2023-02-17 12:54:40 +00:00
|
|
|
}, {
|
|
|
|
name: "ignored_empty",
|
2023-02-20 15:34:35 +00:00
|
|
|
body: getConfigResp{
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: aghalg.NBTrue,
|
|
|
|
Interval: float64(hour.Milliseconds()),
|
|
|
|
Ignored: []string{
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
},
|
2023-02-20 15:34:35 +00:00
|
|
|
wantCode: http.StatusUnprocessableEntity,
|
2023-02-20 08:17:28 +00:00
|
|
|
wantErr: "ignored: host name is empty\n",
|
2023-02-20 15:34:35 +00:00
|
|
|
}, {
|
|
|
|
name: "enabled_is_null",
|
|
|
|
body: getConfigResp{
|
|
|
|
Enabled: aghalg.NBNull,
|
|
|
|
Interval: float64(hour.Milliseconds()),
|
|
|
|
Ignored: []string{},
|
|
|
|
},
|
|
|
|
wantCode: http.StatusUnprocessableEntity,
|
|
|
|
wantErr: "enabled is null\n",
|
2023-02-17 12:54:40 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
handlers := map[string]http.Handler{}
|
|
|
|
|
|
|
|
conf := Config{
|
|
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
2023-02-20 08:17:28 +00:00
|
|
|
Limit: time.Hour * 24,
|
2023-02-17 12:54:40 +00:00
|
|
|
Enabled: true,
|
|
|
|
UnitID: func() (id uint32) { return 0 },
|
|
|
|
HTTPRegister: func(
|
|
|
|
_ string,
|
|
|
|
url string,
|
|
|
|
handler http.HandlerFunc,
|
|
|
|
) {
|
|
|
|
handlers[url] = handler
|
|
|
|
},
|
|
|
|
ConfigModified: func() {},
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := New(conf)
|
|
|
|
require.NoError(t, err)
|
2023-02-20 08:17:28 +00:00
|
|
|
|
2023-02-17 12:54:40 +00:00
|
|
|
s.Start()
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
|
|
|
|
buf, err := json.Marshal(tc.body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configGet = "/control/stats/config"
|
|
|
|
configPut = "/control/stats/config/update"
|
|
|
|
)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPut, configPut, bytes.NewReader(buf))
|
|
|
|
rw := httptest.NewRecorder()
|
2023-02-20 08:17:28 +00:00
|
|
|
|
2023-02-17 12:54:40 +00:00
|
|
|
handlers[configPut].ServeHTTP(rw, req)
|
|
|
|
require.Equal(t, tc.wantCode, rw.Code)
|
|
|
|
|
2023-02-20 08:17:28 +00:00
|
|
|
if tc.wantCode != http.StatusOK {
|
2023-02-20 15:34:35 +00:00
|
|
|
assert.Equal(t, tc.wantCode, rw.Code)
|
2023-02-20 08:17:28 +00:00
|
|
|
assert.Equal(t, tc.wantErr, rw.Body.String())
|
2023-02-17 12:54:40 +00:00
|
|
|
|
2023-02-20 08:17:28 +00:00
|
|
|
return
|
2023-02-17 12:54:40 +00:00
|
|
|
}
|
2023-02-20 08:17:28 +00:00
|
|
|
|
|
|
|
resp := httptest.NewRequest(http.MethodGet, configGet, nil)
|
|
|
|
rw = httptest.NewRecorder()
|
|
|
|
|
|
|
|
handlers[configGet].ServeHTTP(rw, resp)
|
|
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
|
2023-02-20 15:34:35 +00:00
|
|
|
ans := getConfigResp{}
|
2023-02-20 08:17:28 +00:00
|
|
|
jerr := json.Unmarshal(rw.Body.Bytes(), &ans)
|
|
|
|
require.NoError(t, jerr)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.body, ans)
|
2023-02-17 12:54:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|