github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/report_test.go (about) 1 package edgeworkers 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/tools" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestGetSummaryReport(t *testing.T) { 16 tests := map[string]struct { 17 params GetSummaryReportRequest 18 responseStatus int 19 responseBody string 20 expectedPath string 21 expectedResponse *GetSummaryReportResponse 22 withError error 23 }{ 24 "200 OK - get summary overview report": { 25 params: GetSummaryReportRequest{ 26 Start: "2022-01-10T03:00:00Z", 27 End: "2022-01-14T13:22:28Z", 28 EdgeWorker: "1234", 29 Status: tools.StringPtr(StatusSuccess), 30 }, 31 expectedPath: "/edgeworkers/v1/reports/1?edgeWorker=1234&end=2022-01-14T13%3A22%3A28Z&start=2022-01-10T03%3A00%3A00Z&status=success", 32 responseBody: ` 33 { 34 "reportId": 1, 35 "name": "Overall summary", 36 "description": "This report contains an overview of other reports.", 37 "start": "2022-01-10T03:00:00Z", 38 "end": "2022-01-14T13:22:28Z", 39 "data": { 40 "memory": { 41 "avg": 3607.069168, 42 "min": 0.0, 43 "max": 458044.0 44 }, 45 "successes": { 46 "total": 88119 47 }, 48 "initDuration": { 49 "avg": 1.2559162420382166, 50 "min": 0.33, 51 "max": 28.975 52 }, 53 "execDuration": { 54 "avg": 0.11508884576538543, 55 "min": 0.005, 56 "max": 9.415 57 }, 58 "errors": { 59 "total": 0 60 }, 61 "invocations": { 62 "total": 88119 63 } 64 } 65 }`, 66 responseStatus: http.StatusOK, 67 expectedResponse: &GetSummaryReportResponse{ 68 ReportID: 1, 69 Name: "Overall summary", 70 Description: "This report contains an overview of other reports.", 71 Start: "2022-01-10T03:00:00Z", 72 End: "2022-01-14T13:22:28Z", 73 Data: DataSummary{ 74 Memory: &Summary{ 75 Avg: 3607.069168, 76 Min: 0.0, 77 Max: 458044.0, 78 }, 79 Successes: &Total{Total: 88119}, 80 InitDuration: &Summary{ 81 Avg: 1.2559162420382166, 82 Min: 0.33, 83 Max: 28.975, 84 }, 85 ExecDuration: &Summary{ 86 Avg: 0.11508884576538543, 87 Min: 0.005, 88 Max: 9.415, 89 }, 90 Errors: &Total{Total: 0}, 91 Invocations: &Total{Total: 88119}, 92 }, 93 }, 94 }, 95 "missing mandatory params": { 96 params: GetSummaryReportRequest{}, 97 withError: ErrStructValidation, 98 }, 99 "invalid status": { 100 params: GetSummaryReportRequest{ 101 Start: "2021-12-04T00:00:00Z", 102 End: "2022-01-01T00:00:00Z", 103 EdgeWorker: "37017", 104 Status: tools.StringPtr("not_valid"), 105 }, 106 withError: ErrStructValidation, 107 }, 108 "empty status": { 109 params: GetSummaryReportRequest{ 110 Start: "2021-12-04T00:00:00Z", 111 End: "2022-01-01T00:00:00Z", 112 EdgeWorker: "37017", 113 Status: tools.StringPtr(""), 114 }, 115 expectedPath: "/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 116 withError: ErrStructValidation, 117 }, 118 "invalid event handler": { 119 params: GetSummaryReportRequest{ 120 Start: "2021-12-04T00:00:00Z", 121 End: "2022-01-01T00:00:00Z", 122 EdgeWorker: "37017", 123 EventHandler: tools.StringPtr("not_valid"), 124 }, 125 withError: ErrStructValidation, 126 }, 127 "empty event handler": { 128 params: GetSummaryReportRequest{ 129 Start: "2021-12-04T00:00:00Z", 130 End: "2022-01-01T00:00:00Z", 131 EdgeWorker: "37017", 132 EventHandler: tools.StringPtr(""), 133 }, 134 expectedPath: "/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 135 withError: ErrStructValidation, 136 }, 137 "500 internal server error - get group which does not exist": { 138 params: GetSummaryReportRequest{ 139 Start: "2021-12-04T00:00:00Z", 140 End: "2022-01-01T00:00:00Z", 141 EdgeWorker: "37017", 142 }, 143 responseStatus: http.StatusInternalServerError, 144 responseBody: ` 145 { 146 "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 147 "title": "Server Error", 148 "status": 500, 149 "instance": "host_name/edgeworkers/v1/reports/1", 150 "method": "GET", 151 "serverIp": "104.81.220.111", 152 "clientIp": "89.64.55.111", 153 "requestId": "a73affa111", 154 "requestTime": "2022-01-17T00:00:00Z" 155 }`, 156 expectedPath: "/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 157 withError: &Error{ 158 Type: "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 159 Title: "Server Error", 160 Status: 500, 161 Instance: "host_name/edgeworkers/v1/reports/1", 162 Method: "GET", 163 ServerIP: "104.81.220.111", 164 ClientIP: "89.64.55.111", 165 RequestID: "a73affa111", 166 RequestTime: "2022-01-17T00:00:00Z", 167 }, 168 }, 169 "403 Forbidden - incorrect credentials": { 170 params: GetSummaryReportRequest{ 171 Start: "2021-12-04T00:00:00Z", 172 End: "2022-01-01T00:00:00Z", 173 EdgeWorker: "37017", 174 }, 175 responseStatus: http.StatusForbidden, 176 responseBody: ` 177 { 178 "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 179 "title": "Forbidden", 180 "status": 403, 181 "detail": "The client does not have the grant needed for the request", 182 "instance": "host_name/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 183 "authzRealm": "scuomder224df6ct.dkekfr3qqg4dghpj", 184 "method": "GET", 185 "serverIp": "104.81.220.111", 186 "clientIp": "89.64.55.111", 187 "requestId": "a73affa111", 188 "requestTime": "2022-01-10T10:56:32Z" 189 }`, 190 expectedPath: "/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 191 withError: &Error{ 192 Type: "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 193 Title: "Forbidden", 194 Status: 403, 195 Detail: "The client does not have the grant needed for the request", 196 Instance: "host_name/edgeworkers/v1/reports/1?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 197 AuthzRealm: "scuomder224df6ct.dkekfr3qqg4dghpj", 198 Method: "GET", 199 ServerIP: "104.81.220.111", 200 ClientIP: "89.64.55.111", 201 RequestID: "a73affa111", 202 RequestTime: "2022-01-10T10:56:32Z", 203 }, 204 }, 205 } 206 for name, test := range tests { 207 t.Run(name, func(t *testing.T) { 208 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 209 assert.Equal(t, test.expectedPath, r.URL.String()) 210 assert.Equal(t, http.MethodGet, r.Method) 211 w.WriteHeader(test.responseStatus) 212 _, err := w.Write([]byte(test.responseBody)) 213 assert.NoError(t, err) 214 })) 215 client := mockAPIClient(t, mockServer) 216 result, err := client.GetSummaryReport(context.Background(), test.params) 217 if test.withError != nil { 218 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 219 return 220 } 221 require.NoError(t, err) 222 assert.Equal(t, test.expectedResponse, result) 223 }) 224 } 225 } 226 227 func TestGetReport(t *testing.T) { 228 tests := map[string]struct { 229 params GetReportRequest 230 responseStatus int 231 responseBody string 232 expectedPath string 233 expectedResponse *GetReportResponse 234 withError error 235 }{ 236 "200 OK - get report with id 2": { 237 params: GetReportRequest{ 238 ReportID: 2, 239 Start: "2021-12-04T00:00:00Z", 240 End: "2022-01-17T11:28:11Z", 241 EdgeWorker: "37017", 242 }, 243 responseStatus: http.StatusOK, 244 expectedPath: "/edgeworkers/v1/reports/2?edgeWorker=37017&end=2022-01-17T11%3A28%3A11Z&start=2021-12-04T00%3A00%3A00Z", 245 responseBody: `{ 246 "reportId": 2, 247 "name": "Initialization and execution times by EdgeWorker ID and event handler", 248 "description": "This report lists execution and initialization times, grouped by EdgeWorker ID and event handler.", 249 "start": "2022-01-10T03:00:00Z", 250 "end": "2022-01-17T10:43:15Z", 251 "data": [ 252 { 253 "edgeWorkerId": 37017, 254 "data": { 255 "onClientRequest": [ 256 { 257 "startDateTime": "2022-01-10T03:00:00Z", 258 "edgeWorkerVersion": "10.18", 259 "invocations": 8, 260 "execDuration": { 261 "avg": 0.113125, 262 "min": 0.095, 263 "max": 0.148 264 } 265 }, 266 { 267 "startDateTime": "2022-01-10T03:05:00Z", 268 "edgeWorkerVersion": "10.18", 269 "invocations": 15, 270 "execDuration": { 271 "avg": 0.14859999999999998, 272 "min": 0.087, 273 "max": 0.348 274 } 275 } 276 ], 277 "onOriginRequest": [ 278 { 279 "startDateTime": "2022-01-10T03:45:00Z", 280 "edgeWorkerVersion": "10.18", 281 "invocations": 1, 282 "execDuration": { 283 "avg": 0.0, 284 "min": 0.0, 285 "max": 0.0 286 } 287 }, 288 { 289 "startDateTime": "2022-01-10T06:00:00Z", 290 "edgeWorkerVersion": "10.18", 291 "invocations": 1, 292 "execDuration": { 293 "avg": 0.0, 294 "min": 0.0, 295 "max": 0.0 296 } 297 } 298 ], 299 "onOriginResponse": [ 300 { 301 "startDateTime": "2022-01-10T03:45:00Z", 302 "edgeWorkerVersion": "10.18", 303 "invocations": 1, 304 "execDuration": { 305 "avg": 0.0, 306 "min": 0.0, 307 "max": 0.0 308 } 309 }, 310 { 311 "startDateTime": "2022-01-10T06:00:00Z", 312 "edgeWorkerVersion": "10.18", 313 "invocations": 1, 314 "execDuration": { 315 "avg": 0.0, 316 "min": 0.0, 317 "max": 0.0 318 } 319 } 320 ], 321 "onClientResponse": [ 322 { 323 "startDateTime": "2022-01-10T03:00:00Z", 324 "edgeWorkerVersion": "10.18", 325 "invocations": 8, 326 "execDuration": { 327 "avg": 0.029625, 328 "min": 0.018, 329 "max": 0.059 330 } 331 }, 332 { 333 "startDateTime": "2022-01-10T03:05:00Z", 334 "edgeWorkerVersion": "10.18", 335 "invocations": 15, 336 "execDuration": { 337 "avg": 0.031933333, 338 "min": 0.018, 339 "max": 0.062 340 } 341 } 342 ], 343 "responseProvider": [ 344 { 345 "startDateTime": "2022-01-10T03:45:00Z", 346 "edgeWorkerVersion": "10.18", 347 "invocations": 1, 348 "execDuration": { 349 "avg": 0.0, 350 "min": 0.0, 351 "max": 0.0 352 } 353 }, 354 { 355 "startDateTime": "2022-01-10T06:00:00Z", 356 "edgeWorkerVersion": "10.18", 357 "invocations": 1, 358 "execDuration": { 359 "avg": 0.0, 360 "min": 0.0, 361 "max": 0.0 362 } 363 } 364 ], 365 "init": [ 366 { 367 "startDateTime": "2022-01-10T03:05:00Z", 368 "edgeWorkerVersion": "10.18", 369 "invocations": 7, 370 "initDuration": { 371 "avg": 1.03, 372 "min": 1.03, 373 "max": 1.03 374 } 375 }, 376 { 377 "startDateTime": "2022-01-10T05:10:00Z", 378 "edgeWorkerVersion": "10.18", 379 "invocations": 1, 380 "initDuration": { 381 "avg": 0.729, 382 "min": 0.729, 383 "max": 0.729 384 } 385 } 386 ] 387 } 388 } 389 ] 390 }`, 391 expectedResponse: &GetReportResponse{ 392 ReportID: 2, 393 Name: "Initialization and execution times by EdgeWorker ID and event handler", 394 Description: "This report lists execution and initialization times, grouped by EdgeWorker ID and event handler.", 395 Start: "2022-01-10T03:00:00Z", 396 End: "2022-01-17T10:43:15Z", 397 Data: []ReportData{ 398 { 399 EdgeWorkerID: 37017, 400 Data: Data{ 401 OnClientRequest: &OnClientRequest{ 402 { 403 StartDateTime: "2022-01-10T03:00:00Z", 404 EdgeWorkerVersion: "10.18", 405 ExecDuration: &Summary{ 406 Avg: 0.113125, 407 Min: 0.095, 408 Max: 0.148, 409 }, 410 Invocations: 8, 411 }, 412 { 413 StartDateTime: "2022-01-10T03:05:00Z", 414 EdgeWorkerVersion: "10.18", 415 ExecDuration: &Summary{ 416 Avg: 0.14859999999999998, 417 Min: 0.087, 418 Max: 0.348, 419 }, 420 Invocations: 15, 421 }, 422 }, 423 OnOriginRequest: &OnOriginRequest{ 424 { 425 StartDateTime: "2022-01-10T03:45:00Z", 426 EdgeWorkerVersion: "10.18", 427 ExecDuration: &Summary{ 428 Avg: 0.0, 429 Min: 0.0, 430 Max: 0.0, 431 }, 432 Invocations: 1, 433 }, 434 { 435 StartDateTime: "2022-01-10T06:00:00Z", 436 EdgeWorkerVersion: "10.18", 437 ExecDuration: &Summary{ 438 Avg: 0.0, 439 Min: 0.0, 440 Max: 0.0, 441 }, 442 Invocations: 1, 443 }, 444 }, 445 OnOriginResponse: &OnOriginResponse{ 446 { 447 StartDateTime: "2022-01-10T03:45:00Z", 448 EdgeWorkerVersion: "10.18", 449 ExecDuration: &Summary{ 450 Avg: 0.0, 451 Min: 0.0, 452 Max: 0.0, 453 }, 454 Invocations: 1, 455 }, 456 { 457 StartDateTime: "2022-01-10T06:00:00Z", 458 EdgeWorkerVersion: "10.18", 459 ExecDuration: &Summary{ 460 Avg: 0.0, 461 Min: 0.0, 462 Max: 0.0, 463 }, 464 Invocations: 1, 465 }, 466 }, 467 OnClientResponse: &OnClientResponse{ 468 { 469 StartDateTime: "2022-01-10T03:00:00Z", 470 EdgeWorkerVersion: "10.18", 471 ExecDuration: &Summary{ 472 Avg: 0.029625, 473 Min: 0.018, 474 Max: 0.059, 475 }, 476 Invocations: 8, 477 }, 478 { 479 StartDateTime: "2022-01-10T03:05:00Z", 480 EdgeWorkerVersion: "10.18", 481 ExecDuration: &Summary{ 482 Avg: 0.031933333, 483 Min: 0.018, 484 Max: 0.062, 485 }, 486 Invocations: 15, 487 }, 488 }, 489 ResponseProvider: &ResponseProvider{ 490 { 491 StartDateTime: "2022-01-10T03:45:00Z", 492 EdgeWorkerVersion: "10.18", 493 ExecDuration: &Summary{ 494 Avg: 0.0, 495 Min: 0.0, 496 Max: 0.0, 497 }, 498 Invocations: 1, 499 }, 500 { 501 StartDateTime: "2022-01-10T06:00:00Z", 502 EdgeWorkerVersion: "10.18", 503 ExecDuration: &Summary{ 504 Avg: 0.0, 505 Min: 0.0, 506 Max: 0.0, 507 }, 508 Invocations: 1, 509 }, 510 }, 511 Init: &Init{ 512 { 513 StartDateTime: "2022-01-10T03:05:00Z", 514 EdgeWorkerVersion: "10.18", 515 Invocations: 7, 516 InitDuration: Summary{ 517 Avg: 1.03, 518 Min: 1.03, 519 Max: 1.03, 520 }, 521 }, 522 { 523 StartDateTime: "2022-01-10T05:10:00Z", 524 EdgeWorkerVersion: "10.18", 525 Invocations: 1, 526 InitDuration: Summary{ 527 Avg: 0.729, 528 Min: 0.729, 529 Max: 0.729, 530 }, 531 }, 532 }, 533 }, 534 }, 535 }, 536 }, 537 }, 538 "200 OK - get report with id 3": { 539 params: GetReportRequest{ 540 ReportID: 3, 541 Start: "2021-12-04T00:00:00Z", 542 End: "2022-01-17T11:28:11Z", 543 EdgeWorker: "37017", 544 }, 545 responseStatus: http.StatusOK, 546 expectedPath: "/edgeworkers/v1/reports/3?edgeWorker=37017&end=2022-01-17T11%3A28%3A11Z&start=2021-12-04T00%3A00%3A00Z", 547 responseBody: ` 548 { 549 "reportId": 3, 550 "name": "Execution statuses by EdgeWorker ID and event handler", 551 "description": "This report lists execution statuses, grouped by EdgeWorker ID and event handler.", 552 "start": "2022-01-10T03:00:00Z", 553 "end": "2022-01-17T11:28:11Z", 554 "data": [ 555 { 556 "edgeWorkerId": 37017, 557 "data": { 558 "responseProvider": [ 559 { 560 "startDateTime": "2022-01-10T03:45:00Z", 561 "edgeWorkerVersion": "10.18", 562 "invocations": 1, 563 "status": "unimplementedEventHandler" 564 }, 565 { 566 "startDateTime": "2022-01-10T06:00:00Z", 567 "edgeWorkerVersion": "10.18", 568 "invocations": 1, 569 "status": "unimplementedEventHandler" 570 } 571 ], 572 "onOriginRequest": [ 573 { 574 "startDateTime": "2022-01-10T03:45:00Z", 575 "edgeWorkerVersion": "10.18", 576 "invocations": 1, 577 "status": "unimplementedEventHandler" 578 }, 579 { 580 "startDateTime": "2022-01-10T06:00:00Z", 581 "edgeWorkerVersion": "10.18", 582 "invocations": 1, 583 "status": "unimplementedEventHandler" 584 } 585 ], 586 "onClientResponse": [ 587 { 588 "startDateTime": "2022-01-10T03:00:00Z", 589 "edgeWorkerVersion": "10.18", 590 "invocations": 8, 591 "status": "success" 592 }, 593 { 594 "startDateTime": "2022-01-10T03:05:00Z", 595 "edgeWorkerVersion": "10.18", 596 "invocations": 15, 597 "status": "success" 598 } 599 ], 600 "onOriginResponse": [ 601 { 602 "startDateTime": "2022-01-10T03:45:00Z", 603 "edgeWorkerVersion": "10.18", 604 "invocations": 1, 605 "status": "unimplementedEventHandler" 606 }, 607 { 608 "startDateTime": "2022-01-10T06:00:00Z", 609 "edgeWorkerVersion": "10.18", 610 "invocations": 1, 611 "status": "unimplementedEventHandler" 612 } 613 ], 614 "onClientRequest": [ 615 { 616 "startDateTime": "2022-01-10T03:00:00Z", 617 "edgeWorkerVersion": "10.18", 618 "invocations": 8, 619 "status": "success" 620 }, 621 { 622 "startDateTime": "2022-01-10T03:05:00Z", 623 "edgeWorkerVersion": "10.18", 624 "invocations": 15, 625 "status": "success" 626 } 627 ] 628 } 629 } 630 ] 631 }`, 632 expectedResponse: &GetReportResponse{ 633 ReportID: 3, 634 Name: "Execution statuses by EdgeWorker ID and event handler", 635 Description: "This report lists execution statuses, grouped by EdgeWorker ID and event handler.", 636 Start: "2022-01-10T03:00:00Z", 637 End: "2022-01-17T11:28:11Z", 638 Data: []ReportData{ 639 { 640 EdgeWorkerID: 37017, 641 Data: Data{ 642 ResponseProvider: &ResponseProvider{ 643 { 644 StartDateTime: "2022-01-10T03:45:00Z", 645 EdgeWorkerVersion: "10.18", 646 Invocations: 1, 647 Status: tools.StringPtr("unimplementedEventHandler"), 648 }, 649 { 650 StartDateTime: "2022-01-10T06:00:00Z", 651 EdgeWorkerVersion: "10.18", 652 Invocations: 1, 653 Status: tools.StringPtr("unimplementedEventHandler"), 654 }, 655 }, 656 OnOriginRequest: &OnOriginRequest{ 657 { 658 StartDateTime: "2022-01-10T03:45:00Z", 659 EdgeWorkerVersion: "10.18", 660 Invocations: 1, 661 Status: tools.StringPtr("unimplementedEventHandler"), 662 }, 663 { 664 StartDateTime: "2022-01-10T06:00:00Z", 665 EdgeWorkerVersion: "10.18", 666 Invocations: 1, 667 Status: tools.StringPtr("unimplementedEventHandler"), 668 }, 669 }, 670 OnClientResponse: &OnClientResponse{ 671 { 672 StartDateTime: "2022-01-10T03:00:00Z", 673 EdgeWorkerVersion: "10.18", 674 Invocations: 8, 675 Status: tools.StringPtr("success"), 676 }, 677 { 678 StartDateTime: "2022-01-10T03:05:00Z", 679 EdgeWorkerVersion: "10.18", 680 Invocations: 15, 681 Status: tools.StringPtr("success"), 682 }, 683 }, 684 OnOriginResponse: &OnOriginResponse{ 685 { 686 StartDateTime: "2022-01-10T03:45:00Z", 687 EdgeWorkerVersion: "10.18", 688 Invocations: 1, 689 Status: tools.StringPtr("unimplementedEventHandler"), 690 }, 691 { 692 StartDateTime: "2022-01-10T06:00:00Z", 693 EdgeWorkerVersion: "10.18", 694 Invocations: 1, 695 Status: tools.StringPtr("unimplementedEventHandler"), 696 }, 697 }, 698 OnClientRequest: &OnClientRequest{ 699 { 700 StartDateTime: "2022-01-10T03:00:00Z", 701 EdgeWorkerVersion: "10.18", 702 Invocations: 8, 703 Status: tools.StringPtr("success"), 704 }, 705 { 706 StartDateTime: "2022-01-10T03:05:00Z", 707 EdgeWorkerVersion: "10.18", 708 Invocations: 15, 709 Status: tools.StringPtr("success"), 710 }, 711 }, 712 }, 713 }, 714 }, 715 }, 716 }, 717 "200 OK - get report with id 4": { 718 params: GetReportRequest{ 719 ReportID: 4, 720 Start: "2022-01-10T03:00:00Z", 721 End: "2022-01-17T12:07:48Z", 722 EdgeWorker: "37017", 723 }, 724 responseStatus: http.StatusOK, 725 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-17T12%3A07%3A48Z&start=2022-01-10T03%3A00%3A00Z", 726 responseBody: ` 727 { 728 "reportId": 4, 729 "name": "Memory usage by EdgeWorker ID and event handler", 730 "description": "This report lists memory usage, grouped by EdgeWorker ID and event handler.", 731 "start": "2022-01-10T03:00:00Z", 732 "end": "2022-01-17T12:07:48Z", 733 "data": [ 734 { 735 "edgeWorkerId": 37017, 736 "data": { 737 "responseProvider": [ 738 { 739 "startDateTime": "2022-01-10T03:45:00Z", 740 "edgeWorkerVersion": "10.18", 741 "invocations": 1, 742 "memory": { 743 "avg": 0.0, 744 "min": 0.0, 745 "max": 0.0 746 } 747 }, 748 { 749 "startDateTime": "2022-01-10T06:00:00Z", 750 "edgeWorkerVersion": "10.18", 751 "invocations": 1, 752 "memory": { 753 "avg": 0.0, 754 "min": 0.0, 755 "max": 0.0 756 } 757 } 758 ], 759 "onOriginRequest": [ 760 { 761 "startDateTime": "2022-01-10T03:45:00Z", 762 "edgeWorkerVersion": "10.18", 763 "invocations": 1, 764 "memory": { 765 "avg": 0.0, 766 "min": 0.0, 767 "max": 0.0 768 } 769 }, 770 { 771 "startDateTime": "2022-01-10T06:00:00Z", 772 "edgeWorkerVersion": "10.18", 773 "invocations": 1, 774 "memory": { 775 "avg": 0.0, 776 "min": 0.0, 777 "max": 0.0 778 } 779 } 780 ], 781 "onClientResponse": [ 782 { 783 "startDateTime": "2022-01-10T03:00:00Z", 784 "edgeWorkerVersion": "10.18", 785 "invocations": 8, 786 "memory": { 787 "avg": 0.0, 788 "min": 0.0, 789 "max": 0.0 790 } 791 }, 792 { 793 "startDateTime": "2022-01-10T03:05:00Z", 794 "edgeWorkerVersion": "10.18", 795 "invocations": 15, 796 "memory": { 797 "avg": 0.0, 798 "min": 0.0, 799 "max": 0.0 800 } 801 } 802 ], 803 "onOriginResponse": [ 804 { 805 "startDateTime": "2022-01-10T03:45:00Z", 806 "edgeWorkerVersion": "10.18", 807 "invocations": 1, 808 "memory": { 809 "avg": 0.0, 810 "min": 0.0, 811 "max": 0.0 812 } 813 }, 814 { 815 "startDateTime": "2022-01-10T06:00:00Z", 816 "edgeWorkerVersion": "10.18", 817 "invocations": 1, 818 "memory": { 819 "avg": 0.0, 820 "min": 0.0, 821 "max": 0.0 822 } 823 } 824 ], 825 "onClientRequest": [ 826 { 827 "startDateTime": "2022-01-10T03:00:00Z", 828 "edgeWorkerVersion": "10.18", 829 "invocations": 8, 830 "memory": { 831 "avg": 1480.0, 832 "min": 1256.0, 833 "max": 1704.0 834 } 835 }, 836 { 837 "startDateTime": "2022-01-10T03:05:00Z", 838 "edgeWorkerVersion": "10.18", 839 "invocations": 15, 840 "memory": { 841 "avg": 1608.266667, 842 "min": 1256.0, 843 "max": 2544.0 844 } 845 } 846 ] 847 } 848 } 849 ] 850 }`, 851 expectedResponse: &GetReportResponse{ 852 ReportID: 4, 853 Name: "Memory usage by EdgeWorker ID and event handler", 854 Description: "This report lists memory usage, grouped by EdgeWorker ID and event handler.", 855 Start: "2022-01-10T03:00:00Z", 856 End: "2022-01-17T12:07:48Z", 857 Data: []ReportData{ 858 { 859 EdgeWorkerID: 37017, 860 Data: Data{ 861 ResponseProvider: &ResponseProvider{ 862 { 863 StartDateTime: "2022-01-10T03:45:00Z", 864 EdgeWorkerVersion: "10.18", 865 Invocations: 1, 866 Memory: &Summary{ 867 Avg: 0.0, 868 Min: 0.0, 869 Max: 0.0, 870 }, 871 }, 872 { 873 StartDateTime: "2022-01-10T06:00:00Z", 874 EdgeWorkerVersion: "10.18", 875 Invocations: 1, 876 Memory: &Summary{ 877 Avg: 0.0, 878 Min: 0.0, 879 Max: 0.0, 880 }, 881 }, 882 }, 883 OnOriginRequest: &OnOriginRequest{ 884 { 885 StartDateTime: "2022-01-10T03:45:00Z", 886 EdgeWorkerVersion: "10.18", 887 Invocations: 1, 888 Memory: &Summary{ 889 Avg: 0.0, 890 Min: 0.0, 891 Max: 0.0, 892 }, 893 }, 894 { 895 StartDateTime: "2022-01-10T06:00:00Z", 896 EdgeWorkerVersion: "10.18", 897 Invocations: 1, 898 Memory: &Summary{ 899 Avg: 0.0, 900 Min: 0.0, 901 Max: 0.0, 902 }, 903 }, 904 }, 905 OnClientResponse: &OnClientResponse{ 906 { 907 StartDateTime: "2022-01-10T03:00:00Z", 908 EdgeWorkerVersion: "10.18", 909 Invocations: 8, 910 Memory: &Summary{ 911 Avg: 0.0, 912 Min: 0.0, 913 Max: 0.0, 914 }, 915 }, 916 { 917 StartDateTime: "2022-01-10T03:05:00Z", 918 EdgeWorkerVersion: "10.18", 919 Invocations: 15, 920 Memory: &Summary{ 921 Avg: 0.0, 922 Min: 0.0, 923 Max: 0.0, 924 }, 925 }, 926 }, 927 OnOriginResponse: &OnOriginResponse{ 928 { 929 StartDateTime: "2022-01-10T03:45:00Z", 930 EdgeWorkerVersion: "10.18", 931 Invocations: 1, 932 Memory: &Summary{ 933 Avg: 0.0, 934 Min: 0.0, 935 Max: 0.0, 936 }, 937 }, 938 { 939 StartDateTime: "2022-01-10T06:00:00Z", 940 EdgeWorkerVersion: "10.18", 941 Invocations: 1, 942 Memory: &Summary{ 943 Avg: 0.0, 944 Min: 0.0, 945 Max: 0.0, 946 }, 947 }, 948 }, 949 OnClientRequest: &OnClientRequest{ 950 { 951 StartDateTime: "2022-01-10T03:00:00Z", 952 EdgeWorkerVersion: "10.18", 953 Invocations: 8, 954 Memory: &Summary{ 955 Avg: 1480.0, 956 Min: 1256.0, 957 Max: 1704.0, 958 }, 959 }, 960 { 961 StartDateTime: "2022-01-10T03:05:00Z", 962 EdgeWorkerVersion: "10.18", 963 Invocations: 15, 964 Memory: &Summary{ 965 Avg: 1608.266667, 966 Min: 1256.0, 967 Max: 2544.0, 968 }, 969 }, 970 }, 971 }, 972 }, 973 }, 974 }, 975 }, 976 "200 OK - get report without data": { 977 params: GetReportRequest{ 978 ReportID: 4, 979 Start: "2021-12-04T00:00:00Z", 980 End: "2022-01-01T00:00:00Z", 981 EdgeWorker: "37017", 982 Status: tools.StringPtr(StatusSuccess), 983 EventHandler: tools.StringPtr(EventHandlerOnClientRequest), 984 }, 985 responseStatus: http.StatusOK, 986 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&eventHandler=onClientRequest&start=2021-12-04T00%3A00%3A00Z&status=success", 987 responseBody: `{ 988 "reportId": 4, 989 "name": "Memory usage by EdgeWorker ID and event handler", 990 "description": "This report lists memory usage, grouped by EdgeWorker ID and event handler.", 991 "start": "2021-12-04T00:00:00Z", 992 "end": "2022-01-01T00:00:00Z", 993 "data": [] 994 }`, 995 expectedResponse: &GetReportResponse{ 996 ReportID: 4, 997 Name: "Memory usage by EdgeWorker ID and event handler", 998 Description: "This report lists memory usage, grouped by EdgeWorker ID and event handler.", 999 Start: "2021-12-04T00:00:00Z", 1000 End: "2022-01-01T00:00:00Z", 1001 Data: []ReportData{}, 1002 }, 1003 }, 1004 "missing mandatory params": { 1005 params: GetReportRequest{}, 1006 withError: ErrStructValidation, 1007 }, 1008 "invalid status": { 1009 params: GetReportRequest{ 1010 ReportID: 4, 1011 Start: "2021-12-04T00:00:00Z", 1012 End: "2022-01-01T00:00:00Z", 1013 EdgeWorker: "37017", 1014 Status: tools.StringPtr("not_valid"), 1015 }, 1016 withError: ErrStructValidation, 1017 }, 1018 "empty status": { 1019 params: GetReportRequest{ 1020 ReportID: 4, 1021 Start: "2021-12-04T00:00:00Z", 1022 End: "2022-01-01T00:00:00Z", 1023 EdgeWorker: "37017", 1024 Status: tools.StringPtr(""), 1025 }, 1026 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1027 withError: ErrStructValidation, 1028 }, 1029 "invalid event handler": { 1030 params: GetReportRequest{ 1031 ReportID: 4, 1032 Start: "2021-12-04T00:00:00Z", 1033 End: "2022-01-01T00:00:00Z", 1034 EdgeWorker: "37017", 1035 EventHandler: tools.StringPtr("not_valid"), 1036 }, 1037 withError: ErrStructValidation, 1038 }, 1039 "empty event handler": { 1040 params: GetReportRequest{ 1041 ReportID: 4, 1042 Start: "2021-12-04T00:00:00Z", 1043 End: "2022-01-01T00:00:00Z", 1044 EdgeWorker: "37017", 1045 EventHandler: tools.StringPtr(""), 1046 }, 1047 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1048 withError: ErrStructValidation, 1049 }, 1050 "500 internal server error - get group which does not exist": { 1051 params: GetReportRequest{ 1052 ReportID: 4, 1053 Start: "2021-12-04T00:00:00Z", 1054 End: "2022-01-01T00:00:00Z", 1055 EdgeWorker: "37017", 1056 }, 1057 responseStatus: http.StatusInternalServerError, 1058 responseBody: ` 1059 { 1060 "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 1061 "title": "Server Error", 1062 "status": 500, 1063 "instance": "host_name/edgeworkers/v1/reports/4", 1064 "method": "GET", 1065 "serverIp": "104.81.220.111", 1066 "clientIp": "89.64.55.111", 1067 "requestId": "a73affa111", 1068 "requestTime": "2022-01-01T00:00:00Z" 1069 }`, 1070 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1071 withError: &Error{ 1072 Type: "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 1073 Title: "Server Error", 1074 Status: 500, 1075 Instance: "host_name/edgeworkers/v1/reports/4", 1076 Method: "GET", 1077 ServerIP: "104.81.220.111", 1078 ClientIP: "89.64.55.111", 1079 RequestID: "a73affa111", 1080 RequestTime: "2022-01-01T00:00:00Z", 1081 }, 1082 }, 1083 "403 Forbidden - incorrect credentials": { 1084 params: GetReportRequest{ 1085 ReportID: 4, 1086 Start: "2021-12-04T00:00:00Z", 1087 End: "2022-01-01T00:00:00Z", 1088 EdgeWorker: "37017", 1089 }, 1090 responseStatus: http.StatusForbidden, 1091 responseBody: ` 1092 { 1093 "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 1094 "title": "Forbidden", 1095 "status": 403, 1096 "detail": "The client does not have the grant needed for the request", 1097 "instance": "host_name/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1098 "authzRealm": "scuomder224df6ct.dkekfr3qqg4dghpj", 1099 "method": "GET", 1100 "serverIp": "104.81.220.111", 1101 "clientIp": "89.64.55.111", 1102 "requestId": "a73affa111", 1103 "requestTime": "2022-01-10T10:56:32Z" 1104 }`, 1105 expectedPath: "/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1106 withError: &Error{ 1107 Type: "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 1108 Title: "Forbidden", 1109 Status: 403, 1110 Detail: "The client does not have the grant needed for the request", 1111 Instance: "host_name/edgeworkers/v1/reports/4?edgeWorker=37017&end=2022-01-01T00%3A00%3A00Z&start=2021-12-04T00%3A00%3A00Z", 1112 AuthzRealm: "scuomder224df6ct.dkekfr3qqg4dghpj", 1113 Method: "GET", 1114 ServerIP: "104.81.220.111", 1115 ClientIP: "89.64.55.111", 1116 RequestID: "a73affa111", 1117 RequestTime: "2022-01-10T10:56:32Z", 1118 }, 1119 }, 1120 } 1121 for name, test := range tests { 1122 t.Run(name, func(t *testing.T) { 1123 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1124 assert.Equal(t, test.expectedPath, r.URL.String()) 1125 assert.Equal(t, http.MethodGet, r.Method) 1126 w.WriteHeader(test.responseStatus) 1127 _, err := w.Write([]byte(test.responseBody)) 1128 assert.NoError(t, err) 1129 })) 1130 client := mockAPIClient(t, mockServer) 1131 result, err := client.GetReport(context.Background(), test.params) 1132 if test.withError != nil { 1133 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 1134 return 1135 } 1136 require.NoError(t, err) 1137 assert.Equal(t, test.expectedResponse, result) 1138 }) 1139 } 1140 } 1141 1142 func TestListReports(t *testing.T) { 1143 tests := map[string]struct { 1144 responseStatus int 1145 responseBody string 1146 expectedPath string 1147 expectedResponse *ListReportsResponse 1148 withError error 1149 }{ 1150 "200 OK - list EdgeWorker reports": { 1151 responseStatus: http.StatusOK, 1152 responseBody: ` 1153 { 1154 "reports": [ 1155 { 1156 "reportId": 1, 1157 "name": "Overall summary", 1158 "description": "This report contains an overview of other reports." 1159 }, 1160 { 1161 "reportId": 2, 1162 "name": "Initialization and execution times by EdgeWorker ID and event handler", 1163 "description": "This report lists execution and initialization times, grouped by EdgeWorker ID and event handler." 1164 }, 1165 { 1166 "reportId": 3, 1167 "name": "Execution statuses by EdgeWorker ID and event handler", 1168 "description": "This report lists execution statuses, grouped by EdgeWorker ID and event handler." 1169 }, 1170 { 1171 "reportId": 4, 1172 "name": "Memory usage by EdgeWorker ID and event handler", 1173 "description": "This report lists memory usage, grouped by EdgeWorker ID and event handler." 1174 } 1175 ] 1176 }`, 1177 expectedPath: "/edgeworkers/v1/reports", 1178 expectedResponse: &ListReportsResponse{[]ReportResponse{ 1179 { 1180 ReportID: 1, 1181 Name: "Overall summary", 1182 Description: "This report contains an overview of other reports.", 1183 }, 1184 { 1185 ReportID: 2, 1186 Name: "Initialization and execution times by EdgeWorker ID and event handler", 1187 Description: "This report lists execution and initialization times, grouped by EdgeWorker ID and event handler.", 1188 }, 1189 { 1190 ReportID: 3, 1191 Name: "Execution statuses by EdgeWorker ID and event handler", 1192 Description: "This report lists execution statuses, grouped by EdgeWorker ID and event handler.", 1193 }, 1194 { 1195 ReportID: 4, 1196 Name: "Memory usage by EdgeWorker ID and event handler", 1197 Description: "This report lists memory usage, grouped by EdgeWorker ID and event handler.", 1198 }, 1199 }}, 1200 }, 1201 "403 Forbidden - incorrect credentials": { 1202 responseStatus: http.StatusForbidden, 1203 responseBody: ` 1204 { 1205 "type": "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 1206 "title": "Forbidden", 1207 "status": 403, 1208 "detail": "The client does not have the grant needed for the request", 1209 "instance": "host_name/edgeworkers/v1/reports", 1210 "authzRealm": "scuomder224df6ct.dkekfr3qqg4dghpj", 1211 "method": "GET", 1212 "serverIp": "104.81.220.111", 1213 "clientIp": "89.64.55.111", 1214 "requestId": "a73affa111", 1215 "requestTime": "2022-01-10T12:31:29Z" 1216 }`, 1217 expectedPath: "/edgeworkers/v1/reports", 1218 withError: &Error{ 1219 Type: "https://problems.luna-dev.akamaiapis.net/-/pep-authz/deny", 1220 Title: "Forbidden", 1221 Status: 403, 1222 Detail: "The client does not have the grant needed for the request", 1223 Instance: "host_name/edgeworkers/v1/reports", 1224 AuthzRealm: "scuomder224df6ct.dkekfr3qqg4dghpj", 1225 Method: "GET", 1226 ServerIP: "104.81.220.111", 1227 ClientIP: "89.64.55.111", 1228 RequestID: "a73affa111", 1229 RequestTime: "2022-01-10T12:31:29Z", 1230 }, 1231 }, 1232 "500 internal server error": { 1233 responseStatus: http.StatusInternalServerError, 1234 responseBody: ` 1235 { 1236 "type": "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 1237 "title": "Server Error", 1238 "status": 500, 1239 "instance": "host_name/edgeworkers/v1/reports", 1240 "method": "GET", 1241 "serverIp": "104.81.220.111", 1242 "clientIp": "89.64.55.111", 1243 "requestId": "a73affa111", 1244 "requestTime": "2022-01-10T12:31:29Z" 1245 }`, 1246 expectedPath: "/edgeworkers/v1/reports", 1247 withError: &Error{ 1248 Type: "https://problems.luna-dev.akamaiapis.net/-/resource-impl/forward-origin-error", 1249 Title: "Server Error", 1250 Status: 500, 1251 Instance: "host_name/edgeworkers/v1/reports", 1252 Method: "GET", 1253 ServerIP: "104.81.220.111", 1254 ClientIP: "89.64.55.111", 1255 RequestID: "a73affa111", 1256 RequestTime: "2022-01-10T12:31:29Z", 1257 }, 1258 }, 1259 } 1260 1261 for name, test := range tests { 1262 t.Run(name, func(t *testing.T) { 1263 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 1264 assert.Equal(t, test.expectedPath, r.URL.String()) 1265 assert.Equal(t, http.MethodGet, r.Method) 1266 w.WriteHeader(test.responseStatus) 1267 _, err := w.Write([]byte(test.responseBody)) 1268 assert.NoError(t, err) 1269 })) 1270 client := mockAPIClient(t, mockServer) 1271 result, err := client.ListReports(context.Background()) 1272 if test.withError != nil { 1273 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 1274 return 1275 } 1276 require.NoError(t, err) 1277 assert.Equal(t, test.expectedResponse, result) 1278 }) 1279 } 1280 }