@@ -2,7 +2,6 @@ package agent
22
33import (
44 "testing"
5- "time"
65
76 "yc-agent/internal/config"
87 "yc-agent/internal/logger"
@@ -11,6 +10,7 @@ import (
1110)
1211
1312func TestRun (t * testing.T ) {
13+ // Tests basic mode validation error cases that return immediately
1414 // Save original config and restore after tests
1515 originalConfig := config .GlobalConfig
1616 defer func () {
@@ -58,131 +58,23 @@ func TestRun(t *testing.T) {
5858 err := Run ()
5959 assert .Equal (t , ErrConflictingMode , err )
6060 })
61- }
62-
63- func TestModeValidation (t * testing.T ) {
64- // Save original config and restore after tests
65- originalConfig := config .GlobalConfig
66- defer func () {
67- config .GlobalConfig = originalConfig
68- }()
6961
70- // Initialize logger for tests
71- logger .Init ("" , 0 , 0 , "info" )
62+ t .Run ("all three modes conflict" , func (t * testing.T ) {
63+ config .GlobalConfig = config.Config {
64+ Options : config.Options {
65+ Pid : "12345" , // OnDemand mode enabled
66+ M3 : true , // M3 mode enabled
67+ Port : 8080 , // API mode enabled
68+ },
69+ }
7270
73- // Create temp directory for test artifacts
74- tempDir := t .TempDir ()
75-
76- tests := []struct {
77- name string
78- pid string
79- m3 bool
80- port int
81- expectedErr error
82- description string
83- }{
84- {
85- name : "no mode specified" ,
86- pid : "" ,
87- m3 : false ,
88- port : 0 ,
89- expectedErr : ErrNothingCanBeDone ,
90- description : "neither ondemand, m3, nor api mode is enabled" ,
91- },
92- {
93- name : "ondemand only" ,
94- pid : "12345" ,
95- m3 : false ,
96- port : 0 ,
97- expectedErr : nil ,
98- description : "valid ondemand mode" ,
99- },
100- {
101- name : "m3 only" ,
102- pid : "" ,
103- m3 : true ,
104- port : 0 ,
105- expectedErr : nil ,
106- description : "valid m3 mode" ,
107- },
108- {
109- name : "api only" ,
110- pid : "" ,
111- m3 : false ,
112- port : 8080 ,
113- expectedErr : nil ,
114- description : "valid api mode" ,
115- },
116- {
117- name : "ondemand and m3 conflict" ,
118- pid : "12345" ,
119- m3 : true ,
120- port : 0 ,
121- expectedErr : ErrConflictingMode ,
122- description : "ondemand and m3 cannot run together" ,
123- },
124- {
125- name : "m3 and api together" ,
126- pid : "" ,
127- m3 : true ,
128- port : 8080 ,
129- expectedErr : nil ,
130- description : "m3 and api can run together" ,
131- },
132- {
133- name : "ondemand and api together" ,
134- pid : "12345" ,
135- m3 : false ,
136- port : 8080 ,
137- expectedErr : nil ,
138- description : "ondemand and api can run together (backward compatibility)" ,
139- },
140- {
141- name : "all three modes" ,
142- pid : "12345" ,
143- m3 : true ,
144- port : 8080 ,
145- expectedErr : ErrConflictingMode ,
146- description : "ondemand conflicts with m3 even with api mode" ,
147- },
148- }
149-
150- for _ , tt := range tests {
151- t .Run (tt .name , func (t * testing.T ) {
152- config .GlobalConfig = config.Config {
153- Options : config.Options {
154- Pid : tt .pid ,
155- M3 : tt .m3 ,
156- Port : tt .port ,
157- Address : "localhost" ,
158- OnlyCapture : true , // Required to avoid upload logic in ondemand mode
159- JavaHomePath : "/usr/lib/jvm/java-11" ,
160- StoragePath : tempDir , // Use temp directory for test artifacts
161- },
162- }
163-
164- // Create a channel to catch the error or timeout
165- errChan := make (chan error , 1 )
166-
167- go func () {
168- err := Run ()
169- errChan <- err
170- }()
171-
172- // Wait for error or timeout
173- select {
174- case err := <- errChan :
175- assert .Equal (t , tt .expectedErr , err , tt .description )
176- case <- time .After (100 * time .Millisecond ):
177- // If we timeout, it means Run() is blocking (which is expected for m3/api modes)
178- assert .Nil (t , tt .expectedErr , "%s: expected error %v, but Run() is blocking" , tt .description , tt .expectedErr )
179- // This is expected behavior for valid m3/api modes - they block indefinitely
180- }
181- })
182- }
71+ err := Run ()
72+ assert .Equal (t , ErrConflictingMode , err )
73+ })
18374}
18475
18576func TestResolvePidsFromToken (t * testing.T ) {
77+ // Tests PID resolution from process tokens - uses nonexistent token to avoid env dependency
18678 // Save original config and restore after tests
18779 originalConfig := config .GlobalConfig
18880 defer func () {
@@ -198,14 +90,3 @@ func TestResolvePidsFromToken(t *testing.T) {
19890 assert .Empty (t , pids , "expected empty pids slice for nonexistent token" )
19991 })
20092}
201-
202- func TestModeLogic (t * testing.T ) {
203- // Save original config and restore after tests
204- originalConfig := config .GlobalConfig
205- defer func () {
206- config .GlobalConfig = originalConfig
207- }()
208-
209- // Initialize logger for tests
210- logger .Init ("" , 0 , 0 , "info" )
211- }
0 commit comments