github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/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.fileSystemPath", "/tmp/hyperledger/test")
    36  	viper.Set("chaincode.executetimeout", "30s")
    37  	overrideLogModules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"}
    38  	for _, module := range overrideLogModules {
    39  		viper.Set("logging."+module, "INFO")
    40  	}
    41  
    42  	msptesttools.LoadMSPSetupForTesting()
    43  
    44  	go func() {
    45  		cmd := startCmd()
    46  		assert.NoError(t, cmd.Execute(), "expected to successfully start command")
    47  	}()
    48  
    49  	timer := time.NewTimer(time.Second * 3)
    50  	defer timer.Stop()
    51  
    52  	// waiting for pid file will be created
    53  loop:
    54  	for {
    55  		select {
    56  		case <-timer.C:
    57  			t.Errorf("timeout waiting for start command")
    58  		default:
    59  			_, err := os.Stat("/tmp/hyperledger/test/peer.pid")
    60  			if err != nil {
    61  				time.Sleep(200 * time.Millisecond)
    62  			} else {
    63  				break loop
    64  			}
    65  		}
    66  	}
    67  
    68  	pidFile, err := ioutil.ReadFile("/tmp/hyperledger/test/peer.pid")
    69  	if err != nil {
    70  		t.Fail()
    71  		t.Errorf("can't delete pid file")
    72  	}
    73  	pid, err := strconv.Atoi(string(pidFile))
    74  	killerr := syscall.Kill(pid, syscall.SIGTERM)
    75  	if killerr != nil {
    76  		t.Errorf("Error trying to kill -15 pid %d: %s", pid, killerr)
    77  	}
    78  
    79  	os.RemoveAll("/tmp/hyperledger/test")
    80  }
    81  
    82  func TestWritePid(t *testing.T) {
    83  	var tests = []struct {
    84  		name     string
    85  		fileName string
    86  		pid      int
    87  		expected bool
    88  	}{
    89  		{
    90  			name:     "readPid success",
    91  			fileName: "/tmp/hyperledger/test/peer.pid",
    92  			pid:      os.Getpid(),
    93  			expected: true,
    94  		},
    95  		{
    96  			name:     "readPid error",
    97  			fileName: "",
    98  			pid:      os.Getpid(),
    99  			expected: false,
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		test := test
   105  		t.Run(test.name, func(t *testing.T) {
   106  			t.Logf("Running test %s", test.name)
   107  			if test.expected {
   108  				err := writePid(test.fileName, test.pid)
   109  				os.Remove(test.fileName)
   110  				assert.NoError(t, err, "expected to successfully write pid file")
   111  			} else {
   112  				err := writePid(test.fileName, test.pid)
   113  				assert.Error(t, err, "addition of empty pid filename should fail")
   114  			}
   115  		})
   116  	}
   117  }