sermoni

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

services_test.go (3319B)


      1 package services
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 	"sermoni/internal/config"
      7 	"sermoni/internal/database"
      8 	"testing"
      9 )
     10 
     11 func (s1 *Service) equals(s2 *Service) bool {
     12 	switch {
     13 	case s1.ID != s2.ID:
     14 		return false
     15 	case s1.Name != s2.Name:
     16 		return false
     17 	case s1.Description != s2.Description:
     18 		return false
     19 	case s1.ExpectationPeriod != s2.ExpectationPeriod:
     20 		return false
     21 	default:
     22 		return true
     23 	}
     24 }
     25 
     26 var (
     27 	token1 = "my-great-token"
     28 	token2 = "my-other-token"
     29 	token3 = "my-third-token"
     30 )
     31 
     32 var testServices = []*Service{
     33 	{
     34 		Token:             token1,
     35 		Name:              "tester @ dev-computer",
     36 		Description:       "This describes the service in more detail",
     37 		ExpectationPeriod: 282342,
     38 	},
     39 	{Token: token2, Name: "tester2", ExpectationPeriod: 300003},
     40 	{Token: token3, Name: "third @ tester"},
     41 }
     42 
     43 func TestAddService(t *testing.T) {
     44 	err := Add(testServices[0])
     45 	if err != nil {
     46 		fmt.Println(err)
     47 		t.Fatal("unexpected error when adding service")
     48 	}
     49 	if err = Add(testServices[1]); err != nil {
     50 		fmt.Println(err)
     51 		t.Fatal("unexpected error when adding second service")
     52 	}
     53 	if err = Add(testServices[1]); err == nil {
     54 		t.Fatal("no error returned when trying to re-use a service token")
     55 	}
     56 	err = Add(testServices[2])
     57 	if err != nil {
     58 		t.Fatal("unexpected error when adding third service")
     59 	}
     60 
     61 	// Simulate ID generation for testServices after adding them to DB, to avoid
     62 	// possible interferrence (shouldn't be a problem, but doesn't hurt to be sure).
     63 	// bbolt should always start ID sequences on 1, so this assumes that the service ID
     64 	// equals the testService index + 1
     65 	for i, service := range testServices {
     66 		service.ID = uint64(i) + 1
     67 	}
     68 }
     69 
     70 func TestDeleteService(t *testing.T) {
     71 	var di uint64 = 1 // Deletion index
     72 	err := Delete(di + 1)
     73 	if err != nil {
     74 		fmt.Println(err)
     75 		t.Fatal("unexpected error when trying to delete service")
     76 	}
     77 	if err = Delete(di + 1); err == nil {
     78 		t.Fatal("no error returned when trying to delete non-existing service")
     79 	}
     80 
     81 	// Assert that the service token is deleted too
     82 	if service := GetByToken(token2); service != nil {
     83 		t.Fatal("the service token was not deleted")
     84 	}
     85 
     86 	// Delete from testServices too
     87 	testServices = append(testServices[:di], testServices[di+1:]...)
     88 }
     89 
     90 func TestGetByID(t *testing.T) {
     91 	var gi uint64 = 0 // Get index
     92 	testService := testServices[gi]
     93 	service := GetByID(gi + 1)
     94 	if !service.equals(testService) {
     95 		t.Fatal("stored service doesn't match original")
     96 	}
     97 	if service = GetByID(23423); service != nil {
     98 		t.Fatal("returned service for invalid id")
     99 	}
    100 }
    101 
    102 func TestGetByToken(t *testing.T) {
    103 	testService := testServices[1]
    104 	service := GetByToken(token3)
    105 	if !service.equals(testService) {
    106 		t.Fatal("stored service doesn't match original")
    107 	}
    108 	if service = GetByToken("not-a-token"); service != nil {
    109 		t.Fatal("returned service for invalid token")
    110 	}
    111 }
    112 
    113 func TestGetAll(t *testing.T) {
    114 	services := GetAll()
    115 	for i, service := range services {
    116 		if !service.equals(testServices[i]) {
    117 			t.Fatal("stored service doesn't match original")
    118 		}
    119 	}
    120 	if services[0].equals(testServices[1]) {
    121 		t.Fatal("unexpected match between two services")
    122 	}
    123 }
    124 
    125 func TestMain(m *testing.M) {
    126 	// (Re)create the test database
    127 	testDB := "test.db"
    128 	os.Remove(testDB)
    129 	database.Open(testDB)
    130 	config.InitConfig()
    131 	defer database.Close()
    132 	os.Exit(m.Run())
    133 }