Skip to content

Commit 2e7d6c2

Browse files
Apply review suggestions to the integration test
1 parent 527f7d2 commit 2e7d6c2

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

internal/integrationtest/arduino-cli.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,14 @@ func NewArduinoCliWithinEnvironment(env *Environment, config *ArduinoCLIConfig)
123123
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
124124
// The Environment must be disposed by calling the CleanUp method via defer.
125125
func CreateEnvForDaemon(t *testing.T) (*Environment, *ArduinoCLI) {
126-
return CreateEnvForDaemonWithUserAgent(t, "cli-test/0.0.0")
127-
}
128-
129-
// CreateEnvForDaemonWithUserAgent performs the minimum required operations to start the arduino-cli daemon.
130-
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
131-
// The Environment must be disposed by calling the CleanUp method via defer.
132-
func CreateEnvForDaemonWithUserAgent(t *testing.T, userAgent string) (*Environment, *ArduinoCLI) {
133126
env := NewEnvironment(t)
134127

135128
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
136129
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
137130
UseSharedStagingFolder: true,
138131
})
139132

140-
_ = cli.StartDaemon(false, userAgent)
133+
_ = cli.StartDaemon(false)
141134
return env, cli
142135
}
143136

@@ -417,7 +410,7 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
417410
}
418411

