...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting/algorithm_test.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package defaulting
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"testing"
    23  
    24  	structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
    25  	"k8s.io/apimachinery/pkg/util/json"
    26  )
    27  
    28  func TestDefault(t *testing.T) {
    29  	tests := []struct {
    30  		name     string
    31  		json     string
    32  		schema   *structuralschema.Structural
    33  		expected string
    34  	}{
    35  		{"empty", "null", nil, "null"},
    36  		{"scalar", "4", &structuralschema.Structural{
    37  			Generic: structuralschema.Generic{
    38  				Default: structuralschema.JSON{Object: "foo"},
    39  			},
    40  		}, "4"},
    41  		{"scalar array", "[1,2]", &structuralschema.Structural{
    42  			Items: &structuralschema.Structural{
    43  				Generic: structuralschema.Generic{
    44  					Default: structuralschema.JSON{Object: "foo"},
    45  				},
    46  			},
    47  		}, "[1,2]"},
    48  		{"object array", `[{"a":1},{"b":1},{"c":1}]`, &structuralschema.Structural{
    49  			Items: &structuralschema.Structural{
    50  				Properties: map[string]structuralschema.Structural{
    51  					"a": {
    52  						Generic: structuralschema.Generic{
    53  							Default: structuralschema.JSON{Object: "A"},
    54  						},
    55  					},
    56  					"b": {
    57  						Generic: structuralschema.Generic{
    58  							Default: structuralschema.JSON{Object: "B"},
    59  						},
    60  					},
    61  					"c": {
    62  						Generic: structuralschema.Generic{
    63  							Default: structuralschema.JSON{Object: "C"},
    64  						},
    65  					},
    66  				},
    67  			},
    68  		}, `[{"a":1,"b":"B","c":"C"},{"a":"A","b":1,"c":"C"},{"a":"A","b":"B","c":1}]`},
    69  		{"object array object", `{"array":[{"a":1},{"b":2}],"object":{"a":1},"additionalProperties":{"x":{"a":1},"y":{"b":2}}}`, &structuralschema.Structural{
    70  			Properties: map[string]structuralschema.Structural{
    71  				"array": {
    72  					Items: &structuralschema.Structural{
    73  						Properties: map[string]structuralschema.Structural{
    74  							"a": {
    75  								Generic: structuralschema.Generic{
    76  									Default: structuralschema.JSON{Object: "A"},
    77  								},
    78  							},
    79  							"b": {
    80  								Generic: structuralschema.Generic{
    81  									Default: structuralschema.JSON{Object: "B"},
    82  								},
    83  							},
    84  						},
    85  					},
    86  				},
    87  				"object": {
    88  					Properties: map[string]structuralschema.Structural{
    89  						"a": {
    90  							Generic: structuralschema.Generic{
    91  								Default: structuralschema.JSON{Object: "N"},
    92  							},
    93  						},
    94  						"b": {
    95  							Generic: structuralschema.Generic{
    96  								Default: structuralschema.JSON{Object: "O"},
    97  							},
    98  						},
    99  					},
   100  				},
   101  				"additionalProperties": {
   102  					Generic: structuralschema.Generic{
   103  						AdditionalProperties: &structuralschema.StructuralOrBool{
   104  							Structural: &structuralschema.Structural{
   105  								Properties: map[string]structuralschema.Structural{
   106  									"a": {
   107  										Generic: structuralschema.Generic{
   108  											Default: structuralschema.JSON{Object: "alpha"},
   109  										},
   110  									},
   111  									"b": {
   112  										Generic: structuralschema.Generic{
   113  											Default: structuralschema.JSON{Object: "beta"},
   114  										},
   115  									},
   116  								},
   117  							},
   118  						},
   119  					},
   120  				},
   121  				"foo": {
   122  					Generic: structuralschema.Generic{
   123  						Default: structuralschema.JSON{Object: "bar"},
   124  					},
   125  				},
   126  			},
   127  		}, `{"array":[{"a":1,"b":"B"},{"a":"A","b":2}],"object":{"a":1,"b":"O"},"additionalProperties":{"x":{"a":1,"b":"beta"},"y":{"a":"alpha","b":2}},"foo":"bar"}`},
   128  		{"empty and null", `[{},{"a":1},{"a":0},{"a":0.0},{"a":""},{"a":null},{"a":[]},{"a":{}}]`, &structuralschema.Structural{
   129  			Items: &structuralschema.Structural{
   130  				Properties: map[string]structuralschema.Structural{
   131  					"a": {
   132  						Generic: structuralschema.Generic{
   133  							Default: structuralschema.JSON{Object: "A"},
   134  						},
   135  					},
   136  				},
   137  			},
   138  		}, `[{"a":"A"},{"a":1},{"a":0},{"a":0.0},{"a":""},{"a":"A"},{"a":[]},{"a":{}}]`},
   139  		{"null in nullable list", `[null]`, &structuralschema.Structural{
   140  			Generic: structuralschema.Generic{
   141  				Nullable: true,
   142  			},
   143  			Items: &structuralschema.Structural{
   144  				Properties: map[string]structuralschema.Structural{
   145  					"a": {
   146  						Generic: structuralschema.Generic{
   147  							Default: structuralschema.JSON{Object: "A"},
   148  						},
   149  					},
   150  				},
   151  			},
   152  		}, `[null]`},
   153  		{"null in non-nullable list", `[null]`, &structuralschema.Structural{
   154  			Generic: structuralschema.Generic{
   155  				Nullable: false,
   156  			},
   157  			Items: &structuralschema.Structural{
   158  				Generic: structuralschema.Generic{
   159  					Default: structuralschema.JSON{Object: "A"},
   160  				},
   161  			},
   162  		}, `["A"]`},
   163  		{"null in nullable object", `{"a": null}`, &structuralschema.Structural{
   164  			Generic: structuralschema.Generic{},
   165  			Properties: map[string]structuralschema.Structural{
   166  				"a": {
   167  					Generic: structuralschema.Generic{
   168  						Nullable: true,
   169  						Default:  structuralschema.JSON{Object: "A"},
   170  					},
   171  				},
   172  			},
   173  		}, `{"a": null}`},
   174  		{"null in non-nullable object", `{"a": null}`, &structuralschema.Structural{
   175  			Properties: map[string]structuralschema.Structural{
   176  				"a": {
   177  					Generic: structuralschema.Generic{
   178  						Nullable: false,
   179  						Default:  structuralschema.JSON{Object: "A"},
   180  					},
   181  				},
   182  			},
   183  		}, `{"a": "A"}`},
   184  		{"null in nullable object with additionalProperties", `{"a": null}`, &structuralschema.Structural{
   185  			Generic: structuralschema.Generic{
   186  				AdditionalProperties: &structuralschema.StructuralOrBool{
   187  					Structural: &structuralschema.Structural{
   188  						Generic: structuralschema.Generic{
   189  							Nullable: true,
   190  							Default:  structuralschema.JSON{Object: "A"},
   191  						},
   192  					},
   193  				},
   194  			},
   195  		}, `{"a": null}`},
   196  		{"null in non-nullable object with additionalProperties", `{"a": null}`, &structuralschema.Structural{
   197  			Generic: structuralschema.Generic{
   198  				AdditionalProperties: &structuralschema.StructuralOrBool{
   199  					Structural: &structuralschema.Structural{
   200  						Generic: structuralschema.Generic{
   201  							Nullable: false,
   202  							Default:  structuralschema.JSON{Object: "A"},
   203  						},
   204  					},
   205  				},
   206  			},
   207  		}, `{"a": "A"}`},
   208  		{"null unknown field", `{"a": null}`, &structuralschema.Structural{
   209  			Generic: structuralschema.Generic{
   210  				AdditionalProperties: &structuralschema.StructuralOrBool{
   211  					Bool: true,
   212  				},
   213  			},
   214  		}, `{"a": null}`},
   215  	}
   216  	for _, tt := range tests {
   217  		t.Run(tt.name, func(t *testing.T) {
   218  			var in interface{}
   219  			if err := json.Unmarshal([]byte(tt.json), &in); err != nil {
   220  				t.Fatal(err)
   221  			}
   222  
   223  			var expected interface{}
   224  			if err := json.Unmarshal([]byte(tt.expected), &expected); err != nil {
   225  				t.Fatal(err)
   226  			}
   227  
   228  			Default(in, tt.schema)
   229  			if !reflect.DeepEqual(in, expected) {
   230  				var buf bytes.Buffer
   231  				enc := json.NewEncoder(&buf)
   232  				enc.SetIndent("", "  ")
   233  				err := enc.Encode(in)
   234  				if err != nil {
   235  					t.Fatalf("unexpected result mashalling error: %v", err)
   236  				}
   237  				t.Errorf("expected: %s\ngot: %s", tt.expected, buf.String())
   238  			}
   239  		})
   240  	}
   241  }
   242  

View as plain text