File: roundtrip.go

package info (click to toggle)
golang-k8s-component-base 0.32.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,432 kB
  • sloc: makefile: 4
file content (109 lines) | stat: -rw-r--r-- 3,482 bytes parent folder | download
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
/*
Copyright 2019 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 testing

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/google/go-cmp/cmp"
	apiequality "k8s.io/apimachinery/pkg/api/equality"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/runtime/serializer"
)

// RoundTripTest runs roundtrip tests for given scheme
func RoundTripTest(t *testing.T, scheme *runtime.Scheme, codecs serializer.CodecFactory) {
	tc := GetRoundtripTestCases(t, scheme, codecs)
	RunTestsOnYAMLData(t, tc)
}

// GetRoundtripTestCases returns the testcases for roundtrip testing for given scheme
func GetRoundtripTestCases(t *testing.T, scheme *runtime.Scheme, codecs serializer.CodecFactory) []TestCase {
	cases := []TestCase{}
	versionsForKind := map[schema.GroupKind][]string{}
	for gvk := range scheme.AllKnownTypes() {
		if gvk.Version != runtime.APIVersionInternal {
			versionsForKind[gvk.GroupKind()] = append(versionsForKind[gvk.GroupKind()], gvk.Version)
		}
	}

	for gk, versions := range versionsForKind {
		testdir := filepath.Join("testdata", gk.Kind, "roundtrip")
		dirs, err := os.ReadDir(testdir)
		if err != nil {
			t.Fatalf("failed to read testdir %s: %v", testdir, err)
		}

		for _, dir := range dirs {
			for _, vin := range versions {
				for _, vout := range versions {
					marshalGVK := gk.WithVersion(vout)
					codec, err := getCodecForGV(codecs, marshalGVK.GroupVersion())
					if err != nil {
						t.Fatalf("failed to get codec for %v: %v", marshalGVK.GroupVersion().String(), err)
					}

					testname := dir.Name()
					cases = append(cases, TestCase{
						name:  fmt.Sprintf("%s_%sTo%s_%s", gk.Kind, vin, vout, testname),
						in:    filepath.Join(testdir, testname, vin+".yaml"),
						out:   filepath.Join(testdir, testname, vout+".yaml"),
						codec: codec,
					})
				}
			}
		}
	}
	return cases
}

func roundTrip(t *testing.T, tc TestCase) {
	object := decodeYAML(t, tc.in, tc.codec)

	// original object of internal type
	original := object

	// encode (serialize) the object using the provided codec
	data, err := runtime.Encode(tc.codec, object)
	if err != nil {
		t.Fatalf("failed to encode object: %v", err)
	}

	// ensure that the encoding should not alter the object
	if !apiequality.Semantic.DeepEqual(original, object) {
		t.Fatalf("encode altered the object, diff (- want, + got): \n%v", cmp.Diff(original, object))
	}

	// decode (deserialize) the encoded data back into an object
	obj2, err := runtime.Decode(tc.codec, data)
	if err != nil {
		t.Fatalf("failed to decode: %v", err)
	}

	// ensure that the object produced from decoding the encoded data is equal
	// to the original object
	if !apiequality.Semantic.DeepEqual(original, obj2) {
		t.Fatalf("object was not the same after roundtrip, diff (- want, + got):\n%v", cmp.Diff(object, obj2))
	}

	// match with the input file, checks if they're the same after roundtrip
	matchOutputFile(t, data, tc.out)
}