-
Notifications
You must be signed in to change notification settings - Fork 81
/
helpers.go
109 lines (94 loc) · 3.1 KB
/
helpers.go
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 (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
// TestCase defines a testcase for roundtrip and defaulting tests
type TestCase struct {
name, in, out string
codec runtime.Codec
}
// RunTestsOnYAMLData decodes the yaml file from specified path, encodes the object and matches
// with expected yaml in specified path
func RunTestsOnYAMLData(t *testing.T, tests []TestCase) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
roundTrip(t, tc)
})
}
}
func decodeYAML(t *testing.T, path string, codec runtime.Codec) runtime.Object {
content, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
// decode to internal type
object, err := runtime.Decode(codec, content)
if err != nil {
t.Fatal(err)
}
return object
}
func getCodecForGV(codecs serializer.CodecFactory, gv schema.GroupVersion) (runtime.Codec, error) {
mediaType := runtime.ContentTypeYAML
serializerInfo, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
if !ok {
return nil, fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
}
codec := codecs.CodecForVersions(serializerInfo.Serializer, codecs.UniversalDeserializer(), gv, nil)
return codec, nil
}
func matchOutputFile(t *testing.T, actual []byte, expectedFilePath string) {
expected, err := ioutil.ReadFile(expectedFilePath)
if err != nil && !os.IsNotExist(err) {
t.Fatalf("couldn't read test data: %v", err)
}
needsUpdate := false
const updateEnvVar = "UPDATE_COMPONENTCONFIG_FIXTURE_DATA"
if os.IsNotExist(err) {
needsUpdate = true
if os.Getenv(updateEnvVar) != "true" {
t.Error("couldn't find test data")
}
} else {
if !bytes.Equal(expected, actual) {
t.Errorf("Output does not match expected, diff (- want, + got):\n%s\n",
cmp.Diff(string(expected), string(actual)))
needsUpdate = true
}
}
if needsUpdate {
if os.Getenv(updateEnvVar) == "true" {
if err := os.MkdirAll(filepath.Dir(expectedFilePath), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(expectedFilePath, actual, 0644); err != nil {
t.Fatal(err)
}
t.Error("wrote expected test data... verify, commit, and rerun tests")
} else {
t.Errorf("if the diff is expected because of a new type or a new field, "+
"re-run with %s=true to update the compatibility data or generate missing files", updateEnvVar)
}
}
}