...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/internal/fields_test.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/internal

     1  /*
     2  Copyright 2018 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 internal
    18  
    19  import (
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
    27  )
    28  
    29  // TestFieldsRoundTrip tests that a fields trie can be round tripped as a path set
    30  func TestFieldsRoundTrip(t *testing.T) {
    31  	tests := []metav1.FieldsV1{
    32  		{
    33  			Raw: []byte(`{"f:metadata":{".":{},"f:name":{}}}`),
    34  		},
    35  		EmptyFields,
    36  	}
    37  
    38  	for _, test := range tests {
    39  		set, err := FieldsToSet(test)
    40  		if err != nil {
    41  			t.Fatalf("Failed to create path set: %v", err)
    42  		}
    43  		output, err := SetToFields(set)
    44  		if err != nil {
    45  			t.Fatalf("Failed to create fields trie from path set: %v", err)
    46  		}
    47  		if !reflect.DeepEqual(test, output) {
    48  			t.Fatalf("Expected round-trip:\ninput: %v\noutput: %v", test, output)
    49  		}
    50  	}
    51  }
    52  
    53  // TestFieldsToSetError tests that errors are picked up by FieldsToSet
    54  func TestFieldsToSetError(t *testing.T) {
    55  	tests := []struct {
    56  		fields    metav1.FieldsV1
    57  		errString string
    58  	}{
    59  		{
    60  			fields: metav1.FieldsV1{
    61  				Raw: []byte(`{"k:{invalid json}":{"f:name":{},".":{}}}`),
    62  			},
    63  			errString: "ReadObjectCB",
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		_, err := FieldsToSet(test.fields)
    69  		if err == nil || !strings.Contains(err.Error(), test.errString) {
    70  			t.Fatalf("Expected error to contain %q but got: %v", test.errString, err)
    71  		}
    72  	}
    73  }
    74  
    75  // TestSetToFieldsError tests that errors are picked up by SetToFields
    76  func TestSetToFieldsError(t *testing.T) {
    77  	validName := "ok"
    78  	invalidPath := fieldpath.Path([]fieldpath.PathElement{{}, {FieldName: &validName}})
    79  
    80  	tests := []struct {
    81  		set       fieldpath.Set
    82  		errString string
    83  	}{
    84  		{
    85  			set:       *fieldpath.NewSet(invalidPath),
    86  			errString: "invalid PathElement",
    87  		},
    88  	}
    89  
    90  	for _, test := range tests {
    91  		_, err := SetToFields(test.set)
    92  		if err == nil || !strings.Contains(err.Error(), test.errString) {
    93  			t.Fatalf("Expected error to contain %q but got: %v", test.errString, err)
    94  		}
    95  	}
    96  }
    97  
    98  func BenchmarkSetToFields(b *testing.B) {
    99  	set := fieldpath.NewSet(
   100  		fieldpath.MakePathOrDie("foo", 0, "bar", "baz"),
   101  		fieldpath.MakePathOrDie("foo", 0, "bar", "zot"),
   102  		fieldpath.MakePathOrDie("foo", 0, "bar"),
   103  		fieldpath.MakePathOrDie("foo", 0),
   104  		fieldpath.MakePathOrDie("foo", 1, "bar", "baz"),
   105  		fieldpath.MakePathOrDie("foo", 1, "bar"),
   106  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "first")),
   107  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "first"), "bar"),
   108  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "second"), "bar"),
   109  	)
   110  
   111  	b.ReportAllocs()
   112  	b.ResetTimer()
   113  	for n := 0; n < b.N; n++ {
   114  		_, err := SetToFields(*set)
   115  		if err != nil {
   116  			b.Fatal(err)
   117  		}
   118  	}
   119  }
   120  
   121  func BenchmarkFieldsToSet(b *testing.B) {
   122  	set := fieldpath.NewSet(
   123  		fieldpath.MakePathOrDie("foo", 0, "bar", "baz"),
   124  		fieldpath.MakePathOrDie("foo", 0, "bar", "zot"),
   125  		fieldpath.MakePathOrDie("foo", 0, "bar"),
   126  		fieldpath.MakePathOrDie("foo", 0),
   127  		fieldpath.MakePathOrDie("foo", 1, "bar", "baz"),
   128  		fieldpath.MakePathOrDie("foo", 1, "bar"),
   129  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "first")),
   130  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "first"), "bar"),
   131  		fieldpath.MakePathOrDie("qux", fieldpath.KeyByFields("name", "second"), "bar"),
   132  	)
   133  	fields, err := SetToFields(*set)
   134  	if err != nil {
   135  		b.Fatal(err)
   136  	}
   137  	b.ReportAllocs()
   138  	b.ResetTimer()
   139  	for n := 0; n < b.N; n++ {
   140  		_, err := FieldsToSet(fields)
   141  		if err != nil {
   142  			b.Fatal(err)
   143  		}
   144  	}
   145  }
   146  

View as plain text