def test_nova_server_creation_nics_ordering(self):
        """NIC list keeps the order of the relationships.

        The nics= list passed to nova.server.create should be ordered
        depending on the relationships to the networks (as defined in the
        blueprint).
        """
        ctx = self._make_vm_ctx_with_networks(
            management_network_name='network1',
            networks=[
                {'id': '1'},
                {'id': '2'},
                {'id': '3'},
                {'id': '4'},
                {'id': '5'},
                {'id': '6'},
            ])
        server = {'meta': {}}

        _prepare_server_nics(
            self.mock_neutron, ctx, server)

        self.assertEqual(
            ['1', '2', '3', '4', '5', '6'],
            [n['net-id'] for n in server['nics']])
    def test_server_creation_prepends_mgmt_network(self):
        """If the management network isn't in a relation, it's the first NIC.

        Creating the server examines the relationships, and if it doesn't find
        a relationship to the management network, it adds the management
        network to the NICs list, as the first element.
        """
        ctx = self._make_vm_ctx_with_networks(
            management_network_name='other',
            networks=[
                {'id': '1'},
                {'id': '2'},
                {'id': '3'},
                {'id': '4'},
                {'id': '5'},
                {'id': '6'},
            ])
        server = {'meta': {}}

        _prepare_server_nics(
            self.mock_neutron, ctx, server)

        first_nic = server['nics'][0]
        self.assertEqual('other', first_nic['net-id'])
        self.assertEqual(7, len(server['nics']))
    def test_server_creation_uses_relation_mgmt_nic(self):
        """If the management network is in a relation, it isn't prepended.

        If the server has a relationship to the management network,
        a new NIC isn't prepended to the list.
        """
        ctx = self._make_vm_ctx_with_networks(
            management_network_name='network1',
            networks=[
                {
                    'id': '1'
                },
                {
                    'id': '2'
                },
                {
                    'id': '3'
                },
                {
                    'id': '4'
                },
                {
                    'id': '5'
                },
                {
                    'id': '6'
                },
            ])
        server = {'meta': {}}

        _prepare_server_nics(self.mock_neutron, ctx, server)
        self.assertEqual(6, len(server['nics']))
    def test_server_creation_prepends_mgmt_network(self):
        """If the management network isn't in a relation, it's the first NIC.

        Creating the server examines the relationships, and if it doesn't find
        a relationship to the management network, it adds the management
        network to the NICs list, as the first element.
        """
        ctx = self._make_vm_ctx_with_networks(management_network_name='other',
                                              networks=[
                                                  {
                                                      'id': '1'
                                                  },
                                                  {
                                                      'id': '2'
                                                  },
                                                  {
                                                      'id': '3'
                                                  },
                                                  {
                                                      'id': '4'
                                                  },
                                                  {
                                                      'id': '5'
                                                  },
                                                  {
                                                      'id': '6'
                                                  },
                                              ])
        server = {'meta': {}}

        _prepare_server_nics(self.mock_neutron, ctx, server)

        first_nic = server['nics'][0]
        self.assertEqual('other', first_nic['net-id'])
        self.assertEqual(7, len(server['nics']))
    def test_nova_server_creation_nics_ordering(self):
        """NIC list keeps the order of the relationships.

        The nics= list passed to nova.server.create should be ordered
        depending on the relationships to the networks (as defined in the
        blueprint).
        """
        ctx = self._make_vm_ctx_with_networks(
            management_network_name='network1',
            networks=[
                {
                    'id': '1'
                },
                {
                    'id': '2'
                },
                {
                    'id': '3'
                },
                {
                    'id': '4'
                },
                {
                    'id': '5'
                },
                {
                    'id': '6'
                },
            ])
        server = {'meta': {}}

        _prepare_server_nics(self.mock_neutron, ctx, server)

        self.assertEqual(['1', '2', '3', '4', '5', '6'],
                         [n['net-id'] for n in server['nics']])
    def test_port_not_to_mgmt_network(self):
        """A NICs list entry is added with the network and the port.

        A relationship to a port must not only add a NIC, but the NIC must
        also make sure to use that port.
        """
        ports = [{'id': '1'}]
        ctx = self._make_vm_ctx_with_ports('other', ports)
        server = {'meta': {}}

        _prepare_server_nics(self.mock_neutron, ctx, server)
        expected = [{'net-id': 'other'}, {'port-id': '1'}]
        self.assertEqual(expected, server['nics'])
    def test_network_with_port(self):
        """Port on the management network is used to connect to it.

        The NICs list entry for the management network contains the
        port-id of the port from the relationship, but doesn't contain net-id.
        """
        ports = [{'id': '1'}]
        ctx = self._make_vm_ctx_with_ports('network1', ports)
        server = {'meta': {}}

        _prepare_server_nics(self.mock_neutron, ctx, server)

        self.assertEqual([{'port-id': '1'}], server['nics'])
    def test_network_with_port(self):
        """Port on the management network is used to connect to it.

        The NICs list entry for the management network contains the
        port-id of the port from the relationship, but doesn't contain net-id.
        """
        ports = [{'id': '1'}]
        ctx = self._make_vm_ctx_with_ports('network1', ports)
        server = {'meta': {}}

        _prepare_server_nics(
            self.mock_neutron, ctx, server)

        self.assertEqual([{'port-id': '1'}], server['nics'])
    def test_port_not_to_mgmt_network(self):
        """A NICs list entry is added with the network and the port.

        A relationship to a port must not only add a NIC, but the NIC must
        also make sure to use that port.
        """
        ports = [{'id': '1'}]
        ctx = self._make_vm_ctx_with_ports('other', ports)
        server = {'meta': {}}

        _prepare_server_nics(
            self.mock_neutron, ctx, server)
        expected = [
            {'net-id': 'other'},
            {'port-id': '1'}
        ]
        self.assertEqual(expected, server['nics'])
    def test_server_creation_uses_relation_mgmt_nic(self):
        """If the management network is in a relation, it isn't prepended.

        If the server has a relationship to the management network,
        a new NIC isn't prepended to the list.
        """
        ctx = self._make_vm_ctx_with_networks(
            management_network_name='network1',
            networks=[
                {'id': '1'},
                {'id': '2'},
                {'id': '3'},
                {'id': '4'},
                {'id': '5'},
                {'id': '6'},
            ])
        server = {'meta': {}}

        _prepare_server_nics(
            self.mock_neutron, ctx, server)
        self.assertEqual(6, len(server['nics']))