Skip to content

Commit

Permalink
Fix device2yaml.rb
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
robertcheramy committed Nov 16, 2024
1 parent 04eafd1 commit f6cf93d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
14 changes: 10 additions & 4 deletions examples/device-simulation/device2yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
79 changes: 79 additions & 0 deletions examples/device-simulation/yaml/routeros_CHR_7.16.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion spec/model/routeros_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@
_(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

status, result = @node.run

_(status).must_equal :success
# result2file(result, 'model-output.txt')
_(result.to_cfg).must_equal mockmodel.oxidized_output
end
end

0 comments on commit f6cf93d

Please sign in to comment.