419412
// StartDaemon starts the Arduino CLI daemon. It returns the address of the daemon.
420-
func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string {
413+
func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
421414
args := []string{"daemon", "--json"}
422415
if cli.cliConfigPath != nil {
423416
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
@@ -460,7 +453,7 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool, userAgent string) string {
460453
conn, err := grpc.NewClient(
461454
cli.daemonAddr,
462455
grpc.WithTransportCredentials(insecure.NewCredentials()),
463-
grpc.WithUserAgent(userAgent),
456+
grpc.WithUserAgent("cli-test/0.0.0"),
464457
)
465458
if err != nil {
466459
connErr = err

internal/integrationtest/core/core_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestCoreSearch(t *testing.T) {
6969

7070
// Set up an http server to serve our custom index file
7171
test_index := paths.New("..", "testdata", "test_index.json")
72-
url := env.HTTPServeFile(8000, test_index, false)
72+
url := env.HTTPServeFile(8000, test_index)
7373

7474
// Run update-index with our test index
7575
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -161,7 +161,7 @@ func TestCoreSearchNoArgs(t *testing.T) {
161161

162162
// Set up an http server to serve our custom index file
163163
testIndex := paths.New("..", "testdata", "test_index.json")
164-
url := env.HTTPServeFile(8000, testIndex, false)
164+
url := env.HTTPServeFile(8000, testIndex)
165165

166166
// update custom index and install test core (installed cores affect `core search`)
167167
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -749,7 +749,7 @@ func TestCoreSearchSortedResults(t *testing.T) {
749749

750750
// Set up the server to serve our custom index file
751751
testIndex := paths.New("..", "testdata", "test_index.json")
752-
url := env.HTTPServeFile(8000, testIndex, false)
752+
url := env.HTTPServeFile(8000, testIndex)
753753

754754
// update custom index
755755
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -821,7 +821,7 @@ func TestCoreListSortedResults(t *testing.T) {
821821

822822
// Set up the server to serve our custom index file
823823
testIndex := paths.New("..", "testdata", "test_index.json")
824-
url := env.HTTPServeFile(8000, testIndex, false)
824+
url := env.HTTPServeFile(8000, testIndex)
825825

826826
// update custom index
827827
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -892,7 +892,7 @@ func TestCoreListDeprecatedPlatformWithInstalledJson(t *testing.T) {
892892

893893
// Set up the server to serve our custom index file
894894
testIndex := paths.New("..", "testdata", "test_index.json")
895-
url := env.HTTPServeFile(8000, testIndex, false)
895+
url := env.HTTPServeFile(8000, testIndex)
896896

897897
// update custom index
898898
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -1110,8 +1110,8 @@ func TestCoreInstallRunsToolPostInstallScript(t *testing.T) {
11101110
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
11111111
defer env.CleanUp()
11121112

1113-
url := env.HTTPServeFile(8080, paths.New("testdata", "package_with_postinstall_index.json"), false)
1114-
env.HTTPServeFile(8081, paths.New("testdata", "core_with_postinst.zip"), false)
1113+
url := env.HTTPServeFile(8080, paths.New("testdata", "package_with_postinstall_index.json"))
1114+
env.HTTPServeFile(8081, paths.New("testdata", "core_with_postinst.zip"))
11151115

11161116
_, _, err := cli.Run("core", "update-index", "--additional-urls", url.String())
11171117
require.NoError(t, err)
@@ -1129,7 +1129,7 @@ func TestCoreBrokenDependency(t *testing.T) {
11291129

11301130
// Set up an http server to serve our custom index file
11311131
test_index := paths.New("..", "testdata", "test_index.json")
1132-
url := env.HTTPServeFile(8000, test_index, false)
1132+
url := env.HTTPServeFile(8000, test_index)
11331133

11341134
// Run update-index with our test index
11351135
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
@@ -1145,7 +1145,7 @@ func TestCoreUpgradeWarningWithPackageInstalledButNotIndexed(t *testing.T) {
11451145
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
11461146
defer env.CleanUp()
11471147

1148-
url := env.HTTPServeFile(8000, paths.New("..", "testdata", "test_index.json"), false).String()
1148+
url := env.HTTPServeFile(8000, paths.New("..", "testdata", "test_index.json")).String()
11491149

11501150
t.Run("missing additional-urls", func(t *testing.T) {
11511151
// update index
@@ -1187,7 +1187,7 @@ func TestCoreHavingIncompatibleDepTools(t *testing.T) {
11871187
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
11881188
defer env.CleanUp()
11891189

1190-
url := env.HTTPServeFile(8000, paths.New("..", "testdata", "test_index.json"), false).String()
1190+
url := env.HTTPServeFile(8000, paths.New("..", "testdata", "test_index.json")).String()
11911191
additionalURLs := "--additional-urls=" + url
11921192

11931193
_, _, err := cli.Run("core", "update-index", additionalURLs)

internal/integrationtest/daemon/daemon_test.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"errors"
2121
"fmt"
2222
"io"
23+
"maps"
24+
"net/http"
25+
"net/http/httptest"
26+
"strings"
2327
"testing"
2428
"time"
2529

@@ -562,15 +566,42 @@ func TestDaemonUserAgent(t *testing.T) {
562566
// Set up an http server to serve our custom index file
563567
// The user-agent is tested inside the HTTPServeFile function
564568
test_index := paths.New("..", "testdata", "test_index.json")
565-
url := env.HTTPServeFile(8000, test_index, true)
569+
url := env.HTTPServeFile(8000, test_index)
570+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
571+
// Test that the user-agent contains metadata from the context when the CLI is in daemon mode
572+
userAgent := r.Header.Get("User-Agent")
573+
574+
require.Contains(t, userAgent, "cli-test/0.0.0")
575+
require.Contains(t, userAgent, "grpc-go")
576+
// Depends on how we built the client we may have git-snapshot or 0.0.0-git in dev releases
577+
require.Condition(t, func() (success bool) {
578+
return strings.Contains(userAgent, "arduino-cli/git-snapshot") ||
579+
strings.Contains(userAgent, "arduino-cli/0.0.0-git")
580+
})
581+
582+
proxiedReq, err := http.NewRequest(r.Method, url.String(), r.Body)
583+
require.NoError(t, err)
584+
maps.Copy(proxiedReq.Header, r.Header)
585+
586+
proxiedResp, err := http.DefaultTransport.RoundTrip(proxiedReq)
587+
require.NoError(t, err)
588+
defer proxiedResp.Body.Close()
589+
590+
// Copy the headers from the proxy response to the original response
591+
maps.Copy(r.Header, proxiedReq.Header)
592+
w.WriteHeader(proxiedResp.StatusCode)
593+
io.Copy(w, proxiedResp.Body)
594+
}))
595+
defer ts.Close()
566596

567597
grpcInst := cli.Create()
568598
require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) {
569599
fmt.Printf("INIT> %v\n", ir.GetMessage())
570600
}))
571601

572602
// Set extra indexes
573-
err := cli.SetValue("board_manager.additional_urls", `["http://127.0.0.1:8000/test_index.json"]`)
603+
additionalURL := ts.URL + "/test_index.json"
604+
err := cli.SetValue("board_manager.additional_urls", fmt.Sprintf(`["%s"]`, additionalURL))
574605
require.NoError(t, err)
575606

576607
{
@@ -579,7 +610,7 @@ func TestDaemonUserAgent(t *testing.T) {
579610
res, err := analyzeUpdateIndexClient(t, cl)
580611
require.NoError(t, err)
581612
require.Len(t, res, 2)
582-
require.True(t, res[url.String()].GetSuccess())
613+
require.True(t, res[additionalURL].GetSuccess())
583614
}
584615
}
585616

internal/integrationtest/http_server.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,11 @@ import (
2626

2727
// HTTPServeFile spawn an http server that serve a single file. The server
2828
// 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, isDaemon bool) *url.URL {
29+
func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {
3030
t := env.T()
3131
mux := http.NewServeMux()
3232
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
3333
http.ServeFile(w, r, path.String())
34-
if isDaemon {
35-
// Test that the user-agent contains metadata from the context when the CLI is in daemon mode
36-
userAgent := r.Header.Get("User-Agent")
37-
require.Contains(t, userAgent, "arduino-cli/git-snapshot")
38-
require.Contains(t, userAgent, "cli-test/0.0.0")
39-
require.Contains(t, userAgent, "grpc-go")
40-
}
4134
})
4235
server := &http.Server{
4336
Addr: fmt.Sprintf(":%d", port),

0 commit comments

Comments
 (0)