class TestUpdateVpcPeering(fakes.TestVpc):

    _data = fakes.FakeVpcPeering.create_one()

    columns = (
        'id',
        'name',
        'local_vpc_info',
        'peer_vpc_info',
        'description',
        'created_at',
        'updated_at',
        'status'
    )

    data = fakes.gen_data(_data, columns)

    def setUp(self):
        super(TestUpdateVpcPeering, self).setUp()

        self.cmd = peering.UpdateVpcPeering(self.app, None)

        self.client.find_peering = mock.Mock(return_value=self._data)
        self.client.update_peering = mock.Mock(return_value=self._data)

    def test_update(self):
        arglist = [
            self._data.name,
            '--name', 'test-peering-updated',
            '--description', 'vpc peering updated',
        ]
        verifylist = [
            ('peering', self._data.name),
            ('name', 'test-peering-updated'),
            ('description', 'vpc peering updated'),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)

        self.client.find_peering.assert_called_with(self._data.name)
        self.client.update_peering.assert_called_with(
            self._data.id,
            name='test-peering-updated',
            description='vpc peering updated'
        )
        self.assertEqual(self.columns, columns)
class TestAddVpcRoute(fakes.TestVpc):

    _data = fakes.FakeVpcRoute.create_one()

    columns = ('id', 'type', 'nexthop', 'destination', 'router_id',
               'project_id')

    data = fakes.gen_data(_data, columns)

    def setUp(self):
        super(TestAddVpcRoute, self).setUp()

        self.cmd = route.AddVpcRoute(self.app, None)
        self.client.add_route = mock.Mock(
            return_value=fakes.FakeVpcRoute.create_one())

    def test_add_route(self):
        arglist = [
            '--nexthop',
            'test-peering-uuid',
            '--destination',
            '192.168.1.0/24',
            '--router-id',
            'test-router-uuid',
        ]
        verifylist = [
            ('nexthop', 'test-peering-uuid'),
            ('type', 'peering'),
            ('destination', '192.168.1.0/24'),
            ('router_id', 'test-router-uuid'),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'nexthop': 'test-peering-uuid',
            'type': 'peering',
            'destination': '192.168.1.0/24',
            'router_id': 'test-router-uuid'
        }

        self.client.add_route.assert_called_with(**attrs)
        self.assertEqual(self.columns, columns)
class TestShowVpcPeering(fakes.TestVpc):

    _data = fakes.FakeVpcPeering.create_one()

    columns = (
        'id',
        'name',
        'local_vpc_info',
        'peer_vpc_info',
        'description',
        'created_at',
        'updated_at',
        'status'
    )

    data = fakes.gen_data(_data, columns)

    def setUp(self):
        super(TestShowVpcPeering, self).setUp()

        self.cmd = peering.ShowVpcPeering(self.app, None)

        self.client.find_peering = mock.Mock(return_value=self._data)

    def test_show_no_options(self):
        arglist = []
        verifylist = []

        # Testing that a call without the required argument will fail and
        # throw a "ParserExecption"
        self.assertRaises(tests_utils.ParserException,
                          self.check_parser, self.cmd, arglist, verifylist)

    def test_show(self):
        arglist = [
            self._data.id,
        ]

        verifylist = [
            ('peering', self._data.id),
        ]

        # Verify cm is triggered with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        self.client.find_peering.assert_called_with(self._data.id)

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)

    def test_show_non_existent(self):
        arglist = [
            'unexist_vpc_peering',
        ]

        verifylist = [
            ('peering', 'unexist_vpc_peering'),
        ]

        # Verify cm is triggered with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        find_mock_result = exceptions.CommandError('Resource Not Found')
        self.client.find_peering = (
            mock.Mock(side_effect=find_mock_result)
        )

        # Trigger the action
        try:
            self.cmd.take_action(parsed_args)
        except Exception as e:
            self.assertEqual('Resource Not Found', str(e))
        self.client.find_peering.assert_called_with('unexist_vpc_peering')
class TestCreateVpcPeering(fakes.TestVpc):

    _data = fakes.FakeVpcPeering.create_one()

    columns = (
        'id',
        'name',
        'local_vpc_info',
        'peer_vpc_info',
        'description',
        'created_at',
        'updated_at',
        'status'
    )

    data = fakes.gen_data(_data, columns)

    def setUp(self):
        super(TestCreateVpcPeering, self).setUp()

        self.cmd = peering.CreateVpcPeering(self.app, None)
        self.client.create_peering = mock.Mock(
            return_value=fakes.FakeVpcPeering.create_one())
        self.client.get_project_id = mock.Mock(
            return_value='test-local-project-uuid')

    def test_create_different_project(self):
        arglist = [
            'test-peering',
            '--local-router-id', 'test-local-router-uuid',
            '--peer-router-id', 'test-peer-router-uuid',
            '--peer-project-id', 'test-peer-project-uuid',
            '--description', 'test-peering',
        ]
        verifylist = [
            ('name', 'test-peering'),
            ('local_router_id', 'test-local-router-uuid'),
            ('peer_router_id', 'test-peer-router-uuid'),
            ('peer_project_id', 'test-peer-project-uuid'),
            ('description', 'test-peering'),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'name': 'test-peering',
            'request_vpc_info': {
                'vpc_id': 'test-local-router-uuid',
                'tenant_id': 'test-local-project-uuid'
            },
            'accept_vpc_info': {
                'vpc_id': 'test-peer-router-uuid',
                'tenant_id': 'test-peer-project-uuid'
            },
            'description': 'test-peering'
        }

        self.client.create_peering.assert_called_with(**attrs)
        self.assertEqual(self.columns, columns)

    def test_create_same_project(self):
        arglist = [
            'test-peering',
            '--local-router-id', 'test-local-router-uuid',
            '--peer-router-id', 'test-peer-router-uuid',
            '--description', 'test-peering',
        ]
        verifylist = [
            ('name', 'test-peering'),
            ('local_router_id', 'test-local-router-uuid'),
            ('peer_router_id', 'test-peer-router-uuid'),
            ('description', 'test-peering'),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'name': 'test-peering',
            'request_vpc_info': {
                'vpc_id': 'test-local-router-uuid'
            },
            'accept_vpc_info': {
                'vpc_id': 'test-peer-router-uuid'
            },
            'description': 'test-peering'
        }

        self.client.create_peering.assert_called_with(**attrs)
        self.assertEqual(self.columns, columns)