# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from tempest.lib import exceptions as lib_exc
from tempest import test

from neutron.tests.tempest.api import base_security_groups as base
from neutron.tests.tempest import config

CONF = config.CONF


class NegativeSecGroupTest(base.BaseSecGroupTest):

    @classmethod
    @test.requires_ext(extension="security-group", service="network")
    def resource_setup(cls):
        super(NegativeSecGroupTest, cls).resource_setup()

    @test.attr(type=['negative', 'gate'])
    @test.idempotent_id('0d9c7791-f2ad-4e2f-ac73-abf2373b0d2d')
    def test_create_security_group_rule_with_invalid_ports(self):
        group_create_body, _ = self._create_security_group()

        # Create rule for tcp protocol with invalid ports
        states = [(-16, 80, 'Invalid value for port -16'),
                  (80, 79, 'port_range_min must be <= port_range_max'),
                  (80, 65536, 'Invalid value for port 65536'),
                  (None, 6, 'port_range_min must be <= port_range_max'),
                  (-16, 65536, 'Invalid value for port')]
        for pmin, pmax, msg in states:
            ex = self.assertRaises(
                lib_exc.BadRequest, self.client.create_security_group_rule,
                security_group_id=group_create_body['security_group']['id'],
                protocol='tcp', port_range_min=pmin, port_range_max=pmax,
                direction='ingress', ethertype=self.ethertype)
            self.assertIn(msg, str(ex))

        # Create rule for icmp protocol with invalid ports
        states = [(1, 256, 'Invalid value for ICMP code'),
                  (-1, 25, 'Invalid value'),
                  (None, 6, 'ICMP type (port-range-min) is missing'),
                  (300, 1, 'Invalid value for ICMP type')]
        for pmin, pmax, msg in states:
            ex = self.assertRaises(
                lib_exc.BadRequest, self.client.create_security_group_rule,
                security_group_id=group_create_body['security_group']['id'],
                protocol='icmp', port_range_min=pmin, port_range_max=pmax,
                direction='ingress', ethertype=self.ethertype)
            self.assertIn(msg, str(ex))

    @test.attr(type='negative')
    @test.idempotent_id('55100aa8-b24f-333c-0bef-64eefd85f15c')
    def test_update_default_security_group_name(self):
        sg_list = self.client.list_security_groups(name='default')
        sg = sg_list['security_groups'][0]
        self.assertRaises(lib_exc.Conflict, self.client.update_security_group,
                          sg['id'], name='test')


class NegativeSecGroupIPv6Test(NegativeSecGroupTest):
    _ip_version = 6

@@ -1062,1730 +1062,8 @@
 ()%0A%0A
-    @test.attr(type=%5B'negative', 'gate'%5D)%0A    @test.idempotent_id('0d9c7791-f2ad-4e2f-ac73-abf2373b0d2d')%0A    def test_create_security_group_rule_with_invalid_ports(self):%0A        group_create_body, _ = self._create_security_group()%0A%0A        # Create rule for tcp protocol with invalid ports%0A        states = %5B(-16, 80, 'Invalid value for port -16'),%0A                  (80, 79, 'port_range_min must be %3C= port_range_max'),%0A                  (80, 65536, 'Invalid value for port 65536'),%0A                  (None, 6, 'port_range_min must be %3C= port_range_max'),%0A                  (-16, 65536, 'Invalid value for port')%5D%0A        for pmin, pmax, msg in states:%0A            ex = self.assertRaises(%0A                lib_exc.BadRequest, self.client.create_security_group_rule,%0A                security_group_id=group_create_body%5B'security_group'%5D%5B'id'%5D,%0A                protocol='tcp', port_range_min=pmin, port_range_max=pmax,%0A                direction='ingress', ethertype=self.ethertype)%0A            self.assertIn(msg, str(ex))%0A%0A        # Create rule for icmp protocol with invalid ports%0A        states = %5B(1, 256, 'Invalid value for ICMP code'),%0A                  (-1, 25, 'Invalid value'),%0A                  (None, 6, 'ICMP type (port-range-min) is missing'),%0A                  (300, 1, 'Invalid value for ICMP type')%5D%0A        for pmin, pmax, msg in states:%0A            ex = self.assertRaises(%0A                lib_exc.BadRequest, self.client.create_security_group_rule,%0A                security_group_id=group_create_body%5B'security_group'%5D%5B'id'%5D,%0A                protocol='icmp', port_range_min=pmin, port_range_max=pmax,%0A                direction='ingress', ethertype=self.ethertype)%0A            self.assertIn(msg, str(ex))%0A%0A
     

Remove a SGs API test following sync to Tempest