Skip to content

Commit

Permalink
need better testing for scandir
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed May 17, 2017
1 parent dfb3a56 commit c7eb557
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 88 deletions.
2 changes: 2 additions & 0 deletions scandir.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ func (s *ScanDir) Start(ctl Control) {
if err != nil {
log.Fatalf("Error getting the md5sum: %s", err)
}
s.Lock()
// restart if file changed
if md5 != s.services[serviceName] {
s.services[serviceName] = md5
log.Printf("Stopping: %s\n", serviceName)
ctl.SendSignal(filepath.Join(s.sdir, serviceName, "immortal.sock"), "halt")
}
s.Unlock()
log.Printf("Starting: %s\n", serviceName)
// try to start before via socket
if _, err := ctl.SendSignal(filepath.Join(s.sdir, serviceName, "immortal.sock"), "start"); err != nil {
Expand Down
89 changes: 1 addition & 88 deletions scandir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

package immortal

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
import "testing"

func TestNewScanDir(t *testing.T) {
_, err := NewScanDir("/tmp")
Expand All @@ -23,84 +17,3 @@ func TestNewScanDirNonexistent(t *testing.T) {
t.Error("Expecting error")
}
}

type mockController struct {
t *testing.T
i, j int
expect []struct {
socket string
signal []string
signalErr bool
cmd, runErr string
}
status chan string
}

func (mc *mockController) GetStatus(socket string) (*Status, error) {
status := &Status{}
return status, nil
}

func (mc *mockController) SendSignal(socket, signal string) (*SignalResponse, error) {
defer func() {
mc.status <- signal
}()
return nil, nil
}

func (mc *mockController) FindServices(dir string) ([]*ServiceStatus, error) {
return nil, nil
}

func (mc *mockController) PurgeServices(dir string) error {
return nil
}

func (mc *mockController) Run(command string) ([]byte, error) {
defer func() {
mc.status <- "Run"
}()
return nil, nil
}

func TestScanner(t *testing.T) {
dir, err := ioutil.TempDir("", "scaner")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
s, err := NewScanDir(dir)
if err != nil {
t.Fatal(err)
}
ctl := &mockController{
status: make(chan string),
}

// start scanner loop
go s.Start(ctl)

if err = ioutil.WriteFile(filepath.Join(dir, "run.yml"), []byte("stage 0"), 0644); err != nil {
t.Fatal(err)
}

var status string
status = <-ctl.status
expect(t, status, "Run")
expect(t, "2bf41d668dd3b0909d58f982aff35a25", s.services["run"])

// change service contents, a restart (exit, start) is expected
if err = ioutil.WriteFile(filepath.Join(dir, "run.yml"), []byte("stage 1"), 0644); err != nil {
t.Fatal(err)
}
status = <-ctl.status
expect(t, status, "halt")
status = <-ctl.status
expect(t, status, "start")

// remove service, exit
if err := os.Remove(filepath.Join(dir, "run.yml")); err != nil {
t.Fatal(err)
}

}

0 comments on commit c7eb557

Please sign in to comment.