Skip to content

Commit fdfcb06

Browse files
update the PlatformUpgrade daemon
1 parent 0a2d2a7 commit fdfcb06

File tree

3 files changed

+175
-21
lines changed

3 files changed

+175
-21
lines changed

Diff for: commands/daemon/daemon.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeRequest,
270270
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{Progress: p}) },
271271
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{TaskProgress: p}) },
272272
)
273-
if err != nil {
274-
return convertErrorToRPCStatus(err)
273+
if err2 := syncSend.Send(resp); err2 != nil {
274+
return err2
275275
}
276-
return syncSend.Send(resp)
276+
return convertErrorToRPCStatus(err)
277277
}
278278

279279
// PlatformSearch FIXMEDOC

Diff for: internal/integrationtest/arduino-cli.go

+12
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,15 @@ func (inst *ArduinoCLIInstance) UpdateIndex(ctx context.Context, ignoreCustomPac
458458
logCallf(">>> UpdateIndex(%+v)\n", req)
459459
return updCl, err
460460
}
461+
462+
// PlatformUpgrade calls the "PlatformUpgrade" gRPC method.
463+
func (inst *ArduinoCLIInstance) PlatformUpgrade(ctx context.Context, packager, arch string, skipPostInst bool) (commands.ArduinoCoreService_PlatformUpgradeClient, error) {
464+
installCl, err := inst.cli.daemonClient.PlatformUpgrade(ctx, &commands.PlatformUpgradeRequest{
465+
Instance: inst.instance,
466+
PlatformPackage: packager,
467+
Architecture: arch,
468+
SkipPostInstall: skipPostInst,
469+
})
470+
logCallf(">>> PlatformUpgrade(%v:%v)\n", packager, arch)
471+
return installCl, err
472+
}

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

+160-18
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ import (
1919
"context"
2020
"fmt"
2121
"io"
22+
"os"
2223
"testing"
2324
"time"
2425

26+
"github.com/arduino/arduino-cli/arduino"
2527
"github.com/arduino/arduino-cli/internal/integrationtest"
2628
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2729
"github.com/arduino/go-paths-helper"
30+
2831
"github.com/stretchr/testify/require"
2932
)
3033

@@ -215,34 +218,18 @@ func TestDaemonCoreUpdateIndex(t *testing.T) {
215218
` "http://downloads.arduino.cc/package_inexistent_index.json"]`)
216219
require.NoError(t, err)
217220

218-
analyzeUpdateIndexClient := func(cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) {
219-
analyzer := NewDownloadProgressAnalyzer(t)
220-
for {
221-
msg, err := cl.Recv()
222-
// fmt.Println("DOWNLOAD>", msg)
223-
if err == io.EOF {
224-
return analyzer.Results, nil
225-
}
226-
if err != nil {
227-
return analyzer.Results, err
228-
}
229-
require.NoError(t, err)
230-
analyzer.Process(msg.GetDownloadProgress())
231-
}
232-
}
233-
234221
{
235222
cl, err := grpcInst.UpdateIndex(context.Background(), true)
236223
require.NoError(t, err)
237-
res, err := analyzeUpdateIndexClient(cl)
224+
res, err := analyzeUpdateIndexClient(t, cl)
238225
require.NoError(t, err)
239226
require.Len(t, res, 1)
240227
require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Success)
241228
}
242229
{
243230
cl, err := grpcInst.UpdateIndex(context.Background(), false)
244231
require.NoError(t, err)
245-
res, err := analyzeUpdateIndexClient(cl)
232+
res, err := analyzeUpdateIndexClient(t, cl)
246233
require.Error(t, err)
247234
require.Len(t, res, 3)
248235
require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Success)
@@ -413,3 +400,158 @@ func TestDaemonLibrariesRescanOnInstall(t *testing.T) {
413400
}
414401

