github.com/aiven/aiven-go-client@v1.36.0/mirrormaker_replication_flow_test.go (about) 1 package aiven 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "reflect" 8 "testing" 9 ) 10 11 func setupMirrormakerReplicationFlowTestCase(t *testing.T) (*Client, func(t *testing.T)) { 12 t.Log("setup Kafka Mirrormaker 2 Replication Flow test case") 13 14 const ( 15 UserName = "test@aiven.io" 16 UserPassword = "testabcd" 17 AccessToken = "some-random-token" 18 ) 19 20 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 if r.URL.Path == "/userauth" { 22 w.Header().Set("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 err := json.NewEncoder(w).Encode(authResponse{ 25 Token: AccessToken, 26 State: "active", 27 }) 28 29 if err != nil { 30 t.Error(err) 31 } 32 return 33 } 34 35 if r.URL.Path == "/project/test-pr/service/test-sr/mirrormaker/replication-flows" { 36 w.Header().Set("Content-Type", "application/json") 37 w.WriteHeader(http.StatusOK) 38 _, err := w.Write([]byte(` 39 { 40 "errors": [ 41 ], 42 "message": "Completed", 43 "replication_flows": [{ 44 "enabled": true, 45 "offset_syncs_topic_location": "source", 46 "source_cluster": "kafka-sc", 47 "target_cluster": "kafka-tc", 48 "topics": [ 49 ".*" 50 ], 51 "topics.blacklist": [ 52 ".*[\\-\\.]internal", 53 ".*\\.replica", 54 "__.*" 55 ] 56 }] 57 } 58 `)) 59 if err != nil { 60 t.Error(err) 61 } 62 return 63 } 64 65 if r.URL.Path == "/project/test-pr/service/test-sr/mirrormaker/replication-flows/kafka-sc/kafka-tc" { 66 w.Header().Set("Content-Type", "application/json") 67 w.WriteHeader(http.StatusOK) 68 _, err := w.Write([]byte(` 69 { 70 "replication_flow": { 71 "enabled": true, 72 "offset_syncs_topic_location": "source", 73 "source_cluster": "kafka-sc", 74 "target_cluster": "kafka-tc", 75 "topics": [ 76 ".*" 77 ], 78 "topics.blacklist": [ 79 ".*[\\-\\.]internal", 80 ".*\\.replica", 81 "__.*" 82 ] 83 } 84 } 85 `)) 86 if err != nil { 87 t.Error(err) 88 } 89 return 90 } 91 })) 92 93 apiUrl = ts.URL 94 95 c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version()) 96 if err != nil { 97 t.Fatalf("user authentication error: %s", err) 98 } 99 100 return c, func(t *testing.T) { 101 t.Log("teardown Kafka Mirrormaker 2 Replication Flow test case") 102 ts.Close() 103 } 104 } 105 106 func TestMirrorMakerReplicationFlowHandler_Create(t *testing.T) { 107 c, tearDown := setupMirrormakerReplicationFlowTestCase(t) 108 defer tearDown(t) 109 110 type fields struct { 111 client *Client 112 } 113 type args struct { 114 project string 115 service string 116 req MirrorMakerReplicationFlowRequest 117 } 118 tests := []struct { 119 name string 120 fields fields 121 args args 122 wantErr bool 123 }{ 124 { 125 "basic", 126 fields{c}, 127 args{ 128 project: "test-pr", 129 service: "test-sr", 130 req: MirrorMakerReplicationFlowRequest{ 131 ReplicationFlow{ 132 Enabled: true, 133 SourceCluster: "kafka-sc", 134 TargetCluster: "kafka-tc", 135 Topics: []string{ 136 ".*", 137 }, 138 TopicsBlacklist: []string{ 139 ".*[\\-\\.]internal", 140 ".*\\.replica", 141 "__.*", 142 }, 143 }, 144 }, 145 }, 146 false, 147 }, 148 } 149 for _, tt := range tests { 150 t.Run(tt.name, func(t *testing.T) { 151 h := &MirrorMakerReplicationFlowHandler{ 152 client: tt.fields.client, 153 } 154 err := h.Create(tt.args.project, tt.args.service, tt.args.req) 155 if (err != nil) != tt.wantErr { 156 t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr) 157 return 158 } 159 }) 160 } 161 } 162 163 func TestMirrorMakerReplicationFlowHandler_Update(t *testing.T) { 164 c, tearDown := setupMirrormakerReplicationFlowTestCase(t) 165 defer tearDown(t) 166 167 type fields struct { 168 client *Client 169 } 170 type args struct { 171 project string 172 service string 173 sourceCluster string 174 targetCluster string 175 req MirrorMakerReplicationFlowRequest 176 } 177 tests := []struct { 178 name string 179 fields fields 180 args args 181 want *MirrorMakerReplicationFlowResponse 182 wantErr bool 183 }{ 184 { 185 "basic", 186 fields{ 187 client: c, 188 }, 189 args{ 190 project: "test-pr", 191 service: "test-sr", 192 sourceCluster: "kafka-sc", 193 targetCluster: "kafka-tc", 194 req: MirrorMakerReplicationFlowRequest{ 195 ReplicationFlow{ 196 Enabled: true, 197 SourceCluster: "kafka-sc", 198 TargetCluster: "kafka-tc", 199 Topics: []string{ 200 ".*", 201 }, 202 TopicsBlacklist: []string{ 203 ".*[\\-\\.]internal", 204 ".*\\.replica", 205 "__.*", 206 }, 207 }, 208 }, 209 }, 210 &MirrorMakerReplicationFlowResponse{ 211 ReplicationFlow: ReplicationFlow{ 212 Enabled: true, 213 OffsetSyncsTopicLocation: "source", 214 SourceCluster: "kafka-sc", 215 TargetCluster: "kafka-tc", 216 Topics: []string{ 217 ".*", 218 }, 219 TopicsBlacklist: []string{ 220 ".*[\\-\\.]internal", 221 ".*\\.replica", 222 "__.*", 223 }, 224 }, 225 }, 226 false, 227 }, 228 } 229 for _, tt := range tests { 230 t.Run(tt.name, func(t *testing.T) { 231 h := &MirrorMakerReplicationFlowHandler{ 232 client: tt.fields.client, 233 } 234 got, err := h.Update(tt.args.project, tt.args.service, tt.args.sourceCluster, tt.args.targetCluster, tt.args.req) 235 if (err != nil) != tt.wantErr { 236 t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr) 237 return 238 } 239 if !reflect.DeepEqual(got, tt.want) { 240 t.Errorf("Update() got = %v, want %v", got, tt.want) 241 } 242 }) 243 } 244 } 245 246 func TestMirrorMakerReplicationFlowHandler_List(t *testing.T) { 247 c, tearDown := setupMirrormakerReplicationFlowTestCase(t) 248 defer tearDown(t) 249 250 type fields struct { 251 client *Client 252 } 253 type args struct { 254 project string 255 service string 256 } 257 tests := []struct { 258 name string 259 fields fields 260 args args 261 want *MirrorMakerReplicationFlowsResponse 262 wantErr bool 263 }{ 264 { 265 "basic", 266 fields{ 267 client: c, 268 }, 269 args{ 270 project: "test-pr", 271 service: "test-sr", 272 }, 273 &MirrorMakerReplicationFlowsResponse{ 274 APIResponse: APIResponse{ 275 Message: "Completed", 276 Errors: []Error{}, 277 }, 278 ReplicationFlows: []ReplicationFlow{ 279 { 280 Enabled: true, 281 OffsetSyncsTopicLocation: "source", 282 SourceCluster: "kafka-sc", 283 TargetCluster: "kafka-tc", 284 Topics: []string{ 285 ".*", 286 }, 287 TopicsBlacklist: []string{ 288 ".*[\\-\\.]internal", 289 ".*\\.replica", 290 "__.*", 291 }, 292 }, 293 }, 294 }, 295 false, 296 }, 297 } 298 for _, tt := range tests { 299 t.Run(tt.name, func(t *testing.T) { 300 h := &MirrorMakerReplicationFlowHandler{ 301 client: tt.fields.client, 302 } 303 got, err := h.List(tt.args.project, tt.args.service) 304 if (err != nil) != tt.wantErr { 305 t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr) 306 return 307 } 308 if !reflect.DeepEqual(got, tt.want) { 309 t.Errorf("List() got = %v, want %v", got, tt.want) 310 } 311 }) 312 } 313 } 314 315 func TestMirrorMakerReplicationFlowHandler_Delete(t *testing.T) { 316 c, tearDown := setupMirrormakerReplicationFlowTestCase(t) 317 defer tearDown(t) 318 319 type fields struct { 320 client *Client 321 } 322 type args struct { 323 project string 324 service string 325 sourceCluster string 326 targetCluster string 327 } 328 tests := []struct { 329 name string 330 fields fields 331 args args 332 wantErr bool 333 }{ 334 { 335 "basic", 336 fields{ 337 client: c, 338 }, 339 args{ 340 project: "test-pr", 341 service: "test-sr", 342 sourceCluster: "kafka-sc", 343 targetCluster: "kafka-tc", 344 }, 345 false, 346 }, 347 } 348 for _, tt := range tests { 349 t.Run(tt.name, func(t *testing.T) { 350 h := &MirrorMakerReplicationFlowHandler{ 351 client: tt.fields.client, 352 } 353 if err := h.Delete(tt.args.project, tt.args.service, tt.args.sourceCluster, tt.args.targetCluster); (err != nil) != tt.wantErr { 354 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) 355 } 356 }) 357 } 358 } 359 360 func TestMirrorMakerReplicationFlowHandler_Get(t *testing.T) { 361 c, tearDown := setupMirrormakerReplicationFlowTestCase(t) 362 defer tearDown(t) 363 364 type fields struct { 365 client *Client 366 } 367 type args struct { 368 project string 369 service string 370 sourceCluster string 371 targetCluster string 372 } 373 tests := []struct { 374 name string 375 fields fields 376 args args 377 want *MirrorMakerReplicationFlowResponse 378 wantErr bool 379 }{ 380 { 381 "basic", 382 fields{c}, 383 args{ 384 project: "test-pr", 385 service: "test-sr", 386 sourceCluster: "kafka-sc", 387 targetCluster: "kafka-tc", 388 }, 389 &MirrorMakerReplicationFlowResponse{ 390 ReplicationFlow: ReplicationFlow{ 391 Enabled: true, 392 OffsetSyncsTopicLocation: "source", 393 SourceCluster: "kafka-sc", 394 TargetCluster: "kafka-tc", 395 Topics: []string{ 396 ".*", 397 }, 398 TopicsBlacklist: []string{ 399 ".*[\\-\\.]internal", 400 ".*\\.replica", 401 "__.*", 402 }, 403 }, 404 }, 405 false, 406 }, 407 } 408 for _, tt := range tests { 409 t.Run(tt.name, func(t *testing.T) { 410 h := &MirrorMakerReplicationFlowHandler{ 411 client: tt.fields.client, 412 } 413 got, err := h.Get(tt.args.project, tt.args.service, tt.args.sourceCluster, tt.args.targetCluster) 414 if (err != nil) != tt.wantErr { 415 t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) 416 return 417 } 418 if !reflect.DeepEqual(got, tt.want) { 419 t.Errorf("Get() got = %v, want %v", got, tt.want) 420 } 421 }) 422 } 423 }