Skip to content

Commit a65410b

Browse files
authored
chore(tests): Add unit tests to image-updater pkg/health, pkg/version and pkg/registry (argoproj-labs#771)
Signed-off-by: Mangaal <[email protected]>
1 parent 80d2731 commit a65410b

File tree

12 files changed

+1368
-2
lines changed

12 files changed

+1368
-2
lines changed

pkg/health/health_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package health
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
"time"
9+
)
10+
11+
// Unit test function
12+
func TestStartHealthServer_InvalidPort(t *testing.T) {
13+
// Use an invalid port number
14+
port := -1
15+
errCh := StartHealthServer(port)
16+
defer close(errCh) // Close the error channel after the test completes
17+
select {
18+
case err := <-errCh:
19+
if err == nil {
20+
t.Error("Expected error, got nil")
21+
} else if err.Error() != fmt.Sprintf("listen tcp: address %d: invalid port", port) {
22+
t.Errorf("Expected error message about invalid port, got %v", err)
23+
}
24+
case <-time.After(2 * time.Second):
25+
t.Error("Timed out waiting for error")
26+
}
27+
}
28+
29+
func TestHealthProbe(t *testing.T) {
30+
// Create a mock HTTP request
31+
req, err := http.NewRequest("GET", "/healthz", nil)
32+
if err != nil {
33+
t.Fatalf("Failed to create request: %v", err)
34+
}
35+
36+
// Create a mock HTTP response recorder
37+
w := httptest.NewRecorder()
38+
39+
// Call the HealthProbe function directly
40+
HealthProbe(w, req)
41+
42+
// Check the response status code
43+
if w.Code != http.StatusOK {
44+
t.Errorf("Expected status OK; got %d", w.Code)
45+
}
46+
47+
// Check the response body
48+
expectedBody := "OK\n"
49+
if body := w.Body.String(); body != expectedBody {
50+
t.Errorf("Expected body %q; got %q", expectedBody, body)
51+
}
52+
}

0 commit comments

Comments
 (0)