diff --git a/commands/daemon/monitor_test.go b/commands/daemon/monitor_test.go deleted file mode 100644 index 185d774f574..00000000000 --- a/commands/daemon/monitor_test.go +++ /dev/null @@ -1,140 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -// These tests are mocked and won't work on OSX -// +build !darwin - -package daemon_test - -import ( - "context" - "io" - "reflect" - "testing" - - "bou.ke/monkey" - "github.com/arduino/arduino-cli/arduino/monitors" - "github.com/arduino/arduino-cli/commands/daemon" - "github.com/arduino/arduino-cli/rpc/monitor" - st "github.com/golang/protobuf/ptypes/struct" - "github.com/stretchr/testify/assert" - "google.golang.org/grpc/metadata" -) - -var ( - recvCounter int - resPortName string - resBaudRate int - resWrittenToSerial []byte - resReadFromSerial []byte -) - -type TestStreamingOpenServer struct{} - -func (s *TestStreamingOpenServer) Send(mon *monitor.StreamingOpenResp) error { - // if we're here, the Monitor read something from the target and - // is sending it back to the stream client - resReadFromSerial = mon.GetData() - return nil -} - -func (s *TestStreamingOpenServer) Recv() (*monitor.StreamingOpenReq, error) { - // if we're here, the monitor is reading the stream client - // we only send 3 messages, one for config, another with data and a final - // one with EOF so the monitor will gracefully exit - recvCounter++ - if recvCounter == 1 { - // send the first message containing the configuration - additionalFields := make(map[string]*st.Value, 1) - additionalFields["BaudRate"] = &st.Value{ - Kind: &st.Value_NumberValue{ - NumberValue: float64(42), - }, - } - return &monitor.StreamingOpenReq{ - Content: &monitor.StreamingOpenReq_MonitorConfig{ - MonitorConfig: &monitor.MonitorConfig{ - Target: "/dev/tty42", - Type: monitor.MonitorConfig_SERIAL, - AdditionalConfig: &st.Struct{ - Fields: additionalFields, - }, - }, - }, - }, nil - } else if recvCounter == 2 { - return &monitor.StreamingOpenReq{ - Content: &monitor.StreamingOpenReq_Data{ - Data: []byte("Hello Serial, this if for you!"), - }, - }, nil - } - - return nil, io.EOF -} - -func (s *TestStreamingOpenServer) SetHeader(metadata.MD) error { return nil } -func (s *TestStreamingOpenServer) SendHeader(metadata.MD) error { return nil } -func (s *TestStreamingOpenServer) SetTrailer(metadata.MD) {} -func (s *TestStreamingOpenServer) Context() context.Context { return context.Background() } -func (s *TestStreamingOpenServer) SendMsg(m interface{}) error { return nil } -func (s *TestStreamingOpenServer) RecvMsg(m interface{}) error { return nil } - -func mockOpenSerialMonitor(portName string, baudRate int) (*monitors.SerialMonitor, error) { - // this function will be called by the Monitor as soon as it receives the - // first message from the stream client - - // save parameters so the Test function can assert on the values passed to the monitor - // by the client - resPortName = portName - resBaudRate = baudRate - - mon := &monitors.SerialMonitor{} - monkey.PatchInstanceMethod(reflect.TypeOf(mon), "Close", func(_ *monitors.SerialMonitor) error { - return nil - }) - monkey.PatchInstanceMethod(reflect.TypeOf(mon), "Read", func(_ *monitors.SerialMonitor, bytes []byte) (int, error) { - copy(bytes, "I am Serial") - return len(bytes), nil - }) - monkey.PatchInstanceMethod(reflect.TypeOf(mon), "Write", func(_ *monitors.SerialMonitor, bytes []byte) (int, error) { - resWrittenToSerial = bytes - return len(bytes), nil - }) - - return mon, nil -} - -func TestConnect(t *testing.T) { - monkey.Patch(monitors.OpenSerialMonitor, mockOpenSerialMonitor) - - svc := daemon.MonitorService{} - stream := &TestStreamingOpenServer{} - - // let the monitor go, this will return when the monitor receives - // the EOF from the stream client - assert.Nil(t, svc.StreamingOpen(stream)) - - // ensure port setup was correct - assert.Equal(t, "/dev/tty42", resPortName) - assert.Equal(t, 42, resBaudRate) - - // ensure the serial received the message - assert.Equal(t, []byte("Hello Serial, this if for you!"), resWrittenToSerial) - - // ensure the monitor read from the serial, output is truncated because the test - // doesn't consume the whole buffer - assert.Equal(t, []byte("I am Ser"), resReadFromSerial) -} diff --git a/go.mod b/go.mod index 92a3cc2d8f4..a56fc9aface 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ go 1.14 replace github.com/GeertJohan/go.rice => github.com/cmaglie/go.rice v1.0.1 require ( - bou.ke/monkey v1.0.1 github.com/GeertJohan/go.rice v1.0.0 github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c github.com/arduino/go-paths-helper v1.3.1 diff --git a/go.sum b/go.sum index 46b9fa660b9..8973cb413b5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U= -bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -80,8 +78,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= @@ -313,8 +309,6 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90 h1:7THRSvPuzF1bql5kyFzX0JM0vpGhwuhskgJrJsbZ80Y= -google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=