fix nil dereference and start adding unit tests

This commit is contained in:
Andrew Wang 2021-12-06 00:41:12 -05:00
parent 7f98384fcd
commit 18aae3afa6
6 changed files with 27 additions and 6 deletions

View File

@ -1,3 +1,6 @@
# Merlin
[![Build Status](https://ci.csclub.uwaterloo.ca/api/badges/public/mirror/status.svg)](https://ci.csclub.uwaterloo.ca/public/mirror)
This folder contains the code for merlin (which does the actual syncing) and arthur (which sends commands to merlin).
### TODO FOR SYNC SCRIPTS
@ -17,7 +20,6 @@ This folder contains the code for merlin (which does the actual syncing) and art
- [ ] wget
### TODO
- [ ] logging
- [ ] detect if an rsync process is stuck (\*\*)
- [ ] place each rsync process in a separate cgroup (\*\*\*)

View File

@ -29,7 +29,7 @@ const (
DEFAULT_LOG_DIR = "/home/mirror/merlin/logs"
DEFAULT_RSYNC_LOG_DIR = "/home/mirror/merlin/logs-rsync"
DEFAULT_ZFSSYNC_LOG_DIR = "/home/mirror/merlin/logs-zfssync"
DEFAULT_SOCK_PATH = "/run/merlin/merlin.sock"
DEFAULT_SOCK_PATH = "/run/merlin.sock"
)
var frequencies = map[string]int{
@ -143,6 +143,7 @@ type RepoState struct {
// save the current state of the repo to a file
func (repo *Repo) SaveState() {
repo.Logger.Debug("Saving state")
state_cfg := ini.Empty()
if err := ini.ReflectFrom(state_cfg, &repo.State); err != nil {
repo.Logger.Error(err.Error())
@ -154,6 +155,7 @@ func (repo *Repo) SaveState() {
if _, err := state_cfg.WriteTo(file); err != nil {
repo.Logger.Error(err.Error())
}
repo.Logger.Debug("Saved state")
}
// start sync job for this repo if more than repo.Frequency seconds have elapsed since its last job

View File

@ -0,0 +1 @@
package common

View File

View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"errors"
"fmt"
"io"
"log"
@ -84,9 +85,11 @@ func unixSockListener(connChan chan net.Conn, stopLisChan chan struct{}) {
// must remove cfg.SockPath otherwise get "bind: address already in use"
if filepath.Ext(cfg.SockPath) != ".sock" {
panic(fmt.Errorf("Socket file must end with .sock"))
} else if pathInfo, _ := os.Stat(cfg.SockPath); pathInfo.IsDir() {
panic(fmt.Errorf("Value specified for socket file is a directory"))
} else if err := os.Remove(cfg.SockPath); err != nil {
} else if _, err := os.Stat(cfg.SockPath); err == nil {
if err := os.Remove(cfg.SockPath); err != nil {
panic(err)
}
} else if !errors.Is(err, os.ErrNotExist) {
panic(err)
}
@ -94,13 +97,18 @@ func unixSockListener(connChan chan net.Conn, stopLisChan chan struct{}) {
if err != nil {
panic(err)
}
outLogger.Println("Listening to unix socket at " + cfg.SockPath)
go func() {
for {
// will exit when ear is closed
conn, err := ear.Accept()
if err != nil {
errLogger.Println(err.Error())
if ne, ok := err.(net.Error); ok && ne.Temporary() {
errLogger.Println("Accepted socket error: " + err.Error())
continue
}
errLogger.Println("Unhandlable socket error: " + err.Error())
return
}
connChan <- conn
@ -195,6 +203,7 @@ runLoop:
runAsManyAsPossible()
}
// give time for jobs to terminate before exiting
for {
select {
case done := <-doneChan:

7
merlin/merlin_test.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestSock1(t *testing.T) {
}