Skip to content

Commit 5e2bd87

Browse files
committed
Fix: Use the mock data_collector in various tests
1 parent d954b2e commit 5e2bd87

File tree

5 files changed

+57
-96
lines changed

5 files changed

+57
-96
lines changed

pkg/jobs/job_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,18 @@ package jobs
33
import (
44
"context"
55
"errors"
6-
"io"
7-
"log"
86
"os"
97
"path/filepath"
108
"testing"
119
"time"
1210

1311
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
12+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/mock"
1413
)
1514

1615
// Test successful job execution and file writing
1716
func TestJobCollect_Success(t *testing.T) {
18-
tmpDir := t.TempDir()
19-
dc := &data_collector.DataCollector{
20-
BaseDir: tmpDir,
21-
Logger: log.New(io.Discard, "", 0),
22-
}
23-
17+
dc := mock.SetupMockDataCollector(t)
2418
job := Job{
2519
Name: "test-job",
2620
Timeout: time.Second,
@@ -40,7 +34,7 @@ func TestJobCollect_Success(t *testing.T) {
4034
t.Fatalf("expected not skipped")
4135
}
4236
// Check file was written
43-
content, err := os.ReadFile(filepath.Join(tmpDir, "output.txt"))
37+
content, err := os.ReadFile(filepath.Join(dc.BaseDir, "output.txt"))
4438
if err != nil {
4539
t.Fatalf("file not written: %v", err)
4640
}
@@ -51,10 +45,7 @@ func TestJobCollect_Success(t *testing.T) {
5145

5246
// Test job skipped scenario
5347
func TestJobCollect_Skipped(t *testing.T) {
54-
dc := &data_collector.DataCollector{
55-
BaseDir: t.TempDir(),
56-
Logger: log.New(io.Discard, "", 0),
57-
}
48+
dc := mock.SetupMockDataCollector(t)
5849
job := Job{
5950
Name: "skip-job",
6051
Timeout: time.Second,
@@ -73,10 +64,7 @@ func TestJobCollect_Skipped(t *testing.T) {
7364

7465
// Test job error scenario
7566
func TestJobCollect_Error(t *testing.T) {
76-
dc := &data_collector.DataCollector{
77-
BaseDir: t.TempDir(),
78-
Logger: log.New(io.Discard, "", 0),
79-
}
67+
dc := mock.SetupMockDataCollector(t)
8068
job := Job{
8169
Name: "error-job",
8270
Timeout: time.Second,
@@ -95,10 +83,7 @@ func TestJobCollect_Error(t *testing.T) {
9583

9684
// Test job timeout scenario
9785
func TestJobCollect_Timeout(t *testing.T) {
98-
dc := &data_collector.DataCollector{
99-
BaseDir: t.TempDir(),
100-
Logger: log.New(io.Discard, "", 0),
101-
}
86+
dc := mock.SetupMockDataCollector(t)
10287
job := Job{
10388
Name: "timeout-job",
10489
Timeout: time.Millisecond * 10,

pkg/jobs/ngf_job_list_test.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
10+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/mock"
1111
corev1 "k8s.io/api/core/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/client-go/kubernetes/fake"
@@ -38,7 +38,6 @@ func TestNGFJobList(t *testing.T) {
3838
}
3939

4040
func TestNGFJobExecNginxGatewayVersion(t *testing.T) {
41-
tmpDir := t.TempDir()
4241
client := fake.NewSimpleClientset(&corev1.Pod{
4342
ObjectMeta: metav1.ObjectMeta{
4443
Name: "nginx-gateway-test-pod",
@@ -51,14 +50,10 @@ func TestNGFJobExecNginxGatewayVersion(t *testing.T) {
5150
},
5251
})
5352

54-
dc := &data_collector.DataCollector{
55-
BaseDir: tmpDir,
56-
Namespaces: []string{"default"},
57-
K8sCoreClientSet: client,
58-
Logger: log.New(io.Discard, "", 0),
59-
PodExecutor: func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
60-
return []byte("gateway version output"), nil
61-
},
53+
dc := mock.SetupMockDataCollector(t)
54+
dc.K8sCoreClientSet = client
55+
dc.PodExecutor = func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
56+
return []byte("gateway version output"), nil
6257
}
6358

6459
jobs := NGFJobList()
@@ -97,7 +92,6 @@ func TestNGFJobExecNginxGatewayVersion(t *testing.T) {
9792
}
9893

9994
func TestNGFJobExecNginxT(t *testing.T) {
100-
tmpDir := t.TempDir()
10195
client := fake.NewSimpleClientset(&corev1.Pod{
10296
ObjectMeta: metav1.ObjectMeta{
10397
Name: "nginx-gateway-test-pod",
@@ -110,15 +104,13 @@ func TestNGFJobExecNginxT(t *testing.T) {
110104
},
111105
})
112106

113-
dc := &data_collector.DataCollector{
114-
BaseDir: tmpDir,
115-
Namespaces: []string{"default"},
116-
K8sCoreClientSet: client,
117-
Logger: log.New(io.Discard, "", 0),
118-
PodExecutor: func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
119-
return []byte("nginx -t output"), nil
120-
},
107+
dc := mock.SetupMockDataCollector(t)
108+
dc.Namespaces = []string{"default"}
109+
dc.K8sCoreClientSet = client
110+
dc.PodExecutor = func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
111+
return []byte("nginx -t output"), nil
121112
}
113+
dc.Logger = log.New(io.Discard, "", 0)
122114

123115
jobs := NGFJobList()
124116
var tJob Job

pkg/jobs/ngx_job_list_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@ package jobs
22

33
import (
44
"context"
5-
"io"
6-
"log"
75
"path/filepath"
86
"strings"
97
"testing"
108
"time"
119

12-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
10+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/mock"
1311
corev1 "k8s.io/api/core/v1"
1412
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1513
"k8s.io/client-go/kubernetes/fake"
1614
)
1715

1816
func TestNGXJobList_ExecNginxT(t *testing.T) {
19-
tmpDir := t.TempDir()
20-
dc := &data_collector.DataCollector{
21-
BaseDir: tmpDir,
22-
Logger: log.New(io.Discard, "", 0),
23-
Namespaces: []string{"default"},
24-
}
17+
// dc := &data_collector.DataCollector{
18+
// BaseDir: tmpDir,
19+
// Logger: log.New(io.Discard, "", 0),
20+
// Namespaces: []string{"default"},
21+
// }
22+
23+
dc := mock.SetupMockDataCollector(t)
2524

2625
// Create a fake pod named "nginx-123" in the "default" namespace
2726
pod := &corev1.Pod{
@@ -62,8 +61,8 @@ func TestNGXJobList_ExecNginxT(t *testing.T) {
6261
if string(content) != "nginx -T output" {
6362
t.Errorf("unexpected file content: %s", string(content))
6463
}
65-
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(tmpDir)) {
66-
t.Errorf("file path %s does not start with tmpDir %s", file, tmpDir)
64+
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(dc.BaseDir)) {
65+
t.Errorf("file path %s does not start with tmpDir %s", file, dc.BaseDir)
6766
}
6867
found = true
6968
}

pkg/jobs/nic_job_list_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package jobs
33
import (
44
"context"
55
"encoding/json"
6-
"io"
7-
"log"
86
"path/filepath"
97
"strings"
108
"testing"
119
"time"
1210

1311
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds"
14-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
12+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/mock"
1513
corev1 "k8s.io/api/core/v1"
1614
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1715
"k8s.io/client-go/kubernetes/fake"
@@ -28,12 +26,9 @@ func mockQueryCRD(crd crds.Crd, namespace string, ctx context.Context) ([]byte,
2826
}
2927

3028
func TestNICJobList_ExecJobs(t *testing.T) {
31-
tmpDir := t.TempDir()
32-
dc := &data_collector.DataCollector{
33-
BaseDir: tmpDir,
34-
Logger: log.New(io.Discard, "", 0),
35-
Namespaces: []string{"test-ns"},
36-
}
29+
dc := mock.SetupMockDataCollector(t)
30+
dc.Namespaces = []string{"test-ns"}
31+
3732
// Mock PodExecutor and QueryCRD
3833
dc.PodExecutor = mockPodExecutor
3934
dc.QueryCRD = mockQueryCRD
@@ -58,7 +53,7 @@ func TestNICJobList_ExecJobs(t *testing.T) {
5853
t.Errorf("Job %s returned error: %v", job.Name, result.Error)
5954
}
6055
for file, content := range result.Files {
61-
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(tmpDir)) {
56+
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(dc.BaseDir)) {
6257
t.Errorf("File path %s does not start with tmpDir", file)
6358
}
6459
if len(content) == 0 {
@@ -72,12 +67,8 @@ func TestNICJobList_ExecJobs(t *testing.T) {
7267
}
7368

7469
func TestNICJobList_CRDObjects(t *testing.T) {
75-
tmpDir := t.TempDir()
76-
dc := &data_collector.DataCollector{
77-
BaseDir: tmpDir,
78-
Logger: log.New(io.Discard, "", 0),
79-
Namespaces: []string{"test-ns"},
80-
}
70+
dc := mock.SetupMockDataCollector(t)
71+
dc.Namespaces = []string{"test-ns"}
8172
dc.QueryCRD = mockQueryCRD
8273

8374
jobList := NICJobList()
@@ -93,7 +84,7 @@ func TestNICJobList_CRDObjects(t *testing.T) {
9384
t.Errorf("CRD job returned error: %v", result.Error)
9485
}
9586
for file, content := range result.Files {
96-
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(tmpDir)) {
87+
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(dc.BaseDir)) {
9788
t.Errorf("File path %s does not start with tmpDir", file)
9889
}
9990
var out map[string]interface{}

pkg/jobs/nim_job_list_test.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ package jobs
22

33
import (
44
"context"
5-
"io"
6-
"log"
75
"path/filepath"
86
"strings"
97
"testing"
108
"time"
119

12-
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/data_collector"
10+
"github.com/nginxinc/nginx-k8s-supportpkg/pkg/mock"
1311
corev1 "k8s.io/api/core/v1"
1412
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1513
"k8s.io/apimachinery/pkg/runtime"
1614
"k8s.io/client-go/kubernetes/fake"
1715
)
1816

1917
func TestNIMJobList_ExecJobs(t *testing.T) {
20-
tmpDir := t.TempDir()
21-
dc := &data_collector.DataCollector{
22-
BaseDir: tmpDir,
23-
Logger: log.New(io.Discard, "", 0),
24-
Namespaces: []string{"default"},
25-
}
18+
// dc := &data_collector.DataCollector{
19+
// BaseDir: tmpDir,
20+
// Logger: log.New(io.Discard, "", 0),
21+
// Namespaces: []string{"default"},
22+
// }
23+
dc := mock.SetupMockDataCollector(t)
24+
dc.Namespaces = []string{"default"}
2625

2726
// Create fake pods for each job type
2827
pods := []*corev1.Pod{
@@ -94,8 +93,8 @@ func TestNIMJobList_ExecJobs(t *testing.T) {
9493
t.Errorf("Job %s returned error: %v", job.Name, result.Error)
9594
}
9695
for file, content := range result.Files {
97-
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(tmpDir)) {
98-
t.Errorf("File path %s does not start with tmpDir %s", file, tmpDir)
96+
if !strings.HasPrefix(filepath.ToSlash(file), filepath.ToSlash(dc.BaseDir)) {
97+
t.Errorf("File path %s does not start with tmpDir %s", file, dc.BaseDir)
9998
}
10099
if len(content) == 0 {
101100
t.Errorf("File %s has empty content", file)
@@ -108,25 +107,20 @@ func TestNIMJobList_ExecJobs(t *testing.T) {
108107
}
109108

110109
func TestNIMJobList_ExcludeFlags(t *testing.T) {
111-
tmpDir := t.TempDir()
112-
dc := &data_collector.DataCollector{
113-
BaseDir: tmpDir,
114-
Logger: log.New(io.Discard, "", 0),
115-
Namespaces: []string{"default"},
116-
K8sCoreClientSet: fake.NewSimpleClientset(&corev1.Pod{
117-
ObjectMeta: metav1.ObjectMeta{
118-
Name: "clickhouse-456",
119-
Namespace: "default",
120-
},
121-
Spec: corev1.PodSpec{
122-
Containers: []corev1.Container{{Name: "clickhouse-server"}},
123-
},
124-
}),
125-
PodExecutor: func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
126-
return []byte("output"), nil
110+
dc := mock.SetupMockDataCollector(t)
111+
dc.Namespaces = []string{"default"}
112+
dc.K8sCoreClientSet = fake.NewSimpleClientset(&corev1.Pod{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "clickhouse-456",
115+
Namespace: "default",
116+
},
117+
Spec: corev1.PodSpec{
118+
Containers: []corev1.Container{{Name: "clickhouse-server"}},
127119
},
120+
})
121+
dc.PodExecutor = func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) {
122+
return []byte("output"), nil
128123
}
129-
130124
// Test ExcludeTimeSeriesData for exec-clickhouse-data
131125
dc.ExcludeTimeSeriesData = true
132126
for _, job := range NIMJobList() {

0 commit comments

Comments
 (0)