<commit_before>#    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.api.baremetal import base
from tempest.common.utils import data_utils
from tempest import exceptions as exc
from tempest import test


class TestPorts(base.BaseBaremetalTest):
    """Tests for ports."""

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

        chassis = self.create_chassis()['chassis']
        self.node = self.create_node(chassis['uuid'])['node']

    @test.attr(type='smoke')
    def test_create_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()

        result = self.create_port(node_id=node_id, address=address)

        port = result['port']

        resp, body = self.client.show_port(port['uuid'])

        self.assertEqual(200, resp.status)
        self.assertEqual(port['uuid'], body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual({}, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_create_port_specifying_uuid(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        uuid = data_utils.rand_uuid()

        self.create_port(node_id=node_id, address=address, uuid=uuid)

        resp, body = self.client.show_port(uuid)

        self.assertEqual(200, resp.status)
        self.assertEqual(uuid, body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual({}, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_create_port_with_extra(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key': 'value'}

        result = self.create_port(node_id=node_id, address=address,
                                  extra=extra)
        port = result['port']

        resp, body = self.client.show_port(port['uuid'])

        self.assertEqual(200, resp.status)
        self.assertEqual(port['uuid'], body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual(extra, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_delete_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        port_id = self.create_port(node_id=node_id, address=address)['port'][
            'uuid']

        resp = self.delete_port(port_id)

        self.assertEqual(204, resp.status)
        self.assertRaises(exc.NotFound, self.client.show_port, port_id)

    @test.attr(type='smoke')
    def test_show_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key': 'value'}

        port_id = self.create_port(node_id=node_id, address=address,
                                   extra=extra)['port']['uuid']

        resp, port = self.client.show_port(port_id)

        self.assertEqual(200, resp.status)
        self.assertEqual(port_id, port['uuid'])
        self.assertEqual(address, port['address'])
        self.assertEqual(extra, port['extra'])

    @test.attr(type='smoke')
    def test_show_port_with_links(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()

        port_id = self.create_port(node_id=node_id, address=address)['port'][
            'uuid']

        resp, body = self.client.show_port(port_id)

        self.assertEqual(200, resp.status)
        self.assertIn('links', body.keys())
        self.assertEqual(2, len(body['links']))
        self.assertIn(port_id, body['links'][0]['href'])

    @test.attr(type='smoke')
    def test_list_ports(self):
        node_id = self.node['uuid']

        uuids = [self.create_port(node_id=node_id,
                                  address=data_utils.rand_mac_address())
                 ['port']['uuid'] for i in xrange(5)]

        resp, body = self.client.list_ports()
        self.assertEqual(200, resp.status)
        loaded_uuids = [p['uuid'] for p in body['ports']]

        for uuid in uuids:
            self.assertIn(uuid, loaded_uuids)

        # Verify self links.
        for port in body['ports']:
            self.validate_self_link('ports', port['uuid'],
                                    port['links'][0]['href'])

    @test.attr(type='smoke')
    def test_list_with_limit(self):
        node_id = self.node['uuid']

        for i in xrange(5):
            self.create_port(node_id=node_id,
                             address=data_utils.rand_mac_address())

        resp, body = self.client.list_ports(limit=3)
        self.assertEqual(200, resp.status)
        self.assertEqual(3, len(body['ports']))

        next_marker = body['ports'][-1]['uuid']
        self.assertIn(next_marker, body['next'])

    def test_list_ports_details(self):
        node_id = self.node['uuid']

        uuids = [
            self.create_port(node_id=node_id,
                             address=data_utils.rand_mac_address())
            ['port']['uuid'] for i in range(0, 5)]

        resp, body = self.client.list_ports_detail()
        self.assertEqual(200, resp.status)

        ports_dict = {port['uuid']: port for port in body['ports']
                      if port['uuid'] in uuids}

        for uuid in uuids:
            self.assertIn(uuid, ports_dict)
            port = ports_dict[uuid]
            self.assertIn('extra', port)
            self.assertIn('node_uuid', port)
            # never expose the node_id
            self.assertNotIn('node_id', port)
            # Verify self link.
            self.validate_self_link('ports', port['uuid'],
                                    port['links'][0]['href'])

    @test.attr(type='smoke')
    def test_update_port_replace(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

        port_id = self.create_port(node_id=node_id, address=address,
                                   extra=extra)['port']['uuid']

        new_address = data_utils.rand_mac_address()
        new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
                     'key3': 'new-value3'}

        patch = [{'path': '/address',
                  'op': 'replace',
                  'value': new_address},
                 {'path': '/extra/key1',
                  'op': 'replace',<commit_msg>Use old dict generator syntax in baremetal/test_ports<commit_after>#    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.api.baremetal import base
from tempest.common.utils import data_utils
from tempest import exceptions as exc
from tempest import test


class TestPorts(base.BaseBaremetalTest):
    """Tests for ports."""

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

        chassis = self.create_chassis()['chassis']
        self.node = self.create_node(chassis['uuid'])['node']

    @test.attr(type='smoke')
    def test_create_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()

        result = self.create_port(node_id=node_id, address=address)

        port = result['port']

        resp, body = self.client.show_port(port['uuid'])

        self.assertEqual(200, resp.status)
        self.assertEqual(port['uuid'], body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual({}, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_create_port_specifying_uuid(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        uuid = data_utils.rand_uuid()

        self.create_port(node_id=node_id, address=address, uuid=uuid)

        resp, body = self.client.show_port(uuid)

        self.assertEqual(200, resp.status)
        self.assertEqual(uuid, body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual({}, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_create_port_with_extra(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key': 'value'}

        result = self.create_port(node_id=node_id, address=address,
                                  extra=extra)
        port = result['port']

        resp, body = self.client.show_port(port['uuid'])

        self.assertEqual(200, resp.status)
        self.assertEqual(port['uuid'], body['uuid'])
        self.assertEqual(address, body['address'])
        self.assertEqual(extra, body['extra'])
        self.assertEqual(node_id, body['node_uuid'])

    @test.attr(type='smoke')
    def test_delete_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        port_id = self.create_port(node_id=node_id, address=address)['port'][
            'uuid']

        resp = self.delete_port(port_id)

        self.assertEqual(204, resp.status)
        self.assertRaises(exc.NotFound, self.client.show_port, port_id)

    @test.attr(type='smoke')
    def test_show_port(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key': 'value'}

        port_id = self.create_port(node_id=node_id, address=address,
                                   extra=extra)['port']['uuid']

        resp, port = self.client.show_port(port_id)

        self.assertEqual(200, resp.status)
        self.assertEqual(port_id, port['uuid'])
        self.assertEqual(address, port['address'])
        self.assertEqual(extra, port['extra'])

    @test.attr(type='smoke')
    def test_show_port_with_links(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()

        port_id = self.create_port(node_id=node_id, address=address)['port'][
            'uuid']

        resp, body = self.client.show_port(port_id)

        self.assertEqual(200, resp.status)
        self.assertIn('links', body.keys())
        self.assertEqual(2, len(body['links']))
        self.assertIn(port_id, body['links'][0]['href'])

    @test.attr(type='smoke')
    def test_list_ports(self):
        node_id = self.node['uuid']

        uuids = [self.create_port(node_id=node_id,
                                  address=data_utils.rand_mac_address())
                 ['port']['uuid'] for i in xrange(5)]

        resp, body = self.client.list_ports()
        self.assertEqual(200, resp.status)
        loaded_uuids = [p['uuid'] for p in body['ports']]

        for uuid in uuids:
            self.assertIn(uuid, loaded_uuids)

        # Verify self links.
        for port in body['ports']:
            self.validate_self_link('ports', port['uuid'],
                                    port['links'][0]['href'])

    @test.attr(type='smoke')
    def test_list_with_limit(self):
        node_id = self.node['uuid']

        for i in xrange(5):
            self.create_port(node_id=node_id,
                             address=data_utils.rand_mac_address())

        resp, body = self.client.list_ports(limit=3)
        self.assertEqual(200, resp.status)
        self.assertEqual(3, len(body['ports']))

        next_marker = body['ports'][-1]['uuid']
        self.assertIn(next_marker, body['next'])

    def test_list_ports_details(self):
        node_id = self.node['uuid']

        uuids = [
            self.create_port(node_id=node_id,
                             address=data_utils.rand_mac_address())
            ['port']['uuid'] for i in range(0, 5)]

        resp, body = self.client.list_ports_detail()
        self.assertEqual(200, resp.status)

        ports_dict = dict((port['uuid'], port) for port in body['ports']
                          if port['uuid'] in uuids)

        for uuid in uuids:
            self.assertIn(uuid, ports_dict)
            port = ports_dict[uuid]
            self.assertIn('extra', port)
            self.assertIn('node_uuid', port)
            # never expose the node_id
            self.assertNotIn('node_id', port)
            # Verify self link.
            self.validate_self_link('ports', port['uuid'],
                                    port['links'][0]['href'])

    @test.attr(type='smoke')
    def test_update_port_replace(self):
        node_id = self.node['uuid']
        address = data_utils.rand_mac_address()
        extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

        port_id = self.create_port(node_id=node_id, address=address,
                                   extra=extra)['port']['uuid']

        new_address = data_utils.rand_mac_address()
        new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
                     'key3': 'new-value3'}

        patch = [{'path': '/address',
                  'op': 'replace',
                  'value': new_address},
                 {'path': '/extra/key1',
                  'op': 'replace',
{'fraction_non_alphanumeric': 0.13448915525114155, 'fraction_numerical': 0.007848173515981735, 'mean_word_length': 2.442288801571709, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697674', 'n_tokens_mistral': 4280, 'n_tokens_neox': 4135, 'n_words': 819}