Skip to content

Commit

Permalink
feat(config): Fix tests and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
skhokhlov committed May 22, 2023
1 parent 56dfe7b commit e5aa7e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
15 changes: 8 additions & 7 deletions config/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ var (

// Config is the configuration for the UI of Gatus
type Config struct {
Title string `yaml:"title,omitempty"` // Title of the page
Description string `yaml:"description,omitempty"` // Meta description of the page
Header string `yaml:"header,omitempty"` // Header is the text at the top of the page
Logo string `yaml:"logo,omitempty"` // Logo to display on the page
Link string `yaml:"link,omitempty"` // Link to open when clicking on the logo
Buttons []Button `yaml:"buttons,omitempty"` // Buttons to display below the header
MaximumNumberOfResults int // MaximumNumberOfResults to display on the page
Title string `yaml:"title,omitempty"` // Title of the page
Description string `yaml:"description,omitempty"` // Meta description of the page
Header string `yaml:"header,omitempty"` // Header is the text at the top of the page
Logo string `yaml:"logo,omitempty"` // Logo to display on the page
Link string `yaml:"link,omitempty"` // Link to open when clicking on the logo
Buttons []Button `yaml:"buttons,omitempty"` // Buttons to display below the header

MaximumNumberOfResults int // MaximumNumberOfResults to display on the page, it's not configurable because we're passing it from the storage config
}

// Button is the configuration for a button on the UI
Expand Down
73 changes: 37 additions & 36 deletions controller/handler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,66 @@ import (
"net/http"
"testing"

"github.com/TwiN/gatus/v5/config"
"github.com/TwiN/gatus/v5/storage"
)

func TestExtractPageAndPageSizeFromRequest(t *testing.T) {
type Scenario struct {
Name string
Page string
PageSize string
ExpectedPage int
ExpectedPageSize int
Name string
Page string
PageSize string
ExpectedPage int
ExpectedPageSize int
MaximumNumberOfResults int
}
scenarios := []Scenario{
{
Page: "1",
PageSize: "20",
ExpectedPage: 1,
ExpectedPageSize: 20,
Page: "1",
PageSize: "20",
ExpectedPage: 1,
ExpectedPageSize: 20,
MaximumNumberOfResults: 20,
},
{
Page: "2",
PageSize: "10",
ExpectedPage: 2,
ExpectedPageSize: 10,
Page: "2",
PageSize: "10",
ExpectedPage: 2,
ExpectedPageSize: 10,
MaximumNumberOfResults: 40,
},
{
Page: "2",
PageSize: "10",
ExpectedPage: 2,
ExpectedPageSize: 10,
Page: "2",
PageSize: "10",
ExpectedPage: 2,
ExpectedPageSize: 10,
MaximumNumberOfResults: 200,
},
{
Page: "1",
PageSize: "999999",
ExpectedPage: 1,
ExpectedPageSize: storage.DefaultMaximumNumberOfResults,
Page: "1",
PageSize: "999999",
ExpectedPage: 1,
ExpectedPageSize: storage.DefaultMaximumNumberOfResults,
MaximumNumberOfResults: 100,
},
{
Page: "-1",
PageSize: "-1",
ExpectedPage: DefaultPage,
ExpectedPageSize: DefaultPageSize,
Page: "-1",
PageSize: "-1",
ExpectedPage: DefaultPage,
ExpectedPageSize: DefaultPageSize,
MaximumNumberOfResults: 20,
},
{
Page: "invalid",
PageSize: "invalid",
ExpectedPage: DefaultPage,
ExpectedPageSize: DefaultPageSize,
Page: "invalid",
PageSize: "invalid",
ExpectedPage: DefaultPage,
ExpectedPageSize: DefaultPageSize,
MaximumNumberOfResults: 100,
},
}
storageCfg := &storage.Config{}
storageCfg.ValidateAndSetDefaults()
cfg := &config.Config{
Storage: storageCfg,
}
for _, scenario := range scenarios {
t.Run("page-"+scenario.Page+"-pageSize-"+scenario.PageSize, func(t *testing.T) {
request, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/statuses?page=%s&pageSize=%s", scenario.Page, scenario.PageSize), http.NoBody)
actualPage, actualPageSize := extractPageAndPageSizeFromRequest(request, cfg.Storage.MaximumNumberOfResults)
actualPage, actualPageSize := extractPageAndPageSizeFromRequest(request, scenario.MaximumNumberOfResults)
if actualPage != scenario.ExpectedPage {
t.Errorf("expected %d, got %d", scenario.ExpectedPage, actualPage)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (c *Config) ValidateAndSetDefaults() error {
if c.Type == TypeMemory && len(c.Path) > 0 {
return ErrMemoryStorageDoesNotSupportPath
}
if c.MaximumNumberOfResults == 0 {
if c.MaximumNumberOfResults <= 0 {
c.MaximumNumberOfResults = DefaultMaximumNumberOfResults
}
if c.MaximumNumberOfEvents == 0 {
if c.MaximumNumberOfEvents <= 0 {
c.MaximumNumberOfEvents = DefaultMaximumNumberOfEvents
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions storage/store/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func TestStore_BrokenSchema(t *testing.T) {
t.Fatal("expected no error, got", err.Error())
}
// Break
_, _ = store.db.Exec("drop table endpoints")
_, _ = store.db.Exec("DROP TABLE endpoints")
// And now we'll try to insert something in our broken schema
if err := store.Insert(&testEndpoint, &testSuccessfulResult); err == nil {
t.Fatal("expected an error")
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestStore_BrokenSchema(t *testing.T) {
t.Fatal("expected no error, got", err.Error())
}
// Break
_, _ = store.db.Exec("drop table endpoint_events")
_, _ = store.db.Exec("DROP TABLE endpoint_events")
if err := store.Insert(&testEndpoint, &testSuccessfulResult); err != nil {
t.Fatal("expected no error, because this should silently fails, got", err.Error())
}
Expand All @@ -438,7 +438,7 @@ func TestStore_BrokenSchema(t *testing.T) {
t.Fatal("expected no error, got", err.Error())
}
// Break
_, _ = store.db.Exec("drop table endpoint_results")
_, _ = store.db.Exec("DROP TABLE endpoint_results")
if err := store.Insert(&testEndpoint, &testSuccessfulResult); err == nil {
t.Fatal("expected an error")
}
Expand All @@ -454,7 +454,7 @@ func TestStore_BrokenSchema(t *testing.T) {
t.Fatal("expected no error, got", err.Error())
}
// Break
_, _ = store.db.Exec("drop table endpoint_result_conditions")
_, _ = store.db.Exec("DROP TABLE endpoint_result_conditions")
if err := store.Insert(&testEndpoint, &testSuccessfulResult); err == nil {
t.Fatal("expected an error")
}
Expand All @@ -467,7 +467,7 @@ func TestStore_BrokenSchema(t *testing.T) {
t.Fatal("expected no error, got", err.Error())
}
// Break
_, _ = store.db.Exec("drop table endpoint_uptimes")
_, _ = store.db.Exec("DROP TABLE endpoint_uptimes")
if err := store.Insert(&testEndpoint, &testSuccessfulResult); err != nil {
t.Fatal("expected no error, because this should silently fails, got", err.Error())
}
Expand Down

0 comments on commit e5aa7e6

Please sign in to comment.