Skip to content

Commit 7e255e9

Browse files
author
Roberto Sora
authored
Check json logs entries in compile and upload python integration tests (#393)
* add whitebox testing to combo compile upload command integ test * add log lines to compile and upload in case of success and related tests * check log traces for exact sequences * add virtualenv folder linting exclude and licence header cosmetics
1 parent 84172d8 commit 7e255e9

File tree

10 files changed

+96
-40
lines changed

10 files changed

+96
-40
lines changed

commands/compile/compile.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
4747
return nil, errors.New("invalid instance")
4848
}
4949

50-
logrus.Info("Executing `arduino compile`")
50+
logrus.Tracef("Compile %s for %s started", req.GetSketchPath(), req.GetFqbn())
5151
if req.GetSketchPath() == "" {
5252
return nil, fmt.Errorf("missing sketchPath")
5353
}
@@ -202,18 +202,20 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
202202
// Copy .hex file to sketch directory
203203
srcHex := paths.New(outputPath)
204204
dstHex := exportPath.Join(exportFile + ext)
205-
logrus.WithField("from", srcHex).WithField("to", dstHex).Print("copying sketch build output")
205+
logrus.WithField("from", srcHex).WithField("to", dstHex).Debug("copying sketch build output")
206206
if err = srcHex.CopyTo(dstHex); err != nil {
207207
return nil, fmt.Errorf("copying output file: %s", err)
208208
}
209209

210210
// Copy .elf file to sketch directory
211211
srcElf := paths.New(outputPath[:len(outputPath)-3] + "elf")
212212
dstElf := exportPath.Join(exportFile + ".elf")
213-
logrus.WithField("from", srcElf).WithField("to", dstElf).Print("copying sketch build output")
213+
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
214214
if err = srcElf.CopyTo(dstElf); err != nil {
215215
return nil, fmt.Errorf("copying elf file: %s", err)
216216
}
217217

218+
logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
219+
218220
return &rpc.CompileResp{}, nil
219221
}

commands/upload/upload.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
// Upload FIXMEDOC
4242
func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStream io.Writer) (*rpc.UploadResp, error) {
43-
logrus.Info("Executing `arduino upload`")
43+
logrus.Tracef("Upload %s on %s started", req.GetSketchPath(), req.GetFqbn())
4444

4545
// TODO: make a generic function to extract sketch from request
4646
// and remove duplication in commands/compile.go
@@ -248,6 +248,9 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
248248
if err := cmd.Wait(); err != nil {
249249
return nil, fmt.Errorf("uploading error: %s", err)
250250
}
251+
252+
logrus.Tracef("Upload %s on %s successful", sketch.Name, fqbnIn)
253+
251254
return &rpc.UploadResp{}, nil
252255
}
253256

test/.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[flake8]
2-
max-line-length = 120
2+
exclude = venv
3+
max-line-length = 120

test/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2019 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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].

test/common.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino

test/conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino

test/test_board.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino

test/test_compile.py

+56-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
15-
import pytest
1615
import json
1716
import os
17+
import pytest
1818

1919
from .common import running_on_ci
2020

@@ -42,33 +42,47 @@ def test_compile_with_simple_sketch(run_command, data_dir):
4242
result = run_command("core install arduino:avr")
4343
assert result.ok
4444

45-
sketch_path = os.path.join(data_dir, "CompileIntegrationTest")
45+
sketch_name = "CompileIntegrationTest"
46+
sketch_path = os.path.join(data_dir, sketch_name)
47+
fqbn = "arduino:avr:uno"
4648

4749
# Create a test sketch
48-
result = run_command("sketch new CompileIntegrationTest")
50+
result = run_command("sketch new {}".format(sketch_name))
4951
assert result.ok
5052
assert "Sketch created in: {}".format(sketch_path) in result.stdout
5153

