|
| 1 | +package testtools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +var arch = flag.String("arch", "amd64", "target architecture") |
| 15 | + |
| 16 | +func TestStableToUnstable(t *testing.T) { |
| 17 | + fmt.Printf("***** ARCH %s ***** \n", *arch) |
| 18 | + tagAppCli := FetchDebPackage(t, "arduino-app-cli", "latest", *arch) |
| 19 | + FetchDebPackage(t, "arduino-router", "latest", *arch) |
| 20 | + majorTag := majorTag(t, tagAppCli) |
| 21 | + _ = minorTag(t, tagAppCli) |
| 22 | + |
| 23 | + fmt.Printf("Updating from stable version %s to unstable version %s \n", tagAppCli, majorTag) |
| 24 | + fmt.Printf("Building local deb version %s \n", majorTag) |
| 25 | + buildDebVersion(t, majorTag, *arch) |
| 26 | + // fmt.Printf("Check folder structure and deb downloaded\n") |
| 27 | + // ls(t) |
| 28 | + fmt.Println("**** BUILD docker image *****") |
| 29 | + buildDockerImage(t, "test.Dockerfile", "apt-test-update-image", *arch) |
| 30 | + fmt.Println("**** RUN docker image *****") |
| 31 | + runDockerContainer(t, "apt-test-update", "apt-test-update-image") |
| 32 | + preUpdateVersion := runDockerSystemVersion(t, "apt-test-update") |
| 33 | + runDockerSystemUpdate(t, "apt-test-update") |
| 34 | + postUpdateVersion := runDockerSystemVersion(t, "apt-test-update") |
| 35 | + runDockerCleanUp(t, "apt-test-update") |
| 36 | + require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli+"\n") |
| 37 | + require.Equal(t, postUpdateVersion, "Arduino App CLI "+majorTag+"\n") |
| 38 | +} |
| 39 | + |
| 40 | +func TestClientUpdate(t *testing.T) { |
| 41 | + |
| 42 | + fmt.Printf("***** ARCH %s ***** \n", *arch) |
| 43 | + tagAppCli := FetchDebPackage(t, "arduino-app-cli", "latest", *arch) |
| 44 | + FetchDebPackage(t, "arduino-router", "latest", *arch) |
| 45 | + majorTag := majorTag(t, tagAppCli) |
| 46 | + |
| 47 | + fmt.Println("**** RUN docker image *****") |
| 48 | + runDockerContainer(t, "apt-test-update", "apt-test-update-image") |
| 49 | + preUpdateVersion := runDockerSystemVersion(t, "apt-test-update") |
| 50 | + |
| 51 | + runDockerDaemon(t, "apt-test-update") |
| 52 | + time.Sleep(5 * time.Second) //wait for the daemon to be fully started |
| 53 | + status := putUpdateRequest(t, "http://127.0.0.1:8800/v1/system/update/apply") |
| 54 | + fmt.Printf("Response status: %s\n", status) |
| 55 | + |
| 56 | + itr := NewSSEClient(context.Background(), "GET", "http://localhost:8800/v1/system/update/events") |
| 57 | + |
| 58 | + for event, err := range itr { |
| 59 | + if err != nil { |
| 60 | + log.Printf("Error receiving SSE event: %v", err) |
| 61 | + } |
| 62 | + fmt.Printf("Received event: ID=%s, Event=%s, Data=%s\n", event.ID, event.Event, string(event.Data)) |
| 63 | + if string(event.Data) == "Download complete" { |
| 64 | + fmt.Println("✅ Download complete — exiting successfully.") |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + postUpdateVersion := runDockerSystemVersion(t, "apt-test-update") |
| 69 | + |
| 70 | + require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli+"\n") |
| 71 | + require.Equal(t, postUpdateVersion, "Arduino App CLI "+majorTag+"\n") |
| 72 | + runDockerCleanUp(t, "apt-test-update") |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +func TestUnstableToStable(t *testing.T) { |
| 77 | + tagAppCli := FetchDebPackage(t, "arduino-app-cli", "latest", *arch) |
| 78 | + FetchDebPackage(t, "arduino-router", "latest", *arch) |
| 79 | + minorTag := minorTag(t, tagAppCli) |
| 80 | + moveDeb(t, "build/stable", "build/", "arduino-app-cli", tagAppCli, *arch) |
| 81 | + |
| 82 | + fmt.Printf("Updating from unstable version %s to stable version %s \n", minorTag, tagAppCli) |
| 83 | + fmt.Printf("Building local deb version %s \n", minorTag) |
| 84 | + buildDebVersion(t, minorTag, *arch) |
| 85 | + moveDeb(t, "build/", "build/stable", "arduino-app-cli", minorTag, *arch) |
| 86 | + |
| 87 | + fmt.Printf("Check folder structure and deb downloaded\n") |
| 88 | + ls(t) |
| 89 | + |
| 90 | + fmt.Println("**** BUILD docker image *****") |
| 91 | + buildDockerImage(t, "test.Dockerfile", "test-apt-update-unstable-image", *arch) |
| 92 | + fmt.Println("**** RUN docker image *****") |
| 93 | + runDockerContainer(t, "test-apt-update-unstable", "test-apt-update-unstable-image") |
| 94 | + preUpdateVersion := runDockerSystemVersion(t, "apt-test-update-unstable") |
| 95 | + runDockerSystemUpdate(t, "apt-test-update-unstable") |
| 96 | + postUpdateVersion := runDockerSystemVersion(t, "apt-test-update-unstable") |
| 97 | + runDockerCleanUp(t, "apt-test-update-unstable") |
| 98 | + require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli+"\n") |
| 99 | + require.Equal(t, postUpdateVersion, "Arduino App CLI "+minorTag+"\n") |
| 100 | + |
| 101 | +} |
0 commit comments