99 "encoding/json"
1010 "fmt"
1111 "io"
12+ "net/http"
1213 "os"
1314 "os/exec"
1415 "path/filepath"
@@ -25,7 +26,6 @@ type TestOrchestrator struct {
2526 deviceConfig * DeviceConfig
2627 ffxLogProc * os.Process
2728 targetLogFile * os.File
28- repoName string
2929}
3030
3131var (
4141func NewTestOrchestrator (deviceConfig * DeviceConfig ) * TestOrchestrator {
4242 return & TestOrchestrator {
4343 deviceConfig : deviceConfig ,
44- repoName : fmt .Sprintf ("repo-%d" , os .Getpid ()),
4544 }
4645}
4746
@@ -172,27 +171,12 @@ func (r *TestOrchestrator) setupFfx() error {
172171 {"config" , "set" , "daemon.autostart" , "false" },
173172 {"config" , "set" , "overnet.cso" , "only" },
174173 {"config" , "set" , "ffx-repo-add" , "true" },
175- // Set a unique repository server name for this run.
176- {"config" , "set" , "repository.default" , r .repoName },
177- // Disable the daemon based repo server.
178- {"config" , "set" , "repository.server.enabled" , "false" },
179174 }
180-
181175 for _ , cmd := range cmds {
182176 if out , err := r .ffx .RunCmdSync (cmd ... ); err != nil {
183177 return fmt .Errorf ("ffx setup %v: %w out: %s" , cmd , err , out )
184178 }
185179 }
186-
187- // If there is a log dir, set it instead of the default
188- log_dir := os .Getenv ("TEST_UNDECLARED_OUTPUTS_DIR" )
189- if log_dir != "" {
190- cmd := []string {"config" , "set" , "log.dir" , log_dir }
191- if out , err := r .ffx .RunCmdSync (cmd ... ); err != nil {
192- return fmt .Errorf ("ffx setup %v: %w out: %s" , cmd , err , out )
193- }
194- }
195-
196180 if err := r .dumpFfxConfig (); err != nil {
197181 return fmt .Errorf ("dumpFfxConfig: %w" , err )
198182 }
@@ -289,20 +273,10 @@ func (r *TestOrchestrator) startEmulator(productDir string) error {
289273}
290274
291275/* Step 4 - Serving packages. */
292- /*
293- Serving packages requires:
294- * Creating the package repository or having a downloaded product bundle.
295- * Publishing a package to make sure the metadata is up to date. (Can we use --refresh metadata instead?)
296- * Starting the package server process
297- * Registering the package server on the target device.
298- * Package servers are managed by name. or if using product bundles, the product bundle directory.
299-
300- */
301276func (r * TestOrchestrator ) servePackages (in * RunInput , productDir string ) error {
302- if err := r .serveAndWait ( productDir ); err != nil {
303- return fmt .Errorf ("serveAndWait : %w" , err )
277+ if out , err := r .ffx . RunCmdSync ( "repository" , "add-from-pm" , productDir ); err != nil {
278+ return fmt .Errorf ("ffx repository add-from-pm : %w out: %s " , err , out )
304279 }
305-
306280 // It is important to always publish, even if there is nothing in
307281 // in.Target().PackageArchives, because it will force the package metadata
308282 // to be refreshed (see b/309847820).
@@ -318,41 +292,39 @@ func (r *TestOrchestrator) servePackages(in *RunInput, productDir string) error
318292 return fmt .Errorf ("ffx debug symbol-index add %s: %w out: %s" , buildID , err , out )
319293 }
320294 }
321-
322- if _ , err := r .ffx .RunCmdSync ("repository" , "server" , "list" ); err != nil {
323- return fmt .Errorf ("ffx repository server list: %w" , err )
295+ if err := r .serveAndWait (); err != nil {
296+ return fmt .Errorf ("serveAndWait: %w" , err )
297+ }
298+ if _ , err := r .ffx .RunCmdSync ("repository" , "list" ); err != nil {
299+ return fmt .Errorf ("ffx repository list: %w" , err )
324300 }
325301 return nil
326302}
327303
328- func (r * TestOrchestrator ) serveAndWait (productDir string ) error {
304+ func (r * TestOrchestrator ) serveAndWait () error {
329305 port := os .Getenv ("FUCHSIA_PACKAGE_SERVER_PORT" )
330306 if port == "" {
331- // Use a dynamic port unless the environment is specific.
332- port = "0"
307+ port = "8083"
333308 }
334309 addr := fmt .Sprintf ("[::]:%s" , port )
335- args := []string {
336- "repository" , "server" , "start" ,
337- "--background" , "--no-device" ,
338- "--address" , addr ,
339- "--product-bundle" , productDir ,
340- "--repository" , r .repoName ,
341- }
342- if _ , err := r .ffx .RunCmdSync (args ... ); err != nil {
310+ if _ , err := r .ffx .RunCmdAsync ("repository" , "server" , "start" , "--address" , addr ); err != nil {
343311 return fmt .Errorf ("ffx repository server start: %w" , err )
344312 }
345-
346- // The server start command when using `--background` waits for the server
347- // to actually start before exiting, so this check is a double check.
348- running , err := r .ffx .IsPackageServerRunning (r .repoName )
349- if err != nil {
350- return fmt .Errorf ("ffx isPackageServerRunning: %w" , err )
351- }
352- if ! running {
353- return fmt .Errorf ("repository %s is not running" , r .repoName )
354- }
355- return nil
313+ return utils .RunWithRetries (context .Background (), 500 * time .Millisecond , 5 , func () error {
314+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("http://localhost:%s" , port ), nil )
315+ if err != nil {
316+ return fmt .Errorf ("http.NewRequest: %w" , err )
317+ }
318+ resp , err := http .DefaultClient .Do (req )
319+ if err != nil {
320+ return fmt .Errorf ("http.DefaultClient.Do: %w" , err )
321+ }
322+ // Check the response status code
323+ if resp .StatusCode != 200 {
324+ return fmt .Errorf ("resp.StatusCode: got %d, want 200" , resp .StatusCode )
325+ }
326+ return nil
327+ })
356328}
357329
358330/* Step 5 - Reach Device */
@@ -363,7 +335,6 @@ func (r *TestOrchestrator) reachDevice() error {
363335 return fmt .Errorf ("ffx target add: %w" , err )
364336 }
365337 }
366-
367338 if _ , err := r .ffx .RunCmdSync ("target" , "wait" ); err != nil {
368339 return fmt .Errorf ("ffx target wait: %w" , err )
369340 }
@@ -373,19 +344,16 @@ func (r *TestOrchestrator) reachDevice() error {
373344 if err := r .dumpFfxLog (); err != nil {
374345 return fmt .Errorf ("dumpFfxLog: %w" , err )
375346 }
376-
377- // Register the repo server using the aliases configured with the running server.
378347 if out , err := r .ffx .RunCmdSync (
379348 "target" ,
380349 "repository" ,
381350 "register" ,
382351 "--repository" ,
383- r . repoName ,
352+ "devhost" ,
384353 "--alias" ,
385354 "fuchsia.com" ,
386355 "--alias" ,
387- "chromium.org" ,
388- ); err != nil {
356+ "chromium.org" ); err != nil {
389357 return fmt .Errorf ("ffx target repository register: %w out: %s" , err , out )
390358 }
391359 return nil
@@ -513,7 +481,7 @@ func writeJSON(filename string, data any) error {
513481
514482/* Cleanup */
515483func (r * TestOrchestrator ) stopPackageServer () {
516- if _ , err := r .ffx .RunCmdSync ("repository" , "server" , "stop" , r . repoName ); err != nil {
484+ if _ , err := r .ffx .RunCmdSync ("repository" , "server" , "stop" ); err != nil {
517485 fmt .Printf ("ffx repository server stop: %v" , err )
518486 }
519487}
0 commit comments