|
| 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 daemon_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDaemonCoreUpdateIndex(t *testing.T) { |
| 29 | + env, cli := createEnvForDaemon(t) |
| 30 | + defer env.CleanUp() |
| 31 | + |
| 32 | + grpcInst := cli.Create() |
| 33 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 34 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 35 | + })) |
| 36 | + |
| 37 | + // Set extra indexes |
| 38 | + err := cli.SetValue( |
| 39 | + "board_manager.additional_urls", ""+ |
| 40 | + `["http://arduino.esp8266.com/stable/package_esp8266com_index.json",`+ |
| 41 | + ` "http://downloads.arduino.cc/package_inexistent_index.json"]`) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + { |
| 45 | + cl, err := grpcInst.UpdateIndex(context.Background(), true) |
| 46 | + require.NoError(t, err) |
| 47 | + res, err := analyzeUpdateIndexStream(t, cl) |
| 48 | + require.NoError(t, err) |
| 49 | + require.Len(t, res, 1) |
| 50 | + require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Successful) |
| 51 | + } |
| 52 | + { |
| 53 | + cl, err := grpcInst.UpdateIndex(context.Background(), false) |
| 54 | + require.NoError(t, err) |
| 55 | + res, err := analyzeUpdateIndexStream(t, cl) |
| 56 | + require.Error(t, err) |
| 57 | + require.Len(t, res, 3) |
| 58 | + require.True(t, res["https://downloads.arduino.cc/packages/package_index.tar.bz2"].Successful) |
| 59 | + require.True(t, res["http://arduino.esp8266.com/stable/package_esp8266com_index.json"].Successful) |
| 60 | + require.False(t, res["http://downloads.arduino.cc/package_inexistent_index.json"].Successful) |
| 61 | + } |
| 62 | +} |
| 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.DownloadResult, error) { |
| 68 | + ongoingDownload := "" |
| 69 | + results := map[string]*commands.DownloadResult{} |
| 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 progress.Url != "" && progress.Url != ongoingDownload { |
| 82 | + require.Empty(t, ongoingDownload, "DownloadProgress: initiated a new download with closing the previous one") |
| 83 | + ongoingDownload = progress.Url |
| 84 | + } |
| 85 | + if progress.Completed { |
| 86 | + require.NotEmpty(t, ongoingDownload, "DownloadProgress: sent a 'completed' download message without starting it") |
| 87 | + ongoingDownload = "" |
| 88 | + } |
| 89 | + if progress.Downloaded > 0 { |
| 90 | + require.NotEmpty(t, ongoingDownload, "DownloadProgress: sent an update but never initiated a download") |
| 91 | + } |
| 92 | + } else if result := msg.GetDownloadResult(); result != nil { |
| 93 | + require.Empty(t, ongoingDownload, "DownloadResult: got a download result with closing it first") |
| 94 | + results[result.Url] = result |
| 95 | + } else { |
| 96 | + require.FailNow(t, "DownloadProgress: received a message without a Progress or a Result") |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments