|
| 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 | + "testing" |
| 21 | + |
| 22 | + "github.com/hyperledger/fabric/core" |
| 23 | + "github.com/hyperledger/fabric/core/comm" |
| 24 | + testpb "github.com/hyperledger/fabric/core/comm/testdata/grpc" |
| 25 | + "github.com/hyperledger/fabric/core/peer" |
| 26 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 27 | + "github.com/spf13/viper" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "golang.org/x/net/context" |
| 30 | +) |
| 31 | + |
| 32 | +type testServiceServer struct{} |
| 33 | + |
| 34 | +func (tss *testServiceServer) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
| 35 | + return new(testpb.Empty), nil |
| 36 | +} |
| 37 | + |
| 38 | +func TestStatusCmd(t *testing.T) { |
| 39 | + |
| 40 | + viper.Set("peer.address", "localhost:7070") |
| 41 | + peerServer, err := peer.CreatePeerServer("localhost:7070", comm.SecureServerConfig{}) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Failed to create peer server (%s)", err) |
| 44 | + } else { |
| 45 | + pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer()) |
| 46 | + go peerServer.Start() |
| 47 | + defer peerServer.Stop() |
| 48 | + |
| 49 | + cmd := statusCmd() |
| 50 | + if err := cmd.Execute(); err != nil { |
| 51 | + t.Fail() |
| 52 | + t.Errorf("expected status command to succeed") |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestStatus(t *testing.T) { |
| 58 | + var tests = []struct { |
| 59 | + name string |
| 60 | + peerAddress string |
| 61 | + listenAddress string |
| 62 | + expected bool |
| 63 | + }{ |
| 64 | + { |
| 65 | + name: "status function to success", |
| 66 | + peerAddress: "localhost:7071", |
| 67 | + listenAddress: "localhost:7071", |
| 68 | + expected: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "admin client error", |
| 72 | + peerAddress: "", |
| 73 | + listenAddress: "localhost:7072", |
| 74 | + expected: false, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, test := range tests { |
| 79 | + test := test |
| 80 | + t.Run(test.name, func(t *testing.T) { |
| 81 | + t.Logf("Running test: %s", test.name) |
| 82 | + viper.Set("peer.address", test.peerAddress) |
| 83 | + peerServer, err := peer.CreatePeerServer(test.listenAddress, comm.SecureServerConfig{}) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("Failed to create peer server (%s)", err) |
| 86 | + } else { |
| 87 | + pb.RegisterAdminServer(peerServer.Server(), core.NewAdminServer()) |
| 88 | + go peerServer.Start() |
| 89 | + defer peerServer.Stop() |
| 90 | + if test.expected { |
| 91 | + assert.NoError(t, status()) |
| 92 | + } else { |
| 93 | + assert.Error(t, status()) |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestStatusWithGetStatusError(t *testing.T) { |
| 101 | + viper.Set("peer.address", "localhost:7073") |
| 102 | + peerServer, err := peer.CreatePeerServer(":7073", comm.SecureServerConfig{}) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to create peer server (%s)", err) |
| 105 | + } |
| 106 | + testpb.RegisterTestServiceServer(peerServer.Server(), &testServiceServer{}) |
| 107 | + go peerServer.Start() |
| 108 | + defer peerServer.Stop() |
| 109 | + assert.Error(t, status()) |
| 110 | +} |
0 commit comments