@@ -16,7 +16,12 @@ package agentendpoint
1616
1717import (
1818 "context"
19+ "fmt"
20+ "net/http"
21+ "net/http/httptest"
22+ "os"
1923 "os/exec"
24+ "path/filepath"
2025 "testing"
2126
2227 "github.com/google/go-cmp/cmp"
@@ -134,3 +139,120 @@ func TestRunExecStep(t *testing.T) {
134139 })
135140 }
136141}
142+
143+ func TestGetGCSObject (t * testing.T ) {
144+ ctx := context .Background ()
145+ testContent := "test script content"
146+ bucket := "test-bucket"
147+ object := "scripts/test.sh"
148+
149+ tests := []struct {
150+ name string
151+ bucket string
152+ object string
153+ handler http.HandlerFunc
154+ wantErr bool
155+ }{
156+ {
157+ name : "Success" ,
158+ bucket : bucket ,
159+ object : object ,
160+ handler : func (w http.ResponseWriter , r * http.Request ) {
161+ w .WriteHeader (http .StatusOK )
162+ w .Write ([]byte (testContent ))
163+ },
164+ wantErr : false ,
165+ },
166+ {
167+ name : "NotFound" ,
168+ bucket : bucket ,
169+ object : object ,
170+ handler : func (w http.ResponseWriter , r * http.Request ) {
171+ w .WriteHeader (http .StatusNotFound )
172+ },
173+ wantErr : true ,
174+ },
175+ {
176+ name : "InvalidLocalPath" ,
177+ bucket : bucket ,
178+ object : "///" ,
179+ handler : func (w http.ResponseWriter , r * http.Request ) {
180+ w .WriteHeader (http .StatusOK )
181+ w .Write ([]byte (testContent ))
182+ },
183+ wantErr : true ,
184+ },
185+ }
186+
187+ for _ , tt := range tests {
188+ t .Run (tt .name , func (t * testing.T ) {
189+ ts := httptest .NewServer (tt .handler )
190+ defer ts .Close ()
191+
192+ os .Setenv ("STORAGE_EMULATOR_HOST" , ts .URL )
193+ defer os .Unsetenv ("STORAGE_EMULATOR_HOST" )
194+
195+ localPath , err := getGCSObject (ctx , tt .bucket , tt .object , 0 )
196+ if (err != nil ) != tt .wantErr {
197+ t .Fatalf ("getGCSObject() error = %v, wantErr %v" , err , tt .wantErr )
198+ }
199+ if err == nil {
200+ defer os .Remove (localPath )
201+ content , err := os .ReadFile (localPath )
202+ if err != nil {
203+ t .Fatalf ("Failed to read downloaded file: %v" , err )
204+ }
205+ if string (content ) != testContent {
206+ t .Errorf ("Content mismatch: got %q, want %q" , string (content ), testContent )
207+ }
208+ if filepath .Base (localPath ) != "test.sh" {
209+ t .Errorf ("Expected filename test.sh, got %s" , filepath .Base (localPath ))
210+ }
211+ }
212+ })
213+ }
214+ }
215+
216+ func TestExecuteCommand (t * testing.T ) {
217+ ctx := context .Background ()
218+ oldRun := run
219+ defer func () { run = oldRun }()
220+
221+ tests := []struct {
222+ name string
223+ mockRun func (* exec.Cmd ) ([]byte , error )
224+ wantCode int32
225+ wantErr bool
226+ }{
227+ {
228+ name : "SystemError" ,
229+ mockRun : func (cmd * exec.Cmd ) ([]byte , error ) {
230+ return nil , fmt .Errorf ("system error" )
231+ },
232+ wantCode : - 1 ,
233+ wantErr : true ,
234+ },
235+ {
236+ name : "CommandExitError" ,
237+ mockRun : func (cmd * exec.Cmd ) ([]byte , error ) {
238+ return []byte ("error output" ), & exec.ExitError {}
239+ },
240+ wantCode : 0 ,
241+ wantErr : false ,
242+ },
243+ }
244+
245+ for _ , tt := range tests {
246+ t .Run (tt .name , func (t * testing.T ) {
247+ run = tt .mockRun
248+ gotCode , err := executeCommand (ctx , "test-path" , nil )
249+ if (err != nil ) != tt .wantErr {
250+ t .Errorf ("%s: executeCommand() error = %v, wantErr %v" , tt .name , err , tt .wantErr )
251+ }
252+ if gotCode != tt .wantCode {
253+ t .Errorf ("%s: executeCommand() exitCode = %d, want %d" , tt .name , gotCode , tt .wantCode )
254+ }
255+ })
256+ }
257+ }
258+
0 commit comments