Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd committed May 2, 2022
1 parent e46760c commit 748c6db
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 203 deletions.
3 changes: 1 addition & 2 deletions cmd/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func Test_retryCommand(t *testing.T) {
db := database.New(database.DefaultConfig())
status, err := db.FindByRequestId(configPath, dag.Status.RequestId)
require.NoError(t, err)
dw, err := db.NewWriterFor(configPath, status.File)
require.NoError(t, err)
dw := &database.Writer{Target: status.File}
err = dw.Open()
require.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions cmd/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func Test_stopCommand(t *testing.T) {
ConfigPath: c,
}

s, err := db.ReadStatusHist(cfg.ConfigPath, 1)
require.NoError(t, err)
s := db.ReadStatusHist(cfg.ConfigPath, 1)
require.Equal(t, 1, len(s))
assert.Equal(t, scheduler.SchedulerStatus_Cancel, s[0].Status.Status)
}
12 changes: 6 additions & 6 deletions internal/admin/handlers/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func readSchedulerLog(c controller.Controller, file string) (*schedulerLog, erro
if err != nil {
return nil, fmt.Errorf("failed to read status file %s", file)
}
logFile = s.Status.Log
logFile = s.Log
}
b, err := os.ReadFile(logFile)
if err != nil {
Expand Down Expand Up @@ -355,11 +355,11 @@ func readStepLog(c controller.Controller, file, stepName, enc string) (*stepLog,
if err != nil {
return nil, fmt.Errorf("failed to read status file %s", file)
}
steps = s.Status.Nodes
stepm[constants.OnSuccess] = s.Status.OnSuccess
stepm[constants.OnFailure] = s.Status.OnFailure
stepm[constants.OnCancel] = s.Status.OnCancel
stepm[constants.OnExit] = s.Status.OnExit
steps = s.Nodes
stepm[constants.OnSuccess] = s.OnSuccess
stepm[constants.OnFailure] = s.OnFailure
stepm[constants.OnCancel] = s.OnCancel
stepm[constants.OnExit] = s.OnExit
}
var step *models.Node = nil
for _, s := range steps {
Expand Down
14 changes: 4 additions & 10 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ func (a *Agent) run() error {
status := a.Status()

log.Println("schedule finished.")
logIgnoreErr("writing status", a.dbWriter.Write(a.Status()))
utils.LogIgnoreErr("writing status", a.dbWriter.Write(a.Status()))

a.reporter.ReportSummary(status, lastErr)
logIgnoreErr("sending email", a.reporter.ReportMail(a.DAG, status))
utils.LogIgnoreErr("sending email", a.reporter.ReportMail(a.DAG, status))

logIgnoreErr("closing data file", a.dbWriter.Close())
logIgnoreErr("data compaction", a.database.Compact(a.DAG.ConfigPath, a.dbFile))
utils.LogIgnoreErr("closing data file", a.dbWriter.Close())
utils.LogIgnoreErr("data compaction", a.database.Compact(a.DAG.ConfigPath, a.dbFile))

return lastErr
}
Expand Down Expand Up @@ -306,12 +306,6 @@ func (a *Agent) checkIsRunning() error {
return nil
}

func logIgnoreErr(action string, err error) {
if err != nil {
log.Printf("%s failed. %s", action, err)
}
}

var (
statusRe = regexp.MustCompile(`^/status[/]?$`)
stopRe = regexp.MustCompile(`^/stop[/]?$`)
Expand Down
29 changes: 0 additions & 29 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package agent

import (
"bytes"
"errors"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -243,31 +239,6 @@ func TestHandleHTTP(t *testing.T) {
require.Equal(t, status.Status, scheduler.SchedulerStatus_Cancel)
}

func TestIgnoreErr(t *testing.T) {
origStdout := os.Stdout
r, w, err := os.Pipe()
require.NoError(t, err)
os.Stdout = w
log.SetOutput(w)

defer func() {
os.Stdout = origStdout
log.SetOutput(origStdout)
}()

logIgnoreErr("test action", errors.New("test error"))
os.Stdout = origStdout
w.Close()

var buf bytes.Buffer
_, err = io.Copy(&buf, r)
require.NoError(t, err)

s := buf.String()
require.Contains(t, s, "test action failed")
require.Contains(t, s, "test error")
}

type mockResponseWriter struct {
status int
body string
Expand Down
16 changes: 6 additions & 10 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ func (s *controller) GetLastStatus() (*models.Status, error) {
db := database.New(database.DefaultConfig())
status, err := db.ReadStatusToday(s.cfg.ConfigPath)
if err != nil {
if err != database.ErrNoDataFile {
var readErr error = nil
if err != database.ErrNoStatusDataToday && err != database.ErrNoStatusData {
fmt.Printf("read status failed : %s", err)
readErr = err
}
return defaultStatus(s.cfg), nil
return defaultStatus(s.cfg), readErr
}
return status, nil
}
Expand All @@ -164,10 +166,7 @@ func (s *controller) GetStatusByRequestId(requestId string) (*models.Status, err

func (s *controller) GetStatusHist(n int) ([]*models.StatusFile, error) {
db := database.New(database.DefaultConfig())
ret, err := db.ReadStatusHist(s.cfg.ConfigPath, n)
if err != nil {
return []*models.StatusFile{}, nil
}
ret := db.ReadStatusHist(s.cfg.ConfigPath, n)
return ret, nil
}

Expand All @@ -193,10 +192,7 @@ func (s *controller) UpdateStatus(status *models.Status) error {
if err != nil {
return err
}
w, err := db.NewWriterFor(s.cfg.ConfigPath, toUpdate.File)
if err != nil {
return err
}
w := &database.Writer{Target: toUpdate.File}
if err := w.Open(); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 748c6db

Please sign in to comment.