github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/peer/node/start_test.go (about) 1 /* 2 Copyright 2017 Hitachi America, Ltd. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package node 18 19 import ( 20 "io/ioutil" 21 "os" 22 "strconv" 23 "syscall" 24 "testing" 25 "time" 26 27 "github.com/hyperledger/fabric/msp/mgmt/testtools" 28 "github.com/spf13/viper" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestStartCmd(t *testing.T) { 33 viper.Set("peer.address", "0.0.0.0:6051") 34 viper.Set("peer.listenAddress", "0.0.0.0:6051") 35 viper.Set("peer.chaincodeListenAddress", "0.0.0.0:6052") 36 viper.Set("peer.fileSystemPath", "/tmp/hyperledger/test") 37 viper.Set("chaincode.executetimeout", "30s") 38 overrideLogModules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"} 39 for _, module := range overrideLogModules { 40 viper.Set("logging."+module, "INFO") 41 } 42 43 msptesttools.LoadMSPSetupForTesting() 44 45 go func() { 46 cmd := startCmd() 47 assert.NoError(t, cmd.Execute(), "expected to successfully start command") 48 }() 49 50 timer := time.NewTimer(time.Second * 3) 51 defer timer.Stop() 52 53 // waiting for pid file will be created 54 loop: 55 for { 56 select { 57 case <-timer.C: 58 t.Errorf("timeout waiting for start command") 59 default: 60 _, err := os.Stat("/tmp/hyperledger/test/peer.pid") 61 if err != nil { 62 time.Sleep(200 * time.Millisecond) 63 } else { 64 break loop 65 } 66 } 67 } 68 69 pidFile, err := ioutil.ReadFile("/tmp/hyperledger/test/peer.pid") 70 if err != nil { 71 t.Fail() 72 t.Errorf("can't delete pid file") 73 } 74 pid, err := strconv.Atoi(string(pidFile)) 75 killerr := syscall.Kill(pid, syscall.SIGTERM) 76 if killerr != nil { 77 t.Errorf("Error trying to kill -15 pid %d: %s", pid, killerr) 78 } 79 80 os.RemoveAll("/tmp/hyperledger/test") 81 } 82 83 func TestWritePid(t *testing.T) { 84 var tests = []struct { 85 name string 86 fileName string 87 pid int 88 expected bool 89 }{ 90 { 91 name: "readPid success", 92 fileName: "/tmp/hyperledger/test/peer.pid", 93 pid: os.Getpid(), 94 expected: true, 95 }, 96 { 97 name: "readPid error", 98 fileName: "", 99 pid: os.Getpid(), 100 expected: false, 101 }, 102 } 103 104 for _, test := range tests { 105 test := test 106 t.Run(test.name, func(t *testing.T) { 107 t.Logf("Running test %s", test.name) 108 if test.expected { 109 err := writePid(test.fileName, test.pid) 110 os.Remove(test.fileName) 111 assert.NoError(t, err, "expected to successfully write pid file") 112 } else { 113 err := writePid(test.fileName, test.pid) 114 assert.Error(t, err, "addition of empty pid filename should fail") 115 } 116 }) 117 } 118 }