...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package dynamic
16
17 import (
18 "io/ioutil"
19 "log"
20 "testing"
21
22 condition "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
23
24 "github.com/ghodss/yaml"
25 apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
26 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27 "k8s.io/apimachinery/pkg/runtime"
28 )
29
30 func UnmarshalFileToCRD(t *testing.T, fileName string) *apiextensions.CustomResourceDefinition {
31 t.Helper()
32 bytes, err := ioutil.ReadFile(fileName)
33 if err != nil {
34 t.Fatalf("error reading file '%v': %v", fileName, err)
35 }
36 o := &apiextensions.CustomResourceDefinition{}
37 err = yaml.Unmarshal(bytes, o)
38 if err != nil {
39 t.Fatalf("error unmarshalling bytes to CRD: %v", err)
40 }
41 return o
42 }
43
44 func UnmarshalToCRD(fileName string) *apiextensions.CustomResourceDefinition {
45 bytes, err := ioutil.ReadFile(fileName)
46 if err != nil {
47 log.Fatalf("error reading file '%v': %v", fileName, err)
48 }
49 o := &apiextensions.CustomResourceDefinition{}
50 err = yaml.Unmarshal(bytes, o)
51 if err != nil {
52 log.Fatalf("error unmarshalling bytes to CRD: %v", err)
53 }
54 return o
55 }
56
57 func GetConditions(t *testing.T, kccResource *unstructured.Unstructured) []condition.Condition {
58
59 type withConditions struct {
60 Conditions []condition.Condition `json:"conditions"`
61 }
62 type withStatusConditions struct {
63 Status withConditions `json:"status"`
64 }
65 var obj withStatusConditions
66
67
68 if err := runtime.DefaultUnstructuredConverter.FromUnstructured(kccResource.UnstructuredContent(), &obj); err != nil {
69 t.Errorf("error converting to object with status.conditions: %v", err)
70 }
71
72 return obj.Status.Conditions
73 }
74
View as plain text