Skip to content

Commit

Permalink
Fixes test to allow checks to already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
thunef committed Jan 16, 2024
1 parent f4fd67d commit 25806d4
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions pkg/monitors/grafana/grafana-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {
config := config.GetControllerConfigTest()

service := GrafanaMonitorService{}
previousResources := len(service.GetAll())
provider := util.GetProviderWithName(config, "Grafana")
if provider == nil {
return
Expand All @@ -30,22 +31,22 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {

mRes := service.GetAll()

if len(mRes) == 0 {
if len(mRes) == previousResources {
t.Errorf("Found empty response for Monitor. Name: %s and URL: %s", m.Name, m.URL)
}
if len(mRes) > 1 {
if len(mRes) > previousResources+1 {
t.Errorf("Found too many response for Monitor, %v, after add.", len(mRes))
}
if mRes[0].Name != m.Name || mRes[0].URL != m.URL {
t.Error("URL and name should be the same", mRes[0], m)
}

monitor, err := service.GetByName(m.Name)

if err != nil {
t.Error("Monitor should've been found", monitor, err)
}
service.Remove(mRes[0])
if monitor.Name != m.Name || monitor.URL != m.URL {
t.Error("URL and name should be the same", mRes[0], m)
}
service.Remove(*monitor)

monitor, err = service.GetByName(m.Name)

Expand All @@ -57,6 +58,7 @@ func TestUpdateMonitorWithCorrectValues(t *testing.T) {
config := config.GetControllerConfigTest()

service := GrafanaMonitorService{}
previousResources := len(service.GetAll())
provider := util.GetProviderWithName(config, "Grafana")
if provider == nil {
return
Expand All @@ -67,33 +69,33 @@ func TestUpdateMonitorWithCorrectValues(t *testing.T) {

mRes := service.GetAll()

if len(mRes) == 0 {
if len(mRes) == previousResources {
t.Errorf("Found empty response for Monitor. Name: %s and URL: %s", m.Name, m.URL)
}
if len(mRes) > 1 {
if len(mRes) > previousResources+1 {
t.Errorf("Found too many response for Monitor, %v, after add.", len(mRes))
}
m2 := models.Monitor{Name: "stakater-test", URL: "https://stakater.com", ID: mRes[0].ID, Config: mRes[0].Config}
service.Update(m2)

mRes2 := service.GetAll()

if len(mRes2) == 0 {
if len(mRes2) == previousResources {
t.Errorf("Found empty response for Monitor. Name: %s and URL: %s", m2.Name, m2.URL)
}
if len(mRes2) > 1 {
if len(mRes2) > previousResources+1 {
t.Errorf("Found too many response for Monitor, %v, after update.", len(mRes2))
}
if mRes2[0].Name != m2.Name || mRes2[0].URL != m2.URL {
t.Error("URL and name should be the same", mRes2[0], m2)
}

monitor, err := service.GetByName(m.Name)

if monitor != nil {
t.Error("Monitor should not exist since it was updated", monitor, err)
}
service.Remove(mRes2[0])
if monitor.Name != m.Name || monitor.URL != m.URL {
t.Error("URL and name should be the same", mRes[0], m)
}
service.Remove(*monitor)

monitor, err = service.GetByName(m2.Name)

Expand Down

0 comments on commit 25806d4

Please sign in to comment.