dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/report/etcd/report_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package etcd 19 20 /* 21 import ( 22 "encoding/json" 23 "reflect" 24 "strconv" 25 "testing" 26 ) 27 28 import ( 29 "github.com/agiledragon/gomonkey" 30 31 gxetcd "github.com/dubbogo/gost/database/kv/etcd/v3" 32 33 "go.etcd.io/etcd/client/v3" 34 ) 35 36 import ( 37 "dubbo.apache.org/dubbo-go/v3/common" 38 "dubbo.apache.org/dubbo-go/v3/common/constant" 39 "dubbo.apache.org/dubbo-go/v3/metadata/identifier" 40 ) 41 42 func newSubscribeMetadataIdentifier() *identifier.SubscriberMetadataIdentifier { 43 return &identifier.SubscriberMetadataIdentifier{ 44 Revision: "subscribe", 45 BaseApplicationMetadataIdentifier: identifier.BaseApplicationMetadataIdentifier{ 46 Application: "provider", 47 }, 48 } 49 } 50 51 func newServiceMetadataIdentifier() *identifier.ServiceMetadataIdentifier { 52 return &identifier.ServiceMetadataIdentifier{ 53 Protocol: "nacos", 54 Revision: "a", 55 BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ 56 ServiceInterface: "com.test.MyTest", 57 Version: "1.0.0", 58 Group: "test_group", 59 Side: "service", 60 }, 61 } 62 } 63 64 func newMetadataIdentifier(side string) *identifier.MetadataIdentifier { 65 return &identifier.MetadataIdentifier{ 66 Application: "test", 67 BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ 68 ServiceInterface: "com.test.MyTest", 69 Version: "1.0.0", 70 Group: "test_group", 71 Side: side, 72 }, 73 } 74 } 75 76 type fields struct { 77 client *gxetcd.Client 78 root string 79 } 80 type args struct { 81 subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier 82 info *common.MetadataInfo 83 providerIdentifier *identifier.MetadataIdentifier 84 serviceDefinitions string 85 consumerMetadataIdentifier *identifier.MetadataIdentifier 86 serviceParameterString string 87 serviceMetadataIdentifier *identifier.ServiceMetadataIdentifier 88 url *common.URL 89 urls string 90 } 91 92 func newEtcdMetadataReport(f fields) *etcdMetadataReport { 93 return &etcdMetadataReport{ 94 client: f.client, 95 root: f.root, 96 } 97 } 98 99 func Test_etcdMetadataReport_PublishAppMetadata(t *testing.T) { 100 var client *gxetcd.Client 101 patches := gomonkey.NewPatches() 102 patches = patches.ApplyMethod(reflect.TypeOf(client), "Put", func(_ *gxetcd.Client, k, v string, opts ...clientv3.OpOption) error { 103 return nil 104 }) 105 defer patches.Reset() 106 107 tests := []struct { 108 name string 109 fields fields 110 args args 111 wantErr bool 112 }{ 113 { 114 name: "test", 115 fields: fields{ 116 client: client, 117 root: "/dubbo", 118 }, 119 args: args{ 120 subscriberMetadataIdentifier: newSubscribeMetadataIdentifier(), 121 info: &common.MetadataInfo{}, 122 }, 123 wantErr: false, 124 }, 125 } 126 for _, tt := range tests { 127 t.Run(tt.name, func(t *testing.T) { 128 e := newEtcdMetadataReport(tt.fields) 129 if err := e.PublishAppMetadata(tt.args.subscriberMetadataIdentifier, tt.args.info); (err != nil) != tt.wantErr { 130 t.Errorf("PublishAppMetadata() error = %v, wantErr %v", err, tt.wantErr) 131 } 132 }) 133 } 134 } 135 136 func Test_etcdMetadataReport_StoreProviderMetadata(t *testing.T) { 137 var client *gxetcd.Client 138 patches := gomonkey.NewPatches() 139 patches = patches.ApplyMethod(reflect.TypeOf(client), "Put", func(_ *gxetcd.Client, k, v string, opts ...clientv3.OpOption) error { 140 return nil 141 }) 142 defer patches.Reset() 143 144 tests := []struct { 145 name string 146 fields fields 147 args args 148 wantErr bool 149 }{ 150 { 151 name: "test", 152 fields: fields{ 153 client: client, 154 root: "/dubbo", 155 }, 156 args: args{ 157 providerIdentifier: newMetadataIdentifier("provuder"), 158 serviceDefinitions: "provider", 159 }, 160 wantErr: false, 161 }, 162 } 163 for _, tt := range tests { 164 t.Run(tt.name, func(t *testing.T) { 165 e := newEtcdMetadataReport(tt.fields) 166 if err := e.StoreProviderMetadata(tt.args.providerIdentifier, tt.args.serviceDefinitions); (err != nil) != tt.wantErr { 167 t.Errorf("StoreProviderMetadata() error = %v, wantErr %v", err, tt.wantErr) 168 } 169 }) 170 } 171 } 172 173 func Test_etcdMetadataReport_StoreConsumerMetadata(t *testing.T) { 174 var client *gxetcd.Client 175 patches := gomonkey.NewPatches() 176 patches = patches.ApplyMethod(reflect.TypeOf(client), "Put", func(_ *gxetcd.Client, k, v string, opts ...clientv3.OpOption) error { 177 return nil 178 }) 179 defer patches.Reset() 180 181 tests := []struct { 182 name string 183 fields fields 184 args args 185 wantErr bool 186 }{ 187 { 188 name: "test", 189 fields: fields{ 190 client: client, 191 root: "/dubbo", 192 }, 193 args: args{ 194 consumerMetadataIdentifier: newMetadataIdentifier("conusmer"), 195 serviceParameterString: "conusmer", 196 }, 197 wantErr: false, 198 }, 199 } 200 for _, tt := range tests { 201 t.Run(tt.name, func(t *testing.T) { 202 e := newEtcdMetadataReport(tt.fields) 203 if err := e.StoreConsumerMetadata(tt.args.consumerMetadataIdentifier, tt.args.serviceParameterString); (err != nil) != tt.wantErr { 204 t.Errorf("StoreConsumerMetadata() error = %v, wantErr %v", err, tt.wantErr) 205 } 206 }) 207 } 208 } 209 210 func Test_etcdMetadataReport_SaveServiceMetadata(t *testing.T) { 211 var client *gxetcd.Client 212 patches := gomonkey.NewPatches() 213 patches = patches.ApplyMethod(reflect.TypeOf(client), "Put", func(_ *gxetcd.Client, k, v string, opts ...clientv3.OpOption) error { 214 return nil 215 }) 216 defer patches.Reset() 217 serviceURL, _ := common.NewURL("registry://localhost:8848", common.WithParamsValue(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER))) 218 219 tests := []struct { 220 name string 221 fields fields 222 args args 223 wantErr bool 224 }{ 225 { 226 name: "test", 227 fields: fields{ 228 client: client, 229 root: "/dubbo", 230 }, 231 args: args{ 232 serviceMetadataIdentifier: newServiceMetadataIdentifier(), 233 url: serviceURL, 234 }, 235 wantErr: false, 236 }, 237 } 238 for _, tt := range tests { 239 t.Run(tt.name, func(t *testing.T) { 240 e := newEtcdMetadataReport(tt.fields) 241 if err := e.SaveServiceMetadata(tt.args.serviceMetadataIdentifier, tt.args.url); (err != nil) != tt.wantErr { 242 t.Errorf("SaveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) 243 } 244 }) 245 } 246 } 247 248 func Test_etcdMetadataReport_SaveSubscribedData(t *testing.T) { 249 var client *gxetcd.Client 250 patches := gomonkey.NewPatches() 251 patches = patches.ApplyMethod(reflect.TypeOf(client), "Put", func(_ *gxetcd.Client, k, v string, opts ...clientv3.OpOption) error { 252 return nil 253 }) 254 defer patches.Reset() 255 256 tests := []struct { 257 name string 258 fields fields 259 args args 260 wantErr bool 261 }{ 262 { 263 name: "test", 264 fields: fields{ 265 client: client, 266 root: "/dubbo", 267 }, 268 args: args{ 269 subscriberMetadataIdentifier: newSubscribeMetadataIdentifier(), 270 urls: "dubbogo", 271 }, 272 wantErr: false, 273 }, 274 } 275 for _, tt := range tests { 276 t.Run(tt.name, func(t *testing.T) { 277 e := newEtcdMetadataReport(tt.fields) 278 if err := e.SaveSubscribedData(tt.args.subscriberMetadataIdentifier, tt.args.urls); (err != nil) != tt.wantErr { 279 t.Errorf("SaveSubscribedData() error = %v, wantErr %v", err, tt.wantErr) 280 } 281 }) 282 } 283 } 284 285 func Test_etcdMetadataReport_RemoveServiceMetadata(t *testing.T) { 286 var client *gxetcd.Client 287 patches := gomonkey.NewPatches() 288 patches = patches.ApplyMethod(reflect.TypeOf(client), "Delete", func(_ *gxetcd.Client, k string) error { 289 return nil 290 }) 291 defer patches.Reset() 292 293 tests := []struct { 294 name string 295 fields fields 296 args args 297 wantErr bool 298 }{ 299 { 300 name: "test", 301 fields: fields{ 302 client: client, 303 root: DEFAULT_ROOT, 304 }, 305 args: args{ 306 serviceMetadataIdentifier: newServiceMetadataIdentifier(), 307 }, 308 wantErr: false, 309 }, 310 } 311 for _, tt := range tests { 312 t.Run(tt.name, func(t *testing.T) { 313 e := newEtcdMetadataReport(tt.fields) 314 if err := e.RemoveServiceMetadata(tt.args.serviceMetadataIdentifier); (err != nil) != tt.wantErr { 315 t.Errorf("RemoveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) 316 } 317 }) 318 } 319 } 320 321 func Test_etcdMetadataReport_GetAppMetadata(t *testing.T) { 322 info := &common.MetadataInfo{} 323 target, _ := json.Marshal(info) 324 var client *gxetcd.Client 325 patches := gomonkey.NewPatches() 326 patches = patches.ApplyMethod(reflect.TypeOf(client), "Get", func(_ *gxetcd.Client, k string) (string, error) { 327 return string(target), nil 328 }) 329 defer patches.Reset() 330 331 tests := []struct { 332 name string 333 fields fields 334 args args 335 want *common.MetadataInfo 336 wantErr bool 337 }{ 338 { 339 name: "test", 340 fields: fields{ 341 client: client, 342 root: DEFAULT_ROOT, 343 }, 344 args: args{ 345 subscriberMetadataIdentifier: newSubscribeMetadataIdentifier(), 346 }, 347 want: &common.MetadataInfo{}, 348 wantErr: false, 349 }, 350 } 351 for _, tt := range tests { 352 t.Run(tt.name, func(t *testing.T) { 353 e := newEtcdMetadataReport(tt.fields) 354 got, err := e.GetAppMetadata(tt.args.subscriberMetadataIdentifier) 355 if (err != nil) != tt.wantErr { 356 t.Errorf("GetAppMetadata() error = %v, wantErr %v", err, tt.wantErr) 357 return 358 } 359 if !reflect.DeepEqual(got, tt.want) { 360 t.Errorf("GetAppMetadata() got = %v, want %v", got, tt.want) 361 } 362 }) 363 } 364 } 365 */