Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Nov 26, 2024
1 parent fe5275c commit 9909162
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion internal/checks/checker.go → internal/checker/checker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down Expand Up @@ -93,7 +93,12 @@ func TestHTTPChecker(t *testing.T) {
t.Run("Invalid URL for HTTP check", func(t *testing.T) {
t.Parallel()

_, err := newHTTPChecker("example", "://invalid-url")
checker, err := newHTTPChecker("example", "://invalid-url")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

err = checker.Check(context.Background()) // Run the check to trigger the error.
if err == nil {
t.Fatal("expected an error, got none")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checks
package checker

import (
"context"
Expand Down
16 changes: 8 additions & 8 deletions internal/config/http_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
"github.com/containeroo/portpatrol/pkg/httputils"
)

Expand All @@ -21,13 +21,13 @@ const (
)

// parseHTTPCheckerOptions parses HTTP checker-specific options from parameters.
func parseHTTPCheckerOptions(params map[string]string) ([]checks.Option, error) {
var opts []checks.Option
func parseHTTPCheckerOptions(params map[string]string) ([]checker.Option, error) {
var opts []checker.Option
unrecognizedParams := trackUnusedParams(params)

// HTTP Method
if method, ok := params[ParamHTTPMethod]; ok && method != "" {
opts = append(opts, checks.WithHTTPMethod(method))
opts = append(opts, checker.WithHTTPMethod(method))
delete(unrecognizedParams, ParamHTTPMethod)
}

Expand All @@ -48,7 +48,7 @@ func parseHTTPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamHTTPHeaders, err)
}
opts = append(opts, checks.WithHTTPHeaders(headers))
opts = append(opts, checker.WithHTTPHeaders(headers))
delete(unrecognizedParams, ParamHTTPHeaders)
}

Expand All @@ -58,7 +58,7 @@ func parseHTTPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamHTTPExpectedStatusCodes, err)
}
opts = append(opts, checks.WithExpectedStatusCodes(codes))
opts = append(opts, checker.WithExpectedStatusCodes(codes))
delete(unrecognizedParams, ParamHTTPExpectedStatusCodes)
}

Expand All @@ -68,7 +68,7 @@ func parseHTTPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamHTTPSkipTLSVerify, err)
}
opts = append(opts, checks.WithHTTPSkipTLSVerify(skip))
opts = append(opts, checker.WithHTTPSkipTLSVerify(skip))
delete(unrecognizedParams, ParamHTTPSkipTLSVerify)
}

Expand All @@ -78,7 +78,7 @@ func parseHTTPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil || timeout <= 0 {
return nil, fmt.Errorf("invalid %q: %w", ParamHTTPTimeout, err)
}
opts = append(opts, checks.WithHTTPTimeout(timeout))
opts = append(opts, checker.WithHTTPTimeout(timeout))
delete(unrecognizedParams, ParamHTTPTimeout)
}

Expand Down
10 changes: 5 additions & 5 deletions internal/config/icmp_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
)

const (
Expand All @@ -13,8 +13,8 @@ const (
)

// parseICMPCheckerOptions parses ICMP checker-specific options from parameters.
func parseICMPCheckerOptions(params map[string]string) ([]checks.Option, error) {
var opts []checks.Option
func parseICMPCheckerOptions(params map[string]string) ([]checker.Option, error) {
var opts []checker.Option
unrecognizedParams := trackUnusedParams(params)

// ICMP Read Timeout
Expand All @@ -23,7 +23,7 @@ func parseICMPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamICMPReadTimeout, err)
}
opts = append(opts, checks.WithICMPReadTimeout(readTimeout))
opts = append(opts, checker.WithICMPReadTimeout(readTimeout))
delete(unrecognizedParams, ParamICMPReadTimeout)
}

Expand All @@ -33,7 +33,7 @@ func parseICMPCheckerOptions(params map[string]string) ([]checks.Option, error)
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamICMPWriteTimeout, err)
}
opts = append(opts, checks.WithICMPWriteTimeout(writeTimeout))
opts = append(opts, checker.WithICMPWriteTimeout(writeTimeout))
delete(unrecognizedParams, ParamICMPWriteTimeout)
}

Expand Down
16 changes: 8 additions & 8 deletions internal/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
)

