@@ -10,14 +10,24 @@ import (
1010 "time"
1111
1212 "github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
13+ "helm.sh/helm/v3/pkg/cli"
14+ "helm.sh/helm/v3/pkg/release"
1315
1416 appsv1 "k8s.io/api/apps/v1"
1517 corev1 "k8s.io/api/core/v1"
1618 rbacv1 "k8s.io/api/rbac/v1"
1719 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820 "k8s.io/apimachinery/pkg/runtime"
1921
22+ helmclient "github.com/mittwald/go-helm-client"
23+ mockHelmClient "github.com/mittwald/go-helm-client/mock"
24+ "go.uber.org/mock/gomock"
25+
26+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
27+ apiextensionsfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
2028 "k8s.io/client-go/kubernetes/fake"
29+ "k8s.io/client-go/rest"
30+ metricsfake "k8s.io/metrics/pkg/client/clientset/versioned/fake"
2131)
2232
2333// helper creates int32 ptr
@@ -64,34 +74,67 @@ func setupDataCollector(t *testing.T) *data_collector.DataCollector {
6474 }
6575
6676 client := fake .NewSimpleClientset (objs ... )
77+ // Mock rest.Config
78+ restConfig := & rest.Config {
79+ Host : "https://mock-k8s-server" ,
80+ }
81+
82+ // Create a CRD clientset (using the real clientset, but not actually connecting)
83+ crd := & apiextensionsv1.CustomResourceDefinition {
84+ ObjectMeta : metav1.ObjectMeta {
85+ Name : "testcrd.example.com" ,
86+ },
87+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
88+ Group : "example.com" ,
89+ Names : apiextensionsv1.CustomResourceDefinitionNames {
90+ Kind : "TestCRD" ,
91+ Plural : "testcrds" ,
92+ Singular : "testcrd" ,
93+ },
94+ Scope : apiextensionsv1 .NamespaceScoped ,
95+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
96+ {
97+ Name : "v1" ,
98+ Served : true ,
99+ Storage : true ,
100+ },
101+ },
102+ },
103+ }
104+ // crdClient := &apiextensionsclientset.Clientset{}
105+ crdClient := apiextensionsfake .NewSimpleClientset (crd )
106+ metricsClient := metricsfake .NewSimpleClientset ()
107+ // helmClient := &FakeHelmClient{}
108+
109+ ctrl := gomock .NewController (t )
110+ defer ctrl .Finish ()
111+ helmClient := mockHelmClient .NewMockClient (ctrl )
112+ if helmClient == nil {
113+ t .Fail ()
114+ }
115+ helmClient .EXPECT ().GetSettings ().Return (& cli.EnvSettings {}).AnyTimes ()
116+ var mockedRelease = release.Release {Name : "test" , Namespace : "test" , Manifest : "apiVersion: v1\n kind: ConfigMap\n metadata:\n name: example-config\n namespace: default\n data:\n key: value\n " }
117+ helmClient .EXPECT ().ListDeployedReleases ().Return ([]* release.Release {& mockedRelease }, nil ).AnyTimes ()
67118
68119 return & data_collector.DataCollector {
69- BaseDir : tmpDir ,
70- Namespaces : []string {"default" },
71- Logger : log .New (io .Discard , "" , 0 ),
72- K8sCoreClientSet : client ,
120+ BaseDir : tmpDir ,
121+ Namespaces : []string {"default" },
122+ Logger : log .New (io .Discard , "" , 0 ),
123+ K8sCoreClientSet : client ,
124+ K8sCrdClientSet : crdClient ,
125+ K8sRestConfig : restConfig ,
126+ K8sMetricsClientSet : metricsClient ,
127+ K8sHelmClientSet : map [string ]helmclient.Client {"default" : helmClient },
128+
73129 // Leave other client sets nil; we will not execute jobs that depend on them in this focused test.
74130 }
75131}
76132
77133func TestCommonJobList_SelectedJobsProduceFiles (t * testing.T ) {
78134 dc := setupDataCollector (t )
79-
80- // Jobs we explicitly validate (keep focused; others require additional fake clients)
81- targetJobs := map [string ]struct {}{
82- "pod-list" : {},
83- "service-list" : {},
84- "deployment-list" : {},
85- "roles-list" : {},
86- "configmap-list" : {},
87- }
88-
89135 jobList := CommonJobList ()
90136
91137 for _ , job := range jobList {
92- if _ , ok := targetJobs [job .Name ]; ! ok {
93- continue
94- }
95138
96139 ch := make (chan JobResult , 1 )
97140 ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
0 commit comments