Skip to content

Commit

Permalink
fix: Fix #230, Add tests for shared collectors (#231)
Browse files Browse the repository at this point in the history
* fix: Fix #230, Add tests for shared collectors

Signed-off-by: Russell Troxel <russell@troxel.io>

* Fix lint: common TestServer

Signed-off-by: Russell Troxel <russell@troxel.io>

---------

Signed-off-by: Russell Troxel <russell@troxel.io>
  • Loading branch information
rtrox committed Oct 18, 2023
1 parent 3155232 commit 8c51f27
Show file tree
Hide file tree
Showing 28 changed files with 844 additions and 26 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
Expand Down
23 changes: 6 additions & 17 deletions internal/arr/collector/bazarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ import (
"testing"

"github.com/onedr0p/exportarr/internal/arr/config"
"github.com/onedr0p/exportarr/internal/test_util"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

const test_fixtures_path = "../test_fixtures/bazarr/"
const API_KEY = "abcdef0123456789abcdef0123456789"
const bazarr_test_fixtures_path = "../test_fixtures/bazarr/"

func newTestBazarrServer(t *testing.T, fn func(http.ResponseWriter, *http.Request)) (*httptest.Server, error) {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fn(w, r)
require.NotEmpty(t, r.URL.Path)
// turns /api/some/path into some_path
endpoint := strings.Replace(strings.Replace(r.URL.Path, "/api/", "", -1), "/", "_", -1)
w.WriteHeader(http.StatusOK)
// NOTE: this assumes there is a file that matches the some_path
json, err := os.ReadFile(test_fixtures_path + endpoint + ".json")
require.NoError(t, err)
_, err = w.Write(json)
require.NoError(t, err)
})), nil
return test_util.NewTestServer(t, bazarr_test_fixtures_path, fn)
}

