Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Aug 21, 2024
1 parent 30da6a3 commit 8d1f422
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/toast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/containeroo/toast/pkg/runner"
)

const version = "0.0.3"
const version = "0.0.4"

// run is the main function of the application
func run(ctx context.Context, getenv func(string) string, output io.Writer) error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func NewChecker(checkType, name, address string, timeout time.Duration, getEnv f
}
}

// IsValidCheckType validates if the check type is supported.
func IsValidCheckType(checkType string) bool {
return checkType == "tcp" || checkType == "http"
}

// InferCheckType infers the check type based on the scheme of the target address.
func InferCheckType(address string) (string, error) {
scheme, _ := extractScheme(address)
Expand Down
39 changes: 39 additions & 0 deletions pkg/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,45 @@ func TestNewChecker(t *testing.T) {
})
}

func TestIsValidCheckType(t *testing.T) {
t.Parallel()

t.Run("Valid TCP Check Type", func(t *testing.T) {
t.Parallel()
if isValid := IsValidCheckType("tcp"); !isValid {
t.Errorf("expected true for check type 'tcp', got false")
}
})

t.Run("Valid HTTP Check Type", func(t *testing.T) {
t.Parallel()
if isValid := IsValidCheckType("http"); !isValid {
t.Errorf("expected true for check type 'http', got false")
}
})

t.Run("Invalid Check Type", func(t *testing.T) {
t.Parallel()
if isValid := IsValidCheckType("invalid"); isValid {
t.Errorf("expected false for check type 'invalid', got true")
}
})

t.Run("Empty Check Type", func(t *testing.T) {
t.Parallel()
if isValid := IsValidCheckType(""); isValid {
t.Errorf("expected false for empty check type, got true")
}
})

t.Run("Random String Check Type", func(t *testing.T) {
t.Parallel()
if isValid := IsValidCheckType("random"); isValid {
t.Errorf("expected false for check type 'random', got true")
}
})
}

func TestInferCheckType(t *testing.T) {
t.Parallel()

Expand Down
39 changes: 39 additions & 0 deletions pkg/checker/http_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -76,4 +77,42 @@ func TestHTTPChecker(t *testing.T) {
t.Fatal("expected an error, got none")
}
})

t.Run("Test cancel HTTP check", func(t *testing.T) {
t.Parallel()

// Set up a test HTTP server that deliberately delays the response
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second) // Delay to ensure the context has time to be canceled
w.WriteHeader(http.StatusOK)
})
server := httptest.NewServer(handler)
defer server.Close()

// Mock environment variables
mockEnv := func(key string) string {
env := map[string]string{
envMethod: "GET",
envHeaders: "Authorization=Bearer token",
envExpectedStatuses: "200",
}
return env[key]
}

// Create the HTTP checker using the mock environment variables
checker, err := NewHTTPChecker("example", server.URL, 5*time.Second, mockEnv)
if err != nil {
t.Fatalf("failed to create HTTPChecker: %v", err)
}

// Cancel the context after a very short time
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()

// Perform the check, expecting a context canceled error
err = checker.Check(ctx)
if err == nil || !strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
t.Errorf("expected context canceled or deadline exceeded error, got %v", err)
}
})
}
7 changes: 1 addition & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ func ParseConfig(getenv func(string) string) (Config, error) {
cfg.CheckType = checkType
}

if !isValidCheckType(cfg.CheckType) {
if !checker.IsValidCheckType(cfg.CheckType) {
return Config{}, fmt.Errorf("unsupported check type: %s", cfg.CheckType)
}

return cfg, nil
}

// isValidCheckType validates if the check type is supported.
func isValidCheckType(checkType string) bool {
return checkType == "tcp" || checkType == "http"
}

0 comments on commit 8d1f422

Please sign in to comment.