Skip to content

Commit eda9364

Browse files
committed
Review: Read mock CRD objects via a YAML declaration
1 parent 9e5a541 commit eda9364

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

pkg/mock/mock_data_collector.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mock
22

33
import (
4+
"embed"
45
"io"
56
"log"
67
"testing"
@@ -22,8 +23,12 @@ import (
2223
"k8s.io/client-go/rest"
2324
metricsfake "k8s.io/metrics/pkg/client/clientset/versioned/fake"
2425
"k8s.io/utils/ptr"
26+
"sigs.k8s.io/yaml"
2527
)
2628

29+
//go:embed testdata/crds.yaml
30+
var testDataFS embed.FS
31+
2732
func SetupMockDataCollector(t *testing.T) *data_collector.DataCollector {
2833
t.Helper()
2934

@@ -70,27 +75,15 @@ func SetupMockDataCollector(t *testing.T) *data_collector.DataCollector {
7075
Host: "https://mock-k8s-server",
7176
}
7277

73-
// Create a CRD clientset (using the real clientset, but not actually connecting)
74-
crd := &apiextensionsv1.CustomResourceDefinition{
75-
ObjectMeta: metav1.ObjectMeta{
76-
Name: "testcrd.example.com",
77-
},
78-
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
79-
Group: "example.com",
80-
Names: apiextensionsv1.CustomResourceDefinitionNames{
81-
Kind: "TestCRD",
82-
Plural: "testcrds",
83-
Singular: "testcrd",
84-
},
85-
Scope: apiextensionsv1.NamespaceScoped,
86-
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
87-
{
88-
Name: "v1",
89-
Served: true,
90-
Storage: true,
91-
},
92-
},
93-
},
78+
// Use embedded file
79+
data, err := testDataFS.ReadFile("testdata/crds.yaml")
80+
if err != nil {
81+
t.Fatalf("failed to read embedded testdata/crds.yaml: %v", err)
82+
}
83+
84+
crd := &apiextensionsv1.CustomResourceDefinition{}
85+
if err := yaml.Unmarshal(data, crd); err != nil {
86+
t.Fatalf("failed to unmarshal CRD from testdata/crds.yaml: %v", err)
9487
}
9588

9689
crdClient := apiextensionsfake.NewClientset(crd)
@@ -100,8 +93,6 @@ func SetupMockDataCollector(t *testing.T) *data_collector.DataCollector {
10093
defer ctrl.Finish()
10194
helmClient := mockHelmClient.NewMockClient(ctrl)
10295

103-
104-
10596
helmClient.EXPECT().GetSettings().Return(&cli.EnvSettings{}).AnyTimes()
10697
var mockedRelease = release.Release{
10798
Name: "test",

pkg/mock/testdata/crds.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: testcrd.example.com
5+
spec:
6+
group: example.com
7+
names:
8+
kind: TestCRD
9+
plural: testcrds
10+
singular: testcrd
11+
scope: Namespaced
12+
versions:
13+
- name: v1
14+
served: true
15+
storage: true
16+
schema:
17+
openAPIV3Schema:
18+
type: object
19+
properties:
20+
spec:
21+
type: object
22+
status:
23+
type: object

0 commit comments

Comments
 (0)