|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "sync" |
| 11 | + "syscall" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "zops.top/prometheus-cve-exporter/config" |
| 16 | +) |
| 17 | + |
| 18 | +func mockUpdateMetrics(*config.Config) {} |
| 19 | + |
| 20 | +func TestNewServer(t *testing.T) { |
| 21 | + cfg := &config.Config{} |
| 22 | + logger := log.New(os.Stdout, "", log.LstdFlags) |
| 23 | + server := NewServer(cfg, logger, mockUpdateMetrics) |
| 24 | + |
| 25 | + if server.cfg != cfg { |
| 26 | + t.Errorf("Expected cfg to be %v, got %v", cfg, server.cfg) |
| 27 | + } |
| 28 | + if server.logger != logger { |
| 29 | + t.Errorf("Expected logger to be %v, got %v", logger, server.logger) |
| 30 | + } |
| 31 | + if server.mux == nil { |
| 32 | + t.Error("Expected mux to be initialized") |
| 33 | + } |
| 34 | + if server.updateMetrics == nil { |
| 35 | + t.Error("Expected updateMetrics to be initialized") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestSetupRouter(t *testing.T) { |
| 40 | + server := NewServer(&config.Config{}, log.New(os.Stdout, "", log.LstdFlags), mockUpdateMetrics) |
| 41 | + server.SetupRouter() |
| 42 | + |
| 43 | + testCases := []struct { |
| 44 | + path string |
| 45 | + expectedCode int |
| 46 | + }{ |
| 47 | + {"/metrics", http.StatusOK}, |
| 48 | + {"/", http.StatusOK}, |
| 49 | + {"/nonexistent", http.StatusNotFound}, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tc := range testCases { |
| 53 | + req, err := http.NewRequest("GET", tc.path, nil) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Could not create request: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + rr := httptest.NewRecorder() |
| 59 | + server.mux.ServeHTTP(rr, req) |
| 60 | + |
| 61 | + if rr.Code != tc.expectedCode { |
| 62 | + t.Errorf("handler returned wrong status code for %s: got %v want %v", |
| 63 | + tc.path, rr.Code, tc.expectedCode) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestHomeHandler(t *testing.T) { |
| 69 | + server := NewServer(&config.Config{}, log.New(os.Stdout, "", log.LstdFlags), mockUpdateMetrics) |
| 70 | + |
| 71 | + req, err := http.NewRequest("GET", "/", nil) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + rr := httptest.NewRecorder() |
| 77 | + handler := http.HandlerFunc(server.homeHandler) |
| 78 | + handler.ServeHTTP(rr, req) |
| 79 | + |
| 80 | + if status := rr.Code; status != http.StatusOK { |
| 81 | + t.Errorf("handler returned wrong status code: got %v want %v", |
| 82 | + status, http.StatusOK) |
| 83 | + } |
| 84 | + |
| 85 | + expected := `<a href="/metrics">Go to metrics</a>` |
| 86 | + if rr.Body.String() != expected { |
| 87 | + t.Errorf("handler returned unexpected body: got %v want %v", |
| 88 | + rr.Body.String(), expected) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestStart(t *testing.T) { |
| 93 | + cfg := &config.Config{Port: 20000} |
| 94 | + logger := log.New(os.Stdout, "", log.LstdFlags) |
| 95 | + s := NewServer(cfg, logger, mockUpdateMetrics) |
| 96 | + s.SetupRouter() |
| 97 | + |
| 98 | + var wg sync.WaitGroup |
| 99 | + wg.Add(1) |
| 100 | + go func() { |
| 101 | + defer wg.Done() |
| 102 | + s.Start() |
| 103 | + }() |
| 104 | + |
| 105 | + // Give some time for the server to start |
| 106 | + time.Sleep(100 * time.Millisecond) |
| 107 | + |
| 108 | + resp, err := http.Get(fmt.Sprintf("http://localhost:%d", cfg.Port)) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("Could not send GET request: %v", err) |
| 111 | + } |
| 112 | + defer resp.Body.Close() |
| 113 | + |
| 114 | + if resp.StatusCode != http.StatusOK { |
| 115 | + t.Errorf("Expected status OK; got %v", resp.Status) |
| 116 | + } |
| 117 | + |
| 118 | + // Shutdown the server |
| 119 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 120 | + defer cancel() |
| 121 | + if err := s.server.Shutdown(ctx); err != nil { |
| 122 | + s.logger.Fatalf("Server forced to shutdown: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + wg.Wait() |
| 126 | +} |
| 127 | + |
| 128 | +func TestUpdateMetricsExecution(t *testing.T) { |
| 129 | + updateMetricsCalled := false |
| 130 | + mockUpdateMetrics := func(*config.Config) { |
| 131 | + updateMetricsCalled = true |
| 132 | + } |
| 133 | + |
| 134 | + cfg := &config.Config{} |
| 135 | + logger := log.New(os.Stdout, "", log.LstdFlags) |
| 136 | + server := NewServer(cfg, logger, mockUpdateMetrics) |
| 137 | + |
| 138 | + server.updateMetrics(cfg) |
| 139 | + |
| 140 | + if !updateMetricsCalled { |
| 141 | + t.Error("Expected UpdateMetrics to be called") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestMainIntegration(t *testing.T) { |
| 146 | + // Backup original os.Args |
| 147 | + oldArgs := os.Args |
| 148 | + defer func() { os.Args = oldArgs }() |
| 149 | + |
| 150 | + // Set up a test config file |
| 151 | + testConfigPath := "test_config.json" |
| 152 | + testPort := 20001 |
| 153 | + testConfigContent := []byte(fmt.Sprintf(`{ |
| 154 | + "nvd_feed_url": "https://test.nvd.feed.url", |
| 155 | + "update_interval": "2h", |
| 156 | + "port": %d, |
| 157 | + "severity": ["HIGH", "CRITICAL"], |
| 158 | + "package_file": "", |
| 159 | + "use_tls": false |
| 160 | + }`, testPort)) |
| 161 | + err := os.WriteFile(testConfigPath, testConfigContent, 0644) |
| 162 | + if err != nil { |
| 163 | + t.Fatalf("Failed to create test config file: %v", err) |
| 164 | + } |
| 165 | + defer os.Remove(testConfigPath) |
| 166 | + |
| 167 | + // Set the command-line argument to use our test config |
| 168 | + os.Args = []string{"cmd", "-config", testConfigPath} |
| 169 | + |
| 170 | + // Run main in a goroutine |
| 171 | + go func() { |
| 172 | + main() |
| 173 | + }() |
| 174 | + |
| 175 | + // Give some time for the server to start |
| 176 | + time.Sleep(100 * time.Millisecond) |
| 177 | + |
| 178 | + // Test if the server is running |
| 179 | + resp, err := http.Get(fmt.Sprintf("http://localhost:%d", testPort)) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Could not send GET request: %v", err) |
| 182 | + } |
| 183 | + defer resp.Body.Close() |
| 184 | + |
| 185 | + if resp.StatusCode != http.StatusOK { |
| 186 | + t.Errorf("Expected status OK; got %v", resp.Status) |
| 187 | + } |
| 188 | + |
| 189 | + // Send shutdown signal |
| 190 | + err = syscall.Kill(syscall.Getpid(), syscall.SIGINT) |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("Failed to send SIGINT signal: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + // Give some time for the server to shut down |
| 196 | + time.Sleep(500 * time.Millisecond) |
| 197 | + |
| 198 | + // Verify that the server has shut down |
| 199 | + _, err = http.Get(fmt.Sprintf("http://localhost:%d", testPort)) |
| 200 | + if err == nil { |
| 201 | + t.Error("Expected an error when connecting to a shutdown server") |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func TestMain(m *testing.M) { |
| 206 | + // Run tests |
| 207 | + code := m.Run() |
| 208 | + os.Exit(code) |
| 209 | +} |
0 commit comments