From f6cf93d694904f54276e44bd3e1b585d408a43e6 Mon Sep 17 00:00:00 2001 From: Robert Cheramy Date: Sat, 16 Nov 2024 07:40:01 +0100 Subject: [PATCH] Fix device2yaml.rb - only change the first space on the first line with \x20 is enougth for YAML and makes the YAML-file better readable - There is no Exception NoSell in device2yaml.rb => just raise - Tested the results with routeros, adding a new YAML simulation file --- examples/device-simulation/device2yaml.rb | 14 +++- .../yaml/routeros_CHR_7.16.yaml | 79 +++++++++++++++++++ spec/model/routeros_spec.rb | 11 ++- 3 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 examples/device-simulation/yaml/routeros_CHR_7.16.yaml diff --git a/examples/device-simulation/device2yaml.rb b/examples/device-simulation/device2yaml.rb index 05a6d6fa4..2d0f2ea9d 100755 --- a/examples/device-simulation/device2yaml.rb +++ b/examples/device-simulation/device2yaml.rb @@ -82,13 +82,19 @@ def shell_wait def yaml_output(prepend = '') # Now print the collected output to @output + firstline = true + # as we want to prepend 'prepend' to each line, we need each_line and chomp # chomp removes the trainling \n @ssh_output.each_line(chomp: true) do |line| # encode line and remove the first and the trailing double quote line = line.dump[1..-2] - # Make sure leading spaces are coded with \0x20 or YAML won't work - line.gsub!(/^ /, '\x20') + if firstline + # Make sure the leading space of the first line (if present) + # is coded with \0x20 or YAML block scalars won't work + line.sub!(/^\A /, '\x20') + firstline = false + end # Make sure trailing white spaces are coded with \0x20 line.gsub!(/ $/, '\x20') # prepend white spaces for the yaml block scalar @@ -178,10 +184,10 @@ def cleanup print data.gsub("\e", '\e') end ch.request_pty(term: 'vt100') do |_ch, success_pty| - raise NoShell, "Can't get PTY" unless success_pty + raise "Can't get PTY" unless success_pty ch.send_channel_request 'shell' do |_ch, success_shell| - raise NoShell, "Can't get shell" unless success_shell + raise "Can't get shell" unless success_shell end end ch.on_extended_data do |_ch, _type, data| diff --git a/examples/device-simulation/yaml/routeros_CHR_7.16.yaml b/examples/device-simulation/yaml/routeros_CHR_7.16.yaml new file mode 100644 index 000000000..c76551caa --- /dev/null +++ b/examples/device-simulation/yaml/routeros_CHR_7.16.yaml @@ -0,0 +1,79 @@ +--- +init_prompt: +commands: + /system resource print: |- + \x20 uptime: 32m36s + version: 7.16 (stable) + build-time: 2024-09-20 13:00:27 + factory-software: 7.1 + free-memory: 165.6MiB + total-memory: 384.0MiB + cpu: QEMU + cpu-count: 1 + cpu-frequency: 2999MHz + cpu-load: 2% + free-hdd-space: 71.2MiB + total-hdd-space: 89.2MiB + write-sect-since-reboot: 584 + write-sect-total: 584 + architecture-name: x86_64 + board-name: CHR QEMU Standard PC (i440FX + PIIX, 1996) + platform: MikroTik + + /system package update print: |- + \x20 channel: stable + installed-version: 7.16 + + /system history print without-paging: |- + + /export show-sensitive: |- + # 2024-11-16 06:25:32 by RouterOS 7.16 + # software id =\x20 + # + /interface ethernet + set [ find default-name=ether1 ] disable-running-check=no + set [ find default-name=ether2 ] disable-running-check=no + set [ find default-name=ether3 ] disable-running-check=no + set [ find default-name=ether4 ] disable-running-check=no + set [ find default-name=ether5 ] disable-running-check=no + set [ find default-name=ether6 ] disable-running-check=no + set [ find default-name=ether7 ] disable-running-check=no + set [ find default-name=ether8 ] disable-running-check=no + /port + set 0 name=serial0 + /ip address + add address=10.0.2.100/24 interface=ether1 network=10.0.2.0 + /ip dhcp-client + add interface=ether1 + /system note + set show-at-login=no + quit: |- + interrupted +oxidized_output: | + # version: 7.16 (stable) + # factory-software: 7.1 + # total-memory: 384.0MiB + # cpu: QEMU + # cpu-count: 1 + # total-hdd-space: 89.2MiB + # architecture-name: x86_64 + # board-name: CHR QEMU Standard PC (i440FX + PIIX, 1996) + # platform: MikroTik# installed-version: 7.16# software id =\x20 + # + /interface ethernet + set [ find default-name=ether1 ] disable-running-check=no + set [ find default-name=ether2 ] disable-running-check=no + set [ find default-name=ether3 ] disable-running-check=no + set [ find default-name=ether4 ] disable-running-check=no + set [ find default-name=ether5 ] disable-running-check=no + set [ find default-name=ether6 ] disable-running-check=no + set [ find default-name=ether7 ] disable-running-check=no + set [ find default-name=ether8 ] disable-running-check=no + /port + set 0 name=serial0 + /ip address + add address=10.0.2.100/24 interface=ether1 network=10.0.2.0 + /ip dhcp-client + add interface=ether1 + /system note + set show-at-login=no diff --git a/spec/model/routeros_spec.rb b/spec/model/routeros_spec.rb index 42decbf9c..345a7ea9b 100644 --- a/spec/model/routeros_spec.rb +++ b/spec/model/routeros_spec.rb @@ -22,6 +22,16 @@ _(result.to_cfg).must_equal mockmodel.oxidized_output end + it 'runs on CHR with 7.16' do + mockmodel = MockSsh.new('examples/device-simulation/yaml/routeros_CHR_7.16.yaml') + Net::SSH.stubs(:start).returns mockmodel + + status, result = @node.run + + _(status).must_equal :success + _(result.to_cfg).must_equal mockmodel.oxidized_output + end + it 'runs on L009UiGS with 7.15.2' do mockmodel = MockSsh.new('examples/device-simulation/yaml/routeros_L009UiGS_7.15.2.yaml') Net::SSH.stubs(:start).returns mockmodel @@ -29,7 +39,6 @@ status, result = @node.run _(status).must_equal :success - # result2file(result, 'model-output.txt') _(result.to_cfg).must_equal mockmodel.oxidized_output end end