class TestIosXrSpitfireHAConnect(unittest.TestCase):
    def setUp(self):
        self.md = MockDeviceTcpWrapperSpitfire(
            port=0, state='spitfire_login,spitfire_console_standby')
        self.md.start()

        self.testbed = """
        devices:
          Router:
            os: iosxr
            series: spitfire
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco123
                enable: cisco123
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
              b:
                protocol: telnet
                ip: 127.0.0.1
                port: {}

        """.format(self.md.ports[0], self.md.ports[1])
        tb = loader.load(self.testbed)
        self.r = tb.devices.Router
        self.r.connect(prompt_recovery=True)

    def test_connect(self):
        self.assertEqual(self.r.active.spawn.match.match_output,
                         'end\r\nRP/0/RP0/CPU0:Router#')

    def test_handle(self):
        self.assertEqual(
            self.r.a.role,
            "active",
        )
        self.assertEqual(self.r.b.role, "standby")

    def test_switchover(self):
        self.r.switchover(sync_standby=False)

    def test_switchover_with_sync(self):
        self.r.switchover(sync_standby=True)

    def tearDown(self):
        self.r.disconnect()
        self.md.stop()
class TestIosXrSpitfirePluginDevice(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.md = MockDeviceTcpWrapperSpitfire(port=0, state='spitfire_login')
        self.md.start()

        self.testbed = """
        devices:
          Router:
            os: iosxr
            series: spitfire
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco123
                enable: cisco123
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
        """.format(self.md.ports[0])

    def test_connect(self):
        tb = loader.load(self.testbed)
        self.r = tb.devices.Router
        self.r.connect()
        self.assertEqual(self.r.spawn.match.match_output,
                         'end\r\nRP/0/RP0/CPU0:Router#')
        self.r.disconnect()

    @classmethod
    def tearDownClass(self):
        self.md.stop()
class TestIosXrSpitfirePluginConnectConfigLock(unittest.TestCase):
    def test_configlocktimeout(self):
        self.md = MockDeviceTcpWrapperSpitfire(port=0, state='spitfire_login')
        self.md.start()

        self.testbed = """
        devices:
          Router:
            os: iosxr
            series: spitfire
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco123
                enable: cisco123
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
        """.format(self.md.ports[0])
        tb = loader.load(self.testbed)
        self.r = tb.devices.Router

        try:
            self.r.connect(prompt_recovery=True)
        except:
            connect_fail = True

        self.assertTrue(connect_fail, "Connection failed ")

    def test_configindefinitelock(self):
        self.md = MockDeviceTcpWrapperSpitfire(
            port=0, state='spitfire_enable_config_lock')
        self.md.start()

        self.testbed = """
        devices:
          Router:
            os: iosxr
            series: spitfire
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco123
                enable: cisco123
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
        """.format(self.md.ports[0])
        tb = loader.load(self.testbed)
        self.r = tb.devices.Router

        try:
            self.r.connect(prompt_recovery=True)
        except:
            connect_fail = True

        self.assertTrue(connect_fail, "Connection failed ")

    def test_configztplock(self):
        self.md = MockDeviceTcpWrapperSpitfire(
            port=0, state='spitfire_enable_ztp_lock')
        self.md.start()

        self.testbed = """
        devices:
          Router:
            os: iosxr
            series: spitfire
            type: router
            tacacs:
                username: cisco
            passwords:
                tacacs: cisco123
                enable: cisco123
            connections:
              defaults:
                class: unicon.Unicon
              a:
                protocol: telnet
                ip: 127.0.0.1
                port: {}
        """.format(self.md.ports[0])
        tb = loader.load(self.testbed)
        self.r = tb.devices.Router

        try:
            self.r.connect(prompt_recovery=True)
        except:
            connect_fail = True

        self.assertTrue(connect_fail, "Connection failed ")

    def tearDown(self):
        self.r.disconnect()
        self.md.stop()