11package testtools
22
33import (
4+ "bufio"
45 "bytes"
6+ "context"
57 "flag"
68 "fmt"
79 "io"
10+ "iter"
811 "log"
12+ "net/http"
913 "os"
1014 "os/exec"
1115 "path/filepath"
@@ -37,9 +41,36 @@ func TestStableToUnstable(t *testing.T) {
3741 preUpdateVersion := runDockerSystemVersion (t , "apt-test-update" )
3842 runDockerSystemUpdate (t , "apt-test-update" )
3943 postUpdateVersion := runDockerSystemVersion (t , "apt-test-update" )
40- runDockerCleanUp (t , "apt-test-update" )
44+ // runDockerCleanUp(t, "apt-test-update")
4145 require .Equal (t , preUpdateVersion , "Arduino App CLI " + tagAppCli + "\n " )
4246 require .Equal (t , postUpdateVersion , "Arduino App CLI " + majorTag + "\n " )
47+ }
48+
49+ func TestClientUpdate (t * testing.T ) {
50+
51+ fmt .Printf ("Check folder structure and deb downloaded\n " )
52+ ls (t )
53+ fmt .Println ("**** BUILD docker image *****" )
54+ buildDockerImage (t , "test.Dockerfile" , "apt-test-update-image" , * arch )
55+ fmt .Println ("**** RUN docker image *****" )
56+ runDockerContainer (t , "apt-test-update" , "apt-test-update-image" )
57+ //Start the daemon
58+ runDockerDaemon (t , "apt-test-update" )
59+ //PUT on the /v1/updates/apply
60+ status := putUpdateRequest (t , "http://localhost:8080/v1/system/update/apply" )
61+ fmt .Printf ("Response status: %s\n " , status )
62+ //ClientSSE
63+
64+ itr := NewSSEClient (context .Background (), "GET" , "http://localhost:8080/v1/system/update/apply" )
65+
66+ for event , err := range itr {
67+ if err != nil {
68+ log .Fatalf ("Error receiving SSE event: %v" , err )
69+ }
70+ fmt .Printf ("Received event: ID=%s, Event=%s, Data=%s\n " , event .ID , event .Event , string (event .Data ))
71+ }
72+
73+ runDockerCleanUp (t , "apt-test-update" )
4374
4475}
4576
@@ -60,7 +91,7 @@ func TestUnstableToStable(t *testing.T) {
6091 fmt .Println ("**** BUILD docker image *****" )
6192 buildDockerImage (t , "test.Dockerfile" , "test-apt-update-unstable-image" , * arch )
6293 fmt .Println ("**** RUN docker image *****" )
63- runDockerContainer (t , "test-apt-update-unstable-image " , "apt- test-update-unstable" )
94+ runDockerContainer (t , "test-apt-update-unstable" , "test-apt- update-unstable-image " )
6495 preUpdateVersion := runDockerSystemVersion (t , "apt-test-update-unstable" )
6596 runDockerSystemUpdate (t , "apt-test-update-unstable" )
6697 postUpdateVersion := runDockerSystemVersion (t , "apt-test-update-unstable" )
@@ -337,3 +368,83 @@ func rm(t *testing.T, pathFile string) {
337368 fmt .Printf ("📦 Removed %s\n " , pathFile )
338369
339370}
371+
372+ func putUpdateRequest (t * testing.T , url string ) string {
373+
374+ t .Helper ()
375+
376+ // Create PUT request
377+ req , err := http .NewRequest (http .MethodPut , url , nil )
378+ if err != nil {
379+ log .Fatalf ("Error creating request: %v" , err )
380+ }
381+
382+ // Optional: add headers if your API needs them
383+ req .Header .Set ("Content-Type" , "application/json" )
384+
385+ // Send the request
386+ client := & http.Client {}
387+ resp , err := client .Do (req )
388+ if err != nil {
389+ log .Fatalf ("Error making request: %v" , err )
390+ }
391+ defer resp .Body .Close ()
392+
393+ // Check status code
394+ return resp .Status
395+ }
396+
397+ func NewSSEClient (ctx context.Context , method , url string ) iter.Seq2 [Event , error ] {
398+ return func (yield func (Event , error ) bool ) {
399+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
400+ if err != nil {
401+ _ = yield (Event {}, err )
402+ return
403+ }
404+
405+ resp , err := http .DefaultClient .Do (req )
406+ if err != nil {
407+ _ = yield (Event {}, err )
408+ return
409+ }
410+ defer resp .Body .Close ()
411+
412+ if resp .StatusCode != 200 {
413+ _ = yield (Event {}, fmt .Errorf ("got response status code %d" , resp .StatusCode ))
414+ return
415+ }
416+
417+ reader := bufio .NewReader (resp .Body )
418+
419+ evt := Event {}
420+ for {
421+ line , err := reader .ReadString ('\n' )
422+ if err != nil {
423+ _ = yield (Event {}, err )
424+ return
425+ }
426+ switch {
427+ case strings .HasPrefix (line , "data:" ):
428+ evt .Data = []byte (strings .TrimSpace (strings .TrimPrefix (line , "data:" )))
429+ case strings .HasPrefix (line , "event:" ):
430+ evt .Event = strings .TrimSpace (strings .TrimPrefix (line , "event:" ))
431+ case strings .HasPrefix (line , "id:" ):
432+ evt .ID = strings .TrimSpace (strings .TrimPrefix (line , "id:" ))
433+ case strings .HasPrefix (line , "\n " ):
434+ if ! yield (evt , nil ) {
435+ return
436+ }
437+ evt = Event {}
438+ default :
439+ _ = yield (Event {}, fmt .Errorf ("unknown line: '%s'" , line ))
440+ return
441+ }
442+ }
443+ }
444+ }
445+
446+ type Event struct {
447+ ID string
448+ Event string
449+ Data []byte // json
450+ }
0 commit comments