From 87be6bf9d512dc8fca0a31d24bb4aef9d5374efc Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 23 Jun 2018 15:58:43 +0300 Subject: [PATCH] add automated arm tests --- Gemfile | 1 + Gemfile.lock | 3 +- test/elf/helper.rb | 72 ++++++++++++++++++++++++++++++++++++++++++ test/elf/test_add.rb | 12 +++++++ test/elf/test_hello.rb | 24 ++++++-------- 5 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 test/elf/helper.rb create mode 100644 test/elf/test_add.rb diff --git a/Gemfile b/Gemfile index 17578d94..6d140157 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ group :test do gem "simplecov" gem "minitest-color" gem 'minitest-fail-fast' + gem "net-ssh" end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 4d3ae15e..53454d18 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,7 +54,7 @@ GEM nenv (0.3.0) net-scp (1.2.1) net-ssh (>= 2.6.5) - net-ssh (4.2.0) + net-ssh (5.0.2) notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) @@ -98,6 +98,7 @@ DEPENDENCIES guard-minitest minitest-color minitest-fail-fast + net-ssh rake rb-readline rubyx! diff --git a/test/elf/helper.rb b/test/elf/helper.rb new file mode 100644 index 00000000..510af467 --- /dev/null +++ b/test/elf/helper.rb @@ -0,0 +1,72 @@ +require_relative "../helper" +require 'net/ssh' +require 'net/scp' + +module Elf + + class FullTest < MiniTest::Test + DEBUG = false + + def setup + Risc.machine.boot + end + + def check(file) + Vool::VoolCompiler.ruby_to_binary( "class Space;def main(arg);#{@input};end;end" ) + writer = Elf::ObjectWriter.new(Risc.machine) + writer.save "test/#{file}.o" + stdout , exit_code = run_ssh(file) + @stdout = "" unless @stdout + assert_equal @stdout , stdout + assert_equal @exit_code , exit_code + end + + def run_ssh( file ) + host = ENV["ARM_HOST"] + return unless host + port = (ENV["ARM_PORT"] || 2222) + user = (ENV["ARM_USER"] || "pi") + binary_file = "/tmp/#{file}" + object_file = binary_file + ".o" + Net::SCP.start(host, user , port: port ) do |scp| + puts "Copying test/#{file}.o to #{object_file}" if DEBUG + scp.upload! "test/#{file}.o", object_file + end + Net::SSH.start(host, user , port: port ) do |ssh| + puts "Linking #{object_file}" if DEBUG + stdout , exit_code = ssh_exec!(ssh , "ld -N -o #{binary_file} #{object_file}") + assert_equal 0 , exit_code , "Linking #{binary_file} failed" + puts "Running #{binary_file}" if DEBUG + stdout , exit_code = ssh_exec!(ssh , binary_file) + puts "Result #{stdout} #{exit_code}" if DEBUG + return stdout , exit_code + end + end + + def ssh_exec!(ssh, command) + stdout_data = "" + exit_code = nil + ssh.open_channel do |channel| + channel.exec(command) do |ch, success| + unless success + raise "FAILED: couldn't execute command (ssh.channel.exec)" + end + channel.on_data do |c,data| + stdout_data+=data + end + channel.on_extended_data do |c,type,data| + raise "#{ssh} received stderr #{data}" + end + channel.on_request("exit-status") do |c,data| + exit_code = data.read_long + end + channel.on_request("exit-signal") do |c, data| + raise "#{ssh} received signal #{data.read_long}" + end + end + end + ssh.loop + [stdout_data, exit_code] + end + end +end diff --git a/test/elf/test_add.rb b/test/elf/test_add.rb new file mode 100644 index 00000000..2b953d4e --- /dev/null +++ b/test/elf/test_add.rb @@ -0,0 +1,12 @@ +require_relative "helper" + +module Elf + class AddTest < FullTest + + def test_add + @input = "return 2 + 2" + @exit_code = 4 + check "add" + end + end +end diff --git a/test/elf/test_hello.rb b/test/elf/test_hello.rb index 3074f3cf..9a91d143 100644 --- a/test/elf/test_hello.rb +++ b/test/elf/test_hello.rb @@ -1,18 +1,14 @@ -require_relative "../helper" +require_relative "helper" -class HelloTest < MiniTest::Test +module Elf + class HelloTest < FullTest - def setup - Risc.machine.boot - end - def check - Vool::VoolCompiler.ruby_to_binary( "class Space;def main(arg);#{@input};end;end" ) - writer = Elf::ObjectWriter.new(Risc.machine) - writer.save "test/hello.o" - end - - def test_string_put - @input = "return 'Hello World!\n'.putstring" - check + def test_string_put + hello = "Hello World!\n" + @input = "return '#{hello}'.putstring" + @stdout = hello + @exit_code = hello.length + check "hello" + end end end