Esempio n. 1
0
    def setUp(self):
        self.semaphore_mocks = {}
        self.factory = SwitchFactory(switch_source=None,
                                     lock_factory=MockLockFactory(
                                         self.semaphore_mocks))

        switch_factory.factories['test_model'] = _FakeSwitch
Esempio n. 2
0
    def test_get_info(self):
        get_distribution_mock = Mock()
        get_distribution_mock.return_value = Distribution(
            version="1.1.111.dev111111111")

        NetmanApi(SwitchFactory(None, ThreadingLockFactory()),
                  get_distribution_callback=get_distribution_mock).hook_to(
                      self.app)

        data, code = self.get("/netman/info")

        assert_that(data, matches_fixture("get_info.json"))
    def setUp(self):
        self.semaphore_mocks = {}
        self.factory = SwitchFactory(switch_source=None, lock_factory=MockLockFactory(self.semaphore_mocks))

        switch_factory.factories['test_model'] = _FakeSwitch
class SwitchFactoryTest(unittest.TestCase):

    def setUp(self):
        self.semaphore_mocks = {}
        self.factory = SwitchFactory(switch_source=None, lock_factory=MockLockFactory(self.semaphore_mocks))

        switch_factory.factories['test_model'] = _FakeSwitch

    def tearDown(self):
        switch_factory.factories.pop('test_model')

    def test_get_connection_to_anonymous_switch(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore

        switch = self.factory.get_anonymous_switch(hostname='hostname', model='test_model', username='******',
                                                   password='******', port=22)

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(_FakeSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(switch.wrapped_switch.switch_descriptor, is_(
                SwitchDescriptor(hostname='hostname', model='test_model', username='******',
                                 password='******', port=22)))

    def test_two_get_connections_on_the_same_switch_should_give_the_same_semaphore(self):
        self.semaphore_mocks['hostname'] = mock.Mock()

        switch1 = self.factory.get_anonymous_switch(hostname='hostname', model='test_model')
        switch2 = self.factory.get_anonymous_switch(hostname='hostname', model='test_model')

        assert_that(switch1.lock, is_(switch2.lock))

    def test_two_get_connections_on_different_switches_should_give_different_semaphores(self):
        self.semaphore_mocks['hostname1'] = mock.Mock()
        self.semaphore_mocks['hostname2'] = mock.Mock()

        switch1 = self.factory.get_anonymous_switch(hostname='hostname1', model='test_model')
        switch2 = self.factory.get_anonymous_switch(hostname='hostname2', model='test_model')

        assert_that(switch1.lock, is_not(switch2.lock))

    def test_get_connection_to_anonymous_remote_switch(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore
        switch = self.factory.get_anonymous_switch(hostname='hostname', model='test_model', username='******',
                                                   password='******', port=22,
                                                   netman_server='https://netman.url.example.org:4443')

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(RemoteSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(switch.wrapped_switch.switch_descriptor, is_(
                SwitchDescriptor(hostname='hostname', model='test_model', username='******',
                                 password='******', port=22,
                                 netman_server='https://netman.url.example.org:4443')))

    def test_get_switch_by_descriptor(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore

        switch = self.factory.get_switch_by_descriptor(SwitchDescriptor(model='test_model', hostname='hostname'))

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(_FakeSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(switch.wrapped_switch.switch_descriptor,
                    is_(SwitchDescriptor(model='test_model', hostname='hostname')))
Esempio n. 5
0
 def setUp(self):
     self.semaphore_mocks = {}
     self.factory = SwitchFactory(switch_source=None, lock_factory=MockLockFactory(self.semaphore_mocks))
Esempio n. 6
0
class SwitchFactoryTest(unittest.TestCase):

    def setUp(self):
        self.semaphore_mocks = {}
        self.factory = SwitchFactory(switch_source=None, lock_factory=MockLockFactory(self.semaphore_mocks))

    def test_get_connection_to_anonymous_switch(self):
        class FakeCiscoSwitch(SwitchBase):
            pass

        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore
        expected_switch = FakeCiscoSwitch(SwitchDescriptor(model='some_model', hostname='hello'))
        factory_mock = mock.Mock()
        factory_mock.return_value = expected_switch
        self.factory.factories['some_model'] = factory_mock

        switch = self.factory.get_anonymous_switch(hostname='hostname', model='some_model', username='******',
                                                   password='******', port=22)

        factory_mock.assert_called_with(SwitchDescriptor(hostname='hostname', model='some_model', username='******',
                                                         password='******', port=22), lock=my_semaphore)
        assert_that(switch, is_(expected_switch))

    def test_factories_are_well_wired(self):

        assert_that(self.factory.factories, is_({
            "cisco": cisco.factory,
            "brocade": brocade.factory,
            "juniper": juniper.standard_factory,
            "juniper_qfx_copper": juniper.qfx_copper_factory,
            "dell": dell.factory_ssh,
            "dell_ssh": dell.factory_ssh,
            "dell_telnet": dell.factory_telnet
        }))

    def test_two_get_connections_on_the_same_switch_should_give_the_same_semaphore(self):
        semaphores = []

        self.semaphore_mocks['hostname'] = mock.Mock()

        self.factory.factories['juniper'] = lambda switch_descriptor, lock: semaphores.append(lock)

        self.factory.get_anonymous_switch(hostname='hostname', model='juniper', username='******', password='******', port=22)
        self.factory.get_anonymous_switch(hostname='hostname', model='juniper', username='******', password='******', port=22)

        assert_that(semaphores[0], is_(semaphores[1]))

    def test_two_get_connections_on_different_switches_should_give_different_semaphores(self):
        semaphores = []

        self.semaphore_mocks['hostname1'] = mock.Mock()
        self.semaphore_mocks['hostname2'] = mock.Mock()

        self.factory.factories['juniper'] = lambda switch_descriptor, lock: semaphores.append(lock)

        self.factory.get_anonymous_switch(hostname='hostname1', model='juniper', username='******', password='******', port=22)
        self.factory.get_anonymous_switch(hostname='hostname2', model='juniper', username='******', password='******', port=22)

        assert_that(semaphores[0], is_not(semaphores[1]))

    def test_get_connection_to_anonymous_remote_switch(self):
        switch = self.factory.get_anonymous_switch(hostname='hostname', model='juniper', username='******',
                                                   password='******', port=22,
                                                   netman_server='https://netman.url.example.org:4443')

        assert_that(switch, instance_of(RemoteSwitch))
        assert_that(switch.switch_descriptor.hostname, equal_to("hostname"))
        assert_that(switch.switch_descriptor.model, equal_to("juniper"))
        assert_that(switch.switch_descriptor.username, equal_to("username"))
        assert_that(switch.switch_descriptor.password, equal_to("password"))
        assert_that(switch.switch_descriptor.port, equal_to(22))
        assert_that(switch.switch_descriptor.netman_server, equal_to('https://netman.url.example.org:4443'))

    def test_get_switch_by_descriptor(self):
        class FakeJuniperSwitch(SwitchBase):
            pass
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hello'] = my_semaphore

        expected_switch = FakeJuniperSwitch(SwitchDescriptor(model='juniper', hostname='hello'))
        factory_mock = mock.Mock()
        factory_mock.return_value = expected_switch
        self.factory.factories['juniper'] = factory_mock

        descriptor = SwitchDescriptor(model='juniper', hostname='hello')
        switch = self.factory.get_switch_by_descriptor(descriptor)

        factory_mock.assert_called_with(descriptor, lock=my_semaphore)
        assert_that(switch, is_(expected_switch))
Esempio n. 7
0
class SwitchFactoryTest(unittest.TestCase):
    def setUp(self):
        self.semaphore_mocks = {}
        self.factory = SwitchFactory(switch_source=None,
                                     lock_factory=MockLockFactory(
                                         self.semaphore_mocks))

        switch_factory.factories['test_model'] = _FakeSwitch

    def tearDown(self):
        switch_factory.factories.pop('test_model')

    def test_get_connection_to_anonymous_switch(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore

        switch = self.factory.get_anonymous_switch(hostname='hostname',
                                                   model='test_model',
                                                   username='******',
                                                   password='******',
                                                   port=22)

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(_FakeSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(
            switch.wrapped_switch.switch_descriptor,
            is_(
                SwitchDescriptor(hostname='hostname',
                                 model='test_model',
                                 username='******',
                                 password='******',
                                 port=22)))

    def test_two_get_connections_on_the_same_switch_should_give_the_same_semaphore(
            self):
        self.semaphore_mocks['hostname'] = mock.Mock()

        switch1 = self.factory.get_anonymous_switch(hostname='hostname',
                                                    model='test_model')
        switch2 = self.factory.get_anonymous_switch(hostname='hostname',
                                                    model='test_model')

        assert_that(switch1.lock, is_(switch2.lock))

    def test_two_get_connections_on_different_switches_should_give_different_semaphores(
            self):
        self.semaphore_mocks['hostname1'] = mock.Mock()
        self.semaphore_mocks['hostname2'] = mock.Mock()

        switch1 = self.factory.get_anonymous_switch(hostname='hostname1',
                                                    model='test_model')
        switch2 = self.factory.get_anonymous_switch(hostname='hostname2',
                                                    model='test_model')

        assert_that(switch1.lock, is_not(switch2.lock))

    def test_get_connection_to_anonymous_remote_switch(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore
        switch = self.factory.get_anonymous_switch(
            hostname='hostname',
            model='test_model',
            username='******',
            password='******',
            port=22,
            netman_server='https://netman.url.example.org:4443')

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(RemoteSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(
            switch.wrapped_switch.switch_descriptor,
            is_(
                SwitchDescriptor(
                    hostname='hostname',
                    model='test_model',
                    username='******',
                    password='******',
                    port=22,
                    netman_server='https://netman.url.example.org:4443')))

    def test_get_switch_by_descriptor(self):
        my_semaphore = mock.Mock()
        self.semaphore_mocks['hostname'] = my_semaphore

        switch = self.factory.get_switch_by_descriptor(
            SwitchDescriptor(model='test_model', hostname='hostname'))

        assert_that(switch, is_(instance_of(FlowControlSwitch)))
        assert_that(switch.wrapped_switch, is_(instance_of(_FakeSwitch)))
        assert_that(switch.lock, is_(my_semaphore))
        assert_that(
            switch.wrapped_switch.switch_descriptor,
            is_(SwitchDescriptor(model='test_model', hostname='hostname')))