const (
Expand All @@ -19,7 +19,7 @@ const (
// TargetChecker represents a checker with its interval.
type TargetChecker struct {
Interval time.Duration
Checker checks.Checker
Checker checker.Checker
}

// LoadTargetCheckers creates a slice of TargetChecker based on the provided target configurations.
Expand All @@ -42,7 +42,7 @@ func LoadTargetCheckers(targets map[string]map[string]string, defaultInterval ti
checkTypeStr = parts[0]
}

checkType, err := checks.GetCheckTypeFromString(checkTypeStr)
checkType, err := checker.GetCheckTypeFromString(checkTypeStr)
if err != nil {
return nil, fmt.Errorf("unsupported check type %q for target %q", checkTypeStr, targetName)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func LoadTargetCheckers(targets map[string]map[string]string, defaultInterval ti
}

// Create the checker using the functional options
chk, err := checks.NewChecker(checkType, name, address, options...)
chk, err := checker.NewChecker(checkType, name, address, options...)
if err != nil {
return nil, fmt.Errorf("failed to create checker for target %q: %w", targetName, err)
}
Expand All @@ -90,13 +90,13 @@ func LoadTargetCheckers(targets map[string]map[string]string, defaultInterval ti
}

// collectCheckerOptions collects functional options for a specific check type.
func collectCheckerOptions(checkType checks.CheckType, params map[string]string, targetName string) ([]checks.Option, error) {
func collectCheckerOptions(checkType checker.CheckType, params map[string]string, targetName string) ([]checker.Option, error) {
switch checkType {
case checks.HTTP:
case checker.HTTP:
return parseHTTPCheckerOptions(params)
case checks.TCP:
case checker.TCP:
return parseTCPCheckerOptions(params)
case checks.ICMP:
case checker.ICMP:
return parseICMPCheckerOptions(params)
default:
return nil, fmt.Errorf("unsupported check type for target %q", targetName)
Expand Down
8 changes: 4 additions & 4 deletions internal/config/tcp_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
)

const ParamTCPTimeout string = "timeout"

// parseTCPCheckerOptions parses TCP checker-specific options from parameters.
func parseTCPCheckerOptions(params map[string]string) ([]checks.Option, error) {
var opts []checks.Option
func parseTCPCheckerOptions(params map[string]string) ([]checker.Option, error) {
var opts []checker.Option
unrecognizedParams := trackUnusedParams(params)

// TCP Timeout
Expand All @@ -20,7 +20,7 @@ func parseTCPCheckerOptions(params map[string]string) ([]checks.Option, error) {
if err != nil {
return nil, fmt.Errorf("invalid %q: %w", ParamTCPTimeout, err)
}
opts = append(opts, checks.WithTCPTimeout(timeout))
opts = append(opts, checker.WithTCPTimeout(timeout))
delete(unrecognizedParams, ParamTCPTimeout)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"log/slog"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
)

// WaitUntilReady continuously attempts to connect to the specified target until it becomes available or the context is canceled.
func WaitUntilReady(ctx context.Context, interval time.Duration, checker checks.Checker, logger *slog.Logger) error {
func WaitUntilReady(ctx context.Context, interval time.Duration, checker checker.Checker, logger *slog.Logger) error {
logger = logger.With(
slog.String("target", checker.GetName()),
slog.String("type", checker.GetType()),
Expand Down
21 changes: 11 additions & 10 deletions internal/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"testing"
"time"

"github.com/containeroo/portpatrol/internal/checks"
"github.com/containeroo/portpatrol/internal/checker"
)

// TestWaitUntilReady_ReadyHTTP ensures WaitUntilReady returns success when the HTTP target is ready.
func TestWaitUntilReady_ReadyHTTP(t *testing.T) {
t.Parallel()

server := &http.Server{Addr: ":9082"}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
go func() { _ = server.ListenAndServe() }()
defer server.Close()

checker, err := checks.NewChecker(checks.HTTP, "HTTPServer", "http://localhost:9082")
checker, err := checker.NewChecker(checker.HTTP, "HTTPServer", "http://localhost:9082/ready")
if err != nil {
t.Fatalf("Failed to create HTTPChecker: %v", err)
}
Expand All @@ -50,14 +50,14 @@ func TestWaitUntilReady_HTTPFailsInitially(t *testing.T) {
t.Parallel()

server := &http.Server{Addr: ":9083"}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/fail", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond) // Simulate a delayed start
w.WriteHeader(http.StatusOK)
})
go func() { _ = server.ListenAndServe() }()
defer server.Close()

checker, err := checks.NewChecker(checks.HTTP, "HTTPServer", "http://localhost:9083")
checker, err := checker.NewChecker(checker.HTTP, "HTTPServer", "http://localhost:9083/fail")
if err != nil {
t.Fatalf("Failed to create HTTPChecker: %v", err)
}
Expand All @@ -84,13 +84,14 @@ func TestWaitUntilReady_HTTPContextCanceled(t *testing.T) {
t.Parallel()

server := &http.Server{Addr: ":9084"}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/canceled", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
w.WriteHeader(http.StatusOK)
})
go func() { _ = server.ListenAndServe() }()
defer server.Close()

checker, err := checks.NewChecker(checks.HTTP, "HTTPServer", "http://localhost:9084")
checker, err := checker.NewChecker(checker.HTTP, "HTTPServer", "http://localhost:9084/canceled")
if err != nil {
t.Fatalf("Failed to create HTTPChecker: %v", err)
}
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestWaitUntilReady_ReadyTCP(t *testing.T) {
}
defer ln.Close()

checker, err := checks.NewChecker(checks.TCP, "TCPServer", "localhost:9085")
checker, err := checker.NewChecker(checker.TCP, "TCPServer", "localhost:9085")
if err != nil {
t.Fatalf("Failed to create TCPChecker: %v", err)
}
Expand Down Expand Up @@ -163,7 +164,7 @@ func TestWaitUntilReady_TCPFailsInitially(t *testing.T) {
}
}()

checker, err := checks.NewChecker(checks.TCP, "TCPServer", "localhost:9086")
checker, err := checker.NewChecker(checker.TCP, "TCPServer", "localhost:9086")
if err != nil {
t.Fatalf("Failed to create TCPChecker: %v", err)
}
Expand All @@ -189,7 +190,7 @@ func TestWaitUntilReady_TCPFailsInitially(t *testing.T) {
func TestWaitUntilReady_TCPContextCanceled(t *testing.T) {
t.Parallel()

checker, err := checks.NewChecker(checks.TCP, "TCPServer", "localhost:9087")
checker, err := checker.NewChecker(checker.TCP, "TCPServer", "localhost:9087")
if err != nil {
t.Fatalf("Failed to create TCPChecker: %v", err)
}
Expand Down

0 comments on commit 9909162

Please sign in to comment.