Skip to content

Commit 063f4f8

Browse files
[skip-changelog] Some integration test refactorings (#1911)
* Integrated 'go.bug.st/testsuite' It doesn't make much sense to keep it separate in a different library * Do not use shared download dir for 'cache clean' command tests * Added method to get cli download dir * Create infra for sharing directories between tests * Slighlty improved test output * Non-parallel testing is no longer required * Add more time to complete tests * Update internal/integrationtest/cache/cache_test.go Co-authored-by: MatteoPologruto <109663225+MatteoPologruto@users.noreply.github.com> Co-authored-by: MatteoPologruto <109663225+MatteoPologruto@users.noreply.github.com>
1 parent 0e0b615 commit 063f4f8

File tree

14 files changed

+232
-29
lines changed

14 files changed

+232
-29
lines changed

Taskfile.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,12 @@ tasks:
9595
- task: go:build
9696
dir: '{{default "./" .GO_MODULE_PATH}}'
9797
cmds:
98-
# "-p 1" will not run test in parallel, this is required for integration tests
9998
- |
10099
go test \
101100
-v \
102101
-short \
103-
-p 1 \
104102
-run '{{default ".*" .GO_TEST_REGEX}}' \
105-
{{default "-timeout 15m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} \
103+
{{default "-timeout 20m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} \
106104
-coverprofile=coverage_unit.txt \
107105
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} \
108106
{{.TEST_LDFLAGS}}

arduino/discovery/discovery_client/go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16i
315315
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
316316
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
317317
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
318-
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
319318
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
320319
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
321320
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

client_example/go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16i
297297
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
298298
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
299299
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
300-
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
301300
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
302301
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
303302
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

docsgen/go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
373373
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
374374
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
375375
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
376-
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
377376
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
378377
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
379378
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ require (
5858
)
5959

6060
require (
61+
github.com/rogpeppe/go-internal v1.3.0
6162
go.bug.st/testifyjson v1.1.1
62-
go.bug.st/testsuite v0.1.0
6363
)
6464

6565
require (

go.sum

+1-2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5H
310310
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
311311
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
312312
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
313+
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
313314
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
314315
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
315316
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -376,8 +377,6 @@ go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2
376377
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
377378
go.bug.st/testifyjson v1.1.1 h1:nHotIMK151LF3vYsU/b2RaoVaWCgrf2kvQeGNoZkGaA=
378379
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
379-
go.bug.st/testsuite v0.1.0 h1:oX4zdIB62+G5A0Kq4dja7Vy8tDiKqKVhhxkzhpMGgog=
380-
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
381380
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
382381
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
383382
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

internal/integrationtest/arduino-cli.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ import (
3333
"github.com/arduino/go-paths-helper"
3434
"github.com/fatih/color"
3535
"github.com/stretchr/testify/require"
36-
"go.bug.st/testsuite"
3736
"google.golang.org/grpc"
3837
)
3938

40-
func init() {
41-
testsuite.ProjectName = "cli"
42-
}
43-
4439
// FindRepositoryRootPath returns the repository root path
4540
func FindRepositoryRootPath(t *testing.T) *paths.Path {
4641
repoRootPath, err := paths.Getwd()
@@ -52,13 +47,18 @@ func FindRepositoryRootPath(t *testing.T) *paths.Path {
5247
return repoRootPath
5348
}
5449

50+
// FindArduinoCLIPath returns the path to the arduino-cli executable
51+
func FindArduinoCLIPath(t *testing.T) *paths.Path {
52+
return FindRepositoryRootPath(t).Join("arduino-cli")
53+
}
54+
5555
// CreateArduinoCLIWithEnvironment performs the minimum amount of actions
5656
// to build the default test environment.
57-
func CreateArduinoCLIWithEnvironment(t *testing.T) (*testsuite.Environment, *ArduinoCLI) {
58-
env := testsuite.NewEnvironment(t)
57+
func CreateArduinoCLIWithEnvironment(t *testing.T) (*Environment, *ArduinoCLI) {
58+
env := NewEnvironment(t)
5959

6060
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
61-
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
61+
ArduinoCLIPath: FindArduinoCLIPath(t),
6262
UseSharedStagingFolder: true,
6363
})
6464

@@ -89,7 +89,7 @@ type ArduinoCLIConfig struct {
8989
}
9090

9191
// NewArduinoCliWithinEnvironment creates a new Arduino CLI client inside the given environment.
92-
func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoCLIConfig) *ArduinoCLI {
92+
func NewArduinoCliWithinEnvironment(env *Environment, config *ArduinoCLIConfig) *ArduinoCLI {
9393
color.NoColor = false
9494
cli := &ArduinoCLI{
9595
path: config.ArduinoCLIPath,
@@ -100,7 +100,11 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
100100
workingDir: env.RootDir(),
101101
}
102102
if config.UseSharedStagingFolder {
103-
cli.stagingDir = env.SharedDownloadsDir()
103+
sharedDir := env.SharedDownloadsDir()
104+
cli.stagingDir = sharedDir.Lock()
105+
env.RegisterCleanUpCallback(func() {
106+
sharedDir.Unlock()
107+
})
104108
}
105109

106110
cli.cliEnvVars = map[string]string{
@@ -137,6 +141,11 @@ func (cli *ArduinoCLI) WorkingDir() *paths.Path {
137141
return cli.workingDir
138142
}
139143

144+
// DownloadDir returns the download directory
145+
func (cli *ArduinoCLI) DownloadDir() *paths.Path {
146+
return cli.stagingDir
147+
}
148+
140149
// CopySketch copies a sketch inside the testing environment and returns its path
141150
func (cli *ArduinoCLI) CopySketch(sketchName string) *paths.Path {
142151
p, err := paths.Getwd()

internal/integrationtest/cache/cache_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
)
2424

2525
func TestCacheClean(t *testing.T) {
26-
// Clean the cache under arduino caching file directory which is "<Arduino configure file path>/staging"
27-
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
26+
// This test should not use shared download directory because it will be cleaned up with 'cache clean' command
27+
env := integrationtest.NewEnvironment(t)
28+
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
29+
ArduinoCLIPath: integrationtest.FindArduinoCLIPath(t),
30+
})
2831
defer env.CleanUp()
2932

3033
_, _, err := cli.Run("cache", "clean")

internal/integrationtest/config/config_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestInitWithExistingCustomConfig(t *testing.T) {
5050
require.Equal(t, config["board_manager"]["additional_urls"].([]interface{})[0].(string), "https://example.com")
5151
require.Equal(t, config["daemon"]["port"].(string), "50051")
5252
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
53-
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
53+
require.Equal(t, config["directories"]["downloads"].(string), cli.DownloadDir().String())
5454
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
5555
require.Empty(t, config["logging"]["file"])
5656
require.Equal(t, config["logging"]["format"].(string), "text")
@@ -71,7 +71,7 @@ func TestInitWithExistingCustomConfig(t *testing.T) {
7171
require.Empty(t, config["board_manager"]["additional_urls"])
7272
require.Equal(t, config["daemon"]["port"].(string), "50051")
7373
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
74-
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
74+
require.Equal(t, config["directories"]["downloads"].(string), cli.DownloadDir().String())
7575
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
7676
require.Empty(t, config["logging"]["file"])
7777
require.Equal(t, config["logging"]["format"].(string), "text")
@@ -96,7 +96,7 @@ func TestInitOverwriteExistingCustomFile(t *testing.T) {
9696
require.Equal(t, config["board_manager"]["additional_urls"].([]interface{})[0].(string), "https://example.com")
9797
require.Equal(t, config["daemon"]["port"].(string), "50051")
9898
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
99-
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
99+
require.Equal(t, config["directories"]["downloads"].(string), cli.DownloadDir().String())
100100
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
101101
require.Empty(t, config["logging"]["file"])
102102
require.Equal(t, config["logging"]["format"].(string), "text")
@@ -115,7 +115,7 @@ func TestInitOverwriteExistingCustomFile(t *testing.T) {
115115
require.Empty(t, config["board_manager"]["additional_urls"])
116116
require.Equal(t, config["daemon"]["port"].(string), "50051")
117117
require.Equal(t, config["directories"]["data"].(string), cli.DataDir().String())
118-
require.Equal(t, config["directories"]["downloads"].(string), env.SharedDownloadsDir().String())
118+
require.Equal(t, config["directories"]["downloads"].(string), cli.DownloadDir().String())
119119
require.Equal(t, config["directories"]["user"].(string), cli.SketchbookDir().String())
120120
require.Empty(t, config["logging"]["file"])
121121
require.Equal(t, config["logging"]["format"].(string), "text")

internal/integrationtest/daemon/daemon_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020

2121
"github.com/arduino/arduino-cli/internal/integrationtest"
2222
"github.com/stretchr/testify/require"
23-
"go.bug.st/testsuite"
2423
)
2524

2625
// createEnvForDaemon performs the minimum required operations to start the arduino-cli daemon.
2726
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
2827
// The Environment must be disposed by calling the CleanUp method via defer.
29-
func createEnvForDaemon(t *testing.T) (*testsuite.Environment, *integrationtest.ArduinoCLI) {
30-
env := testsuite.NewEnvironment(t)
28+
func createEnvForDaemon(t *testing.T) (*integrationtest.Environment, *integrationtest.ArduinoCLI) {
29+
env := integrationtest.NewEnvironment(t)
3130

3231
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
3332
ArduinoCLIPath: integrationtest.FindRepositoryRootPath(t).Join("arduino-cli"),
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 license@arduino.cc.
15+
16+
package integrationtest
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
// ProjectName is the prefix used in the test temp files
26+
var ProjectName = "cli"
27+
28+
// Environment is a test environment for the test suite.
29+
type Environment struct {
30+
rootDir *paths.Path
31+
downloadsDir *SharedDir
32+
t *testing.T
33+
cleanUp func()
34+
}
35+
36+
// NewEnvironment creates a new test environment.
37+
func NewEnvironment(t *testing.T) *Environment {
38+
downloadsDir := NewSharedDir(t, "downloads")
39+
rootDir, err := paths.MkTempDir("", ProjectName)
40+
require.NoError(t, err)
41+
return &Environment{
42+
rootDir: rootDir,
43+
downloadsDir: downloadsDir,
44+
t: t,
45+
cleanUp: func() {
46+
require.NoError(t, rootDir.RemoveAll())
47+
},
48+
}
49+
}
50+
51+
// RegisterCleanUpCallback adds a clean up function to the clean up chain
52+
func (e *Environment) RegisterCleanUpCallback(newCleanUp func()) {
53+
previousCleanUp := e.cleanUp
54+
e.cleanUp = func() {
55+
newCleanUp()
56+
previousCleanUp()
57+
}
58+
}
59+
60+
// CleanUp removes the test environment.
61+
func (e *Environment) CleanUp() {
62+
e.cleanUp()
63+
}
64+
65+
// RootDir returns the root dir of the environment.
66+
func (e *Environment) RootDir() *paths.Path {
67+
return e.rootDir
68+
}
69+
70+
// SharedDownloadsDir return the shared directory for downloads
71+
func (e *Environment) SharedDownloadsDir() *SharedDir {
72+
return e.downloadsDir
73+
}
74+
75+
// T returns the testing environment
76+
func (e *Environment) T() *testing.T {
77+
return e.t
78+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 license@arduino.cc.
15+
16+
package integrationtest
17+
18+
import (
19+
"fmt"
20+
"net/http"
21+
"net/url"
22+
23+
"github.com/arduino/go-paths-helper"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
// HTTPServeFile spawn an http server that serve a single file. The server
28+
// is started on the given port. The URL to the file and a cleanup function are returned.
29+
func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {
30+
mux := http.NewServeMux()
31+
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
32+
http.ServeFile(w, r, path.String())
33+
})
34+
server := &http.Server{
35+
Addr: fmt.Sprintf(":%d", port),
36+
Handler: mux,
37+
}
38+
39+
t := env.T()
40+
fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path.Base()))
41+
require.NoError(t, err)
42+
43+
go func() {
44+
err := server.ListenAndServe()
45+
require.Equal(t, err, http.ErrServerClosed)
46+
}()
47+
48+
env.RegisterCleanUpCallback(func() {
49+
server.Close()
50+
})
51+
52+
return fileURL
53+
}

internal/integrationtest/main/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestVersion(t *testing.T) {
4646
require.NoError(t, err)
4747
require.Contains(t, string(stdout), "Version:")
4848
require.Contains(t, string(stdout), "Commit:")
49-
require.Empty(t, stderr)
49+
require.Empty(t, string(stderr))
5050

5151
// Checks if "version --format json" has a json as an output
5252
stdout, _, err = cli.Run("version", "--format", "json")

0 commit comments

Comments
 (0)