github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/compare_test.go (about) 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 configtx 18 19 import ( 20 "github.com/stretchr/testify/assert" 21 "testing" 22 23 cb "github.com/hyperledger/fabric/protos/common" 24 ) 25 26 func TestCompareConfigValue(t *testing.T) { 27 // Normal equality 28 assert.True(t, comparable{ 29 ConfigValue: &cb.ConfigValue{ 30 Version: 0, 31 ModPolicy: "foo", 32 Value: []byte("bar"), 33 }}.equals(comparable{ 34 ConfigValue: &cb.ConfigValue{ 35 Version: 0, 36 ModPolicy: "foo", 37 Value: []byte("bar"), 38 }}), "Should have found identical config values to be identical") 39 40 // Different Mod Policy 41 assert.False(t, comparable{ 42 ConfigValue: &cb.ConfigValue{ 43 Version: 0, 44 ModPolicy: "foo", 45 Value: []byte("bar"), 46 }}.equals(comparable{ 47 ConfigValue: &cb.ConfigValue{ 48 Version: 0, 49 ModPolicy: "bar", 50 Value: []byte("bar"), 51 }}), "Should have detected different mod policy") 52 53 // Different Value 54 assert.False(t, comparable{ 55 ConfigValue: &cb.ConfigValue{ 56 Version: 0, 57 ModPolicy: "foo", 58 Value: []byte("bar"), 59 }}.equals(comparable{ 60 ConfigValue: &cb.ConfigValue{ 61 Version: 0, 62 ModPolicy: "foo", 63 Value: []byte("foo"), 64 }}), "Should have detected different value") 65 66 // Different Version 67 assert.False(t, comparable{ 68 ConfigValue: &cb.ConfigValue{ 69 Version: 0, 70 ModPolicy: "foo", 71 Value: []byte("bar"), 72 }}.equals(comparable{ 73 ConfigValue: &cb.ConfigValue{ 74 Version: 1, 75 ModPolicy: "foo", 76 Value: []byte("bar"), 77 }}), "Should have detected different version") 78 79 // One nil value 80 assert.False(t, comparable{ 81 ConfigValue: &cb.ConfigValue{ 82 Version: 0, 83 ModPolicy: "foo", 84 Value: []byte("bar"), 85 }}.equals(comparable{}), "Should have detected nil other value") 86 87 } 88 89 func TestCompareConfigPolicy(t *testing.T) { 90 // Normal equality 91 assert.True(t, comparable{ 92 ConfigPolicy: &cb.ConfigPolicy{ 93 Version: 0, 94 ModPolicy: "foo", 95 Policy: &cb.Policy{ 96 Type: 1, 97 Policy: []byte("foo"), 98 }, 99 }}.equals(comparable{ 100 ConfigPolicy: &cb.ConfigPolicy{ 101 Version: 0, 102 ModPolicy: "foo", 103 Policy: &cb.Policy{ 104 Type: 1, 105 Policy: []byte("foo"), 106 }, 107 }}), "Should have found identical config policies to be identical") 108 109 // Different mod policy 110 assert.False(t, comparable{ 111 ConfigPolicy: &cb.ConfigPolicy{ 112 Version: 0, 113 ModPolicy: "foo", 114 Policy: &cb.Policy{ 115 Type: 1, 116 Policy: []byte("foo"), 117 }, 118 }}.equals(comparable{ 119 ConfigPolicy: &cb.ConfigPolicy{ 120 Version: 0, 121 ModPolicy: "bar", 122 Policy: &cb.Policy{ 123 Type: 1, 124 Policy: []byte("foo"), 125 }, 126 }}), "Should have detected different mod policy") 127 128 // Different version 129 assert.False(t, comparable{ 130 ConfigPolicy: &cb.ConfigPolicy{ 131 Version: 0, 132 ModPolicy: "foo", 133 Policy: &cb.Policy{ 134 Type: 1, 135 Policy: []byte("foo"), 136 }, 137 }}.equals(comparable{ 138 ConfigPolicy: &cb.ConfigPolicy{ 139 Version: 1, 140 ModPolicy: "foo", 141 Policy: &cb.Policy{ 142 Type: 1, 143 Policy: []byte("foo"), 144 }, 145 }}), "Should have detected different version") 146 147 // Different policy type 148 assert.False(t, comparable{ 149 ConfigPolicy: &cb.ConfigPolicy{ 150 Version: 0, 151 ModPolicy: "foo", 152 Policy: &cb.Policy{ 153 Type: 1, 154 Policy: []byte("foo"), 155 }, 156 }}.equals(comparable{ 157 ConfigPolicy: &cb.ConfigPolicy{ 158 Version: 0, 159 ModPolicy: "foo", 160 Policy: &cb.Policy{ 161 Type: 2, 162 Policy: []byte("foo"), 163 }, 164 }}), "Should have detected different policy type") 165 166 // Different policy value 167 assert.False(t, comparable{ 168 ConfigPolicy: &cb.ConfigPolicy{ 169 Version: 0, 170 ModPolicy: "foo", 171 Policy: &cb.Policy{ 172 Type: 1, 173 Policy: []byte("foo"), 174 }, 175 }}.equals(comparable{ 176 ConfigPolicy: &cb.ConfigPolicy{ 177 Version: 0, 178 ModPolicy: "foo", 179 Policy: &cb.Policy{ 180 Type: 1, 181 Policy: []byte("bar"), 182 }, 183 }}), "Should have detected different policy value") 184 185 // One nil value 186 assert.False(t, comparable{ 187 ConfigPolicy: &cb.ConfigPolicy{ 188 Version: 0, 189 ModPolicy: "foo", 190 Policy: &cb.Policy{ 191 Type: 1, 192 Policy: []byte("foo"), 193 }, 194 }}.equals(comparable{}), "Should have detected one nil value") 195 196 // One nil policy 197 assert.False(t, comparable{ 198 ConfigPolicy: &cb.ConfigPolicy{ 199 Version: 0, 200 ModPolicy: "foo", 201 Policy: &cb.Policy{ 202 Type: 1, 203 Policy: []byte("foo"), 204 }, 205 }}.equals(comparable{ 206 ConfigPolicy: &cb.ConfigPolicy{ 207 Version: 0, 208 ModPolicy: "foo", 209 Policy: &cb.Policy{ 210 Type: 1, 211 }, 212 }}), "Should have detected one nil policy") 213 } 214 215 func TestCompareConfigGroup(t *testing.T) { 216 // Normal equality 217 assert.True(t, comparable{ 218 ConfigGroup: &cb.ConfigGroup{ 219 Version: 0, 220 ModPolicy: "foo", 221 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 222 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 223 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 224 }}.equals(comparable{ 225 ConfigGroup: &cb.ConfigGroup{ 226 Version: 0, 227 ModPolicy: "foo", 228 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 229 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 230 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 231 }}), "Should have found identical config groups to be identical") 232 233 // Different mod policy 234 assert.False(t, comparable{ 235 ConfigGroup: &cb.ConfigGroup{ 236 Version: 0, 237 ModPolicy: "foo", 238 }}.equals(comparable{ 239 ConfigGroup: &cb.ConfigGroup{ 240 Version: 0, 241 ModPolicy: "bar", 242 }}), "Should have detected different mod policy") 243 244 // Different version 245 assert.False(t, comparable{ 246 ConfigGroup: &cb.ConfigGroup{ 247 Version: 0, 248 ModPolicy: "foo", 249 }}.equals(comparable{ 250 ConfigGroup: &cb.ConfigGroup{ 251 Version: 1, 252 ModPolicy: "foo", 253 }}), "Should have detected different version") 254 255 // Different groups 256 assert.False(t, comparable{ 257 ConfigGroup: &cb.ConfigGroup{ 258 Version: 0, 259 ModPolicy: "foo", 260 Groups: map[string]*cb.ConfigGroup{"Foo1": nil}, 261 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 262 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 263 }}.equals(comparable{ 264 ConfigGroup: &cb.ConfigGroup{ 265 Version: 0, 266 ModPolicy: "foo", 267 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 268 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 269 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 270 }}), "Should have detected different groups entries") 271 272 // Different values 273 assert.False(t, comparable{ 274 ConfigGroup: &cb.ConfigGroup{ 275 Version: 0, 276 ModPolicy: "foo", 277 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 278 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 279 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 280 }}.equals(comparable{ 281 ConfigGroup: &cb.ConfigGroup{ 282 Version: 0, 283 ModPolicy: "foo", 284 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 285 Values: map[string]*cb.ConfigValue{"Foo2": nil}, 286 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 287 }}), "Should have detected fifferent values entries") 288 289 // Different policies 290 assert.False(t, comparable{ 291 ConfigGroup: &cb.ConfigGroup{ 292 Version: 0, 293 ModPolicy: "foo", 294 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 295 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 296 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil}, 297 }}.equals(comparable{ 298 ConfigGroup: &cb.ConfigGroup{ 299 Version: 0, 300 ModPolicy: "foo", 301 Groups: map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil}, 302 Values: map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil}, 303 Policies: map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar4": nil}, 304 }}), "Should have detected fifferent policies entries") 305 }