1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16+ import collections
1617import copy
1718from unittest import mock
1819
@@ -460,6 +461,7 @@ def __init__(self):
460461 self .activated_bindings = set ()
461462 self .conf = mock .Mock ()
462463 self .conf .host = 'host1'
464+ self .network_ports = collections .defaultdict (list )
463465
464466
465467class TestSriovNicSwitchRpcCallbacks (base .BaseTestCase ):
@@ -528,6 +530,12 @@ def test_binding_activate(self):
528530 }
529531 kwargs = self ._create_fake_bindings (fake_port , self .agent .conf .host )
530532 kwargs ['context' ] = self .context
533+
534+ self .agent .network_ports ['network_id' ].append ({
535+ 'port_id' : fake_port ['id' ],
536+ 'device' : 'fake_device'
537+ })
538+
531539 self .sriov_rpc_callback .binding_activate (** kwargs )
532540 # Assert agent.activated_binding set contains the new binding
533541 self .assertIn ((fake_port ['mac_address' ],
@@ -538,10 +546,32 @@ def test_binding_activate_no_host(self):
538546 fake_port = self ._create_fake_port ()
539547 kwargs = self ._create_fake_bindings (fake_port , 'other-host' )
540548 kwargs ['context' ] = self .context
549+
550+ self .agent .network_ports [self .agent .conf .host ].append ({
551+ 'port_id' : fake_port ['id' ],
552+ 'device' : 'fake_device'
553+ })
554+
541555 self .sriov_rpc_callback .binding_activate (** kwargs )
542556 # Assert no bindings were added
543557 self .assertEqual (set (), self .agent .activated_bindings )
544558
559+ def test_binding_activate_port_not_in_network (self ):
560+ fake_port = self ._create_fake_port ()
561+ kwargs = self ._create_fake_bindings (fake_port , self .agent .conf .host )
562+ kwargs ['context' ] = self .context
563+
564+ self .agent .network_ports ['network_id' ] = []
565+
566+ with mock .patch .object (sriov_nic_agent .LOG ,
567+ 'warning' ) as mock_warning :
568+ self .sriov_rpc_callback .binding_activate (** kwargs )
569+ # Check that the warning message was logged
570+ expected_msg = (
571+ "This port is not SRIOV, skip binding for port %s."
572+ )
573+ mock_warning .assert_called_once_with (expected_msg , fake_port ['id' ])
574+
545575 def test_binding_deactivate (self ):
546576 # binding_deactivate() basically does nothing
547577 # call it with both the agent's host and other host to cover
0 commit comments