Skip to content

Commit 8eae685

Browse files
committed
Client test
1 parent 6ed9ecb commit 8eae685

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

internal/testtools/deb_test.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strconv"
1717
"strings"
1818
"testing"
19+
"time"
1920

2021
"github.com/stretchr/testify/require"
2122
)
@@ -38,36 +39,37 @@ func TestStableToUnstable(t *testing.T) {
3839
buildDockerImage(t, "test.Dockerfile", "apt-test-update-image", *arch)
3940
fmt.Println("**** RUN docker image *****")
4041
runDockerContainer(t, "apt-test-update", "apt-test-update-image")
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")
42+
preUpdateVersion := runDockerSystemVersion(t, "apt-test-update")
43+
runDockerSystemUpdate(t, "apt-test-update")
44+
postUpdateVersion := runDockerSystemVersion(t, "apt-test-update")
45+
runDockerCleanUp(t, "apt-test-update")
46+
require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli+"\n")
47+
require.Equal(t, postUpdateVersion, "Arduino App CLI "+majorTag+"\n")
4748
}
4849

4950
func TestClientUpdate(t *testing.T) {
5051

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")
52+
fmt.Println("**** BUILD docker image *****")
53+
buildDockerImage(t, "test.Dockerfile", "apt-test-update-image", *arch)
54+
fmt.Println("**** RUN docker image *****")
55+
runDockerContainer(t, "apt-test-update", "apt-test-update-image")
5756
//Start the daemon
5857
runDockerDaemon(t, "apt-test-update")
58+
time.Sleep(5 * time.Second) //wait for the daemon to be fully started
5959
//PUT on the /v1/updates/apply
60-
status := putUpdateRequest(t, "http://localhost:8080/v1/system/update/apply")
60+
status := putUpdateRequest(t, "http://127.0.0.1:8800/v1/system/update/apply")
6161
fmt.Printf("Response status: %s\n", status)
62-
//ClientSSE
6362

64-
itr := NewSSEClient(context.Background(), "GET", "http://localhost:8080/v1/system/update/apply")
63+
itr := NewSSEClient(context.Background(), "GET", "http://localhost:8800/v1/system/update/events")
6564

6665
for event, err := range itr {
6766
if err != nil {
6867
log.Fatalf("Error receiving SSE event: %v", err)
6968
}
7069
fmt.Printf("Received event: ID=%s, Event=%s, Data=%s\n", event.ID, event.Event, string(event.Data))
70+
if string(event.Data) == "Download complete" {
71+
break
72+
}
7173
}
7274

7375
runDockerCleanUp(t, "apt-test-update")
@@ -233,8 +235,10 @@ func runDockerContainer(t *testing.T, containerName string, containerImageName s
233235

234236
cmd := exec.Command(
235237
"docker", "run", "--rm", "-d",
238+
"-p", "8800:8800",
236239
"--privileged",
237240
"--cgroupns=host",
241+
"--network", "host",
238242
"-v", "/sys/fs/cgroup:/sys/fs/cgroup:rw",
239243
"-v", "/var/run/docker.sock:/var/run/docker.sock",
240244
"-e", "DOCKER_HOST=unix:///var/run/docker.sock",
@@ -287,22 +291,22 @@ func runDockerSystemUpdate(t *testing.T, containerName string) {
287291

288292
}
289293

290-
func runDockerDaemon(t *testing.T, containerName string) string {
294+
func runDockerDaemon(t *testing.T, containerName string) {
291295
t.Helper()
292296

293297
cmd := exec.Command(
294298
"docker", "exec",
295-
"-d", // detached mode
299+
"-d",
296300
"--user", "arduino",
297301
containerName,
298-
"arduino-app-cli", "daemon",
302+
"systemctl", "start", "arduino-app-cli",
299303
)
300304
output, err := cmd.CombinedOutput()
301305
if err != nil {
302-
log.Fatalf("command failed: %v\nOutput: %s", err, output)
306+
log.Fatalf("command failed: %v\n Output: %s", err, output)
303307
}
304308

305-
return string(output)
309+
fmt.Printf("Daemon started: %s\n", output)
306310

307311
}
308312

@@ -373,23 +377,22 @@ func putUpdateRequest(t *testing.T, url string) string {
373377

374378
t.Helper()
375379

376-
// Create PUT request
377380
req, err := http.NewRequest(http.MethodPut, url, nil)
378381
if err != nil {
379382
log.Fatalf("Error creating request: %v", err)
380383
}
381384

382-
// Optional: add headers if your API needs them
383385
req.Header.Set("Content-Type", "application/json")
384386

385-
// Send the request
386387
client := &http.Client{}
387388
resp, err := client.Do(req)
388389
if err != nil {
389-
log.Fatalf("Error making request: %v", err)
390+
log.Fatalf("Error sending request: %v", err)
390391
}
391392
defer resp.Body.Close()
392393

394+
fmt.Printf("Response status: %s\n", resp.Status)
395+
393396
// Check status code
394397
return resp.Status
395398
}

internal/testtools/test.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ RUN usermod -aG docker arduino
2828
RUN echo "deb [trusted=yes arch=${ARCH}] file:/var/www/html/myrepo trixie main" \
2929
> /etc/apt/sources.list.d/my-mock-repo.list
3030

31+
EXPOSE 8800
3132
# CMD: systemd must be PID 1
3233
CMD ["/sbin/init"]

0 commit comments

Comments
 (0)