From 14b725c6d0f49ee97de82dd1ef00d1aeaa4df7a3 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Tue, 11 Jun 2019 18:36:37 +0200 Subject: [PATCH] working, but make test not passing --- pkg/node/node.go | 26 +++++++++++++++++++------- pkg/node/node_test.go | 8 +++++++- pkg/node/rpc_test.go | 10 +++++++++- pkg/util/pathutil/homedir.go | 15 ++++++--------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/pkg/node/node.go b/pkg/node/node.go index 8c64c304e6..a3c4f9b776 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -6,19 +6,21 @@ import ( "context" "errors" "fmt" - "github.com/skycoin/skywire/pkg/util/pathutil" "io" "net" "net/rpc" "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "sync" "syscall" "time" + "github.com/skycoin/skywire/pkg/util/pathutil" + "github.com/skycoin/skycoin/src/util/logging" "github.com/skycoin/skywire/internal/noise" @@ -98,6 +100,8 @@ type Node struct { startedMu sync.RWMutex startedApps map[string]*appBind + pidMu sync.Mutex + rpcListener net.Listener rpcDialers []*noise.RPCClientDialer } @@ -155,7 +159,6 @@ func NewNode(config *Config) (*Node, error) { RoutingTable: node.rt, RouteFinder: routeFinder.NewHTTP(config.Routing.RouteFinder, time.Duration(config.Routing.RouteFinderTimeout)), SetupNodes: config.Routing.SetupNodes, - } r := router.New(rConfig) node.router = r @@ -251,7 +254,7 @@ func (node *Node) dir() string { } func (node *Node) pidFile() *os.File { - f, err := os.OpenFile(filepath.Join(node.dir(),"apps.pid"), os.O_RDWR|os.O_CREATE, 0755) + f, err := os.OpenFile(filepath.Join(node.dir(), "apps.pid"), os.O_RDWR|os.O_CREATE, 0755) if err != nil { panic(err) } @@ -260,6 +263,8 @@ func (node *Node) pidFile() *os.File { } func (node *Node) closePreviousApps() { + node.logger.Info("killing previously ran apps if any...") + pids := node.pidFile() defer pids.Close() // nocheck: err @@ -285,13 +290,14 @@ func (node *Node) closePreviousApps() { func (node *Node) stopUnhandledApp(name string, pid int) { p, err := os.FindProcess(pid) if err != nil { - node.logger.Infof("Previous app %s ran by this node with pid: %d not found", name, pid) + if runtime.GOOS != "windows" { + node.logger.Infof("Previous app %s ran by this node with pid: %d not found", name, pid) + } return } err = p.Signal(syscall.SIGKILL) if err != nil { - node.logger.Warnf("Found hanged app %s with pid %d previously ran by this node, but unable to kill it: %s", name, pid, err) return } @@ -415,9 +421,12 @@ func (node *Node) SpawnApp(config *AppConfig, startCh chan<- struct{}) error { node.startedMu.Lock() bind.pid = pid node.startedMu.Unlock() - appCh <- node.executer.Wait(cmd) + node.pidMu.Lock() + node.logger.Infof("storing app %s pid %d", config.App, pid) node.persistPID(config.App, pid) + node.pidMu.Unlock() + appCh <- node.executer.Wait(cmd) }() srvCh := make(chan error) @@ -452,7 +461,10 @@ func (node *Node) SpawnApp(config *AppConfig, startCh chan<- struct{}) error { func (node *Node) persistPID(name string, pid int) { pidF := node.pidFile() - pathutil.AtomicAppendToFile(pidF.Name(), []byte(fmt.Sprintf("%s %d\n", name, pid))) + pidFName := pidF.Name() + pidF.Close() + + pathutil.AtomicAppendToFile(pidFName, []byte(fmt.Sprintf("%s %d\n", name, pid))) } // StopApp stops running App. diff --git a/pkg/node/node_test.go b/pkg/node/node_test.go index 99903b5be7..6db88af95c 100644 --- a/pkg/node/node_test.go +++ b/pkg/node/node_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "github.com/skycoin/skywire/pkg/util/pathutil" "net" "os" "os/exec" @@ -97,11 +98,16 @@ func TestNodeStartClose(t *testing.T) { } func TestNodeSpawnApp(t *testing.T) { + pk, _ := cipher.GenerateKeyPair() r := new(mockRouter) executer := &MockExecuter{} defer os.RemoveAll("skychat") apps := []AppConfig{{App: "skychat", Version: "1.0", AutoStart: false, Port: 10, Args: []string{"foo"}}} - node := &Node{router: r, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} + node := &Node{router: r, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test"), + config: &Config{}} + node.config.Node.StaticPubKey = pk + pathutil.EnsureDir(node.dir()) + defer os.RemoveAll(node.dir()) require.NoError(t, node.StartApp("skychat")) time.Sleep(100 * time.Millisecond) diff --git a/pkg/node/rpc_test.go b/pkg/node/rpc_test.go index bd03e1259e..ac0f0bd322 100644 --- a/pkg/node/rpc_test.go +++ b/pkg/node/rpc_test.go @@ -3,6 +3,8 @@ package node import ( "context" "encoding/json" + "github.com/skycoin/skywire/pkg/cipher" + "github.com/skycoin/skywire/pkg/util/pathutil" "net" "net/rpc" "os" @@ -47,12 +49,16 @@ func TestListApps(t *testing.T) { } func TestStartStopApp(t *testing.T) { + pk, _ := cipher.GenerateKeyPair() router := new(mockRouter) executer := new(MockExecuter) defer os.RemoveAll("skychat") apps := []AppConfig{{App: "foo", Version: "1.0", AutoStart: false, Port: 10}} - node := &Node{router: router, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test")} + node := &Node{router: router, executer: executer, appsConf: apps, startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test"), config: &Config{}} + node.config.Node.StaticPubKey = pk + pathutil.EnsureDir(node.dir()) + defer os.RemoveAll(node.dir()) rpc := &RPC{node: node} unknownApp := "bar" @@ -119,6 +125,8 @@ func TestRPC(t *testing.T) { startedApps: map[string]*appBind{}, logger: logging.MustGetLogger("test"), } + pathutil.EnsureDir(node.dir()) + defer os.RemoveAll(node.dir()) require.NoError(t, node.StartApp("foo")) require.NoError(t, node.StartApp("bar")) diff --git a/pkg/util/pathutil/homedir.go b/pkg/util/pathutil/homedir.go index 6aa2fe5730..e9cae64523 100644 --- a/pkg/util/pathutil/homedir.go +++ b/pkg/util/pathutil/homedir.go @@ -1,13 +1,13 @@ package pathutil import ( - "fmt" - "github.com/skycoin/skywire/pkg/cipher" "io/ioutil" "os" "path" "path/filepath" "runtime" + + "github.com/skycoin/skywire/pkg/cipher" ) // HomeDir obtains the path to the user's home directory via ENVs. @@ -25,13 +25,13 @@ func HomeDir() string { // NodeDir returns a path to a directory used to store specific node configuration. Such dir is ~/.skywire/{PK} func NodeDir(pk cipher.PubKey) string { - return filepath.Join(HomeDir(),".skycoin","skywire",pk.String()) + return filepath.Join(HomeDir(), ".skycoin", "skywire", pk.String()) } // EnsureDir attempts to create given directory, panics if it fails to do so func EnsureDir(path string) { if _, err := os.Stat(path); os.IsNotExist(err) { - err := os.MkdirAll(path, 0644) + err := os.MkdirAll(path, 0755) if err != nil { panic(err) } @@ -41,7 +41,6 @@ func EnsureDir(path string) { // AtomicWriteFile creates a temp file in which to write data, then calls syscall.Rename to swap it and write it on // filename for an atomic write. On failure temp file is removed and panics. func AtomicWriteFile(filename string, data []byte) { - fmt.Println("got filename: ", filename) dir, name := path.Split(filename) f, err := ioutil.TempFile(dir, name) if err != nil { @@ -63,14 +62,13 @@ func AtomicWriteFile(filename string, data []byte) { } if err != nil { - os.Remove(f.Name()) + os.Remove(f.Name()) // nolint: errcheck + panic(err) } - panic(err) } // AtomicAppendToFile calls AtomicWriteFile but appends new data to destiny file func AtomicAppendToFile(filename string, data []byte) { - fmt.Println("got filename: ", filename) oldFile, err := ioutil.ReadFile(filename) if err != nil { panic(err) @@ -78,4 +76,3 @@ func AtomicAppendToFile(filename string, data []byte) { AtomicWriteFile(filename, append(oldFile, data...)) } -