@@ -2,7 +2,6 @@ package agent
22
33import (
44 "testing"
5- "time"
65
76 "yc-agent/internal/config"
87 "yc-agent/internal/logger"
@@ -74,135 +73,6 @@ func TestRun(t *testing.T) {
7473 })
7574}
7675
77- func TestModeValidation (t * testing.T ) {
78- // SKIP: This test spawns goroutines for blocking modes (m3, api) that never terminate,
79- // causing DATA RACE errors with the -race flag when subsequent test iterations modify
80- // config.GlobalConfig while previous goroutines are still reading it.
81- // Error cases are already covered by TestRun. Blocking mode validation would require
82- // architecture changes (e.g., context cancellation support in Run()).
83- t .Skip ("skipped due to race condition: blocking mode goroutines leak across test iterations" )
84-
85- // Save original config and restore after tests
86- originalConfig := config .GlobalConfig
87- defer func () {
88- config .GlobalConfig = originalConfig
89- }()
90-
91- // Initialize logger for tests
92- logger .Init ("" , 0 , 0 , "info" )
93-
94- // Create temp directory for test artifacts
95- tempDir := t .TempDir ()
96-
97- tests := []struct {
98- name string
99- pid string
100- m3 bool
101- port int
102- expectedErr error
103- description string
104- }{
105- {
106- name : "no mode specified" ,
107- pid : "" ,
108- m3 : false ,
109- port : 0 ,
110- expectedErr : ErrNothingCanBeDone ,
111- description : "neither ondemand, m3, nor api mode is enabled" ,
112- },
113- {
114- name : "ondemand only" ,
115- pid : "12345" ,
116- m3 : false ,
117- port : 0 ,
118- expectedErr : nil ,
119- description : "valid ondemand mode" ,
120- },
121- {
122- name : "m3 only" ,
123- pid : "" ,
124- m3 : true ,
125- port : 0 ,
126- expectedErr : nil ,
127- description : "valid m3 mode" ,
128- },
129- {
130- name : "api only" ,
131- pid : "" ,
132- m3 : false ,
133- port : 8080 ,
134- expectedErr : nil ,
135- description : "valid api mode" ,
136- },
137- {
138- name : "ondemand and m3 conflict" ,
139- pid : "12345" ,
140- m3 : true ,
141- port : 0 ,
142- expectedErr : ErrConflictingMode ,
143- description : "ondemand and m3 cannot run together" ,
144- },
145- {
146- name : "m3 and api together" ,
147- pid : "" ,
148- m3 : true ,
149- port : 8080 ,
150- expectedErr : nil ,
151- description : "m3 and api can run together" ,
152- },
153- {
154- name : "ondemand and api together" ,
155- pid : "12345" ,
156- m3 : false ,
157- port : 8080 ,
158- expectedErr : nil ,
159- description : "ondemand and api can run together (backward compatibility)" ,
160- },
161- {
162- name : "all three modes" ,
163- pid : "12345" ,
164- m3 : true ,
165- port : 8080 ,
166- expectedErr : ErrConflictingMode ,
167- description : "ondemand conflicts with m3 even with api mode" ,
168- },
169- }
170-
171- for _ , tt := range tests {
172- t .Run (tt .name , func (t * testing.T ) {
173- config .GlobalConfig = config.Config {
174- Options : config.Options {
175- Pid : tt .pid ,
176- M3 : tt .m3 ,
177- Port : tt .port ,
178- Address : "localhost" ,
179- OnlyCapture : true , // Required to avoid upload logic in ondemand mode
180- JavaHomePath : "/usr/lib/jvm/java-11" ,
181- StoragePath : tempDir , // Use temp directory for test artifacts
182- },
183- }
184-
185- // Create a channel to catch the error or timeout
186- errChan := make (chan error , 1 )
187-
188- go func () {
189- err := Run ()
190- errChan <- err
191- }()
192-
193- // Wait for error or timeout
194- select {
195- case err := <- errChan :
196- assert .Equal (t , tt .expectedErr , err , tt .description )
197- case <- time .After (100 * time .Millisecond ):
198- // If we timeout, it means Run() is blocking (which is expected for m3/api modes)
199- assert .Nil (t , tt .expectedErr , "%s: expected error %v, but Run() is blocking" , tt .description , tt .expectedErr )
200- // This is expected behavior for valid m3/api modes - they block indefinitely
201- }
202- })
203- }
204- }
205-
20676func TestResolvePidsFromToken (t * testing.T ) {
20777 // Tests PID resolution from process tokens - uses nonexistent token to avoid env dependency
20878 // Save original config and restore after tests
@@ -220,18 +90,3 @@ func TestResolvePidsFromToken(t *testing.T) {
22090 assert .Empty (t , pids , "expected empty pids slice for nonexistent token" )
22191 })
22292}
223-
224- func TestModeLogic (t * testing.T ) {
225- // Placeholder test for future mode logic validation
226- // Save original config and restore after tests
227- originalConfig := config .GlobalConfig
228- defer func () {
229- config .GlobalConfig = originalConfig
230- }()
231-
232- // Initialize logger for tests
233- logger .Init ("" , 0 , 0 , "info" )
234-
235- // Basic validation that config restoration works
236- assert .NotNil (t , originalConfig )
237- }
0 commit comments