...

Source file src/sigs.k8s.io/structured-merge-diff/v4/fieldpath/serialize_test.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/fieldpath

     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 fieldpath
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestSerializeV1(t *testing.T) {
    27  	for i := 0; i < 500; i++ {
    28  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    29  			x := NewSet()
    30  			for j := 0; j < 50; j++ {
    31  				x.Insert(randomPathMaker.makePath(2, 5))
    32  			}
    33  			b, err := x.ToJSON()
    34  			if err != nil {
    35  				t.Fatalf("Failed to serialize %#v: %v", x, err)
    36  			}
    37  			x2 := NewSet()
    38  			err = x2.FromJSON(bytes.NewReader(b))
    39  			if err != nil {
    40  				t.Fatalf("Failed to deserialize %s: %v\n%#v", b, err, x)
    41  			}
    42  			if !x2.Equals(x) {
    43  				b2, _ := x2.ToJSON()
    44  				t.Fatalf("failed to reproduce original:\n\n%s\n\n%s\n\n%s\n\n%s\n", x, b, b2, x2)
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestSerializeV1GoldenData(t *testing.T) {
    51  	examples := []string{
    52  		`{"f:aaa":{},"f:aab":{},"f:aac":{},"f:aad":{},"f:aae":{},"f:aaf":{},"k:{\"name\":\"first\"}":{},"k:{\"name\":\"second\"}":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{},"v:2":{},"v:3":{},"v:\"aa\"":{},"v:\"ab\"":{},"v:true":{},"i:1":{},"i:2":{},"i:3":{},"i:4":{}}`,
    53  		`{"f:aaa":{"k:{\"name\":\"second\"}":{"v:3":{"f:aab":{}}},"v:3":{},"v:true":{}},"f:aab":{"f:aaa":{},"f:aaf":{"k:{\"port\":443,\"protocol\":\"udp\"}":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"k:{\"name\":\"first\"}":{}},"f:aac":{"f:aaa":{"v:1":{}},"f:aac":{},"v:3":{"k:{\"name\":\"second\"}":{}}},"f:aad":{"f:aac":{"v:1":{}},"f:aaf":{"k:{\"name\":\"first\"}":{"k:{\"name\":\"first\"}":{}}},"i:1":{"i:1":{},"i:3":{"v:true":{}}}},"f:aae":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:4":{"f:aaf":{}}},"f:aaf":{"i:1":{"f:aac":{}},"i:2":{},"i:3":{}},"k:{\"name\":\"first\"}":{"f:aad":{"f:aaf":{}}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"f:aaa":{"f:aad":{}}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"f:aac":{},"k:{\"name\":\"first\"}":{"i:3":{}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"i:4":{}}},"v:1":{"f:aac":{"i:4":{}},"f:aaf":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{}},"v:2":{"f:aad":{"f:aaf":{}},"i:1":{}},"v:3":{"f:aaa":{},"k:{\"name\":\"first\"}":{},"i:2":{}},"v:\"aa\"":{"f:aab":{"f:aaf":{}},"f:aae":{},"k:{\"name\":\"first\"}":{"f:aad":{}},"i:2":{}},"v:\"ab\"":{"f:aaf":{"i:4":{}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:1":{"f:aae":{"i:4":{}}}},"v:true":{"k:{\"name\":\"second\"}":{"f:aaa":{}},"i:2":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"i:1":{"i:3":{"f:aaf":{}}},"i:2":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"v:1":{}}},"i:3":{"f:aab":{"v:true":{"v:\"aa\"":{}}},"f:aaf":{},"i:1":{}},"i:4":{"v:\"aa\"":{"f:aab":{"k:{\"name\":\"second\"}":{}}}}}`,
    54  	}
    55  	for i, str := range examples {
    56  		t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
    57  			x := NewSet()
    58  			err := x.FromJSON(strings.NewReader(str))
    59  			if err != nil {
    60  				t.Fatalf("Failed to deserialize %s: %v\n%#v", str, err, x)
    61  			}
    62  			b, err := x.ToJSON()
    63  			if err != nil {
    64  				t.Fatalf("Failed to serialize %#v: %v", x, err)
    65  			}
    66  			if string(b) != str {
    67  				t.Fatalf("Failed;\ngot:  %s\nwant: %s\n", b, str)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestDropUnknown(t *testing.T) {
    74  	input := `{"f:aaa":{},"r:aab":{}}`
    75  	expect := `{"f:aaa":{}}`
    76  	x := NewSet()
    77  	err := x.FromJSON(strings.NewReader(input))
    78  	if err != nil {
    79  		t.Errorf("Failed to deserialize %s: %v\n%#v", input, err, x)
    80  	}
    81  	b, err := x.ToJSON()
    82  	if err != nil {
    83  		t.Errorf("Failed to serialize %#v: %v", x, err)
    84  		return
    85  	}
    86  	if string(b) != expect {
    87  		t.Errorf("Failed;\ngot:  %s\nwant: %s\n", b, expect)
    88  	}
    89  }
    90  

View as plain text