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"
@@ -34,12 +38,39 @@ func TestStableToUnstable(t *testing.T) {
3438 buildDockerImage (t , "test.Dockerfile" , "apt-test-update-image" , * arch )
3539 fmt .Println ("**** RUN docker image *****" )
3640 runDockerContainer (t , "apt-test-update" , "apt-test-update-image" )
37- preUpdateVersion := runDockerSystemVersion (t , "apt-test-update" )
38- runDockerSystemUpdate (t , "apt-test-update" )
39- postUpdateVersion := runDockerSystemVersion (t , "apt-test-update" )
41+ // preUpdateVersion := runDockerSystemVersion(t, "apt-test-update")
42+ // runDockerSystemUpdate(t, "apt-test-update")
43+ // postUpdateVersion := runDockerSystemVersion(t, "apt-test-update")
44+ // //runDockerCleanUp(t, "apt-test-update")
45+ // require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli+"\n")
46+ // 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+
4073 runDockerCleanUp (t , "apt-test-update" )
41- require .Equal (t , preUpdateVersion , "Arduino App CLI " + tagAppCli + "\n " )
42- require .Equal (t , postUpdateVersion , "Arduino App CLI " + majorTag + "\n " )
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" )
@@ -261,11 +292,11 @@ func runDockerDaemon(t *testing.T, containerName string) string {
261292
262293 cmd := exec .Command (
263294 "docker" , "exec" ,
295+ "-d" , // detached mode
264296 "--user" , "arduino" ,
265297 containerName ,
266298 "arduino-app-cli" , "daemon" ,
267299 )
268-
269300 output , err := cmd .CombinedOutput ()
270301 if err != nil {
271302 log .Fatalf ("command failed: %v\n Output: %s" , err , output )
@@ -337,3 +368,82 @@ 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+ func NewSSEClient (ctx context.Context , method , url string ) iter.Seq2 [Event , error ] {
397+ return func (yield func (Event , error ) bool ) {
398+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
399+ if err != nil {
400+ _ = yield (Event {}, err )
401+ return
402+ }
403+
404+ resp , err := http .DefaultClient .Do (req )
405+ if err != nil {
406+ _ = yield (Event {}, err )
407+ return
408+ }
409+ defer resp .Body .Close ()
410+
411+ if resp .StatusCode != 200 {
412+ _ = yield (Event {}, fmt .Errorf ("got response status code %d" , resp .StatusCode ))
413+ return
414+ }
415+
416+ reader := bufio .NewReader (resp .Body )
417+
418+ evt := Event {}
419+ for {
420+ line , err := reader .ReadString ('\n' )
421+ if err != nil {
422+ _ = yield (Event {}, err )
423+ return
424+ }
425+ switch {
426+ case strings .HasPrefix (line , "data:" ):
427+ evt .Data = []byte (strings .TrimSpace (strings .TrimPrefix (line , "data:" )))
428+ case strings .HasPrefix (line , "event:" ):
429+ evt .Event = strings .TrimSpace (strings .TrimPrefix (line , "event:" ))
430+ case strings .HasPrefix (line , "id:" ):
431+ evt .ID = strings .TrimSpace (strings .TrimPrefix (line , "id:" ))
432+ case strings .HasPrefix (line , "\n " ):
433+ if ! yield (evt , nil ) {
434+ return
435+ }
436+ evt = Event {}
437+ default :
438+ _ = yield (Event {}, fmt .Errorf ("unknown line: '%s'" , line ))
439+ return
440+ }
441+ }
442+ }
443+ }
444+
445+ type Event struct {
446+ ID string
447+ Event string
448+ Data []byte // json
449+ }
0 commit comments