Fix tests to pass on linux

This commit is contained in:
bel
2020-03-13 03:40:53 +00:00
parent 6d39ef9aa2
commit 0d6be1e9d8
6 changed files with 185 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ package scheduler
import (
"bytes"
"encoding/gob"
"fmt"
"local/firestormy/logger"
"os/exec"
@@ -41,7 +42,7 @@ func newBashJob(schedule, sh string) (*Job, error) {
if err != nil {
panic(err)
}
logger.New().Info("executed %s: %s", sh, out)
logger.New().Info(fmt.Sprintf("executed %s: %s", sh, out))
},
}, nil
}

View File

@@ -1,9 +1,14 @@
package scheduler
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"local/firestormy/config"
"local/firestormy/logger"
"regexp"
"strings"
"time"
cron "github.com/robfig/cron/v3"
@@ -30,6 +35,61 @@ func New() *Scheduler {
}
}
func NewFromFile(config string) (*Scheduler, error) {
f, err := ioutil.ReadFile(config)
if err != nil {
return nil, err
}
s := New()
for _, line := range bytes.Split(f, []byte("\n")) {
line = cleanLine(line)
if len(line) == 0 {
continue
}
schedule, command := splitScheduleCommand(line)
if len(schedule) == 0 || len(command) == 0 {
continue
}
job, err := NewJob(Bash, schedule, command)
if err != nil {
logger.New().Error(err, "cannot fully parse file: new job error", config, ", sched", schedule, ", comm", command)
continue
}
if err := s.Add(job); err != nil {
logger.New().Error(err, "cannot fully parse file: add job error", config)
continue
}
}
jobs, _ := s.List()
if len(jobs) == 0 {
return nil, errors.New("no jobs parsed from file " + config)
}
return s, nil
}
func cleanLine(b []byte) []byte {
b = bytes.Trim(b, "\t \n")
if len(b) == 0 {
return nil
}
if b[0] == '#' {
return nil
}
return b
}
func splitScheduleCommand(b []byte) (string, string) {
re := regexp.MustCompile(`^((\d+|\*\/\d+|(\d,)*\d+|\*) [ ]*){5}`)
schedule := string(re.Find(b))
if len(schedule) == 0 {
return "", ""
}
command := strings.TrimPrefix(string(b), schedule)
schedule = strings.TrimSpace(schedule)
command = strings.TrimSpace(command)
return schedule, command
}
func (s *Scheduler) Start() error {
jobs, err := s.List()
if err != nil {

View File

@@ -2,8 +2,10 @@ package scheduler
import (
"bytes"
"io/ioutil"
"local/firestormy/config"
"local/storage"
"os"
"testing"
)
@@ -59,3 +61,91 @@ func TestSchedulerStartStop(t *testing.T) {
t.Errorf("%v: %s", n, b.Bytes())
}
}
func TestSchedulerFromFile(t *testing.T) {
was := config.Store
defer func() {
config.Store = was
}()
cases := map[string]struct {
content string
want int
}{
"just a job": {
content: `10 */12 * * * /bin/bash -c "hostname"`,
want: 1,
},
"all wild": {
content: `* * * * * /bin/bash -c "hostname"`,
want: 1,
},
"all single numbers": {
content: `1 1 1 1 1 /bin/bash -c "hostname"`,
want: 1,
},
"all double numbers": {
content: `10 10 10 10 2 /bin/bash -c "hostname"`,
want: 1,
},
"all /\\d+": {
content: `*/11 */2 */3 */4 */1 /bin/bash -c "hostname"`,
want: 1,
},
"2 jobs with 1 comment no whitespace leading": {
content: `# this is my comment
10 */12 * * * /bin/bash -c "hostname"
10 */12 * * * /bin/bash -c "hostname"
`,
want: 2,
},
"2 jobs with 1 comment whitespace leading": {
content: ` # this is my comment
10 */12 * * * /bin/bash -c "hostname"
10 */12 * * * /bin/bash -c "hostname"
`,
want: 2,
},
"2 jobs with crazy whitespace between cron spec": {
content: ` # this is my comment
10 */12 * * * /bin/bash -c "hostname"
10 */12 * * * /bin/bash -c "hostname"
`,
want: 2,
},
"2 jobs with 2 comemnts and 2 empty lines": {
content: ` # this is my comment
# this is a second comment
10 */12 * * * /bin/bash -c "hostname"
10 */12 * * * /bin/bash -c "hostname"
`,
want: 2,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
config.Store, _ = storage.New(storage.MAP)
f, err := ioutil.TempFile(os.TempDir(), "testSchedulerFromFile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
f.Write([]byte(c.content))
f.Close()
s, err := NewFromFile(f.Name())
if err != nil {
t.Fatal(err)
}
jobs, err := s.List()
if err != nil {
t.Fatal(err)
}
if len(jobs) != c.want {
t.Fatalf("want %v, got %v jobs", c.want, jobs)
}
})
}
}