|
| 1 | +package data_collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/nginxinc/nginx-k8s-supportpkg/pkg/crds" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/kubernetes/fake" |
| 16 | + "k8s.io/client-go/rest" |
| 17 | +) |
| 18 | + |
| 19 | +func TestNewDataCollector_Success(t *testing.T) { |
| 20 | + dc := &DataCollector{Namespaces: []string{"default"}} |
| 21 | + err := NewDataCollector(dc) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("expected no error, got %v", err) |
| 24 | + } |
| 25 | + if dc.BaseDir == "" { |
| 26 | + t.Error("BaseDir should be set") |
| 27 | + } |
| 28 | + if dc.Logger == nil { |
| 29 | + t.Error("Logger should be set") |
| 30 | + } |
| 31 | + if dc.LogFile == nil { |
| 32 | + t.Error("LogFile should be set") |
| 33 | + } |
| 34 | + if dc.K8sCoreClientSet == nil { |
| 35 | + t.Error("K8sCoreClientSet should be set") |
| 36 | + } |
| 37 | + if dc.K8sCrdClientSet == nil { |
| 38 | + t.Error("K8sCrdClientSet should be set") |
| 39 | + } |
| 40 | + if dc.K8sMetricsClientSet == nil { |
| 41 | + t.Error("K8sMetricsClientSet should be set") |
| 42 | + } |
| 43 | + if dc.K8sHelmClientSet == nil { |
| 44 | + t.Error("K8sHelmClientSet should be set") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestWrapUp_CreatesTarball(t *testing.T) { |
| 49 | + tmpDir := t.TempDir() |
| 50 | + logFile, _ := os.Create(filepath.Join(tmpDir, "supportpkg.log")) |
| 51 | + dc := &DataCollector{ |
| 52 | + BaseDir: tmpDir, |
| 53 | + LogFile: logFile, |
| 54 | + Logger: log.New(io.Discard, "", 0), |
| 55 | + } |
| 56 | + product := "nginx" |
| 57 | + tarball, err := dc.WrapUp(product) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("WrapUp failed: %v", err) |
| 60 | + } |
| 61 | + if _, err := os.Stat(tarball); err != nil { |
| 62 | + t.Errorf("tarball not created: %v", err) |
| 63 | + } |
| 64 | + _ = os.Remove(tarball) |
| 65 | +} |
| 66 | + |
| 67 | +func TestRealPodExecutor_ReturnsOutput(t *testing.T) { |
| 68 | + dc := &DataCollector{ |
| 69 | + K8sCoreClientSet: fake.NewClientset(), |
| 70 | + K8sRestConfig: &rest.Config{}, |
| 71 | + } |
| 72 | + // Replace RealPodExecutor with a mock for testing |
| 73 | + dc.PodExecutor = func(namespace, pod, container string, command []string, ctx context.Context) ([]byte, error) { |
| 74 | + return []byte("output"), nil |
| 75 | + } |
| 76 | + out, err := dc.PodExecutor("default", "pod", "container", []string{"echo", "hello"}, context.TODO()) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("expected no error, got %v", err) |
| 79 | + } |
| 80 | + if !bytes.Contains(out, []byte("output")) { |
| 81 | + t.Errorf("expected output, got %s", string(out)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestRealQueryCRD_ReturnsErrorOnInvalidConfig(t *testing.T) { |
| 86 | + dc := &DataCollector{ |
| 87 | + K8sRestConfig: &rest.Config{}, |
| 88 | + } |
| 89 | + crd := crds.Crd{Group: "test", Version: "v1", Resource: "foos"} |
| 90 | + _, err := dc.RealQueryCRD(crd, "default", context.TODO()) |
| 91 | + if err == nil { |
| 92 | + t.Error("expected error for invalid config") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestAllNamespacesExist_AllExist(t *testing.T) { |
| 97 | + client := fake.NewClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "default"}}) |
| 98 | + dc := &DataCollector{ |
| 99 | + Namespaces: []string{"default"}, |
| 100 | + K8sCoreClientSet: client, |
| 101 | + Logger: log.New(io.Discard, "", 0), |
| 102 | + } |
| 103 | + if !dc.AllNamespacesExist() { |
| 104 | + t.Error("expected all namespaces to exist") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestAllNamespacesExist_NotExist(t *testing.T) { |
| 109 | + client := fake.NewClientset() |
| 110 | + dc := &DataCollector{ |
| 111 | + Namespaces: []string{"missing"}, |
| 112 | + K8sCoreClientSet: client, |
| 113 | + Logger: log.New(io.Discard, "", 0), |
| 114 | + } |
| 115 | + if dc.AllNamespacesExist() { |
| 116 | + t.Error("expected namespaces to not exist") |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestWrapUp_ErrorOnLogFileClose(t *testing.T) { |
| 121 | + tmpDir := t.TempDir() |
| 122 | + logFile, _ := os.Create(filepath.Join(tmpDir, "supportpkg.log")) |
| 123 | + logFile.Close() // Already closed |
| 124 | + dc := &DataCollector{ |
| 125 | + BaseDir: tmpDir, |
| 126 | + LogFile: logFile, |
| 127 | + Logger: log.New(io.Discard, "", 0), |
| 128 | + } |
| 129 | + _, err := dc.WrapUp("nginx") |
| 130 | + if err == nil { |
| 131 | + t.Error("expected error on closing already closed log file") |
| 132 | + } |
| 133 | +} |
0 commit comments