1/* 2Copyright 2014 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package validation 18 19import ( 20 "testing" 21 "time" 22 23 "k8s.io/api/core/v1" 24 eventsv1 "k8s.io/api/events/v1" 25 eventsv1beta1 "k8s.io/api/events/v1beta1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/kubernetes/pkg/apis/core" 28) 29 30func TestValidateEventForCoreV1Events(t *testing.T) { 31 table := []struct { 32 *core.Event 33 valid bool 34 }{ 35 { 36 &core.Event{ 37 ObjectMeta: metav1.ObjectMeta{ 38 Name: "test1", 39 Namespace: "foo", 40 }, 41 InvolvedObject: core.ObjectReference{ 42 Namespace: "bar", 43 Kind: "Pod", 44 }, 45 }, 46 false, 47 }, { 48 &core.Event{ 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: "test2", 51 Namespace: "aoeu-_-aoeu", 52 }, 53 InvolvedObject: core.ObjectReference{ 54 Namespace: "aoeu-_-aoeu", 55 Kind: "Pod", 56 }, 57 }, 58 false, 59 }, { 60 &core.Event{ 61 ObjectMeta: metav1.ObjectMeta{ 62 Name: "test3", 63 Namespace: metav1.NamespaceDefault, 64 }, 65 InvolvedObject: core.ObjectReference{ 66 APIVersion: "v1", 67 Kind: "Node", 68 }, 69 }, 70 true, 71 }, { 72 &core.Event{ 73 ObjectMeta: metav1.ObjectMeta{ 74 Name: "test4", 75 Namespace: metav1.NamespaceDefault, 76 }, 77 InvolvedObject: core.ObjectReference{ 78 APIVersion: "v1", 79 Kind: "Namespace", 80 }, 81 }, 82 true, 83 }, { 84 &core.Event{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: "test5", 87 Namespace: metav1.NamespaceDefault, 88 }, 89 InvolvedObject: core.ObjectReference{ 90 APIVersion: "apps/v1", 91 Kind: "NoKind", 92 Namespace: metav1.NamespaceDefault, 93 }, 94 }, 95 true, 96 }, { 97 &core.Event{ 98 ObjectMeta: metav1.ObjectMeta{ 99 Name: "test6", 100 Namespace: metav1.NamespaceDefault, 101 }, 102 InvolvedObject: core.ObjectReference{ 103 APIVersion: "batch/v1", 104 Kind: "Job", 105 Namespace: "foo", 106 }, 107 }, 108 false, 109 }, { 110 &core.Event{ 111 ObjectMeta: metav1.ObjectMeta{ 112 Name: "test7", 113 Namespace: metav1.NamespaceDefault, 114 }, 115 InvolvedObject: core.ObjectReference{ 116 APIVersion: "batch/v1", 117 Kind: "Job", 118 Namespace: metav1.NamespaceDefault, 119 }, 120 }, 121 true, 122 }, { 123 &core.Event{ 124 ObjectMeta: metav1.ObjectMeta{ 125 Name: "test8", 126 Namespace: metav1.NamespaceDefault, 127 }, 128 InvolvedObject: core.ObjectReference{ 129 APIVersion: "other/v1beta1", 130 Kind: "Job", 131 Namespace: "foo", 132 }, 133 }, 134 false, 135 }, { 136 &core.Event{ 137 ObjectMeta: metav1.ObjectMeta{ 138 Name: "test9", 139 Namespace: "foo", 140 }, 141 InvolvedObject: core.ObjectReference{ 142 APIVersion: "other/v1beta1", 143 Kind: "Job", 144 Namespace: "foo", 145 }, 146 }, 147 true, 148 }, { 149 &core.Event{ 150 ObjectMeta: metav1.ObjectMeta{ 151 Name: "test10", 152 Namespace: metav1.NamespaceDefault, 153 }, 154 InvolvedObject: core.ObjectReference{ 155 APIVersion: "batch", 156 Kind: "Job", 157 Namespace: "foo", 158 }, 159 }, 160 false, 161 }, { 162 &core.Event{ 163 ObjectMeta: metav1.ObjectMeta{ 164 Name: "test11", 165 Namespace: "foo", 166 }, 167 InvolvedObject: core.ObjectReference{ 168 APIVersion: "batch/v1", 169 Kind: "Job", 170 Namespace: "foo", 171 }, 172 }, 173 true, 174 }, 175 { 176 &core.Event{ 177 ObjectMeta: metav1.ObjectMeta{ 178 Name: "test12", 179 Namespace: "foo", 180 }, 181 InvolvedObject: core.ObjectReference{ 182 APIVersion: "other/v1beta1", 183 Kind: "FooBar", 184 Namespace: "bar", 185 }, 186 }, 187 false, 188 }, 189 { 190 &core.Event{ 191 ObjectMeta: metav1.ObjectMeta{ 192 Name: "test13", 193 Namespace: "", 194 }, 195 InvolvedObject: core.ObjectReference{ 196 APIVersion: "other/v1beta1", 197 Kind: "FooBar", 198 Namespace: "bar", 199 }, 200 }, 201 false, 202 }, 203 { 204 &core.Event{ 205 ObjectMeta: metav1.ObjectMeta{ 206 Name: "test14", 207 Namespace: "foo", 208 }, 209 InvolvedObject: core.ObjectReference{ 210 APIVersion: "other/v1beta1", 211 Kind: "FooBar", 212 Namespace: "", 213 }, 214 }, 215 false, 216 }, 217 } 218 219 for _, item := range table { 220 createErrs := ValidateEventCreate(item.Event, v1.SchemeGroupVersion) 221 if e, a := item.valid, len(createErrs) == 0; e != a { 222 t.Errorf("%v: expected %v, got %v: %v", item.Event.Name, e, a, createErrs) 223 } 224 updateErrs := ValidateEventUpdate(item.Event, &core.Event{}, v1.SchemeGroupVersion) 225 if e, a := item.valid, len(updateErrs) == 0; e != a { 226 t.Errorf("%v: expected %v, got %v: %v", item.Event.Name, e, a, updateErrs) 227 } 228 } 229} 230 231func TestValidateEventForNewV1beta1Events(t *testing.T) { 232 someTime := metav1.MicroTime{Time: time.Unix(1505828956, 0)} 233 table := []struct { 234 *core.Event 235 valid bool 236 msg string 237 }{ 238 { 239 Event: &core.Event{ 240 ObjectMeta: metav1.ObjectMeta{ 241 Name: "test", 242 Namespace: metav1.NamespaceDefault, 243 }, 244 InvolvedObject: core.ObjectReference{ 245 APIVersion: "v1", 246 Kind: "Node", 247 }, 248 EventTime: someTime, 249 }, 250 valid: false, 251 msg: "Old Event with EventTime should trigger new validation and fail", 252 }, 253 { 254 Event: &core.Event{ 255 ObjectMeta: metav1.ObjectMeta{ 256 Name: "test", 257 Namespace: metav1.NamespaceSystem, 258 }, 259 InvolvedObject: core.ObjectReference{ 260 APIVersion: "v1", 261 Kind: "Node", 262 }, 263 EventTime: someTime, 264 ReportingController: "k8s.io/my-controller", 265 ReportingInstance: "node-xyz", 266 Action: "Do", 267 Reason: "Because", 268 }, 269 valid: true, 270 msg: "Valid new Event", 271 }, 272 { 273 Event: &core.Event{ 274 ObjectMeta: metav1.ObjectMeta{ 275 Name: "test", 276 Namespace: metav1.NamespaceSystem, 277 }, 278 InvolvedObject: core.ObjectReference{ 279 APIVersion: "v1", 280 Kind: "Node", 281 }, 282 EventTime: someTime, 283 ReportingController: "my-contr@ller", 284 ReportingInstance: "node-xyz", 285 Action: "Do", 286 Reason: "Because", 287 }, 288 valid: false, 289 msg: "not qualified reportingController", 290 }, 291 { 292 Event: &core.Event{ 293 ObjectMeta: metav1.ObjectMeta{ 294 Name: "test", 295 Namespace: metav1.NamespaceSystem, 296 }, 297 InvolvedObject: core.ObjectReference{ 298 APIVersion: "v1", 299 Kind: "Node", 300 }, 301 EventTime: someTime, 302 ReportingController: "k8s.io/my-controller", 303 ReportingInstance: "node-xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 304 Action: "Do", 305 Reason: "Because", 306 }, 307 valid: false, 308 msg: "too long reporting instance", 309 }, 310 { 311 Event: &core.Event{ 312 ObjectMeta: metav1.ObjectMeta{ 313 Name: "test", 314 Namespace: metav1.NamespaceSystem, 315 }, 316 InvolvedObject: core.ObjectReference{ 317 APIVersion: "v1", 318 Kind: "Node", 319 }, 320 EventTime: someTime, 321 ReportingController: "k8s.io/my-controller", 322 ReportingInstance: "node-xyz", 323 Action: "Do", 324 }, 325 valid: false, 326 msg: "missing reason", 327 }, 328 { 329 Event: &core.Event{ 330 ObjectMeta: metav1.ObjectMeta{ 331 Name: "test", 332 Namespace: metav1.NamespaceSystem, 333 }, 334 InvolvedObject: core.ObjectReference{ 335 APIVersion: "v1", 336 Kind: "Node", 337 }, 338 EventTime: someTime, 339 ReportingController: "k8s.io/my-controller", 340 ReportingInstance: "node-xyz", 341 Reason: "Because", 342 }, 343 valid: false, 344 msg: "missing action", 345 }, 346 { 347 Event: &core.Event{ 348 ObjectMeta: metav1.ObjectMeta{ 349 Name: "test", 350 }, 351 InvolvedObject: core.ObjectReference{ 352 APIVersion: "v1", 353 Kind: "Node", 354 }, 355 EventTime: someTime, 356 ReportingController: "k8s.io/my-controller", 357 ReportingInstance: "node-xyz", 358 Reason: "Because", 359 }, 360 valid: false, 361 msg: "missing namespace", 362 }, 363 { 364 Event: &core.Event{ 365 ObjectMeta: metav1.ObjectMeta{ 366 Name: "test", 367 }, 368 InvolvedObject: core.ObjectReference{ 369 APIVersion: "v1", 370 Kind: "Node", 371 }, 372 EventTime: someTime, 373 ReportingController: "k8s.io/my-controller", 374 ReportingInstance: "node-xyz", 375 Action: "Do", 376 Reason: "Because", 377 Message: `zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 378zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 379zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 380zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 381zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 382zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 383zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 384zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 385zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 386zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 387zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz`, 388 }, 389 valid: false, 390 msg: "too long message", 391 }, 392 } 393 394 for _, item := range table { 395 createErrs := ValidateEventCreate(item.Event, eventsv1beta1.SchemeGroupVersion) 396 if e, a := item.valid, len(createErrs) == 0; e != a { 397 t.Errorf("%v: expected %v, got %v: %v", item.msg, e, a, createErrs) 398 } 399 updateErrs := ValidateEventUpdate(item.Event, &core.Event{}, eventsv1beta1.SchemeGroupVersion) 400 if e, a := item.valid, len(updateErrs) == 0; e != a { 401 t.Errorf("%v: expected %v, got %v: %v", item.msg, e, a, updateErrs) 402 } 403 } 404} 405 406func TestValidateEventCreateForNewV1Events(t *testing.T) { 407 someTime := metav1.MicroTime{Time: time.Unix(1505828956, 0)} 408 table := []struct { 409 *core.Event 410 valid bool 411 msg string 412 }{ 413 { 414 Event: &core.Event{ 415 ObjectMeta: metav1.ObjectMeta{ 416 Name: "test", 417 Namespace: metav1.NamespaceSystem, 418 }, 419 InvolvedObject: core.ObjectReference{ 420 APIVersion: "v1", 421 Kind: "Node", 422 }, 423 EventTime: someTime, 424 ReportingController: "k8s.io/my-controller", 425 ReportingInstance: "node-xyz", 426 Action: "Do", 427 Reason: "Because", 428 Type: "Normal", 429 }, 430 valid: true, 431 msg: "valid new event", 432 }, 433 { 434 Event: &core.Event{ 435 ObjectMeta: metav1.ObjectMeta{ 436 Namespace: metav1.NamespaceSystem, 437 }, 438 InvolvedObject: core.ObjectReference{ 439 APIVersion: "v1", 440 Kind: "Node", 441 }, 442 EventTime: someTime, 443 ReportingController: "k8s.io/my-controller", 444 ReportingInstance: "node-xyz", 445 Reason: "Because", 446 }, 447 valid: false, 448 msg: "missing name in objectMeta", 449 }, 450 { 451 Event: &core.Event{ 452 ObjectMeta: metav1.ObjectMeta{ 453 Name: "test", 454 }, 455 InvolvedObject: core.ObjectReference{ 456 APIVersion: "v1", 457 Kind: "Node", 458 }, 459 EventTime: someTime, 460 ReportingController: "k8s.io/my-controller", 461 ReportingInstance: "node-xyz", 462 Reason: "Because", 463 }, 464 valid: false, 465 msg: "missing namespace in objectMeta", 466 }, 467 { 468 Event: &core.Event{ 469 ObjectMeta: metav1.ObjectMeta{ 470 Name: "test", 471 Namespace: metav1.NamespaceDefault, 472 }, 473 InvolvedObject: core.ObjectReference{ 474 APIVersion: "v1", 475 Kind: "Node", 476 }, 477 }, 478 valid: false, 479 msg: "missing EventTime", 480 }, 481 { 482 Event: &core.Event{ 483 ObjectMeta: metav1.ObjectMeta{ 484 Name: "test", 485 Namespace: metav1.NamespaceSystem, 486 }, 487 InvolvedObject: core.ObjectReference{ 488 APIVersion: "v1", 489 Kind: "Node", 490 }, 491 EventTime: someTime, 492 ReportingController: "my-contr@ller", 493 ReportingInstance: "node-xyz", 494 Action: "Do", 495 Reason: "Because", 496 }, 497 valid: false, 498 msg: "not qualified reportingController", 499 }, 500 { 501 Event: &core.Event{ 502 ObjectMeta: metav1.ObjectMeta{ 503 Name: "test", 504 Namespace: metav1.NamespaceSystem, 505 }, 506 InvolvedObject: core.ObjectReference{ 507 APIVersion: "v1", 508 Kind: "Node", 509 }, 510 EventTime: someTime, 511 ReportingController: "k8s.io/my-controller", 512 ReportingInstance: "node-xyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 513 Action: "Do", 514 Reason: "Because", 515 }, 516 valid: false, 517 msg: "too long reporting instance", 518 }, 519 { 520 Event: &core.Event{ 521 ObjectMeta: metav1.ObjectMeta{ 522 Name: "test", 523 Namespace: metav1.NamespaceSystem, 524 }, 525 InvolvedObject: core.ObjectReference{ 526 APIVersion: "v1", 527 Kind: "Node", 528 }, 529 EventTime: someTime, 530 ReportingController: "k8s.io/my-controller", 531 ReportingInstance: "node-xyz", 532 Action: "Do", 533 }, 534 valid: false, 535 msg: "missing reason", 536 }, 537 { 538 Event: &core.Event{ 539 ObjectMeta: metav1.ObjectMeta{ 540 Name: "test", 541 Namespace: metav1.NamespaceSystem, 542 }, 543 InvolvedObject: core.ObjectReference{ 544 APIVersion: "v1", 545 Kind: "Node", 546 }, 547 EventTime: someTime, 548 ReportingController: "k8s.io/my-controller", 549 ReportingInstance: "node-xyz", 550 Reason: "Because", 551 }, 552 valid: false, 553 msg: "missing action", 554 }, 555 { 556 Event: &core.Event{ 557 ObjectMeta: metav1.ObjectMeta{ 558 Name: "test", 559 }, 560 InvolvedObject: core.ObjectReference{ 561 APIVersion: "v1", 562 Kind: "Node", 563 }, 564 EventTime: someTime, 565 ReportingController: "k8s.io/my-controller", 566 ReportingInstance: "node-xyz", 567 Action: "Do", 568 Reason: "Because", 569 Message: `zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 570zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 571zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 572zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 573zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 574zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 575zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 576zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 577zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 578zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 579zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz`, 580 }, 581 valid: false, 582 msg: "too long message", 583 }, 584 { 585 Event: &core.Event{ 586 ObjectMeta: metav1.ObjectMeta{ 587 Name: "test", 588 Namespace: metav1.NamespaceSystem, 589 }, 590 InvolvedObject: core.ObjectReference{ 591 APIVersion: "v1", 592 Kind: "Node", 593 }, 594 EventTime: someTime, 595 ReportingController: "k8s.io/my-controller", 596 ReportingInstance: "node-xyz", 597 Action: "Do", 598 Reason: "Because", 599 Type: "invalid-type", 600 }, 601 valid: false, 602 msg: "invalid type", 603 }, 604 { 605 Event: &core.Event{ 606 ObjectMeta: metav1.ObjectMeta{ 607 Name: "test", 608 Namespace: metav1.NamespaceSystem, 609 }, 610 InvolvedObject: core.ObjectReference{ 611 APIVersion: "v1", 612 Kind: "Node", 613 }, 614 EventTime: someTime, 615 ReportingController: "k8s.io/my-controller", 616 ReportingInstance: "node-xyz", 617 Action: "Do", 618 Reason: "Because", 619 Type: "Normal", 620 FirstTimestamp: metav1.Time{Time: time.Unix(1505828956, 0)}, 621 }, 622 valid: false, 623 msg: "non-empty firstTimestamp", 624 }, 625 { 626 Event: &core.Event{ 627 ObjectMeta: metav1.ObjectMeta{ 628 Name: "test", 629 Namespace: metav1.NamespaceSystem, 630 }, 631 InvolvedObject: core.ObjectReference{ 632 APIVersion: "v1", 633 Kind: "Node", 634 }, 635 EventTime: someTime, 636 ReportingController: "k8s.io/my-controller", 637 ReportingInstance: "node-xyz", 638 Action: "Do", 639 Reason: "Because", 640 Type: "Normal", 641 LastTimestamp: metav1.Time{Time: time.Unix(1505828956, 0)}, 642 }, 643 valid: false, 644 msg: "non-empty lastTimestamp", 645 }, 646 { 647 Event: &core.Event{ 648 ObjectMeta: metav1.ObjectMeta{ 649 Name: "test", 650 Namespace: metav1.NamespaceSystem, 651 }, 652 InvolvedObject: core.ObjectReference{ 653 APIVersion: "v1", 654 Kind: "Node", 655 }, 656 EventTime: someTime, 657 ReportingController: "k8s.io/my-controller", 658 ReportingInstance: "node-xyz", 659 Action: "Do", 660 Reason: "Because", 661 Type: "Normal", 662 Count: 123, 663 }, 664 valid: false, 665 msg: "non-empty count", 666 }, 667 { 668 Event: &core.Event{ 669 ObjectMeta: metav1.ObjectMeta{ 670 Name: "test", 671 Namespace: metav1.NamespaceSystem, 672 }, 673 InvolvedObject: core.ObjectReference{ 674 APIVersion: "v1", 675 Kind: "Node", 676 }, 677 EventTime: someTime, 678 ReportingController: "k8s.io/my-controller", 679 ReportingInstance: "node-xyz", 680 Action: "Do", 681 Reason: "Because", 682 Type: "Normal", 683 Source: core.EventSource{ 684 Host: "host", 685 }, 686 }, 687 valid: false, 688 msg: "non-empty source", 689 }, 690 { 691 Event: &core.Event{ 692 ObjectMeta: metav1.ObjectMeta{ 693 Name: "test", 694 Namespace: metav1.NamespaceSystem, 695 }, 696 InvolvedObject: core.ObjectReference{ 697 APIVersion: "v1", 698 Kind: "Node", 699 }, 700 EventTime: someTime, 701 ReportingController: "k8s.io/my-controller", 702 ReportingInstance: "node-xyz", 703 Action: "Do", 704 Reason: "Because", 705 Type: "Normal", 706 Series: &core.EventSeries{ 707 Count: 0, 708 LastObservedTime: someTime, 709 }, 710 }, 711 valid: false, 712 msg: "non-nil series with cound < 2", 713 }, 714 { 715 Event: &core.Event{ 716 ObjectMeta: metav1.ObjectMeta{ 717 Name: "test", 718 Namespace: metav1.NamespaceSystem, 719 }, 720 InvolvedObject: core.ObjectReference{ 721 APIVersion: "v1", 722 Kind: "Node", 723 }, 724 EventTime: someTime, 725 ReportingController: "k8s.io/my-controller", 726 ReportingInstance: "node-xyz", 727 Action: "Do", 728 Reason: "Because", 729 Type: "Normal", 730 Series: &core.EventSeries{ 731 Count: 2, 732 }, 733 }, 734 valid: false, 735 msg: "non-nil series with empty lastObservedTime", 736 }, 737 } 738 739 for _, item := range table { 740 createErrs := ValidateEventCreate(item.Event, eventsv1.SchemeGroupVersion) 741 if e, a := item.valid, len(createErrs) == 0; e != a { 742 t.Errorf("%v: expected %v, got %v: %v", item.msg, e, a, createErrs) 743 } 744 } 745} 746 747func TestValidateEventUpdateForNewV1Events(t *testing.T) { 748 someTime := metav1.MicroTime{Time: time.Unix(1505828956, 0)} 749 table := []struct { 750 newEvent *core.Event 751 oldEvent *core.Event 752 valid bool 753 msg string 754 }{ 755 { 756 newEvent: &core.Event{ 757 ObjectMeta: metav1.ObjectMeta{ 758 Name: "test", 759 Namespace: metav1.NamespaceSystem, 760 ResourceVersion: "2", 761 }, 762 InvolvedObject: core.ObjectReference{ 763 APIVersion: "v2", 764 Kind: "Node", 765 }, 766 Series: &core.EventSeries{ 767 Count: 2, 768 LastObservedTime: someTime, 769 }, 770 EventTime: someTime, 771 ReportingController: "k8s.io/my-controller", 772 ReportingInstance: "node-xyz", 773 Action: "Do", 774 Reason: "Yeees", 775 Type: "Normal", 776 }, 777 oldEvent: &core.Event{ 778 ObjectMeta: metav1.ObjectMeta{ 779 Name: "test", 780 Namespace: metav1.NamespaceSystem, 781 ResourceVersion: "2", 782 }, 783 InvolvedObject: core.ObjectReference{ 784 APIVersion: "v2", 785 Kind: "Node", 786 }, 787 Series: &core.EventSeries{ 788 Count: 1, 789 LastObservedTime: someTime, 790 }, 791 EventTime: someTime, 792 ReportingController: "k8s.io/my-controller", 793 ReportingInstance: "node-xyz", 794 Action: "Do", 795 Reason: "Yeees", 796 Type: "Normal", 797 }, 798 valid: true, 799 msg: "valid new updated event", 800 }, 801 { 802 newEvent: &core.Event{ 803 ObjectMeta: metav1.ObjectMeta{ 804 Name: "test", 805 Namespace: metav1.NamespaceSystem, 806 ResourceVersion: "1", 807 }, 808 InvolvedObject: core.ObjectReference{ 809 APIVersion: "v2", 810 Kind: "Node", 811 }, 812 EventTime: someTime, 813 ReportingController: "k8s.io/my-controller", 814 ReportingInstance: "node-xyz", 815 Action: "Do", 816 Reason: "Yeees", 817 Type: "Normal", 818 }, 819 oldEvent: &core.Event{ 820 ObjectMeta: metav1.ObjectMeta{ 821 Name: "test", 822 Namespace: metav1.NamespaceSystem, 823 ResourceVersion: "1", 824 }, 825 InvolvedObject: core.ObjectReference{ 826 APIVersion: "v1", 827 Kind: "Node", 828 }, 829 EventTime: someTime, 830 ReportingController: "k8s.io/my-controller", 831 ReportingInstance: "node-xyz", 832 Action: "Do", 833 Reason: "Yeees", 834 Type: "Normal", 835 }, 836 valid: false, 837 msg: "forbidden updates to involvedObject", 838 }, 839 { 840 newEvent: &core.Event{ 841 ObjectMeta: metav1.ObjectMeta{ 842 Name: "test", 843 Namespace: metav1.NamespaceSystem, 844 ResourceVersion: "1", 845 }, 846 InvolvedObject: core.ObjectReference{ 847 APIVersion: "v1", 848 Kind: "Node", 849 }, 850 EventTime: someTime, 851 ReportingController: "k8s.io/my-controller", 852 ReportingInstance: "node-xyz", 853 Action: "Do", 854 Reason: "Yeees-new", 855 Type: "Normal", 856 }, 857 oldEvent: &core.Event{ 858 ObjectMeta: metav1.ObjectMeta{ 859 Name: "test", 860 Namespace: metav1.NamespaceSystem, 861 ResourceVersion: "1", 862 }, 863 InvolvedObject: core.ObjectReference{ 864 APIVersion: "v1", 865 Kind: "Node", 866 }, 867 EventTime: someTime, 868 ReportingController: "k8s.io/my-controller", 869 ReportingInstance: "node-xyz", 870 Action: "Do", 871 Reason: "Yeees", 872 Type: "Normal", 873 }, 874 valid: false, 875 msg: "forbidden updates to reason", 876 }, 877 { 878 newEvent: &core.Event{ 879 ObjectMeta: metav1.ObjectMeta{ 880 Name: "test", 881 Namespace: metav1.NamespaceSystem, 882 ResourceVersion: "1", 883 }, 884 InvolvedObject: core.ObjectReference{ 885 APIVersion: "v1", 886 Kind: "Node", 887 }, 888 EventTime: someTime, 889 ReportingController: "k8s.io/my-controller", 890 ReportingInstance: "node-xyz", 891 Action: "Do", 892 Reason: "Yeees", 893 Type: "Normal", 894 Message: "new-message", 895 }, 896 oldEvent: &core.Event{ 897 ObjectMeta: metav1.ObjectMeta{ 898 Name: "test", 899 Namespace: metav1.NamespaceSystem, 900 ResourceVersion: "1", 901 }, 902 InvolvedObject: core.ObjectReference{ 903 APIVersion: "v1", 904 Kind: "Node", 905 }, 906 EventTime: someTime, 907 ReportingController: "k8s.io/my-controller", 908 ReportingInstance: "node-xyz", 909 Action: "Do", 910 Reason: "Yeees", 911 Type: "Normal", 912 Message: "message", 913 }, 914 valid: false, 915 msg: "forbidden updates to message", 916 }, 917 { 918 newEvent: &core.Event{ 919 ObjectMeta: metav1.ObjectMeta{ 920 Name: "test", 921 Namespace: metav1.NamespaceSystem, 922 ResourceVersion: "1", 923 }, 924 InvolvedObject: core.ObjectReference{ 925 APIVersion: "v1", 926 Kind: "Node", 927 }, 928 EventTime: someTime, 929 ReportingController: "k8s.io/my-controller", 930 ReportingInstance: "node-xyz", 931 Action: "Do", 932 Reason: "Yeees", 933 Type: "Normal", 934 }, 935 oldEvent: &core.Event{ 936 ObjectMeta: metav1.ObjectMeta{ 937 Name: "test", 938 Namespace: metav1.NamespaceSystem, 939 ResourceVersion: "1", 940 }, 941 InvolvedObject: core.ObjectReference{ 942 APIVersion: "v1", 943 Kind: "Node", 944 }, 945 Source: core.EventSource{ 946 Host: "host", 947 }, 948 EventTime: someTime, 949 ReportingController: "k8s.io/my-controller", 950 ReportingInstance: "node-xyz", 951 Action: "Do", 952 Reason: "Yeees", 953 Type: "Normal", 954 }, 955 valid: false, 956 msg: "forbidden updates to source", 957 }, 958 { 959 newEvent: &core.Event{ 960 ObjectMeta: metav1.ObjectMeta{ 961 Name: "test", 962 Namespace: metav1.NamespaceSystem, 963 ResourceVersion: "1", 964 }, 965 InvolvedObject: core.ObjectReference{ 966 APIVersion: "v1", 967 Kind: "Node", 968 }, 969 EventTime: someTime, 970 ReportingController: "k8s.io/my-controller", 971 ReportingInstance: "node-xyz", 972 Action: "Do", 973 Reason: "Yeees", 974 Type: "Normal", 975 }, 976 oldEvent: &core.Event{ 977 ObjectMeta: metav1.ObjectMeta{ 978 Name: "test", 979 Namespace: metav1.NamespaceSystem, 980 ResourceVersion: "1", 981 }, 982 InvolvedObject: core.ObjectReference{ 983 APIVersion: "v1", 984 Kind: "Node", 985 }, 986 EventTime: someTime, 987 ReportingController: "k8s.io/my-controller", 988 ReportingInstance: "node-xyz", 989 Action: "Do", 990 Reason: "Yeees", 991 Type: "Normal", 992 FirstTimestamp: metav1.Time{Time: time.Unix(1505828956, 0)}, 993 }, 994 valid: false, 995 msg: "forbidden updates to firstTimestamp", 996 }, 997 { 998 newEvent: &core.Event{ 999 ObjectMeta: metav1.ObjectMeta{ 1000 Name: "test", 1001 Namespace: metav1.NamespaceSystem, 1002 ResourceVersion: "1", 1003 }, 1004 InvolvedObject: core.ObjectReference{ 1005 APIVersion: "v1", 1006 Kind: "Node", 1007 }, 1008 EventTime: someTime, 1009 ReportingController: "k8s.io/my-controller", 1010 ReportingInstance: "node-xyz", 1011 Action: "Do", 1012 Reason: "Yeees", 1013 Type: "Normal", 1014 }, 1015 oldEvent: &core.Event{ 1016 ObjectMeta: metav1.ObjectMeta{ 1017 Name: "test", 1018 Namespace: metav1.NamespaceSystem, 1019 ResourceVersion: "1", 1020 }, 1021 InvolvedObject: core.ObjectReference{ 1022 APIVersion: "v1", 1023 Kind: "Node", 1024 }, 1025 EventTime: someTime, 1026 ReportingController: "k8s.io/my-controller", 1027 ReportingInstance: "node-xyz", 1028 Action: "Do", 1029 Reason: "Yeees", 1030 Type: "Normal", 1031 LastTimestamp: metav1.Time{Time: time.Unix(1505828956, 0)}, 1032 }, 1033 valid: false, 1034 msg: "forbidden updates to lastTimestamp", 1035 }, 1036 { 1037 newEvent: &core.Event{ 1038 ObjectMeta: metav1.ObjectMeta{ 1039 Name: "test", 1040 Namespace: metav1.NamespaceSystem, 1041 ResourceVersion: "1", 1042 }, 1043 InvolvedObject: core.ObjectReference{ 1044 APIVersion: "v1", 1045 Kind: "Node", 1046 }, 1047 EventTime: someTime, 1048 ReportingController: "k8s.io/my-controller", 1049 ReportingInstance: "node-xyz", 1050 Action: "Do", 1051 Reason: "Yeees", 1052 Type: "Normal", 1053 }, 1054 oldEvent: &core.Event{ 1055 ObjectMeta: metav1.ObjectMeta{ 1056 Name: "test", 1057 Namespace: metav1.NamespaceSystem, 1058 ResourceVersion: "1", 1059 }, 1060 InvolvedObject: core.ObjectReference{ 1061 APIVersion: "v1", 1062 Kind: "Node", 1063 }, 1064 EventTime: someTime, 1065 ReportingController: "k8s.io/my-controller", 1066 ReportingInstance: "node-xyz", 1067 Action: "Do", 1068 Reason: "Yeees", 1069 Type: "Normal", 1070 Count: 2, 1071 }, 1072 valid: false, 1073 msg: "forbidden updates to count", 1074 }, 1075 { 1076 newEvent: &core.Event{ 1077 ObjectMeta: metav1.ObjectMeta{ 1078 Name: "test", 1079 Namespace: metav1.NamespaceSystem, 1080 ResourceVersion: "1", 1081 }, 1082 InvolvedObject: core.ObjectReference{ 1083 APIVersion: "v1", 1084 Kind: "Node", 1085 }, 1086 EventTime: someTime, 1087 ReportingController: "k8s.io/my-controller", 1088 ReportingInstance: "node-xyz", 1089 Action: "Do", 1090 Reason: "Yeees", 1091 Type: "Warning", 1092 }, 1093 oldEvent: &core.Event{ 1094 ObjectMeta: metav1.ObjectMeta{ 1095 Name: "test", 1096 Namespace: metav1.NamespaceSystem, 1097 ResourceVersion: "1", 1098 }, 1099 InvolvedObject: core.ObjectReference{ 1100 APIVersion: "v1", 1101 Kind: "Node", 1102 }, 1103 EventTime: someTime, 1104 ReportingController: "k8s.io/my-controller", 1105 ReportingInstance: "node-xyz", 1106 Action: "Do", 1107 Reason: "Yeees", 1108 Type: "Normal", 1109 }, 1110 valid: false, 1111 msg: "forbidden updates to type", 1112 }, 1113 { 1114 newEvent: &core.Event{ 1115 ObjectMeta: metav1.ObjectMeta{ 1116 Name: "test", 1117 Namespace: metav1.NamespaceSystem, 1118 ResourceVersion: "1", 1119 }, 1120 InvolvedObject: core.ObjectReference{ 1121 APIVersion: "v1", 1122 Kind: "Node", 1123 }, 1124 EventTime: metav1.MicroTime{Time: time.Unix(1505828999, 0)}, 1125 ReportingController: "k8s.io/my-controller", 1126 ReportingInstance: "node-xyz", 1127 Action: "Do", 1128 Reason: "Yeees", 1129 Type: "Normal", 1130 }, 1131 oldEvent: &core.Event{ 1132 ObjectMeta: metav1.ObjectMeta{ 1133 Name: "test", 1134 Namespace: metav1.NamespaceSystem, 1135 ResourceVersion: "1", 1136 }, 1137 InvolvedObject: core.ObjectReference{ 1138 APIVersion: "v1", 1139 Kind: "Node", 1140 }, 1141 EventTime: someTime, 1142 ReportingController: "k8s.io/my-controller", 1143 ReportingInstance: "node-xyz", 1144 Action: "Do", 1145 Reason: "Yeees", 1146 Type: "Normal", 1147 }, 1148 valid: false, 1149 msg: "forbidden updates to eventTime", 1150 }, 1151 { 1152 newEvent: &core.Event{ 1153 ObjectMeta: metav1.ObjectMeta{ 1154 Name: "test", 1155 Namespace: metav1.NamespaceSystem, 1156 ResourceVersion: "1", 1157 }, 1158 InvolvedObject: core.ObjectReference{ 1159 APIVersion: "v1", 1160 Kind: "Node", 1161 }, 1162 EventTime: someTime, 1163 ReportingController: "k8s.io/my-controller", 1164 ReportingInstance: "node-xyz", 1165 Action: "Undo", 1166 Reason: "Yeees", 1167 Type: "Normal", 1168 }, 1169 oldEvent: &core.Event{ 1170 ObjectMeta: metav1.ObjectMeta{ 1171 Name: "test", 1172 Namespace: metav1.NamespaceSystem, 1173 ResourceVersion: "1", 1174 }, 1175 InvolvedObject: core.ObjectReference{ 1176 APIVersion: "v1", 1177 Kind: "Node", 1178 }, 1179 EventTime: someTime, 1180 ReportingController: "k8s.io/my-controller", 1181 ReportingInstance: "node-xyz", 1182 Action: "Do", 1183 Reason: "Yeees", 1184 Type: "Normal", 1185 }, 1186 valid: false, 1187 msg: "forbidden updates to action", 1188 }, 1189 { 1190 newEvent: &core.Event{ 1191 ObjectMeta: metav1.ObjectMeta{ 1192 Name: "test", 1193 Namespace: metav1.NamespaceSystem, 1194 ResourceVersion: "1", 1195 }, 1196 InvolvedObject: core.ObjectReference{ 1197 APIVersion: "v1", 1198 Kind: "Node", 1199 }, 1200 Related: &core.ObjectReference{ 1201 APIVersion: "v1", 1202 }, 1203 EventTime: someTime, 1204 ReportingController: "k8s.io/my-controller", 1205 ReportingInstance: "node-xyz", 1206 Action: "Do", 1207 Reason: "Yeees", 1208 Type: "Normal", 1209 }, 1210 oldEvent: &core.Event{ 1211 ObjectMeta: metav1.ObjectMeta{ 1212 Name: "test", 1213 Namespace: metav1.NamespaceSystem, 1214 ResourceVersion: "1", 1215 }, 1216 InvolvedObject: core.ObjectReference{ 1217 APIVersion: "v1", 1218 Kind: "Node", 1219 }, 1220 EventTime: someTime, 1221 ReportingController: "k8s.io/my-controller", 1222 ReportingInstance: "node-xyz", 1223 Action: "Do", 1224 Reason: "Yeees", 1225 Type: "Normal", 1226 }, 1227 valid: false, 1228 msg: "forbidden updates to related", 1229 }, 1230 { 1231 newEvent: &core.Event{ 1232 ObjectMeta: metav1.ObjectMeta{ 1233 Name: "test", 1234 Namespace: metav1.NamespaceSystem, 1235 ResourceVersion: "1", 1236 }, 1237 InvolvedObject: core.ObjectReference{ 1238 APIVersion: "v1", 1239 Kind: "Node", 1240 }, 1241 EventTime: someTime, 1242 ReportingController: "k8s.io/my-controller/new", 1243 ReportingInstance: "node-xyz", 1244 Action: "Do", 1245 Reason: "Yeees", 1246 Type: "Normal", 1247 }, 1248 oldEvent: &core.Event{ 1249 ObjectMeta: metav1.ObjectMeta{ 1250 Name: "test", 1251 Namespace: metav1.NamespaceSystem, 1252 ResourceVersion: "1", 1253 }, 1254 InvolvedObject: core.ObjectReference{ 1255 APIVersion: "v1", 1256 Kind: "Node", 1257 }, 1258 EventTime: someTime, 1259 ReportingController: "k8s.io/my-controller", 1260 ReportingInstance: "node-xyz", 1261 Action: "Do", 1262 Reason: "Yeees", 1263 Type: "Normal", 1264 }, 1265 valid: false, 1266 msg: "forbidden updates to reportingController", 1267 }, 1268 { 1269 newEvent: &core.Event{ 1270 ObjectMeta: metav1.ObjectMeta{ 1271 Name: "test", 1272 Namespace: metav1.NamespaceSystem, 1273 ResourceVersion: "1", 1274 }, 1275 InvolvedObject: core.ObjectReference{ 1276 APIVersion: "v1", 1277 Kind: "Node", 1278 }, 1279 EventTime: someTime, 1280 ReportingController: "k8s.io/my-controller", 1281 ReportingInstance: "node-xyz-new", 1282 Action: "Do", 1283 Reason: "Yeees", 1284 Type: "Normal", 1285 }, 1286 oldEvent: &core.Event{ 1287 ObjectMeta: metav1.ObjectMeta{ 1288 Name: "test", 1289 Namespace: metav1.NamespaceSystem, 1290 ResourceVersion: "1", 1291 }, 1292 InvolvedObject: core.ObjectReference{ 1293 APIVersion: "v1", 1294 Kind: "Node", 1295 }, 1296 EventTime: someTime, 1297 ReportingController: "k8s.io/my-controller", 1298 ReportingInstance: "node-xyz", 1299 Action: "Do", 1300 Reason: "Yeees", 1301 Type: "Normal", 1302 }, 1303 valid: false, 1304 msg: "forbidden updates to reportingInstance", 1305 }, 1306 } 1307 1308 for _, item := range table { 1309 updateErrs := ValidateEventUpdate(item.newEvent, item.oldEvent, eventsv1.SchemeGroupVersion) 1310 if e, a := item.valid, len(updateErrs) == 0; e != a { 1311 t.Errorf("%v: expected %v, got %v: %v", item.msg, e, a, updateErrs) 1312 } 1313 } 1314} 1315