Skip to content

Commit

Permalink
refactor: setvar
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal committed Oct 31, 2024
1 parent a94522e commit a4890b2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
46 changes: 21 additions & 25 deletions src/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var loadEnvOnce sync.Once
var (
QBittorrent QBittorrentSettings
Exporter ExporterSettings
ShouldShowError bool
ShouldShowError bool = true
UsingEnvFile bool
)

Expand Down Expand Up @@ -46,28 +46,6 @@ type Features struct {
EnableTracker bool
}

func SetVar(port int, enableTracker bool, loglevel string, baseUrl string, username string, password string, timeout int, enableHighCardinality bool, enableLabelWithHash bool) {
ShouldShowError = true
QBittorrent = QBittorrentSettings{
BaseUrl: baseUrl,
Username: username,
Password: password,
Timeout: time.Duration(timeout) * time.Second,
}
Exporter = ExporterSettings{
Feature: Features{
EnableHighCardinality: enableHighCardinality,
EnableTracker: enableTracker,
},
ExperimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: enableLabelWithHash,
},
LogLevel: loglevel,
Port: port,
}

}

func LoadEnv() {
loadEnvOnce.Do(func() {
var envfile bool
Expand All @@ -88,7 +66,7 @@ func LoadEnv() {
loglevel := logger.SetLogLevel(getEnv(defaultLogLevel))
qbitUsername := getEnv(defaultUsername)
qbitPassword := getEnv(defaultPassword)
qbitURL := strings.TrimSuffix(getEnv(defaultBaseUrl), "/")
baseUrl := strings.TrimSuffix(getEnv(defaultBaseUrl), "/")
exporterPortEnv := getEnv(defaultPort)
timeoutDurationEnv := getEnv(defaultTimeout)
enableTracker := getEnv(defaultDisableTracker)
Expand All @@ -111,7 +89,25 @@ func LoadEnv() {
panic(fmt.Sprintf("%d must be > 0", timeoutDuration))
}

SetVar(exporterPort, envSetToTrue(enableTracker), loglevel, qbitURL, qbitUsername, qbitPassword, timeoutDuration, envSetToTrue(enableHighCardinality), envSetToTrue(labelWithHash))
QBittorrent = QBittorrentSettings{
BaseUrl: baseUrl,
Username: qbitUsername,
Password: qbitPassword,
Timeout: time.Duration(timeoutDuration) * time.Second,
}

Exporter = ExporterSettings{
Feature: Features{
EnableHighCardinality: envSetToTrue(enableHighCardinality),
EnableTracker: envSetToTrue(enableTracker),
},
ExperimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: envSetToTrue(labelWithHash),
},
LogLevel: loglevel,
Port: exporterPort,
}

}

func envSetToTrue(env string) bool {
Expand Down
9 changes: 8 additions & 1 deletion src/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package prom
import (
app "qbit-exp/app"
"testing"
"time"
)

func TestMain(t *testing.T) {
app.SetVar(0, false, "", "http://localhost:8080", "admin", "adminadmin", 30, false, false)
app.QBittorrent = app.QBittorrentSettings{
BaseUrl: "http://localhost:8080",
Username: "admin",
Password: "adminadmin",
Timeout: time.Duration(30) * time.Second,
}

result := app.GetPasswordMasked()

if !isValidMaskedPassword(result) {
Expand Down
15 changes: 8 additions & 7 deletions src/qbit/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

var buff = &bytes.Buffer{}

const twoSeconds = time.Duration(2 * time.Second)
const tenMs = time.Duration(10 * time.Millisecond)
const fiftyMs = time.Duration(50 * time.Millisecond)

func init() {
logger.Log = &logger.Logger{Logger: slog.New(slog.NewTextHandler(buff, &slog.HandlerOptions{}))}
Expand All @@ -41,7 +42,7 @@ func TestAuthSuccess(t *testing.T) {
app.QBittorrent.BaseUrl = ts.URL
app.QBittorrent.Username = "testuser"
app.QBittorrent.Password = "testpass"
app.QBittorrent.Timeout = twoSeconds
app.QBittorrent.Timeout = tenMs

Auth()

Expand All @@ -64,7 +65,7 @@ func TestAuthFail(t *testing.T) {
app.QBittorrent.BaseUrl = ts.URL
app.QBittorrent.Username = "wronguser"
app.QBittorrent.Password = "wrongpass"
app.QBittorrent.Timeout = twoSeconds
app.QBittorrent.Timeout = tenMs

defer func() {
if r := recover(); r == nil {
Expand All @@ -85,7 +86,7 @@ func TestAuthInvalidUrl(t *testing.T) {
app.QBittorrent.BaseUrl = ts.URL + "//"
app.QBittorrent.Username = ""
app.QBittorrent.Password = ""
app.QBittorrent.Timeout = twoSeconds
app.QBittorrent.Timeout = tenMs

defer func() {
if r := recover(); r == nil {
Expand All @@ -99,14 +100,14 @@ func TestAuthInvalidUrl(t *testing.T) {
func TestAuthTimeout(t *testing.T) {
t.Cleanup(resetState)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
time.Sleep(fiftyMs)
}))
defer ts.Close()

app.QBittorrent.BaseUrl = ts.URL
app.QBittorrent.Username = ""
app.QBittorrent.Password = ""
app.QBittorrent.Timeout = twoSeconds
app.QBittorrent.Timeout = tenMs
Auth()

if !strings.Contains(buff.String(), API.QbittorrentTimeOut) {
Expand All @@ -124,7 +125,7 @@ func TestUnknownStatusCode(t *testing.T) {
app.QBittorrent.BaseUrl = ts.URL
app.QBittorrent.Username = ""
app.QBittorrent.Password = ""
app.QBittorrent.Timeout = twoSeconds
app.QBittorrent.Timeout = tenMs
Auth()

if !strings.Contains(buff.String(), strconv.Itoa(http.StatusCreated)) {
Expand Down
4 changes: 2 additions & 2 deletions src/qbit/qbit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func setupMockApp() {
app.QBittorrent.Timeout = 2 * time.Second
app.QBittorrent.Timeout = 10 * time.Millisecond
app.QBittorrent.Cookie = "SID"
app.ShouldShowError = true
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestApiRequest_Forbidden(t *testing.T) {
func TestApiRequest_Timeout(t *testing.T) {
setupMockApp()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
time.Sleep(20 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
Expand Down

0 comments on commit a4890b2

Please sign in to comment.