5254
# Build sketch for arduino:avr:uno
53-
result = run_command("compile -b arduino:avr:uno {}".format(sketch_path))
55+
log_file_name = "compile.log"
56+
log_file_path = os.path.join(data_dir, log_file_name)
57+
result = run_command(
58+
"compile -b {fqbn} {sketch_path} --log-format json --log-file {log_file} --log-level trace".format(
59+
fqbn=fqbn, sketch_path=sketch_path, log_file=log_file_path))
5460
assert result.ok
55-
assert "Sketch uses" in result.stdout
61+
62+
# let's test from the logs if the hex file produced by successful compile is moved to our sketch folder
63+
log_json = open(log_file_path, 'r')
64+
json_log_lines = log_json.readlines()
65+
expected_trace_sequence = [
66+
"Compile {sketch} for {fqbn} started".format(sketch=sketch_path, fqbn=fqbn),
67+
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name, fqbn=fqbn)
68+
]
69+
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)
5670

5771

5872
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
5973
def test_compile_and_compile_combo(run_command, data_dir):
60-
6174
# Init the environment explicitly
6275
result = run_command("core update-index")
6376
assert result.ok
6477

6578
# Install required core(s)
6679
result = run_command("core install arduino:avr")
67-
# result = run_command("core install arduino:samd")
80+
result = run_command("core install arduino:samd")
6881
assert result.ok
6982

7083
# Create a test sketch
71-
sketch_path = os.path.join(data_dir, "CompileAndUploadIntegrationTest")
84+
sketch_name = "CompileAndUploadIntegrationTest"
85+
sketch_path = os.path.join(data_dir, sketch_name)
7286
result = run_command("sketch new CompileAndUploadIntegrationTest")
7387
assert result.ok
7488
assert "Sketch created in: {}".format(sketch_path) in result.stdout
@@ -96,7 +110,7 @@ def test_compile_and_compile_combo(run_command, data_dir):
96110
# }
97111
# ]
98112

99-
detected_boards = []
113+
detected_boards = []
100114

101115
ports = json.loads(result.stdout)
102116
assert isinstance(ports, list)
@@ -109,12 +123,34 @@ def test_compile_and_compile_combo(run_command, data_dir):
109123
assert len(detected_boards) >= 1, "There are no boards available for testing"
110124

111125
# Build sketch for each detected board
112-
for board in detected_boards:
113-
result = run_command(
114-
"compile -b {fqbn} --upload -p {address} {sketch_path}".format(
115-
fqbn=board.get('fqbn'),
116-
address=board.get('address'),
117-
sketch_path=sketch_path)
118-
)
126+
for board in detected_boards:
127+
log_file_name = "{fqbn}-compile.log".format(fqbn=board.get('fqbn'))
128+
log_file_path = os.path.join(data_dir, log_file_name)
129+
command_log_flags = "--log-format json --log-file {} --log-level trace".format(log_file_path)
130+
result = run_command("compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format(
131+
fqbn=board.get('fqbn'),
132+
address=board.get('address'),
133+
sketch_path=sketch_path,
134+
log_flags=command_log_flags
135+
))
119136
assert result.ok
120-
assert "Verify successful" in result.stdout
137+
# check from the logs if the bin file were uploaded on the current board
138+
log_json = open(log_file_path, 'r')
139+
json_log_lines = log_json.readlines()
140+
expected_trace_sequence = [
141+
"Compile {sketch} for {fqbn} started".format(sketch=sketch_path, fqbn=board.get('fqbn')),
142+
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name, fqbn=board.get('fqbn')),
143+
"Upload {sketch} on {fqbn} started".format(sketch=sketch_path, fqbn=board.get('fqbn')),
144+
"Upload {sketch} on {fqbn} successful".format(sketch=sketch_name, fqbn=board.get('fqbn'))
145+
]
146+
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)
147+
148+
149+
def is_message_sequence_in_json_log_traces(message_sequence, log_json_lines):
150+
trace_entries = []
151+
for entry in log_json_lines:
152+
entry = json.loads(entry)
153+
if entry.get("level") == "trace":
154+
if entry.get("msg") in message_sequence:
155+
trace_entries.append(entry.get("msg"))
156+
return message_sequence == trace_entries

test/test_lib.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino

test/test_main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino

0 commit comments

Comments
 (0)