// TestGetSites tests the saving of the ping information to the DB. func TestSavePings(t *testing.T) { db, err := database.InitializeTestDB("") if err != nil { t.Fatal("Failed to create database:", err) } defer db.Close() s1 := database.Site{Name: "Test", IsActive: true, URL: "http://www.github.com", PingIntervalSeconds: 1, TimeoutSeconds: 30} err = s1.CreateSite(db) if err != nil { t.Fatal("Failed to create new site:", err) } // For this test will pass the normal GetSites to use the DB... pinger.ResetHitCount() p := pinger.NewPinger(db, pinger.GetSites, pinger.RequestURLMock, notifier.SendEmailMock, notifier.SendSmsMock) p.Start() // Sleep to allow running the tests before stopping. time.Sleep(7 * time.Second) p.Stop() // Get the site pings since the test began and validate. var saved database.Site err = saved.GetSitePings(db, s1.SiteID, time.Now().Add(-10*time.Second), time.Now()) if err != nil { t.Fatal("Failed to retrieve site pings:", err) } if !saved.Pings[0].SiteDown { t.Error("First ping should show site down.") } if saved.Pings[3].SiteDown { t.Error("Fourth ping should show site up.") } // Get the Site updates to make sure the status changes are being set. err = saved.GetSite(db, s1.SiteID) if err != nil { t.Fatal("Failed to retrieve site updates:", err) } if saved.LastStatusChange.IsZero() { t.Error("Last Status Change time not saved.") } if saved.LastPing.IsZero() { t.Error("Last Ping time not saved.") } if !saved.IsSiteUp { t.Error("Site should be saved as up.") } }
// TestCreatePings tests creating the ping records for a given site. func TestCreatePings(t *testing.T) { var err error db, err := database.InitializeTestDB("") defer db.Close() if err != nil { t.Fatal("Failed to create database:", err) } // First create a site to associate with the pings. s := database.Site{Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30} err = s.CreateSite(db) if err != nil { t.Fatal("Failed to create new site:", err) } // Create a ping result p1 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 22, 00, time.UTC), Duration: 280, HTTPStatusCode: 200, SiteDown: false} err = p1.CreatePing(db) if err != nil { t.Fatal("Failed to create new ping:", err) } // Create a second ping result p2 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 20, 00, time.UTC), Duration: 290, HTTPStatusCode: 200, SiteDown: true} err = p2.CreatePing(db) if err != nil { t.Fatal("Failed to create new ping:", err) } //Get the saved Ping var saved database.Site err = saved.GetSitePings(db, s.SiteID, time.Date(2015, time.November, 10, 23, 00, 00, 00, time.UTC), time.Date(2015, time.November, 10, 23, 59, 00, 00, time.UTC)) if err != nil { t.Fatal("Failed to retrieve saved pings:", err) } // Verify the first ping was Loaded with proper attibutes and sorted last. if !reflect.DeepEqual(p1, saved.Pings[1]) { t.Error("First saved ping not equal to input:\n", saved.Pings[1], p1) } // Verify the second ping was Loaded with proper attributes and sorted first. if !reflect.DeepEqual(p2, saved.Pings[0]) { t.Error("Second saved ping not equal to input:\n", saved.Pings[0], p2) } // Verify that the site reflects the last ping time. err = saved.GetSite(db, s.SiteID) if err != nil { t.Fatal("Failed to retrieve site:", err) } if saved.LastPing != p2.TimeRequest { t.Error("Last Ping on site does not match input:\n", saved.LastPing, p1.TimeRequest) } //Get the first ping for the site. firstping, err := s.GetFirstPing(db) if err != nil { t.Fatal("Failed to retrieve first ping for the site:", err) } if firstping != p2.TimeRequest { t.Error("First Ping on site does not match input:\n", firstping, p2.TimeRequest) } // Create a third ping with conflicting times should error. p3 := database.Ping{SiteID: s.SiteID, TimeRequest: time.Date(2015, time.November, 10, 23, 22, 20, 00, time.UTC), Duration: 300, HTTPStatusCode: 200, SiteDown: false} err = p3.CreatePing(db) if err == nil { t.Fatal("Conflicting pings should throw error.") } }