func TestBazarrCollect(t *testing.T) {
Expand All @@ -42,12 +31,12 @@ func TestBazarrCollect(t *testing.T) {
config := &config.ArrConfig{
URL: ts.URL,
App: "bazarr",
ApiKey: API_KEY,
ApiKey: test_util.API_KEY,
}
collector := NewBazarrCollector(config)
require.NoError(err)

b, err := os.ReadFile(test_fixtures_path + "expected_metrics.txt")
b, err := os.ReadFile(bazarr_test_fixtures_path + "expected_metrics.txt")
require.NoError(err)

expected := strings.Replace(string(b), "SOMEURL", ts.URL, -1)
Expand Down Expand Up @@ -109,7 +98,7 @@ func TestBazarrCollect_FailureDoesntPanic(t *testing.T) {

config := &config.ArrConfig{
URL: ts.URL,
ApiKey: API_KEY,
ApiKey: test_util.API_KEY,
}
collector := NewBazarrCollector(config)

Expand Down
107 changes: 107 additions & 0 deletions internal/arr/collector/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package collector

import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/onedr0p/exportarr/internal/arr/config"
"github.com/onedr0p/exportarr/internal/test_util"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

func TestSystemHealthCollect(t *testing.T) {
var tests = []struct {
name string
config *config.ArrConfig
path string
}{
{
name: "radarr",
config: &config.ArrConfig{
App: "radarr",
ApiVersion: "v3",
},
path: "/api/v3/health",
},
{
name: "sonarr",
config: &config.ArrConfig{
App: "sonarr",
ApiVersion: "v3",
},
path: "/api/v3/health",
},
{
name: "lidarr",
config: &config.ArrConfig{
App: "lidarr",
ApiVersion: "v1",
},
path: "/api/v1/health",
},
{
name: "readarr",
config: &config.ArrConfig{
App: "readarr",
ApiVersion: "v1",
},
path: "/api/v1/health",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
ts, err := test_util.NewTestSharedServer(t, func(w http.ResponseWriter, r *http.Request) {
require.Contains(r.URL.Path, tt.path)
})
require.NoError(err)

defer ts.Close()

tt.config.URL = ts.URL
tt.config.ApiKey = test_util.API_KEY

collector := NewSystemHealthCollector(tt.config)

b, err := os.ReadFile(test_util.COMMON_FIXTURES_PATH + "expected_health_metrics.txt")
require.NoError(err)

expected := strings.Replace(string(b), "SOMEURL", ts.URL, -1)
expected = strings.Replace(expected, "APP", tt.config.App, -1)

f := strings.NewReader(expected)

require.NotPanics(func() {
err = testutil.CollectAndCompare(collector, f)
})
require.NoError(err)
})
}
}

func TestSystemHealthCollect_FailureDoesntPanic(t *testing.T) {
require := require.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()

config := &config.ArrConfig{
URL: ts.URL,
ApiKey: test_util.API_KEY,
}
collector := NewSystemHealthCollector(config)

f := strings.NewReader("")

require.NotPanics(func() {
err := testutil.CollectAndCompare(collector, f)
require.Error(err)
}, "Collecting metrics should not panic on failure")
}
107 changes: 107 additions & 0 deletions internal/arr/collector/history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package collector

import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/onedr0p/exportarr/internal/arr/config"
"github.com/onedr0p/exportarr/internal/test_util"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

func TestHistoryCollect(t *testing.T) {
var tests = []struct {
name string
config *config.ArrConfig
path string
}{
{
name: "radarr",
config: &config.ArrConfig{
App: "radarr",
ApiVersion: "v3",
},
path: "/api/v3/history",
},
{
name: "sonarr",
config: &config.ArrConfig{
App: "sonarr",
ApiVersion: "v3",
},
path: "/api/v3/history",
},
{
name: "lidarr",
config: &config.ArrConfig{
App: "lidarr",
ApiVersion: "v1",
},
path: "/api/v1/history",
},
{
name: "readarr",
config: &config.ArrConfig{
App: "readarr",
ApiVersion: "v1",
},
path: "/api/v1/history",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
ts, err := test_util.NewTestSharedServer(t, func(w http.ResponseWriter, r *http.Request) {
require.Contains(r.URL.Path, tt.path)
})
require.NoError(err)

defer ts.Close()

tt.config.URL = ts.URL
tt.config.ApiKey = test_util.API_KEY

collector := NewHistoryCollector(tt.config)

b, err := os.ReadFile(test_util.COMMON_FIXTURES_PATH + "expected_history_metrics.txt")
require.NoError(err)

expected := strings.Replace(string(b), "SOMEURL", ts.URL, -1)
expected = strings.Replace(expected, "APP", tt.config.App, -1)

f := strings.NewReader(expected)

require.NotPanics(func() {
err = testutil.CollectAndCompare(collector, f)
})
require.NoError(err)
})
}
}

func TestHistoryCollect_FailureDoesntPanic(t *testing.T) {
require := require.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()

config := &config.ArrConfig{
URL: ts.URL,
ApiKey: test_util.API_KEY,
}
collector := NewHistoryCollector(config)

f := strings.NewReader("")

require.NotPanics(func() {
err := testutil.CollectAndCompare(collector, f)
require.Error(err)
}, "Collecting metrics should not panic on failure")
}
2 changes: 1 addition & 1 deletion internal/arr/collector/lidarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (collector *lidarrCollector) Collect(ch chan<- prometheus.Metric) {
if collector.config.EnableAdditionalMetrics {
songFile := model.SongFile{}

var params client.QueryParams
params := client.QueryParams{}
params.Add("artistid", fmt.Sprintf("%d", s.Id))

if err := c.DoRequest("trackfile", &songFile, params); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/arr/collector/prowlarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (collector *prowlarrCollector) Collect(ch chan<- prometheus.Metric) {
startDate := collector.lastStatUpdate.In(time.UTC)
endDate := time.Now().In(time.UTC)

var params client.QueryParams
params := client.QueryParams{}
params.Add("startDate", startDate.Format(time.RFC3339))
params.Add("endDate", endDate.Format(time.RFC3339))

Expand Down
2 changes: 1 addition & 1 deletion internal/arr/collector/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (collector *queueCollector) Collect(ch chan<- prometheus.Metric) {
return
}

var params client.QueryParams
params := client.QueryParams{}
params.Add("page", "1")
if collector.config.EnableUnknownQueueItems {
if collector.config.App == "sonarr" {
Expand Down
Loading

0 comments on commit 8c51f27

Please sign in to comment.