|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2024 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 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 29 | + "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 30 | + "github.com/arduino/go-paths-helper" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | +) |
| 33 | + |
| 34 | +func TestUploadCancelation(t *testing.T) { |
| 35 | + env, cli := integrationtest.CreateEnvForDaemon(t) |
| 36 | + defer env.CleanUp() |
| 37 | + |
| 38 | + grpcInst := cli.Create() |
| 39 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 40 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 41 | + })) |
| 42 | + |
| 43 | + plInst, err := grpcInst.PlatformInstall(context.Background(), "arduino", "avr", "1.8.6", true) |
| 44 | + require.NoError(t, err) |
| 45 | + for { |
| 46 | + msg, err := plInst.Recv() |
| 47 | + if errors.Is(err, io.EOF) { |
| 48 | + break |
| 49 | + } |
| 50 | + require.NoError(t, err) |
| 51 | + fmt.Printf("INSTALL> %v\n", msg) |
| 52 | + } |
| 53 | + |
| 54 | + // Mock avrdude |
| 55 | + cli.InstallMockedAvrdude(t) |
| 56 | + |
| 57 | + // Re-init instance to update changes |
| 58 | + require.NoError(t, grpcInst.Init("", "", func(ir *commands.InitResponse) { |
| 59 | + fmt.Printf("INIT> %v\n", ir.GetMessage()) |
| 60 | + })) |
| 61 | + |
| 62 | + // Build sketch for upload |
| 63 | + sk := paths.New("testdata", "bare_minimum") |
| 64 | + compile, err := grpcInst.Compile(context.Background(), "arduino:avr:uno", sk.String(), "") |
| 65 | + require.NoError(t, err) |
| 66 | + for { |
| 67 | + msg, err := compile.Recv() |
| 68 | + if errors.Is(err, io.EOF) { |
| 69 | + break |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + fmt.Println("COMPILE ERROR>", err) |
| 73 | + require.FailNow(t, "Expected successful compile", "compilation failed") |
| 74 | + break |
| 75 | + } |
| 76 | + if msg.GetOutStream() != nil { |
| 77 | + fmt.Printf("COMPILE OUT> %v\n", string(msg.GetOutStream())) |
| 78 | + } |
| 79 | + if msg.GetErrStream() != nil { |
| 80 | + fmt.Printf("COMPILE ERR> %v\n", string(msg.GetErrStream())) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Try upload and interrupt the call after 1 sec |
| 85 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 86 | + upload, err := grpcInst.Upload(ctx, "arduino:avr:uno", sk.String(), "/dev/ttyACM0", "serial") |
| 87 | + require.NoError(t, err) |
| 88 | + checkFile := "" |
| 89 | + for { |
| 90 | + msg, err := upload.Recv() |
| 91 | + if errors.Is(err, io.EOF) { |
| 92 | + require.FailNow(t, "Expected interrupted upload", "upload succeeded") |
| 93 | + break |
| 94 | + } |
| 95 | + if err != nil { |
| 96 | + fmt.Println("UPLOAD ERROR>", err) |
| 97 | + break |
| 98 | + } |
| 99 | + if out := string(msg.GetOutStream()); out != "" { |
| 100 | + fmt.Printf("UPLOAD OUT> %v\n", out) |
| 101 | + if strings.HasPrefix(out, "CHECKFILE: ") { |
| 102 | + checkFile = strings.TrimSpace(out[11:]) |
| 103 | + } |
| 104 | + } |
| 105 | + if msg.GetErrStream() != nil { |
| 106 | + fmt.Printf("UPLOAD ERR> %v\n", string(msg.GetErrStream())) |
| 107 | + } |
| 108 | + } |
| 109 | + cancel() |
| 110 | + |
| 111 | + // Wait 5 seconds. |
| 112 | + // If the mocked avrdude is not killed it will create a checkfile and it will remove it after 5 seconds. |
| 113 | + time.Sleep(5 * time.Second) |
| 114 | + |
| 115 | + // Test if the checkfile is still there (if the file is there it means that mocked avrdude |
| 116 | + // has been correctly killed). |
| 117 | + require.NotEmpty(t, checkFile) |
| 118 | + require.FileExists(t, checkFile) |
| 119 | + require.NoError(t, os.Remove(checkFile)) |
| 120 | +} |
0 commit comments