Skip to content

Commit e71238f

Browse files
committed
Refactored integration tests
1 parent 7ab07f3 commit e71238f

File tree

3 files changed

+75
-40
lines changed

3 files changed

+75
-40
lines changed

Diff for: arduino/httpclient/httpclient.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var tr = i18n.Tr
3232

3333
// DownloadFile downloads a file from a URL into the specified path. An optional config and options may be passed (or nil to use the defaults).
3434
// A DownloadProgressCB callback function must be passed to monitor download progress.
35-
func DownloadFile(path *paths.Path, URL string, label string, downloadCB rpc.DownloadProgressCB, config *downloader.Config, options ...downloader.DownloadOptions) (downloadError error) {
35+
func DownloadFile(path *paths.Path, URL string, label string, downloadCB rpc.DownloadProgressCB, config *downloader.Config, options ...downloader.DownloadOptions) error {
3636
if config == nil {
3737
c, err := GetDownloaderConfig()
3838
if err != nil {

Diff for: internal/integrationtest/daemon/daemon_core_test.go

+16-39
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"testing"
2323

24+
"github.com/arduino/arduino-cli/internal/integrationtest"
2425
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2526
"github.com/stretchr/testify/require"
2627
)
@@ -41,57 +42,33 @@ func TestDaemonCoreUpdateIndex(t *testing.T) {
4142
` "http://downloads.arduino.cc/package_inexistent_index.json"]`)
4243
require.NoError(t, err)
4344

45+
analyzeUpdateIndexClient := func(cl commands.ArduinoCoreService_UpdateIndexClient) map[string]*commands.DownloadProgressEnd {
46+
analyzer := integrationtest.NewDownloadProgressAnalyzer(t)
47+
for {
48+
msg, err := cl.Recv()
49+
if err == io.EOF {
50+
break
51+
}
52+
require.NoError(t, err)
53+
analyzer.Process(msg.GetDownloadProgress())
54+
}
55+
return analyzer.Results
56+
}
57+
4458
{
4559
cl, err := grpcInst.UpdateIndex(context.Background(), true)
4660
require.NoError(t, err)
47-
res, err := analyzeUpdateIndexStream(t, cl)
48-
require.NoError(t, err)
61+
res := analyzeUpdateIndexClient(cl)
4962
require.Len(t, res, 1)
5063
require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Success)
5164
}
5265
{
5366
cl, err := grpcInst.UpdateIndex(context.Background(), false)
5467
require.NoError(t, err)
55-
res, err := analyzeUpdateIndexStream(t, cl)
56-
require.Error(t, err)
68+
res := analyzeUpdateIndexClient(cl)
5769
require.Len(t, res, 3)
5870
require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Success)
5971
require.True(t, res["http://arduino.esp8266.com/stable/package_esp8266com_index.json"].Success)
6072
require.False(t, res["http://downloads.arduino.cc/package_inexistent_index.json"].Success)
6173
}
6274
}
63-
64-
// analyzeUpdateIndexStream runs an update index checking if the sequence of DownloadProgress and
65-
// DownloadResult messages is correct. It returns a map reporting all the DownloadResults messages
66-
// received (it maps urls to DownloadResults).
67-
func analyzeUpdateIndexStream(t *testing.T, cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) {
68-
ongoingDownload := ""
69-
results := map[string]*commands.DownloadProgressEnd{}
70-
for {
71-
msg, err := cl.Recv()
72-
if err == io.EOF {
73-
return results, nil
74-
}
75-
if err != nil {
76-
return results, err
77-
}
78-
require.NoError(t, err)
79-
fmt.Printf("UPDATE> %+v\n", msg)
80-
if progress := msg.GetDownloadProgress(); progress != nil {
81-
if start := progress.GetStart(); start != nil {
82-
require.Empty(t, ongoingDownload, "DownloadProgressStart: started a download without 'completing' the previous one")
83-
ongoingDownload = start.Url
84-
} else if update := progress.GetUpdate(); update != nil {
85-
require.NotEmpty(t, ongoingDownload, "DownloadProgressUpdate: received update, but the download is not yet started...")
86-
} else if end := progress.GetEnd(); end != nil {
87-
require.NotEmpty(t, ongoingDownload, "DownloadProgress: received a 'completed' notification but never initiated a download")
88-
results[ongoingDownload] = end
89-
ongoingDownload = ""
90-
} else {
91-
require.FailNow(t, "DownloadProgress: received an empty DownloadProgress (without Start, Update or End)")
92-
}
93-
} else {
94-
require.FailNow(t, "DownloadProgress: received an empty message (without a DownloadProgress)")
95-
}
96-
}
97-
}

Diff for: internal/integrationtest/download_progress.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package integrationtest
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
// DownloadProgressAnalyzer analyzes DownloadProgress messages for consistency
26+
type DownloadProgressAnalyzer struct {
27+
t *testing.T
28+
ongoingDownload string
29+
Results map[string]*commands.DownloadProgressEnd
30+
}
31+
32+
// NewDownloadProgressAnalyzer creates a new DownloadProgressAnalyzer
33+
func NewDownloadProgressAnalyzer(t *testing.T) *DownloadProgressAnalyzer {
34+
return &DownloadProgressAnalyzer{
35+
t: t,
36+
Results: map[string]*commands.DownloadProgressEnd{},
37+
}
38+
}
39+
40+
// Process the given DownloadProgress message. If inconsistencies are detected the
41+
// test will fail.
42+
func (a *DownloadProgressAnalyzer) Process(progress *commands.DownloadProgress) {
43+
if progress == nil {
44+
return
45+
}
46+
if start := progress.GetStart(); start != nil {
47+
require.Empty(a.t, a.ongoingDownload, "DownloadProgressStart: started a download without 'completing' the previous one")
48+
a.ongoingDownload = start.Url
49+
} else if update := progress.GetUpdate(); update != nil {
50+
require.NotEmpty(a.t, a.ongoingDownload, "DownloadProgressUpdate: received update, but the download is not yet started...")
51+
} else if end := progress.GetEnd(); end != nil {
52+
require.NotEmpty(a.t, a.ongoingDownload, "DownloadProgress: received a 'completed' notification but never initiated a download")
53+
a.Results[a.ongoingDownload] = end
54+
a.ongoingDownload = ""
55+
} else {
56+
require.FailNow(a.t, "DownloadProgress: received an empty DownloadProgress (without Start, Update or End)")
57+
}
58+
}

0 commit comments

Comments
 (0)