Skip to content

Commit 3375f4f

Browse files
committed
Fix TestPostData
1 parent 4c5dd69 commit 3375f4f

File tree

1 file changed

+77
-81
lines changed

1 file changed

+77
-81
lines changed

internal/agent/ondemand/ondemand_test.go

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@ package ondemand
22

33
import (
44
"fmt"
5+
"io"
6+
"net/http"
7+
"net/http/httptest"
58
"os"
9+
"sync"
610
"testing"
711
"time"
812
"yc-agent/internal/capture"
913
"yc-agent/internal/capture/executils"
1014

1115
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1217
)
1318

1419
const (
1520
api = "tier1app@12312-12233-1442134-112"
1621
host = "https://test.gceasy.io"
1722
)
1823

19-
func init() {
20-
// err := os.Chdir("testdata")
21-
// if err != nil {
22-
// panic(err)
23-
// }
24-
}
25-
2624
func TestFindGCLog(t *testing.T) {
27-
// TODO: Revisit this test - currently failing in CI
28-
// Test requires Java to be installed and MyClass to be available.
29-
// Likely fails in CI due to missing Java environment or test class files.
30-
t.Skip("Skipping until Java environment can be properly set up in CI")
25+
// Skip: Test requires Java environment with compiled MyClass.java to run background Java processes.
26+
// This is an integration test that verifies GC log detection from running Java processes.
27+
t.Skip("Skipping: test requires Java environment with compiled test class")
3128

3229
noGC, err := executils.CommandStartInBackground(executils.Command{"java", "MyClass"})
3330
if err != nil {
@@ -91,86 +88,85 @@ func TestFindGCLog(t *testing.T) {
9188
}
9289

9390
func TestPostData(t *testing.T) {
91+
// Track received requests for assertions
92+
var mu sync.Mutex
93+
receivedRequests := make(map[string]struct {
94+
method string
95+
bodySize int
96+
})
97+
98+
// Create mock HTTP server
99+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
100+
// Read body to get size
101+
body, _ := io.ReadAll(r.Body)
102+
defer r.Body.Close()
103+
104+
// Extract dt parameter
105+
dt := r.URL.Query().Get("dt")
106+
107+
mu.Lock()
108+
receivedRequests[dt] = struct {
109+
method string
110+
bodySize int
111+
}{
112+
method: r.Method,
113+
bodySize: len(body),
114+
}
115+
mu.Unlock()
116+
117+
w.WriteHeader(http.StatusOK)
118+
w.Write([]byte("success"))
119+
}))
120+
defer server.Close()
121+
122+
// Build endpoint using mock server URL
94123
timestamp := time.Now().Format("2006-01-02T15-04-05")
95124
parameters := fmt.Sprintf("de=%s&ts=%s", capture.GetOutboundIP().String(), timestamp)
96-
endpoint := fmt.Sprintf("%s/ycrash-receiver?apiKey=%s&%s", host, api, parameters)
125+
endpoint := fmt.Sprintf("%s/ycrash-receiver?apiKey=%s&%s", server.URL, api, parameters)
126+
127+
// Test data files - path relative to ondemand package
128+
testFiles := []struct {
129+
path string
130+
dtType string
131+
}{
132+
{"../testdata/top.out", "top"},
133+
{"../testdata/disk.out", "df"},
134+
{"../testdata/netstat.out", "ns"},
135+
{"../testdata/ps.out", "ps"},
136+
{"../testdata/vmstat.out", "vmstat"},
137+
{"../testdata/gc.log", "gc"},
138+
{"../testdata/threaddump.out", "td"},
139+
}
97140

