-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
28 changed files
with
844 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.