sermoni

"Service monitor" / cronjob status service
Log | Files | Refs

commit 2a651d2c6909d8e3a5a735159459f5b54720eb92
parent e4be0ae414bf63e9148755dd4d96218dd0f6aaa4
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Fri, 10 Apr 2020 23:14:32 +0200

Finish services_test.go (at least for now)

Diffstat:
A.gitignore | 2++
Mservices/services.go | 2+-
Mservices/services_test.go | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
3 files changed, 72 insertions(+), 15 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +sermoni.db +**/test.db diff --git a/services/services.go b/services/services.go @@ -103,7 +103,7 @@ func Delete(intID uint64) error { // Add adds a new service to monitor // Returns error if the token is unavailable and if the transaction fails in any way -func Add(token string, service Service) error { +func Add(token string, service *Service) error { db := database.GetDB() return db.Update(func(tx *bbolt.Tx) error { var err error diff --git a/services/services_test.go b/services/services_test.go @@ -17,49 +17,104 @@ func intID(id []byte) uint64 { return idInt } -func TestAddService(t *testing.T) { - token1 := "my-great-token" - token2 := "my-other-token" - token3 := "my-third-token" - err := Add(token1, Service{ +func (s1 *Service) equals (s2 *Service) bool { + if s1.ID != s2.ID { + return false + } + if s1.Name != s2.Name { + return false + } + if s1.Description != s2.Description { + return false + } + if s1.ExpectationPeriod != s2.ExpectationPeriod { + return false + } + return true +} + +var ( + token1 = "my-great-token" + token2 = "my-other-token" + token3 = "my-third-token" +) + +var testServices = []*Service{ + &Service{ Name: "tester @ dev-computer", Description: "This describes the service in more detail", ExpectationPeriod: 282342, - }) + }, + &Service{Name: "tester2", ExpectationPeriod: 300003}, + &Service{Name: "third @ tester"}, +} + +func TestAddService(t *testing.T) { + err := Add(token1, testServices[0]) if err != nil { fmt.Println(err) t.Fatal("unexpected error when adding service") } - if err = Add(token2, Service{Name: "tester2"}); err != nil { + if err = Add(token2, testServices[1]); err != nil { fmt.Println(err) t.Fatal("unexpected error when adding second service") } - if err = Add(token2, Service{Name: "another tester"}); err == nil { + if err = Add(token2, testServices[1]); err == nil { t.Fatal("no error returned when trying to re-use a service token") } - err = Add(token3, Service{Name: "third @ tester", ExpectationPeriod: 300003}) + err = Add(token3, testServices[2]) if err != nil { t.Fatal("unexpected error when adding third service") } + + // Simulate ID generation for testServices after adding them to DB, to avoid + // possible interferrence (shouldn't be a problem, but doesn't hurt to be sure). + // bbolt should always start ID sequences on 1, so this assumes that the service ID + // equals the testService index + 1 + for i, service := range testServices { + service.ID = uint64(i) + 1 + } } func TestDeleteService(t *testing.T) { - // bbolt should always start ID sequences on 1, so hard coding IDs should be fine - err := Delete(2) + var di uint64 = 1 // Deletion index + err := Delete(di + 1) if err != nil { fmt.Println(err) t.Fatal("unexpected error when trying to delete service") } - if err = Delete(2); err == nil { + if err = Delete(di + 1); err == nil { t.Fatal("no error returned when trying to delete non-existing service") } + + // Delete from testServices too + testServices = append(testServices[:di], testServices[di+1:]...) +} + +func TestGetByID(t *testing.T) { + var gi uint64 = 0 // Get index + testService := testServices[gi] + service := GetByID(gi + 1) + if !service.equals(testService) { + t.Fatal("stored service doesn't match original") + } +} + +func TestGetByToken(t *testing.T) { + testService := testServices[1] + service := GetByToken(token3) + if !service.equals(testService) { + t.Fatal("stored service doesn't match original") + } } func TestGetAll(t *testing.T) { services := GetAll() - for _, service := range services { - fmt.Printf("%+v\n", service) + for i, service := range services { + if !service.equals(testServices[i]) { + t.Fatal("stored service doesn't match original") + } } }