98-
t.Run("requestFin", func(t *testing.T) {
99-
finEp := fmt.Sprintf("%s/yc-fin?apiKey=%s&%s", host, api, parameters)
100-
RequestFin(finEp)
101-
})
141+
for _, tf := range testFiles {
142+
t.Run(tf.dtType, func(t *testing.T) {
143+
file, err := os.Open(tf.path)
144+
require.NoError(t, err, "Failed to open test file: %s", tf.path)
145+
defer file.Close()
102146

103-
vmstat, err := os.Open("testdata/vmstat.out")
104-
if err != nil {
105-
return
106-
}
107-
defer vmstat.Close()
108-
ps, err := os.Open("testdata/ps.out")
109-
if err != nil {
110-
t.Fatal(err)
111-
}
112-
defer ps.Close()
113-
top, err := os.Open("testdata/top.out")
114-
if err != nil {
115-
t.Fatal(err)
116-
}
117-
defer top.Close()
118-
df, err := os.Open("testdata/disk.out")
119-
if err != nil {
120-
t.Fatal(err)
121-
}
122-
defer df.Close()
123-
netstat, err := os.Open("testdata/netstat.out")
124-
if err != nil {
125-
t.Fatal(err)
126-
}
127-
defer netstat.Close()
128-
gc, err := os.Open("testdata/gc.log")
129-
if err != nil {
130-
t.Fatal(err)
131-
}
132-
defer gc.Close()
133-
td, err := os.Open("testdata/threaddump.out")
134-
if err != nil {
135-
t.Fatal(err)
147+
msg, ok := capture.PostData(endpoint, tf.dtType, file)
148+
assert.True(t, ok, "PostData failed for %s: %s", tf.dtType, msg)
149+
})
136150
}
137-
defer td.Close()
138151

139-
msg, ok := capture.PostData(endpoint, "top", top)
140-
if !ok {
141-
t.Fatal("post data failed", msg)
142-
}
143-
msg, ok = capture.PostData(endpoint, "df", df)
144-
if !ok {
145-
t.Fatal("post data failed", msg)
146-
}
147-
msg, ok = capture.PostData(endpoint, "ns", netstat)
148-
if !ok {
149-
t.Fatal("post data failed", msg)
150-
}
151-
msg, ok = capture.PostData(endpoint, "ps", ps)
152-
if !ok {
153-
t.Fatal("post data failed", msg)
154-
}
155-
msg, ok = capture.PostData(endpoint, "vmstat", vmstat)
156-
if !ok {
157-
t.Fatal("post data failed", msg)
158-
}
159-
msg, ok = capture.PostData(endpoint, "gc", gc)
160-
if !ok {
161-
t.Fatal("post data failed", msg)
162-
}
163-
msg, ok = capture.PostData(endpoint, "td", td)
164-
if !ok {
165-
t.Fatal("post data failed", msg)
152+
// Verify all requests were received correctly
153+
mu.Lock()
154+
defer mu.Unlock()
155+
156+
for _, tf := range testFiles {
157+
req, exists := receivedRequests[tf.dtType]
158+
assert.True(t, exists, "Request for dt=%s was not received", tf.dtType)
159+
if exists {
160+
assert.Equal(t, "POST", req.method, "Expected POST method for dt=%s", tf.dtType)
161+
assert.Greater(t, req.bodySize, 0, "Expected non-empty body for dt=%s", tf.dtType)
162+
}
166163
}
167164
}
168165

169166
func TestWriteMetaInfo(t *testing.T) {
170-
// TODO: Revisit this test - currently failing in CI
171-
// Test makes external HTTP calls to test.gceasy.io which may be unreachable or flaky in CI.
172-
// Should be mocked to avoid external dependencies.
173-
t.Skip("Skipping until external HTTP calls can be properly mocked")
167+
// Skip: Test makes external HTTP calls to test.gceasy.io which is flaky in CI.
168+
// This is an integration test that should be run manually or in a dedicated integration test suite.
169+
t.Skip("Skipping: test makes external HTTP calls to test.gceasy.io")
174170

175171
timestamp := time.Now().Format("2006-01-02T15-04-05")
176172
parameters := fmt.Sprintf("de=%s&ts=%s", capture.GetOutboundIP().String(), timestamp)

0 commit comments

Comments
 (0)