1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common_test
import (
"errors"
"fmt"
"reflect"
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/apiserver/pkg/cel/common"
"k8s.io/apiserver/pkg/cel/openapi"
"k8s.io/kube-openapi/pkg/validation/spec"
)
type TestCase struct {
Name string
// Expected old value after traversal. If nil, then the traversal should fail.
OldValue interface{}
// Expected value after traversal. If nil, then the traversal should fail.
NewValue interface{}
// Whether OldValue and NewValue are considered to be equal.
// Defaults to reflect.DeepEqual comparison of the two. Can be overridden to
// true here if the two values are not DeepEqual, but are considered equal
// for instance due to map-list reordering.
ExpectEqual bool
// Schema to provide to the correlated object
Schema common.Schema
// Array of field names and indexes to traverse to get to the value
KeyPath []interface{}
// Root object to traverse from
RootObject interface{}
RootOldObject interface{}
}
func (c TestCase) Run() error {
// Create the correlated object
correlatedObject := common.NewCorrelatedObject(c.RootObject, c.RootOldObject, c.Schema)
// Traverse the correlated object
var err error
for _, key := range c.KeyPath {
if correlatedObject == nil {
break
}
switch k := key.(type) {
case string:
correlatedObject = correlatedObject.Key(k)
case int:
correlatedObject = correlatedObject.Index(k)
default:
return errors.New("key must be a string or int")
}
if err != nil {
return err
}
}
if correlatedObject == nil {
if c.OldValue != nil || c.NewValue != nil {
return fmt.Errorf("expected non-nil value, got nil")
}
} else {
// Check that the correlated object has the expected values
if !reflect.DeepEqual(correlatedObject.Value, c.NewValue) {
return fmt.Errorf("expected value %v, got %v", c.NewValue, correlatedObject.Value)
}
if !reflect.DeepEqual(correlatedObject.OldValue, c.OldValue) {
return fmt.Errorf("expected old value %v, got %v", c.OldValue, correlatedObject.OldValue)
}
// Check that the correlated object is considered equal to the expected value
if (c.ExpectEqual || reflect.DeepEqual(correlatedObject.Value, correlatedObject.OldValue)) != correlatedObject.CachedDeepEqual() {
return fmt.Errorf("expected equal, got not equal")
}
}
return nil
}
// Creates a *spec.Schema Schema by decoding the given YAML. Panics on error
func mustSchema(source string) *openapi.Schema {
d := yaml.NewYAMLOrJSONDecoder(strings.NewReader(source), 4096)
res := &spec.Schema{}
if err := d.Decode(res); err != nil {
panic(err)
}
return &openapi.Schema{Schema: res}
}
// Creates an *unstructured by decoding the given YAML. Panics on error
func mustUnstructured(source string) interface{} {
d := yaml.NewYAMLOrJSONDecoder(strings.NewReader(source), 4096)
var res interface{}
if err := d.Decode(&res); err != nil {
panic(err)
}
return res
}
func TestCorrelation(t *testing.T) {
// Tests ensure that the output of following keypath using the given
// schema and root objects yields the provided new value and old value.
// If new or old are nil, then ensures that the traversal failed due to
// uncorrelatable field path.
// Also confirms that CachedDeepEqual output is equal to expected result of
// reflect.DeepEqual of the new and old values.
cases := []TestCase{
{
Name: "Basic Key",
RootObject: mustUnstructured(`a: b`),
RootOldObject: mustUnstructured(`a: b`),
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{"a"},
NewValue: "b",
OldValue: "b",
},
{
Name: "Atomic Array not correlatable",
RootObject: mustUnstructured(`[a, b]`),
RootOldObject: mustUnstructured(`[a, b]`),
Schema: mustSchema(`
items:
type: string
`),
KeyPath: []interface{}{1},
},
{
Name: "Added Key Not In Old Object",
RootObject: mustUnstructured(`
a: b
c: d
`),
RootOldObject: mustUnstructured(`
a: b
`),
Schema: mustSchema(`
properties:
a: { type: string }
c: { type: string }
`),
KeyPath: []interface{}{"c"},
},
{
Name: "Added Index Not In Old Object",
RootObject: mustUnstructured(`
- a
- b
- c
`),
RootOldObject: mustUnstructured(`
- a
- b
`),
Schema: mustSchema(`
items:
type: string
`),
KeyPath: []interface{}{2},
},
{
Name: "Changed Index In Old Object not correlatable",
RootObject: []interface{}{
"a",
"b",
},
RootOldObject: []interface{}{
"a",
"oldB",
},
Schema: mustSchema(`
items:
type: string
`),
KeyPath: []interface{}{1},
},
{
Name: "Changed Index In Nested Old Object",
RootObject: []interface{}{
"a",
"b",
},
RootOldObject: []interface{}{
"a",
"oldB",
},
Schema: mustSchema(`
items:
type: string
`),
KeyPath: []interface{}{},
NewValue: []interface{}{"a", "b"},
OldValue: []interface{}{"a", "oldB"},
},
{
Name: "Changed Key In Old Object",
RootObject: map[string]interface{}{
"a": "b",
},
RootOldObject: map[string]interface{}{
"a": "oldB",
},
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{"a"},
NewValue: "b",
OldValue: "oldB",
},
{
Name: "Replaced Key In Old Object",
RootObject: map[string]interface{}{
"a": "b",
},
RootOldObject: map[string]interface{}{
"b": "a",
},
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{},
NewValue: map[string]interface{}{"a": "b"},
OldValue: map[string]interface{}{"b": "a"},
},
{
Name: "Added Key In Old Object",
RootObject: map[string]interface{}{
"a": "b",
},
RootOldObject: map[string]interface{}{},
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{},
NewValue: map[string]interface{}{"a": "b"},
OldValue: map[string]interface{}{},
},
{
Name: "Changed list to map",
RootObject: map[string]interface{}{
"a": "b",
},
RootOldObject: []interface{}{"a", "b"},
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{},
NewValue: map[string]interface{}{"a": "b"},
OldValue: []interface{}{"a", "b"},
},
{
Name: "Changed string to map",
RootObject: map[string]interface{}{
"a": "b",
},
RootOldObject: "a string",
Schema: mustSchema(`
properties:
a: { type: string }
`),
KeyPath: []interface{}{},
NewValue: map[string]interface{}{"a": "b"},
OldValue: "a string",
},
{
Name: "Map list type",
RootObject: mustUnstructured(`
foo:
- bar: baz
val: newBazValue
`),
RootOldObject: mustUnstructured(`
foo:
- bar: fizz
val: fizzValue
- bar: baz
val: bazValue
`),
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
properties:
bar:
type: string
val:
type: string
x-kubernetes-list-type: map
x-kubernetes-list-map-keys:
- bar
`),
KeyPath: []interface{}{"foo", 0, "val"},
NewValue: "newBazValue",
OldValue: "bazValue",
},
{
Name: "Atomic list item should not correlate",
RootObject: mustUnstructured(`
foo:
- bar: baz
val: newValue
`),
RootOldObject: mustUnstructured(`
foo:
- bar: fizz
val: fizzValue
- bar: baz
val: barValue
`),
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
properties:
bar:
type: string
val:
type: string
x-kubernetes-list-type: atomic
`),
KeyPath: []interface{}{"foo", 0, "val"},
},
{
Name: "Map used inside of map list type should correlate",
RootObject: mustUnstructured(`
foo:
- key: keyValue
bar:
baz: newValue
`),
RootOldObject: mustUnstructured(`
foo:
- key: otherKeyValue
bar:
baz: otherOldValue
- key: altKeyValue
bar:
baz: altOldValue
- key: keyValue
bar:
baz: oldValue
`),
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
x-kubernetes-list-type: map
x-kubernetes-list-map-keys:
- key
`),
KeyPath: []interface{}{"foo", 0, "bar", "baz"},
NewValue: "newValue",
OldValue: "oldValue",
},
{
Name: "Map used inside another map should correlate",
RootObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: newValue
`),
RootOldObject: mustUnstructured(`
foo:
key: otherKeyValue
bar:
baz: otherOldValue
altFoo:
key: altKeyValue
bar:
baz: altOldValue
otherFoo:
key: keyValue
bar:
baz: oldValue
`),
Schema: mustSchema(`
properties:
foo:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
`),
KeyPath: []interface{}{"foo", "bar"},
NewValue: map[string]interface{}{"baz": "newValue"},
OldValue: map[string]interface{}{"baz": "otherOldValue"},
},
{
Name: "Nested map equal to old",
RootObject: mustUnstructured(`
foo:
key: newKeyValue
bar:
baz: value
`),
RootOldObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: value
`),
Schema: mustSchema(`
properties:
foo:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
`),
KeyPath: []interface{}{"foo", "bar"},
NewValue: map[string]interface{}{"baz": "value"},
OldValue: map[string]interface{}{"baz": "value"},
},
{
Name: "Re-ordered list considered equal to old value due to map keys",
RootObject: mustUnstructured(`
foo:
- key: keyValue
bar:
baz: value
- key: altKeyValue
bar:
baz: altValue
`),
RootOldObject: mustUnstructured(`
foo:
- key: altKeyValue
bar:
baz: altValue
- key: keyValue
bar:
baz: value
`),
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
x-kubernetes-list-type: map
x-kubernetes-list-map-keys:
- key
`),
KeyPath: []interface{}{"foo"},
NewValue: mustUnstructured(`
- key: keyValue
bar:
baz: value
- key: altKeyValue
bar:
baz: altValue
`),
OldValue: mustUnstructured(`
- key: altKeyValue
bar:
baz: altValue
- key: keyValue
bar:
baz: value
`),
ExpectEqual: true,
},
{
Name: "Correlate unknown string key via additional properties",
RootObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: newValue
`),
RootOldObject: mustUnstructured(`
foo:
key: otherKeyValue
bar:
baz: otherOldValue
`),
Schema: mustSchema(`
properties:
foo:
type: object
additionalProperties:
properties:
baz:
type: string
`),
KeyPath: []interface{}{"foo", "bar", "baz"},
NewValue: "newValue",
OldValue: "otherOldValue",
},
{
Name: "Changed map value",
RootObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: newValue
`),
RootOldObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: oldValue
`),
Schema: mustSchema(`
properties:
foo:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
`),
KeyPath: []interface{}{"foo", "bar"},
NewValue: mustUnstructured(`
baz: newValue
`),
OldValue: mustUnstructured(`
baz: oldValue
`),
},
{
Name: "Changed nested map value",
RootObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: newValue
`),
RootOldObject: mustUnstructured(`
foo:
key: keyValue
bar:
baz: oldValue
`),
Schema: mustSchema(`
properties:
foo:
type: object
properties:
key:
type: string
bar:
type: object
properties:
baz:
type: string
`),
KeyPath: []interface{}{"foo"},
NewValue: mustUnstructured(`
key: keyValue
bar:
baz: newValue
`),
OldValue: mustUnstructured(`
key: keyValue
bar:
baz: oldValue
`),
},
{
Name: "unchanged list type set with atomic map values",
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
x-kubernetes-map-type: atomic
properties:
key:
type: string
bar:
type: string
x-kubernetes-list-type: set
`),
RootObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: value2
`),
RootOldObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: value2
`),
KeyPath: []interface{}{"foo"},
NewValue: mustUnstructured(`
- key: key1
bar: value1
- key: key2
bar: value2
`),
OldValue: mustUnstructured(`
- key: key1
bar: value1
- key: key2
bar: value2
`),
},
{
Name: "changed list type set with atomic map values",
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
x-kubernetes-map-type: atomic
properties:
key:
type: string
bar:
type: string
x-kubernetes-list-type: set
`),
RootObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: newValue2
`),
RootOldObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: value2
`),
KeyPath: []interface{}{"foo"},
NewValue: mustUnstructured(`
- key: key1
bar: value1
- key: key2
bar: newValue2
`),
OldValue: mustUnstructured(`
- key: key1
bar: value1
- key: key2
bar: value2
`),
},
{
Name: "elements of list type set with atomic map values are not correlated",
Schema: mustSchema(`
properties:
foo:
type: array
items:
type: object
x-kubernetes-map-type: atomic
properties:
key:
type: string
bar:
type: string
x-kubernetes-list-type: set
`),
RootObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: newValue2
`),
RootOldObject: mustUnstructured(`
foo:
- key: key1
bar: value1
- key: key2
bar: value2
`),
KeyPath: []interface{}{"foo", 0, "key"},
NewValue: nil,
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
if err := c.Run(); err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
|