415402
}
403+
404+
func TestDaemonCoreUpgradePlatform(t *testing.T) {
405+
refreshInstance := func(t *testing.T, grpcInst *integrationtest.ArduinoCLIInstance) {
406+
require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) {}))
407+
}
408+
updateIndexAndInstallPlatform := func(cli *integrationtest.ArduinoCLI, grpcInst *integrationtest.ArduinoCLIInstance, version string) {
409+
refreshInstance(t, grpcInst)
410+
411+
// adding the additional urls
412+
err := cli.SetValue("board_manager.additional_urls", `["https://arduino.esp8266.com/stable/package_esp8266com_index.json"]`)
413+
require.NoError(t, err)
414+
415+
cl, err := grpcInst.UpdateIndex(context.Background(), false)
416+
require.NoError(t, err)
417+
res, err := analyzeUpdateIndexClient(t, cl)
418+
require.NoError(t, err)
419+
require.Len(t, res, 2)
420+
require.True(t, res["https://arduino.esp8266.com/stable/package_esp8266com_index.json"].Success)
421+
422+
refreshInstance(t, grpcInst)
423+
424+
// installing outdated version
425+
plInst, err := grpcInst.PlatformInstall(context.Background(), "esp8266", "esp8266", version, true)
426+
require.NoError(t, err)
427+
for {
428+
_, err := plInst.Recv()
429+
if err == io.EOF {
430+
break
431+
}
432+
require.NoError(t, err)
433+
}
434+
}
435+
436+
t.Run("upgraded successfully with additional urls", func(t *testing.T) {
437+
t.Run("and install.json is present", func(t *testing.T) {
438+
env, cli := createEnvForDaemon(t)
439+
defer env.CleanUp()
440+
441+
grpcInst := cli.Create()
442+
updateIndexAndInstallPlatform(cli, grpcInst, "3.1.0")
443+
444+
plUpgrade, err := grpcInst.PlatformUpgrade(context.Background(), "esp8266", "esp8266", true)
445+
require.NoError(t, err)
446+
447+
platform, upgradeError := analyzePlatformUpgradeClient(plUpgrade)
448+
require.NoError(t, upgradeError)
449+
require.NotNil(t, platform)
450+
require.True(t, platform.Indexed) // the esp866 is present in the additional-urls
451+
require.False(t, platform.MissingMetadata) // install.json is present
452+
})
453+
t.Run("and install.json is missing", func(t *testing.T) {
454+
env, cli := createEnvForDaemon(t)
455+
defer env.CleanUp()
456+
457+
grpcInst := cli.Create()
458+
updateIndexAndInstallPlatform(cli, grpcInst, "3.1.0")
459+
460+
// remove installed.json{
461+
x := env.RootDir().Join("A/packages/esp8266/hardware/esp8266/3.1.0/installed.json")
462+
require.NoError(t, os.Remove(x.String()))
463+
464+
plUpgrade, err := grpcInst.PlatformUpgrade(context.Background(), "esp8266", "esp8266", true)
465+
require.NoError(t, err)
466+
467+
platform, upgradeError := analyzePlatformUpgradeClient(plUpgrade)
468+
require.NoError(t, upgradeError)
469+
require.NotNil(t, platform)
470+
require.True(t, platform.Indexed) // the esp866 is not present in the additional-urls
471+
require.False(t, platform.MissingMetadata) // install.json is present because the old version got upgraded
472+
473+
})
474+
})
475+
476+
t.Run("upgrade failed", func(t *testing.T) {
477+
t.Run("without additional URLs", func(t *testing.T) {
478+
env, cli := createEnvForDaemon(t)
479+
defer env.CleanUp()
480+
481+
grpcInst := cli.Create()
482+
updateIndexAndInstallPlatform(cli, grpcInst, "3.1.0")
483+
484+
// remove esp8266 from the additional-urls
485+
require.NoError(t, cli.SetValue("board_manager.additional_urls", `[]`))
486+
refreshInstance(t, grpcInst)
487+
488+
plUpgrade, err := grpcInst.PlatformUpgrade(context.Background(), "esp8266", "esp8266", true)
489+
require.NoError(t, err)
490+
491+
platform, upgradeError := analyzePlatformUpgradeClient(plUpgrade)
492+
require.ErrorIs(t, upgradeError, (&arduino.PlatformAlreadyAtTheLatestVersionError{Platform: "esp8266:esp8266"}).ToRPCStatus().Err())
493+
require.NotNil(t, platform)
494+
require.False(t, platform.Indexed) // the esp866 is not present in the additional-urls
495+
require.False(t, platform.MissingMetadata) // install.json is present
496+
})
497+
t.Run("missing both additional URLs and install.json", func(t *testing.T) {
498+
env, cli := createEnvForDaemon(t)
499+
defer env.CleanUp()
500+
501+
grpcInst := cli.Create()
502+
updateIndexAndInstallPlatform(cli, grpcInst, "3.1.0")
503+
504+
// remove additional urls and installed.json
505+
{
506+
require.NoError(t, cli.SetValue("board_manager.additional_urls", `[]`))
507+
refreshInstance(t, grpcInst)
508+
509+
x := env.RootDir().Join("A/packages/esp8266/hardware/esp8266/3.1.0/installed.json")
510+
require.NoError(t, os.Remove(x.String()))
511+
}
512+
513+
plUpgrade, err := grpcInst.PlatformUpgrade(context.Background(), "esp8266", "esp8266", true)
514+
require.NoError(t, err)
515+
516+
platform, upgradeError := analyzePlatformUpgradeClient(plUpgrade)
517+
require.ErrorIs(t, upgradeError, (&arduino.PlatformAlreadyAtTheLatestVersionError{Platform: "esp8266:esp8266"}).ToRPCStatus().Err())
518+
require.NotNil(t, platform)
519+
require.False(t, platform.Indexed) // the esp866 is not present in the additional-urls
520+
require.True(t, platform.MissingMetadata) // install.json is present
521+
})
522+
})
523+
}
524+
525+
func analyzeUpdateIndexClient(t *testing.T, cl commands.ArduinoCoreService_UpdateIndexClient) (map[string]*commands.DownloadProgressEnd, error) {
526+
analyzer := NewDownloadProgressAnalyzer(t)
527+
for {
528+
msg, err := cl.Recv()
529+
if err == io.EOF {
530+
return analyzer.Results, nil
531+
}
532+
if err != nil {
533+
return analyzer.Results, err
534+
}
535+
require.NoError(t, err)
536+
analyzer.Process(msg.GetDownloadProgress())
537+
}
538+
}
539+
540+
func analyzePlatformUpgradeClient(cl commands.ArduinoCoreService_PlatformUpgradeClient) (*commands.Platform, error) {
541+
var platform *commands.Platform
542+
var upgradeError error
543+
for {
544+
msg, err := cl.Recv()
545+
if err == io.EOF {
546+
break
547+
}
548+
if msg.GetPlatform() != nil {
549+
platform = msg.GetPlatform()
550+
}
551+
if err != nil {
552+
upgradeError = err
553+
break
554+
}
555+
}
556+
return platform, upgradeError
557+
}

0 commit comments

Comments
 (0)