@@ -8,147 +8,19 @@ package cc_test
8
8
9
9
import (
10
10
"bytes"
11
- "fmt"
12
- "io/ioutil"
13
- "math/rand"
14
- "os"
15
- "path"
16
11
"testing"
17
- "time"
18
12
19
13
"github.com/golang/protobuf/proto"
20
14
"github.com/hyperledger/fabric/common/chaincode"
21
15
"github.com/hyperledger/fabric/core/cclifecycle"
22
16
"github.com/hyperledger/fabric/core/cclifecycle/mocks"
23
17
"github.com/hyperledger/fabric/core/common/ccprovider"
24
18
"github.com/hyperledger/fabric/protos/common"
25
- "github.com/hyperledger/fabric/protos/peer"
26
19
"github.com/pkg/errors"
27
20
"github.com/stretchr/testify/assert"
28
21
"github.com/stretchr/testify/mock"
29
22
)
30
23
31
- func TestInstalledCCs (t * testing.T ) {
32
- tmpDir := setupDirectoryStructure (t )
33
- defer func () {
34
- os .RemoveAll (tmpDir )
35
- }()
36
- testCases := []struct {
37
- name string
38
- directory string
39
- expected []chaincode.InstalledChaincode
40
- errorContains string
41
- ls cc.DirEnumerator
42
- extractCCFromPath cc.ChaincodeExtractor
43
- }{
44
- {
45
- name : "None empty directory" ,
46
- ls : ioutil .ReadDir ,
47
- extractCCFromPath : ccprovider .LoadPackage ,
48
- expected : []chaincode.InstalledChaincode {
49
- {
50
- Name : "example02" ,
51
- Version : "1.0" ,
52
- Id : []byte {45 , 186 , 93 , 188 , 51 , 158 , 115 , 22 , 174 , 162 , 104 , 63 , 175 , 131 , 156 , 27 , 123 , 30 , 226 , 49 , 61 , 183 , 146 , 17 , 37 , 136 , 17 , 141 , 240 , 102 , 170 , 53 },
53
- },
54
- {
55
- Name : "example04" ,
56
- Version : "1" ,
57
- Id : []byte {45 , 186 , 93 , 188 , 51 , 158 , 115 , 22 , 174 , 162 , 104 , 63 , 175 , 131 , 156 , 27 , 123 , 30 , 226 , 49 , 61 , 183 , 146 , 17 , 37 , 136 , 17 , 141 , 240 , 102 , 170 , 53 },
58
- },
59
- },
60
- directory : "nonempty" ,
61
- },
62
- {
63
- name : "None present directory" ,
64
- ls : ioutil .ReadDir ,
65
- extractCCFromPath : ccprovider .LoadPackage ,
66
- expected : nil ,
67
- directory : "notexistent" ,
68
- },
69
- {
70
- name : "Empty directory" ,
71
- ls : ioutil .ReadDir ,
72
- extractCCFromPath : ccprovider .LoadPackage ,
73
- expected : nil ,
74
- directory : "empty" ,
75
- },
76
- {
77
- name : "No permission to open directory" ,
78
- ls : func (_ string ) ([]os.FileInfo , error ) {
79
- return nil , errors .New ("permission denied" )
80
- },
81
- extractCCFromPath : ccprovider .LoadPackage ,
82
- expected : nil ,
83
- directory : "nopermission" ,
84
- errorContains : "permission denied" ,
85
- },
86
- {
87
- name : "No permission on chaincode files" ,
88
- ls : ioutil .ReadDir ,
89
- extractCCFromPath : func (_ string , _ string , _ string ) (ccprovider.CCPackage , error ) {
90
- return nil , errors .New ("permission denied" )
91
- },
92
- expected : nil ,
93
- directory : "nopermissionforfiles" ,
94
- errorContains : "permission denied" ,
95
- },
96
- }
97
- _ = testCases
98
-
99
- for _ , test := range testCases {
100
- test := test
101
- t .Run (test .name , func (t * testing.T ) {
102
- res , err := cc .InstalledCCs (path .Join (tmpDir , test .directory ), test .ls , test .extractCCFromPath )
103
- assert .Equal (t , test .expected , res )
104
- if test .errorContains == "" {
105
- assert .NoError (t , err )
106
- } else {
107
- assert .Contains (t , err .Error (), test .errorContains )
108
- }
109
- })
110
- }
111
-
112
- }
113
-
114
- func setupDirectoryStructure (t * testing.T ) string {
115
- files := []string {
116
- "example02.1.0" , // Version contains the delimiter '.' is a valid case
117
- "example03" , // No version specified
118
- "example04.1" , // Version doesn't contain the '.' delimiter
119
- }
120
- rand .Seed (time .Now ().UnixNano ())
121
- tmp := path .Join (os .TempDir (), fmt .Sprintf ("%d" , rand .Int ()))
122
- assert .NoError (t , os .Mkdir (tmp , 0755 ))
123
- dir := path .Join (tmp , "empty" )
124
- assert .NoError (t , os .Mkdir (dir , 0755 ))
125
- dir = path .Join (tmp , "nonempty" )
126
- assert .NoError (t , os .Mkdir (dir , 0755 ))
127
- dir = path .Join (tmp , "nopermission" )
128
- assert .NoError (t , os .Mkdir (dir , 0755 ))
129
- dir = path .Join (tmp , "nopermissionforfiles" )
130
- assert .NoError (t , os .Mkdir (dir , 0755 ))
131
- noPermissionFile := path .Join (tmp , "nopermissionforfiles" , "nopermission.1" )
132
- _ , err := os .Create (noPermissionFile )
133
- assert .NoError (t , err )
134
- dir = path .Join (tmp , "nonempty" )
135
- assert .NoError (t , os .Mkdir (path .Join (tmp , "nonempty" , "directory" ), 0755 ))
136
- for _ , f := range files {
137
- file , err := os .Create (path .Join (dir , f ))
138
- assert .NoError (t , err )
139
- cds := & peer.ChaincodeDeploymentSpec {
140
- ChaincodeSpec : & peer.ChaincodeSpec {
141
- ChaincodeId : & peer.ChaincodeID {},
142
- },
143
- }
144
- b , _ := proto .Marshal (cds )
145
- file .Write (b )
146
- file .Close ()
147
- }
148
-
149
- return tmp
150
- }
151
-
152
24
func TestChaincodeInspection (t * testing.T ) {
153
25
acceptAll := func (cc chaincode.Metadata ) bool {
154
26
return true
0 commit comments