|
1 | 1 | package capture |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "os" |
4 | 7 | "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
5 | 11 | "yc-agent/internal/config" |
6 | 12 | ) |
7 | 13 |
|
8 | | -// test case will assert whether the result is OK that is received |
9 | | -// as part of the yc server upload |
10 | 14 | func TestHealthCheck_Run(t *testing.T) { |
11 | | - // TODO: Revisit this test - currently failing in CI |
12 | | - // Test attempts to make HTTP requests to localhost:8080 which is not running in CI. |
13 | | - // Needs proper HTTP server mocking or test server setup. |
14 | | - t.Skip("Skipping until HTTP server can be properly mocked") |
15 | | - |
16 | | - appName := "TestApp" |
17 | | - endpoint := "http://localhost:8080/" |
18 | | - |
19 | | - h := &HealthCheck{ |
20 | | - AppName: appName, |
21 | | - Cfg: config.HealthCheck{ |
22 | | - Endpoint: endpoint, |
23 | | - HTTPBody: `{"status":"ok"}`, |
24 | | - TimeoutSecs: 2, |
25 | | - }, |
26 | | - } |
27 | | - h.SetEndpoint("http://localhost:8080/?dt=healthCheckEndpoint&fileName=healthCheckEndpoint.TestApp.out&appName=TestApp") |
| 15 | + t.Run("should successfully check healthy endpoint", func(t *testing.T) { |
| 16 | + // Create a mock HTTP server that returns a healthy response |
| 17 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + t.Logf("Mock server received request: %s %s", r.Method, r.URL.Path) |
| 19 | + w.WriteHeader(http.StatusOK) |
| 20 | + w.Write([]byte(`{"status":"ok"}`)) |
| 21 | + })) |
| 22 | + defer server.Close() |
| 23 | + |
| 24 | + // Change to temp directory to avoid polluting the working directory |
| 25 | + tmpDir := t.TempDir() |
| 26 | + originalWd, err := os.Getwd() |
| 27 | + require.NoError(t, err) |
| 28 | + defer os.Chdir(originalWd) |
| 29 | + err = os.Chdir(tmpDir) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + h := &HealthCheck{ |
| 33 | + AppName: "TestApp", |
| 34 | + Cfg: config.HealthCheck{ |
| 35 | + Endpoint: server.URL, |
| 36 | + HTTPBody: `{"status":"ok"}`, |
| 37 | + TimeoutSecs: 2, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + t.Logf("Testing health check against mock server: %s", server.URL) |
| 42 | + |
| 43 | + result, err := h.Run() |
| 44 | + |
| 45 | + t.Logf("Result: Ok=%v, Msg=%s", result.Ok, result.Msg) |
| 46 | + if err != nil { |
| 47 | + t.Logf("Error: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + assert.NoError(t, err) |
| 51 | + // Note: result.Ok depends on PostData which may fail without a real yc server |
| 52 | + // The key assertion is that the health check itself completes without error |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("should handle empty endpoint", func(t *testing.T) { |
| 56 | + tmpDir := t.TempDir() |
| 57 | + originalWd, err := os.Getwd() |
| 58 | + require.NoError(t, err) |
| 59 | + defer os.Chdir(originalWd) |
| 60 | + err = os.Chdir(tmpDir) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + h := &HealthCheck{ |
| 64 | + AppName: "TestApp", |
| 65 | + Cfg: config.HealthCheck{ |
| 66 | + Endpoint: "", |
| 67 | + TimeoutSecs: 2, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + result, err := h.Run() |
28 | 72 |
|
29 | | - result, err := h.Run() |
30 | | - if err != nil { |
31 | | - t.Fatalf("unexpected error: %v", err) |
| 73 | + t.Logf("Empty endpoint - Result: Ok=%v, Msg=%s, Err=%v", result.Ok, result.Msg, err) |
| 74 | + |
| 75 | + // Empty endpoint should still return without a Go error (error is logged to file) |
| 76 | + assert.NoError(t, err) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("should handle server returning error status", func(t *testing.T) { |
| 80 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 81 | + t.Logf("Mock server returning 500 error") |
| 82 | + w.WriteHeader(http.StatusInternalServerError) |
| 83 | + w.Write([]byte(`{"error":"internal server error"}`)) |
| 84 | + })) |
| 85 | + defer server.Close() |
| 86 | + |
| 87 | + tmpDir := t.TempDir() |
| 88 | + originalWd, err := os.Getwd() |
| 89 | + require.NoError(t, err) |
| 90 | + defer os.Chdir(originalWd) |
| 91 | + err = os.Chdir(tmpDir) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + h := &HealthCheck{ |
| 95 | + AppName: "TestApp", |
| 96 | + Cfg: config.HealthCheck{ |
| 97 | + Endpoint: server.URL, |
| 98 | + TimeoutSecs: 2, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + result, err := h.Run() |
| 103 | + |
| 104 | + t.Logf("Error status - Result: Ok=%v, Msg=%s, Err=%v", result.Ok, result.Msg, err) |
| 105 | + |
| 106 | + // The Run() method captures the response regardless of status code |
| 107 | + assert.NoError(t, err) |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func TestSanitizeAppNameForFileName(t *testing.T) { |
| 112 | + tests := []struct { |
| 113 | + name string |
| 114 | + input string |
| 115 | + expected string |
| 116 | + }{ |
| 117 | + {"simple name", "MyApp", "MyApp"}, |
| 118 | + {"with spaces", "My App", "My_App"}, |
| 119 | + {"with path traversal", "../../../etc/passwd", "___etc_passwd"}, |
| 120 | + {"with slashes", "app/name", "app_name"}, |
| 121 | + {"with backslashes", "app\\name", "app_name"}, |
| 122 | + {"empty string", "", "default"}, |
| 123 | + {"special chars", "app@name#1", "app_name_1"}, |
32 | 124 | } |
33 | | - if !result.Ok { |
34 | | - t.Fatalf("expected result.Ok=true, got false. Msg: %s", result.Msg) |
| 125 | + |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.name, func(t *testing.T) { |
| 128 | + result := sanitizeAppNameForFileName(tt.input) |
| 129 | + assert.Equal(t, tt.expected, result) |
| 130 | + }) |
35 | 131 | } |
36 | 132 | } |
0 commit comments