Module: TrainPlugins::Juniper::MockResponses

Defined in:
lib/train-juniper/helpers/mock_responses.rb

Overview

Mock responses for common JunOS commands Used when running in mock mode for testing without real devices

Constant Summary collapse

RESPONSES =

Configuration of mock responses Maps command patterns to response methods or strings

{
  'show version' => :mock_show_version_output,
  'show chassis hardware' => :mock_chassis_output,
  'show configuration' => "interfaces {\n    ge-0/0/0 {\n        unit 0;\n    }\n}",
  'show route' => "inet.0: 5 destinations, 5 routes\n0.0.0.0/0       *[Static/5] 00:00:01\n",
  'show system information' => "Hardware: SRX240H2\nOS: JUNOS 12.1X47-D15.4\n",
  'show interfaces' => "Physical interface: ge-0/0/0, Enabled, Physical link is Up\n"
}.freeze

Class Method Summary collapse

Class Method Details

.mock_chassis_outputString

Mock chassis hardware output

Returns:

  • (String)

    mock output for 'show chassis hardware' command



34
35
36
37
38
39
40
# File 'lib/train-juniper/helpers/mock_responses.rb', line 34

def self.mock_chassis_output
  <<~OUTPUT
    Hardware inventory:
    Item             Version  Part number  Serial number     Description
    Chassis                                JN123456          SRX240H2
  OUTPUT
end

.mock_chassis_xml_outputString

Mock chassis hardware XML output

Returns:

  • (String)

    mock XML output for 'show chassis hardware | display xml' command



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/train-juniper/helpers/mock_responses.rb', line 44

def self.mock_chassis_xml_output
  <<~OUTPUT
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos">
      <chassis-inventory xmlns="http://xml.juniper.net/junos/12.1X47/junos-chassis">
        <chassis junos:style="inventory">
          <name>Chassis</name>
          <serial-number>JN123456</serial-number>
          <description>SRX240H2</description>
        </chassis>
      </chassis-inventory>
    </rpc-reply>
  OUTPUT
end

.mock_show_version_outputString

Mock JunOS version output for testing

Returns:

  • (String)

    mock output for 'show version' command



23
24
25
26
27
28
29
30
# File 'lib/train-juniper/helpers/mock_responses.rb', line 23

def self.mock_show_version_output
  <<~OUTPUT
    Hostname: lab-srx
    Model: SRX240H2
    Junos: 12.1X47-D15.4
    JUNOS Software Release [12.1X47-D15.4]
  OUTPUT
end

.mock_version_xml_outputString

Mock version XML output

Returns:

  • (String)

    mock XML output for 'show version | display xml' command



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/train-juniper/helpers/mock_responses.rb', line 60

def self.mock_version_xml_output
  <<~OUTPUT
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos">
      <software-information>
        <host-name>lab-srx</host-name>
        <product-model>SRX240H2</product-model>
        <product-name>srx240h2</product-name>
        <junos-version>12.1X47-D15.4</junos-version>
      </software-information>
    </rpc-reply>
  OUTPUT
end

.response_for(cmd) ⇒ Array<String, Integer>

Get mock response for a command

Parameters:

  • cmd (String)

    the command to get response for

Returns:

  • (Array<String, Integer>)

    tuple of [output, exit_status]



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/train-juniper/helpers/mock_responses.rb', line 76

def self.response_for(cmd)
  # Check if command includes display modifiers
  if cmd.include?('| display xml')
    # Handle XML output requests
    # Use simple string split to avoid ReDoS vulnerability
    base_cmd = cmd.split('|').first.strip

    case base_cmd
    when 'show chassis hardware'
      return [mock_chassis_xml_output, 0]
    when 'show version'
      return [mock_version_xml_output, 0]
    end
  end

  # Standard text response handling
  response = RESPONSES.find { |pattern, _| cmd.match?(/#{pattern}/) }

  if response
    output = response[1].is_a?(Symbol) ? send(response[1]) : response[1]
    [output, 0]
  else
    ["% Unknown command: #{cmd}", 1]
  end
end