15
15
import random
16
16
17
17
import fixtures
18
- from keystoneauth1 import adapter as ksa_adap
19
- import mock
20
18
from neutronclient .common import exceptions as neutron_client_exc
21
19
import os_resource_classes as orc
22
- from oslo_serialization import jsonutils
23
20
from oslo_utils import uuidutils
24
21
25
22
from nova import exception
26
23
from nova .network import constants as neutron_constants
27
24
from nova .network import model as network_model
28
25
from nova .tests .fixtures import nova as nova_fixtures
29
- from nova .tests .unit import fake_requests
30
26
31
27
32
28
class _FakeNeutronClient :
@@ -665,25 +661,6 @@ def setUp(self):
665
661
lambda * args , ** kwargs : network_model .NetworkInfo .hydrate (
666
662
self .nw_info ))
667
663
668
- # Stub out port binding APIs which go through a KSA client Adapter
669
- # rather than python-neutronclient.
670
- self .test .stub_out (
671
- 'nova.network.neutron._get_ksa_client' ,
672
- lambda * args , ** kwargs : mock .Mock (
673
- spec = ksa_adap .Adapter ))
674
- self .test .stub_out (
675
- 'nova.network.neutron.API._create_port_binding' ,
676
- self .create_port_binding )
677
- self .test .stub_out (
678
- 'nova.network.neutron.API._delete_port_binding' ,
679
- self .delete_port_binding )
680
- self .test .stub_out (
681
- 'nova.network.neutron.API._activate_port_binding' ,
682
- self .activate_port_binding )
683
- self .test .stub_out (
684
- 'nova.network.neutron.API._get_port_binding' ,
685
- self .get_port_binding )
686
-
687
664
self .test .stub_out (
688
665
'nova.network.neutron.get_client' , self ._get_client )
689
666
@@ -692,13 +669,12 @@ def _get_client(self, context, admin=False):
692
669
admin = admin or context .is_admin and not context .auth_token
693
670
return _FakeNeutronClient (self , admin )
694
671
695
- def create_port_binding (self , context , client , port_id , data ):
672
+ def create_port_binding (self , port_id , body ):
696
673
if port_id not in self ._ports :
697
- return fake_requests .FakeResponse (
698
- 404 , content = 'Port %s not found' % port_id )
674
+ raise neutron_client_exc .NeutronClientException (status_code = 404 )
699
675
700
676
port = self ._ports [port_id ]
701
- binding = copy .deepcopy (data ['binding' ])
677
+ binding = copy .deepcopy (body ['binding' ])
702
678
703
679
# NOTE(stephenfin): We don't allow changing of backend
704
680
binding ['vif_type' ] = port ['binding:vif_type' ]
@@ -713,61 +689,36 @@ def create_port_binding(self, context, client, port_id, data):
713
689
714
690
self ._port_bindings [port_id ][binding ['host' ]] = binding
715
691
716
- return fake_requests .FakeResponse (
717
- 200 , content = jsonutils .dumps ({'binding' : binding }),
718
- )
692
+ return {'binding' : binding }
719
693
720
- def _get_failure_response_if_port_or_binding_not_exists (
721
- self , port_id , host ,
722
- ):
694
+ def _validate_port_binding (self , port_id , host_id ):
723
695
if port_id not in self ._ports :
724
- return fake_requests .FakeResponse (
725
- 404 , content = 'Port %s not found' % port_id )
726
- if host not in self ._port_bindings [port_id ]:
727
- return fake_requests .FakeResponse (
728
- 404 ,
729
- content = 'Binding for host %s for port %s not found'
730
- % (host , port_id ))
731
-
732
- def delete_port_binding (self , context , client , port_id , host ):
733
- failure = self ._get_failure_response_if_port_or_binding_not_exists (
734
- port_id , host )
735
- if failure is not None :
736
- return failure
696
+ raise neutron_client_exc .NeutronClientException (status_code = 404 )
737
697
738
- del self ._port_bindings [port_id ][host ]
698
+ if host_id not in self ._port_bindings [port_id ]:
699
+ raise neutron_client_exc .NeutronClientException (status_code = 404 )
739
700
740
- return fake_requests .FakeResponse (204 )
701
+ def delete_port_binding (self , port_id , host_id ):
702
+ self ._validate_port_binding (port_id , host_id )
703
+ del self ._port_bindings [port_id ][host_id ]
741
704
742
- def _activate_port_binding (self , port_id , host ):
705
+ def _activate_port_binding (self , port_id , host_id ):
743
706
# It makes sure that only one binding is active for a port
744
- for h , binding in self ._port_bindings [port_id ].items ():
745
- if h == host :
707
+ for host , binding in self ._port_bindings [port_id ].items ():
708
+ if host == host_id :
746
709
# NOTE(gibi): neutron returns 409 if this binding is already
747
710
# active but nova does not depend on this behaviour yet.
748
711
binding ['status' ] = 'ACTIVE'
749
712
else :
750
713
binding ['status' ] = 'INACTIVE'
751
714
752
- def activate_port_binding (self , context , client , port_id , host ):
753
- failure = self ._get_failure_response_if_port_or_binding_not_exists (
754
- port_id , host )
755
- if failure is not None :
756
- return failure
757
-
758
- self ._activate_port_binding (port_id , host )
759
-
760
- return fake_requests .FakeResponse (200 )
761
-
762
- def get_port_binding (self , context , client , port_id , host ):
763
- failure = self ._get_failure_response_if_port_or_binding_not_exists (
764
- port_id , host )
765
- if failure is not None :
766
- return failure
715
+ def activate_port_binding (self , port_id , host_id ):
716
+ self ._validate_port_binding (port_id , host_id )
717
+ self ._activate_port_binding (port_id , host_id )
767
718
768
- binding = { "binding" : self . _port_bindings [ port_id ][ host ]}
769
- return fake_requests . FakeResponse (
770
- 200 , content = jsonutils . dumps ( binding ))
719
+ def show_port_binding ( self , port_id , host_id ):
720
+ self . _validate_port_binding ( port_id , host_id )
721
+ return { 'binding' : self . _port_bindings [ port_id ][ host_id ]}
771
722
772
723
def _list_resource (self , resources , retrieve_all , ** _params ):
773
724
# If 'fields' is passed we need to strip that out since it will mess
0 commit comments