class TestShowSecurityGroupNetwork(TestSecurityGroupNetwork):

    # The security group rule to be shown with the group.
    _security_group_rule = \
        network_fakes.FakeSecurityGroupRule.create_one_security_group_rule()

    # The security group to be shown.
    _security_group = \
        network_fakes.FakeSecurityGroup.create_one_security_group(
            attrs={'security_group_rules': [_security_group_rule._info]}
        )

    columns = (
        'description',
        'id',
        'name',
        'project_id',
        'rules',
        'tags',
    )

    data = (
        _security_group.description,
        _security_group.id,
        _security_group.name,
        _security_group.project_id,
        security_group.NetworkSecurityGroupRulesColumn(
            [_security_group_rule._info]),
        _security_group.tags,
    )

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

        self.network.find_security_group = mock.Mock(
            return_value=self._security_group)

        # Get the command object to test
        self.cmd = security_group.ShowSecurityGroup(self.app, self.namespace)

    def test_show_no_options(self):
        self.assertRaises(tests_utils.ParserException, self.check_parser,
                          self.cmd, [], [])

    def test_show_all_options(self):
        arglist = [
            self._security_group.id,
        ]
        verifylist = [
            ('group', self._security_group.id),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.network.find_security_group.assert_called_once_with(
            self._security_group.id, ignore_missing=False)
        self.assertEqual(self.columns, columns)
        self.assertItemEqual(self.data, data)
class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork):

    project = identity_fakes.FakeProject.create_one_project()
    domain = identity_fakes.FakeDomain.create_one_domain()
    # The security group to be created.
    _security_group = (
        network_fakes.FakeSecurityGroup.create_one_security_group())

    columns = (
        'description',
        'id',
        'name',
        'project_id',
        'rules',
        'tags',
    )

    data = (
        _security_group.description,
        _security_group.id,
        _security_group.name,
        _security_group.project_id,
        security_group.NetworkSecurityGroupRulesColumn([]),
        _security_group.tags,
    )

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

        self.network.create_security_group = mock.Mock(
            return_value=self._security_group)

        self.projects_mock.get.return_value = self.project
        self.domains_mock.get.return_value = self.domain
        self.network.set_tags = mock.Mock(return_value=None)

        # Get the command object to test
        self.cmd = security_group.CreateSecurityGroup(self.app, self.namespace)

    def test_create_no_options(self):
        self.assertRaises(tests_utils.ParserException, self.check_parser,
                          self.cmd, [], [])

    def test_create_min_options(self):
        arglist = [
            self._security_group.name,
        ]
        verifylist = [
            ('name', self._security_group.name),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.network.create_security_group.assert_called_once_with(
            **{
                'description': self._security_group.name,
                'name': self._security_group.name,
            })
        self.assertEqual(self.columns, columns)
        self.assertItemEqual(self.data, data)

    def test_create_all_options(self):
        arglist = [
            '--description',
            self._security_group.description,
            '--project',
            self.project.name,
            '--project-domain',
            self.domain.name,
            self._security_group.name,
        ]
        verifylist = [
            ('description', self._security_group.description),
            ('name', self._security_group.name),
            ('project', self.project.name),
            ('project_domain', self.domain.name),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        columns, data = self.cmd.take_action(parsed_args)

        self.network.create_security_group.assert_called_once_with(
            **{
                'description': self._security_group.description,
                'name': self._security_group.name,
                'tenant_id': self.project.id,
            })
        self.assertEqual(self.columns, columns)
        self.assertItemEqual(self.data, data)

    def _test_create_with_tag(self, add_tags=True):
        arglist = [self._security_group.name]
        if add_tags:
            arglist += ['--tag', 'red', '--tag', 'blue']
        else:
            arglist += ['--no-tag']

        verifylist = [
            ('name', self._security_group.name),
        ]
        if add_tags:
            verifylist.append(('tags', ['red', 'blue']))
        else:
            verifylist.append(('no_tag', True))

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        columns, data = (self.cmd.take_action(parsed_args))

        self.network.create_security_group.assert_called_once_with(
            **{
                'description': self._security_group.name,
                'name': self._security_group.name,
            })
        if add_tags:
            self.network.set_tags.assert_called_once_with(
                self._security_group, tests_utils.CompareBySet(['red',
                                                                'blue']))
        else:
            self.assertFalse(self.network.set_tags.called)
        self.assertEqual(self.columns, columns)
        self.assertItemEqual(self.data, data)

    def test_create_with_tags(self):
        self._test_create_with_tag(add_tags=True)

    def test_create_with_no_tag(self):
        self._test_create_with_tag(add_tags=False)