def test_learn_hostname(self):
        states = {
            'exec': 'Linux',
            'exec2': 'Linux',
            'exec3': 'Linux',
            'exec4': 'host',
            'exec5': 'agent-lab9-pm',
            'exec6': 'agent-lab11-pm',
            'exec7': 'localhost',
            'exec8': 'vm-7',
            'exec9': 'dev-server',
            'exec10': 'dev-1-name',
            'exec11': 'new-host',
            'exec12': 'host',
            'exec13': 'host',
            'exec14': 'rally',
            'exec15': LinuxSettings().DEFAULT_LEARNED_HOSTNAME,
            'sma_prompt': 'sma03',
            'sma_prompt_1': 'pod-esa01',
            'exec18': LinuxSettings().DEFAULT_LEARNED_HOSTNAME,
        }

        for state in states:
            print('\n\n## Testing state %s ##' % state)
            testbed = """
            devices:
              lnx:
                os: linux
                type: linux
                tacacs:
                    username: admin
                passwords:
                    linux: admin
                connections:
                  defaults:
                    class: unicon.Unicon
                  cli:
                    command: mock_device_cli --os linux --state {state}
            """.format(state=state)
            tb = loader.load(testbed)
            c = tb.devices.lnx
            c.connect(learn_hostname=True)
            self.assertEqual(c.learned_hostname, states[state])

            # only check for supported prompts
            if states[state] != LinuxSettings().DEFAULT_LEARNED_HOSTNAME:
                x = c.execute('xml')
                self.assertEqual(
                    x.replace('\r', ''),
                    mock_data['exec']['commands']['xml']['response'].strip())
                x = c.execute('banner1')
                self.assertEqual(
                    x.replace('\r', ''), mock_data['exec']['commands']
                    ['banner1']['response'].strip())
                x = c.execute('banner2')
                self.assertEqual(
                    x.replace('\r', ''), mock_data['exec']['commands']
                    ['banner2']['response'].strip())
 def test_override_prompt(self):
     settings = LinuxSettings()
     prompt = 'prompt'
     settings.PROMPT = prompt
     c = Connection(hostname='linux',
                    start=['mock_device_cli --os linux --state exec'],
                    os='linux',
                    settings=settings)
     assert c.state_machine.states[0].pattern == prompt
 def test_override_shell_prompt(self):
     settings = LinuxSettings()
     prompt = 'shell_prompt'
     settings.SHELL_PROMPT = prompt
     c = Connection(hostname='linux',
                    start=['mock_device_cli --os linux --state exec'],
                    os='linux',
                    settings=settings,
                    learn_hostname=True)
     c.connect()
     assert c.state_machine.states[0].pattern == prompt
    def test_os_TERM(self):
        testbed = """
      devices:
        lnx:
          os: linux
          type: linux
          connections:
            defaults:
              class: unicon.Unicon
            vty:
              command: bash
      """

        tb = loader.load(testbed)
        l = tb.devices.lnx
        s = LinuxSettings()
        delattr(s, 'TERM')
        delattr(s, 'ENV')
        l.connect(settings=s)
        l.execute('PS1=bash#')
        # forcing the prompt pattern without $
        # echo $TERM is matched as a prompt pattern depending on timing
        l.state_machine.get_state(
            'shell').pattern = r'^(.*?([>~%]|[^#\s]#))\s?$'
        term = l.execute('echo $TERM')
        self.assertEqual(term, os.environ.get('TERM', 'dumb'))
예제 #5
0
class LinuxConnection(BaseLinuxConnection):
    """
    Connection class for Linux connections.
    """
    os = 'linux'
    platform = None
    chassis_type = 'single_rp'
    # TODO Recheck this single_rp value for linux
    state_machine_class = LinuxStateMachine
    connection_provider_class = LinuxConnectionProvider
    subcommand_list = LinuxServiceList
    settings = LinuxSettings()