|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 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 channel |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/hyperledger/fabric/peer/common" |
| 26 | + cb "github.com/hyperledger/fabric/protos/common" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | +) |
| 30 | + |
| 31 | +func TestUpdateChannel(t *testing.T) { |
| 32 | + InitMSP() |
| 33 | + |
| 34 | + dir, err := ioutil.TempDir("/tmp", "createinvaltest-") |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("couldn't create temp dir") |
| 37 | + } |
| 38 | + defer os.RemoveAll(dir) // clean up |
| 39 | + |
| 40 | + mockchannel := "mockchannel" |
| 41 | + |
| 42 | + configtxFile := filepath.Join(dir, mockchannel) |
| 43 | + if _, err = createTxFile(configtxFile, cb.HeaderType_CONFIG_UPDATE, mockchannel); err != nil { |
| 44 | + t.Fatalf("couldn't create tx file") |
| 45 | + } |
| 46 | + |
| 47 | + signer, err := common.GetDefaultSigner() |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Get default signer error: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + mockCF := &ChannelCmdFactory{ |
| 53 | + BroadcastFactory: mockBroadcastClientFactory, |
| 54 | + Signer: signer, |
| 55 | + DeliverClient: &mockDeliverClient{}, |
| 56 | + } |
| 57 | + |
| 58 | + cmd := updateCmd(mockCF) |
| 59 | + |
| 60 | + AddFlags(cmd) |
| 61 | + |
| 62 | + args := []string{"-c", mockchannel, "-f", configtxFile, "-o", "localhost:7050"} |
| 63 | + cmd.SetArgs(args) |
| 64 | + |
| 65 | + assert.NoError(t, cmd.Execute()) |
| 66 | +} |
| 67 | + |
| 68 | +func TestUpdateChannelMissingConfigTxFlag(t *testing.T) { |
| 69 | + InitMSP() |
| 70 | + mockchannel := "mockchannel" |
| 71 | + |
| 72 | + signer, err := common.GetDefaultSigner() |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Get default signer error: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + mockCF := &ChannelCmdFactory{ |
| 78 | + BroadcastFactory: mockBroadcastClientFactory, |
| 79 | + Signer: signer, |
| 80 | + DeliverClient: &mockDeliverClient{}, |
| 81 | + } |
| 82 | + |
| 83 | + cmd := updateCmd(mockCF) |
| 84 | + |
| 85 | + AddFlags(cmd) |
| 86 | + |
| 87 | + args := []string{"-c", mockchannel, "-o", "localhost:7050"} |
| 88 | + cmd.SetArgs(args) |
| 89 | + |
| 90 | + assert.Error(t, cmd.Execute()) |
| 91 | +} |
| 92 | + |
| 93 | +func TestUpdateChannelMissingConfigTxFile(t *testing.T) { |
| 94 | + InitMSP() |
| 95 | + mockchannel := "mockchannel" |
| 96 | + |
| 97 | + signer, err := common.GetDefaultSigner() |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Get default signer error: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + mockCF := &ChannelCmdFactory{ |
| 103 | + BroadcastFactory: mockBroadcastClientFactory, |
| 104 | + Signer: signer, |
| 105 | + DeliverClient: &mockDeliverClient{}, |
| 106 | + } |
| 107 | + |
| 108 | + cmd := createCmd(mockCF) |
| 109 | + |
| 110 | + AddFlags(cmd) |
| 111 | + |
| 112 | + args := []string{"-c", mockchannel, "-f", "Non-existant", "-o", "localhost:7050"} |
| 113 | + cmd.SetArgs(args) |
| 114 | + |
| 115 | + assert.Error(t, cmd.Execute()) |
| 116 | +} |
| 117 | + |
| 118 | +func TestUpdateChannelMissingChannelID(t *testing.T) { |
| 119 | + InitMSP() |
| 120 | + |
| 121 | + dir, err := ioutil.TempDir("/tmp", "createinvaltest-") |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("couldn't create temp dir") |
| 124 | + } |
| 125 | + defer os.RemoveAll(dir) // clean up |
| 126 | + |
| 127 | + mockchannel := "mockchannel" |
| 128 | + |
| 129 | + configtxFile := filepath.Join(dir, mockchannel) |
| 130 | + if _, err = createTxFile(configtxFile, cb.HeaderType_CONFIG_UPDATE, mockchannel); err != nil { |
| 131 | + t.Fatalf("couldn't create tx file") |
| 132 | + } |
| 133 | + |
| 134 | + signer, err := common.GetDefaultSigner() |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Get default signer error: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + mockCF := &ChannelCmdFactory{ |
| 140 | + BroadcastFactory: mockBroadcastClientFactory, |
| 141 | + Signer: signer, |
| 142 | + DeliverClient: &mockDeliverClient{}, |
| 143 | + } |
| 144 | + |
| 145 | + cmd := updateCmd(mockCF) |
| 146 | + |
| 147 | + AddFlags(cmd) |
| 148 | + |
| 149 | + args := []string{"-f", configtxFile, "-o", "localhost:7050"} |
| 150 | + cmd.SetArgs(args) |
| 151 | + |
| 152 | + assert.Error(t, cmd.Execute()) |
| 153 | +} |
0 commit comments