sermoni

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

commit e4be0ae414bf63e9148755dd4d96218dd0f6aaa4
parent 8737e753f8175907af1e8315311feabb8cb4cd2f
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Fri, 10 Apr 2020 22:26:38 +0200

Fix services.Delete

Diffstat:
Mservices/services.go | 28++++++++++++++++++++++------
Mservices/services_test.go | 27++++++++++++---------------
2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/services/services.go b/services/services.go @@ -72,16 +72,32 @@ func GetAll() []*Service { // Delete deletes the given service if it exists func Delete(intID uint64) error { db := database.GetDB() - id := []byte(strconv.FormatUint(intID, 10)) + serviceID := []byte(strconv.FormatUint(intID, 10)) return db.Update(func(tx *bbolt.Tx) error { - var b *bbolt.Bucket + var b, stb *bbolt.Bucket if b = tx.Bucket(database.BucketKeyServices); b == nil { log.Panic("The services bucket does not exist") } - if b.Bucket(id) == nil { + if stb = tx.Bucket(database.BucketKeyServiceTokens); b == nil { + log.Panic("The service-tokens bucket does not exist") + } + + // Delete the entry from root services bucket + if b.Bucket(serviceID) == nil { return errors.New("no service for the given id") } - return b.DeleteBucket(id) + if err := b.DeleteBucket(serviceID); err != nil { + return err + } + + // Find the token entry and delete it from service-tokens bucket + c := stb.Cursor() + for token, id := c.First(); token != nil; token, id = c.Next() { + if string(id) == string(serviceID) { + return stb.Delete(token) + } + } + return errors.New("service id not found in the service-tokens bucket") }) } @@ -155,10 +171,10 @@ func (service *Service) fromBucket(id []byte, sb *bbolt.Bucket) { // Quick fix: Convert to string, then int // Uses default value 0 if an error occurs intPeriod, err := strconv.ParseUint(string(period), 10, 64) - if err != nil { + if err == nil { service.ExpectationPeriod = intPeriod - log.Printf("Couldn't convert period to int for service") } else { + log.Println("Couldn't convert period to int for service") service.ExpectationPeriod = 0 } } diff --git a/services/services_test.go b/services/services_test.go @@ -27,9 +27,11 @@ func TestAddService(t *testing.T) { ExpectationPeriod: 282342, }) if err != nil { + fmt.Println(err) t.Fatal("unexpected error when adding service") } if err = Add(token2, Service{Name: "tester2"}); err != nil { + fmt.Println(err) t.Fatal("unexpected error when adding second service") } if err = Add(token2, Service{Name: "another tester"}); err == nil { @@ -39,24 +41,19 @@ func TestAddService(t *testing.T) { if err != nil { t.Fatal("unexpected error when adding third service") } - - /* - // Add new - fmt.Printf("DELETE ERR: %v\n", services.Delete( - services.IntID(services.GetIDFromToken("token1")))) - fmt.Printf("ADD ERR: %v\n", services fmt.Printf("ADD ERR: %v\n", services.Add("token2", services.Service{Name: "This"})) - fmt.Printf("ADD ERR: %v\n", services.Add("token2", services.Service{Name: "This again"})) - fmt.Printf("token1: %+v\n", services.GetByToken("token1")) - fmt.Printf("DELETE ERR: %v\n", services.Delete( - services.IntID(services.GetIDFromToken("token1")))) - fmt.Printf("token1: %+v\n", services.GetByToken("token1")) - fmt.Printf("DELETE ERR: %v\n", services.Delete( - services.IntID(services.GetIDFromToken("token1")))) - */ } + func TestDeleteService(t *testing.T) { - return + // bbolt should always start ID sequences on 1, so hard coding IDs should be fine + err := Delete(2) + if err != nil { + fmt.Println(err) + t.Fatal("unexpected error when trying to delete service") + } + if err = Delete(2); err == nil { + t.Fatal("no error returned when trying to delete non-existing service") + } } func TestGetAll(t *testing.T) {