func TestRoundTripManagedFields(t *testing.T) {
	tests := []string{
		`null
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    v:3:
      f:alsoPi: {}
    v:3.1415:
      f:pi: {}
    v:false:
      f:notTrue: {}
  manager: foo
  operation: Update
  time: "2001-02-03T04:05:06Z"
- apiVersion: v1beta1
  fieldsType: FieldsV1
  fieldsV1:
    i:5:
      f:i: {}
  manager: foo
  operation: Update
  time: "2011-12-13T14:15:16Z"
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:spec:
      f:containers:
        k:{"name":"c"}:
          f:image: {}
          f:name: {}
  manager: foo
  operation: Apply
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:labels:
        f:app: {}
      f:name: {}
    f:spec:
      f:replicas: {}
      f:selector:
        f:matchLabels:
          f:app: {}
      f:template:
        f:medatada:
          f:labels:
            f:app: {}
        f:spec:
          f:containers:
            k:{"name":"nginx"}:
              .: {}
              f:image: {}
              f:name: {}
              f:ports:
                i:0:
                  f:containerPort: {}
  manager: foo
  operation: Update
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:allowVolumeExpansion: {}
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
      f:parameters:
        f:resturl: {}
        f:restuser: {}
        f:secretName: {}
        f:secretNamespace: {}
    f:provisioner: {}
  manager: foo
  operation: Apply
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
    f:spec:
      f:group: {}
      f:names:
        f:kind: {}
        f:plural: {}
        f:shortNames:
          i:0: {}
        f:singular: {}
      f:scope: {}
      f:versions:
        k:{"name":"v1"}:
          f:name: {}
          f:served: {}
          f:storage: {}
  manager: foo
  operation: Update
`,
	}

	for _, test := range tests {
		t.Run(test, func(t *testing.T) {
			var unmarshaled []metav1.ManagedFieldsEntry
			if err := yaml.Unmarshal([]byte(test), &unmarshaled); err != nil {
				t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
			}
			decoded, err := decodeManagedFields(unmarshaled)
			if err != nil {
				t.Fatalf("did not expect decoding error but got: %v", err)
			}
			encoded, err := encodeManagedFields(&decoded)
			if err != nil {
				t.Fatalf("did not expect encoding error but got: %v", err)
			}
			marshaled, err := yaml.Marshal(&encoded)
			if err != nil {
				t.Fatalf("did not expect yaml marshalling error but got: %v", err)
			}
			if !reflect.DeepEqual(string(marshaled), test) {
				t.Fatalf("expected:\n%v\nbut got:\n%v", test, string(marshaled))
			}
		})
	}
}
// TestRoundTripManagedFields will roundtrip ManagedFields from the wire format
// (api format) to the format used by sigs.k8s.io/structured-merge-diff and back
TestRoundTripManagedFields will roundtrip ManagedFields from the wire format
(api format) to the format used by sigs.k8s.io/structured-merge-diff and back
// TestRoundTripManagedFields will roundtrip ManagedFields from the wire format // ( api format ) to the format used by sigs . k8s . io / structured - merge - diff and back
func TestRoundTripManagedFields(t *testing.T) {
	tests := []string{
		`null
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    v:3:
      f:alsoPi: {}
    v:3.1415:
      f:pi: {}
    v:false:
      f:notTrue: {}
  manager: foo
  operation: Update
  time: "2001-02-03T04:05:06Z"
- apiVersion: v1beta1
  fieldsType: FieldsV1
  fieldsV1:
    i:5:
      f:i: {}
  manager: foo
  operation: Update
  time: "2011-12-13T14:15:16Z"
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:spec:
      f:containers:
        k:{"name":"c"}:
          f:image: {}
          f:name: {}
  manager: foo
  operation: Apply
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:labels:
        f:app: {}
      f:name: {}
    f:spec:
      f:replicas: {}
      f:selector:
        f:matchLabels:
          f:app: {}
      f:template:
        f:medatada:
          f:labels:
            f:app: {}
        f:spec:
          f:containers:
            k:{"name":"nginx"}:
              .: {}
              f:image: {}
              f:name: {}
              f:ports:
                i:0:
                  f:containerPort: {}
  manager: foo
  operation: Update
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:allowVolumeExpansion: {}
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
      f:parameters:
        f:resturl: {}
        f:restuser: {}
        f:secretName: {}
        f:secretNamespace: {}
    f:provisioner: {}
  manager: foo
  operation: Apply
`,
		`- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
    f:spec:
      f:group: {}
      f:names:
        f:kind: {}
        f:plural: {}
        f:shortNames:
          i:0: {}
        f:singular: {}
      f:scope: {}
      f:versions:
        k:{"name":"v1"}:
          f:name: {}
          f:served: {}
          f:storage: {}
  manager: foo
  operation: Update
`,
	}
	for _, test := range tests {
		t.Run(test, func(t *testing.T) {
			var unmarshaled []metav1.ManagedFieldsEntry
			if err := yaml.Unmarshal([]byte(test), &unmarshaled); err != nil {
				t.Fatalf("did not expect yaml unmarshalling error but got: %v", err)
			}
			decoded, err := decodeManagedFields(unmarshaled)
			if err != nil {
				t.Fatalf("did not expect decoding error but got: %v", err)
			}
			encoded, err := encodeManagedFields(&decoded)
			if err != nil {
				t.Fatalf("did not expect encoding error but got: %v", err)
			}
			marshaled, err := yaml.Marshal(&encoded)
			if err != nil {
				t.Fatalf("did not expect yaml marshalling error but got: %v", err)
			}
			if !reflect.DeepEqual(string(marshaled), test) {
				t.Fatalf("expected:\n%v\nbut got:\n%v", test, string(marshaled))
			}
		})
	}
}
func TestRoundTripManagedFields ( t * testing . T ) { tests := [ ] string { `null
` , `- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    v:3:
      f:alsoPi: {}
    v:3.1415:
      f:pi: {}
    v:false:
      f:notTrue: {}
  manager: foo
  operation: Update
  time: "2001-02-03T04:05:06Z"
- apiVersion: v1beta1
  fieldsType: FieldsV1
  fieldsV1:
    i:5:
      f:i: {}
  manager: foo
  operation: Update
  time: "2011-12-13T14:15:16Z"
` , `- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:spec:
      f:containers:
        k:{"name":"c"}:
          f:image: {}
          f:name: {}
  manager: foo
  operation: Apply
` , `- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:labels:
        f:app: {}
      f:name: {}
    f:spec:
      f:replicas: {}
      f:selector:
        f:matchLabels:
          f:app: {}
      f:template:
        f:medatada:
          f:labels:
            f:app: {}
        f:spec:
          f:containers:
            k:{"name":"nginx"}:
              .: {}
              f:image: {}
              f:name: {}
              f:ports:
                i:0:
                  f:containerPort: {}
  manager: foo
  operation: Update
` , `- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:allowVolumeExpansion: {}
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
      f:parameters:
        f:resturl: {}
        f:restuser: {}
        f:secretName: {}
        f:secretNamespace: {}
    f:provisioner: {}
  manager: foo
  operation: Apply
` , `- apiVersion: v1
  fieldsType: FieldsV1
  fieldsV1:
    f:apiVersion: {}
    f:kind: {}
    f:metadata:
      f:name: {}
    f:spec:
      f:group: {}
      f:names:
        f:kind: {}
        f:plural: {}
        f:shortNames:
          i:0: {}
        f:singular: {}
      f:scope: {}
      f:versions:
        k:{"name":"v1"}:
          f:name: {}
          f:served: {}
          f:storage: {}
  manager: foo
  operation: Update
` , } for _ , test := range tests { t . Run ( test , func ( t * testing . T ) { var unmarshaled [ ] metav1 . ManagedFieldsEntry if err := yaml . Unmarshal ( [ ] byte ( test ) , & unmarshaled ) ; err != nil { t . Fatalf ( " " , err ) } decoded , err := decodeManagedFields ( unmarshaled ) if err != nil { t . Fatalf ( " " , err ) } encoded , err := encodeManagedFields ( & decoded ) if err != nil { t . Fatalf ( " " , err ) } marshaled , err := yaml . Marshal ( & encoded ) if err != nil { t . Fatalf ( " " , err ) } if ! reflect . DeepEqual ( string ( marshaled ) , test ) { t . Fatalf ( " \n \n \n " , test , string ( marshaled ) ) } } ) } }
TestRoundTripManagedFields will roundtrip ManagedFields from the wire format
(api format) to the format used by sigs.k8s.io/structured-merge-diff and back
TestRoundTripManagedFields will roundtrip ManagedFields from the wire format ( api format ) to the format used by sigs . k8s . io / structured - merge - diff and back