Commit 377d1b95 authored by ale's avatar ale

Add more tests

parent a1df0c2a
......@@ -24,12 +24,8 @@ func NewAgent(ctx context.Context, configMgr *ConfigManager, ms MetadataStore) (
return nil, err
}
// Create a Scheduler and tell the configMgr to reload its
// configuration too. Don't forget to call the initialization
// function f() right now, since configMgr has already loaded
// a config when we start (even though this may result in f()
// being called twice, if the unbuffered notify channel in
// Notify hasn't been read yet).
// Create a Scheduler and register with the configMgr so we
// can reload the schedule on configuration changes.
sched := jobs.NewScheduler()
stopCh := make(chan struct{})
notifyCh := configMgr.Notify()
......
package tabacco
import (
"context"
"testing"
"git.autistici.org/ale/tabacco/jobs"
)
type fakeManager struct{}
func (m *fakeManager) BackupJob(context.Context, []SourceSpec) (Backup, jobs.Job, error) {
return Backup{}, nil, nil
}
func (m *fakeManager) Backup(context.Context, []SourceSpec) (Backup, error) {
return Backup{}, nil
}
func (m *fakeManager) RestoreJob(context.Context, FindRequest, string) (jobs.Job, error) {
return nil, nil
}
func (m *fakeManager) Restore(context.Context, FindRequest, string) error {
return nil
}
func (m *fakeManager) Close() error {
return nil
}
func (m *fakeManager) GetStatus() ([]jobs.Status, []jobs.Status, []jobs.Status) {
return nil, nil, nil
}
func TestMakeSchedule(t *testing.T) {
sourceSpecs := []SourceSpec{
{
Name: "source1",
Handler: "file1",
Schedule: "@random_every 1d",
Atoms: []Atom{
{
Name: "user1",
RelativePath: "user1",
},
{
Name: "user2",
RelativePath: "user2",
},
},
},
{
Name: "source2",
Handler: "dbpipe",
Schedule: "35 3 * * *",
AtomsCommand: "echo user1 user1 ; echo user2 user2",
},
}
mgr := &fakeManager{}
_, err := makeSchedule(context.Background(), mgr, sourceSpecs, 1234)
if err != nil {
t.Fatal(err)
}
}
......@@ -122,6 +122,7 @@ func TestBackup(t *testing.T) {
Params: map[string]interface{}{
"path": "/source/of/file1",
},
PreBackupCommand: "echo hello",
},
{
Name: "dbpipe",
......
package tabacco
import (
"os"
"testing"
)
func TestReadConfig(t *testing.T) {
conf, err := ReadConfig("testdata/agent.yml")
if err != nil {
t.Fatal("ReadConfig()", err)
}
if len(conf.SourceSpecs) != 1 {
t.Fatalf("no SourceSpecs in config: %+v", conf)
}
if len(conf.HandlerSpecs) != 1 {
t.Fatalf("no HandlerSpecs in config: %+v", conf)
}
}
func TestConfigManager(t *testing.T) {
conf, err := ReadConfig("testdata/agent.yml")
if err != nil {
t.Fatal("ReadConfig()", err)
}
mgr, err := NewConfigManager(conf)
if err != nil {
t.Fatal("NewConfigManager()", err)
}
defer mgr.Close()
// Test one of the accessor methods.
if s := mgr.getSourceSpecs(); len(s) != 1 {
t.Fatalf("getSourceSpecs() bad result: %+v", s)
}
// Test the Notify() mechanism by checking that it triggers
// right away when setting up a new listener.
<-mgr.Notify()
}
func TestRandomSeed(t *testing.T) {
seedFile := ".test_seed"
defer os.Remove(seedFile) // nolint
seed := mustGetSeed(seedFile)
if seed == 0 {
t.Fatal("seed is zero")
}
seed2 := mustGetSeed(seedFile)
if seed2 != seed {
t.Fatal("failed to persist random seed")
}
}
......@@ -11,6 +11,8 @@ import (
"time"
"github.com/robfig/cron"
"git.autistici.org/ale/tabacco/util"
)
// JobGeneratorFunc is a function that returns a new Job.
......@@ -257,7 +259,7 @@ func (s *Scheduler) GetStatus() []CronJobStatus {
func parseSchedule(s string, hostSeed int64) (cron.Schedule, error) {
switch {
case strings.HasPrefix(s, "@random_every "):
period, err := time.ParseDuration(s[14:])
period, err := util.ParseDuration(s[14:])
if err != nil {
return nil, err
}
......
---
queue_config:
workers:
backup: 2
repository:
name: main
type: restic
params:
uri: /tmp/resticdb
password: pippero
metadb:
url: http://localhost:5332
random_seed_file: .random_seed
name: file
type: file
params:
path: /usr/share/man/man4
name: source1
handler: file
schedule: "@random_every 2m"
package util
import (
"errors"
"time"
)
var unitMap = map[string]int64{
"ns": int64(time.Nanosecond),
"us": int64(time.Microsecond),
"µs": int64(time.Microsecond), // U+00B5 = micro symbol
"μs": int64(time.Microsecond), // U+03BC = Greek letter mu
"ms": int64(time.Millisecond),
"s": int64(time.Second),
"m": int64(time.Minute),
"h": int64(time.Hour),
"d": int64(24 * time.Hour),
"w": int64(7 * 24 * time.Hour),
"mo": int64(28 * 24 * time.Hour),
}
// ParseDuration is a copy of time.ParseDuration that understands
// days, weeks and months.
//
// nolint
func ParseDuration(s string) (time.Duration, error) {
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
orig := s
var d int64
neg := false
// Consume [-+]?
if s != "" {
c := s[0]
if c == '-' || c == '+' {
neg = c == '-'
s = s[1:]
}
}
// Special case: if all that is left is "0", this is zero.
if s == "0" {
return 0, nil
}
if s == "" {
return 0, errors.New("time: invalid duration " + orig)
}
for s != "" {
var (
v, f int64 // integers before, after decimal point
scale float64 = 1 // value = v + f/scale
)
var err error
// The next character must be [0-9.]
if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
return 0, errors.New("time: invalid duration " + orig)
}
// Consume [0-9]*
pl := len(s)
v, s, err = leadingInt(s)
if err != nil {
return 0, errors.New("time: invalid duration " + orig)
}
pre := pl != len(s) // whether we consumed anything before a period
// Consume (\.[0-9]*)?
post := false
if s != "" && s[0] == '.' {
s = s[1:]
pl := len(s)
f, scale, s = leadingFraction(s)
post = pl != len(s)
}
if !pre && !post {
// no digits (e.g. ".s" or "-.s")
return 0, errors.New("time: invalid duration " + orig)
}
// Consume unit.
i := 0
for ; i < len(s); i++ {
c := s[i]
if c == '.' || '0' <= c && c <= '9' {
break
}
}
if i == 0 {
return 0, errors.New("time: missing unit in duration " + orig)
}
u := s[:i]
s = s[i:]
unit, ok := unitMap[u]
if !ok {
return 0, errors.New("time: unknown unit " + u + " in duration " + orig)
}
if v > (1<<63-1)/unit {
// overflow
return 0, errors.New("time: invalid duration " + orig)
}
v *= unit
if f > 0 {
// float64 is needed to be nanosecond accurate for fractions of hours.
// v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
v += int64(float64(f) * (float64(unit) / scale))
if v < 0 {
// overflow
return 0, errors.New("time: invalid duration " + orig)
}
}
d += v
if d < 0 {
// overflow
return 0, errors.New("time: invalid duration " + orig)
}
}
if neg {
d = -d
}
return time.Duration(d), nil
}
var errLeadingInt = errors.New("time: bad [0-9]*") // never printed
// leadingInt consumes the leading [0-9]* from s.
func leadingInt(s string) (x int64, rem string, err error) {
i := 0
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
if x > (1<<63-1)/10 {
// overflow
return 0, "", errLeadingInt
}
x = x*10 + int64(c) - '0'
if x < 0 {
// overflow
return 0, "", errLeadingInt
}
}
return x, s[i:], nil
}
// leadingFraction consumes the leading [0-9]* from s.
// It is used only for fractions, so does not return an error on overflow,
// it just stops accumulating precision.
func leadingFraction(s string) (x int64, scale float64, rem string) {
i := 0
scale = 1
overflow := false
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
if overflow {
continue
}
if x > (1<<63-1)/10 {
// It's possible for overflow to give a positive number, so take care.
overflow = true
continue
}
y := x*10 + int64(c) - '0'
if y < 0 {
overflow = true
continue
}
x = y
scale *= 10
}
return x, scale, s[i:]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment