def test_least_loaded_node_scheduler_create_label(self): with patch.object(Node.objects, "get_items") as node_objects, \ patch.object(NodeLabel, "save", autospec=True) as nodelabel_save, \ patch.object(NodeLabel, "node") as nodelabel_node_add: slice = Slice(name="mysite_test1", default_flavor=None, default_isolation="vm") instance1 = Instance(id=1) node1 = Node(hostname="my.node.com", id=4567) node1.instances = MockObjectList(initial=[]) node2 = Node(hostname="my.node.com", id=8910) node2.instances = MockObjectList(initial=[instance1]) # Fake out the existence of a NodeLabel object. TODO: Extend the mock framework to support the model__field # syntax. node1.nodelabels__name = None node2.nodelabels__name = None node_objects.return_value = [node1, node2] # should pick the node with the least number of instances sched = LeastLoadedNodeScheduler( slice, label="foo", constrain_by_service_instance=True) (picked_node, parent) = sched.pick() self.assertNotEqual(picked_node, None) self.assertEqual(picked_node.id, node1.id) # NodeLabel should have been created and saved self.assertEqual(nodelabel_save.call_count, 1) self.assertEqual(nodelabel_save.call_args[0][0].name, "foo") # The NodeLabel's node field should have been added to NodeLabel.node.add.assert_called_with(node1)
def test_get_lan_network(self): with patch.object(VEGService.objects, "get_items") as vegservice_objects: vegservice = VEGService( name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) vegservice_objects.return_value = [vegservice] self.tenant.owner = vegservice self.slice.networks = MockObjectList([self.priv_network]) lan_network = self.policy.get_lan_network(self.tenant, None) self.assertEqual(lan_network, self.priv_network)
def test_get_lan_network_noexist(self): with patch.object(VEGService.objects, "get_items") as vegservice_objects: vegservice = VEGService( name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) vegservice_objects.return_value = [vegservice] self.tenant.owner = vegservice self.slice.networks = MockObjectList() with self.assertRaises(Exception) as e: self.policy.get_lan_network(self.tenant, None) self.assertEqual(e.exception.message, "No lan_network")
def test_least_loaded_node_scheduler_two_nodes(self): with patch.object(Node.objects, "get_items") as node_objects: slice = Slice(name="mysite_test1", default_flavor=None, default_isolation="vm") instance1 = Instance(id=1) node1 = Node(hostname="my.node.com", id=4567) node1.instances = MockObjectList(initial=[]) node2 = Node(hostname="my.node.com", id=8910) node2.instances = MockObjectList(initial=[instance1]) node_objects.return_value = [node1, node2] # should pick the node with the fewest instance (node1) sched = LeastLoadedNodeScheduler(slice) (picked_node, parent) = sched.pick() self.assertNotEqual(picked_node, None) self.assertEqual(picked_node.id, node1.id)
def test_get_lan_network_toomany(self): with patch.object(VEGService.objects, "get_items") as vegservice_objects: some_other_network = Network(name="mysite_test1_private", template=self.priv_template) vegservice = VEGService( name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) vegservice_objects.return_value = [vegservice] self.tenant.owner = vegservice self.slice.networks = MockObjectList( [self.priv_network, some_other_network]) with self.assertRaises(Exception) as e: lan_network = self.policy.get_lan_network(self.tenant, None) self.assertEqual( e.exception.message, "The vEG slice should only have one non-management private network" )
def test_cleanup_orphans(self): with patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: vrtenant = AddressManagerServiceInstance(id=1) self.tenant.address_service_instance = vrtenant some_other_vrtenant = AddressManagerServiceInstance(id=2) link = ServiceInstanceLink( subscriber_service_instance=self.tenant, provider_service_instance=some_other_vrtenant) self.tenant.subscribed_links = MockObjectList(initial=[link]) amsi_objects.return_value = [some_other_vrtenant] self.policy.cleanup_orphans(self.tenant) amsi_delete.assert_called()
def test_least_loaded_node_scheduler_with_label(self): with patch.object(Node.objects, "get_items") as node_objects: slice = Slice(name="mysite_test1", default_flavor=None, default_isolation="vm") instance1 = Instance(id=1) node1 = Node(hostname="my.node.com", id=4567) node1.instances = MockObjectList(initial=[]) node2 = Node(hostname="my.node.com", id=8910) node2.instances = MockObjectList(initial=[instance1]) # Fake out the existence of a NodeLabel object. TODO: Extend the mock framework to support the model__field # syntax. node1.nodelabels__name = None node2.nodelabels__name = "foo" node_objects.return_value = [node1, node2] # should pick the node with the label, even if it has a greater number of instances sched = LeastLoadedNodeScheduler(slice, label="foo") (picked_node, parent) = sched.pick() self.assertNotEqual(picked_node, None) self.assertEqual(picked_node.id, node2.id)
def test_least_loaded_node_scheduler(self): with patch.object(Node.objects, "get_items") as node_objects: slice = Slice(name="mysite_test1", default_flavor=None, default_isolation="vm") node = Node(hostname="my.node.com", id=4567) node.instances = MockObjectList(initial=[]) node_objects.return_value = [node] sched = LeastLoadedNodeScheduler(slice) (picked_node, parent) = sched.pick() self.assertNotEqual(picked_node, None) self.assertEqual(picked_node.id, node.id)
def setUp(self): global DeferredException self.sys_path_save = sys.path # Setting up the config module from xosconfig import Config config = os.path.join(test_path, "../test_config.yaml") Config.clear() Config.init(config, "synchronizer-config-schema.yaml") # Mock the kafka producer self.mockxoskafka = MagicMock() modules = { 'xoskafka': self.mockxoskafka, 'xoskafka.XOSKafkaProducer': self.mockxoskafka.XOSKafkaProducer, } self.module_patcher = patch.dict('sys.modules', modules) self.module_patcher.start() from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), ("rcord", "rcord.xproto")]) import xossynchronizer.modelaccessor import mock_modelaccessor reload(mock_modelaccessor) # in case nose2 loaded it in a previous test reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test from mock_modelaccessor import MockObjectList from xossynchronizer.modelaccessor import model_accessor self.model_accessor = model_accessor # necessary to reset XOSKafkaProducer's call_count import onos_event reload(onos_event) from onos_event import OnosPortEventStep, XOSKafkaProducer from onos_event import XOSKafkaProducer self.XOSKafkaProducer = XOSKafkaProducer # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.event_step = OnosPortEventStep self.volt_service = VOLTService(name="volt", id=1112, backend_code=1, backend_status="succeeded") self.oltdevice = OLTDevice(name="myolt", device_id="of:0000000000000001", switch_datapath_id="of:0000000000000001", switch_port="1") self.ponport = PONPort(olt_device = self.oltdevice) self.onudevice = ONUDevice(pon_port = self.ponport) self.subscriber = RCORDSubscriber(name="somesubscriber") self.voltsi = VOLTServiceInstance() # chain it all together self.oltdevice.pon_ports = MockObjectList([self.ponport]) self.ponport.onu_devices = MockObjectList([self.onudevice]) self.onudevice.volt_service_instances = MockObjectList([self.voltsi]) self.voltsi.westbound_service_instances = [self.subscriber] self.log = Mock()
def setUp(self): global DeferredException self.sys_path_save = sys.path config = os.path.join(test_path, "../test_config.yaml") from xosconfig import Config Config.clear() Config.init(config, 'synchronizer-config-schema.yaml') from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), ("rcord", "rcord.xproto"), ("onos-service", "onos.xproto")]) import xossynchronizer.modelaccessor import mock_modelaccessor reload( mock_modelaccessor) # in case nose2 loaded it in a previous test reload(xossynchronizer.modelaccessor ) # in case nose2 loaded it in a previous test from xossynchronizer.modelaccessor import model_accessor from mock_modelaccessor import MockObjectList from kubernetes_event import KubernetesPodDetailsEventStep # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.model_accessor = model_accessor self.event_step = KubernetesPodDetailsEventStep self.onos = ONOSService(name="myonos", id=1111, rest_hostname="onos-url", rest_port="8181", rest_username="******", rest_password="******", backend_code=1, backend_status="succeeded") self.fcservice = VOLTService(name="myoltservice", id=1112, backend_code=1, backend_status="succeeded", provider_services=[self.onos]) self.fcsi1 = VOLTServiceInstance(name="myfcsi1", owner=self.fcservice, backend_code=1, backend_status="succeeded") self.fcsi2 = VOLTServiceInstance(name="myfcsi2", owner=self.fcservice, backend_code=1, backend_status="succeeded") self.fcservice.service_instances = MockObjectList( [self.fcsi1, self.fcsi2]) self.log = Mock()
def setUp(self): global DeferredException self.sys_path_save = sys.path # Setting up the config module from xosconfig import Config config = os.path.join(test_path, "../test_config.yaml") Config.clear() Config.init(config, "synchronizer-config-schema.yaml") # END Setting up the config module from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config mock_modelaccessor_config(test_path, [("onos-service", "onos.xproto"), ]) import xossynchronizer.modelaccessor import mock_modelaccessor reload(mock_modelaccessor) # in case nose2 loaded it in a previous test reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test from xossynchronizer.modelaccessor import model_accessor self.model_accessor = model_accessor from mock_modelaccessor import MockObjectList from kubernetes_event import KubernetesPodDetailsEventStep # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.event_step = KubernetesPodDetailsEventStep self.onos = ONOSService(name="myonos", rest_hostname="onos-url", rest_port="8181", rest_username="******", rest_password="******", backend_code=1, backend_status="succeeded") self.attr = ServiceInstanceAttribute( name="foo", value="bar" ) self.mockAllAttr = Mock() self.mockAllAttr.all.return_value = [self.attr] self.app1 = ONOSApp(name="myapp1", owner=self.onos, backend_code=1, backend_status="succeeded", service_instance_attributes=self.mockAllAttr) self.app2 = ONOSApp(name="myapp2", owner=self.onos, backend_code=1, backend_status="succeeded", service_instance_attributes=self.mockAllAttr) self.onos.service_instances = MockObjectList([self.app1, self.app2]) self.log = Mock()
def setUp(self): global DeferredException self.sys_path_save = sys.path # Setting up the config module from xosconfig import Config config = os.path.join(test_path, "../test_config.yaml") Config.clear() Config.init(config, "synchronizer-config-schema.yaml") # END Setting up the config module from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"), ("onos-service", "onos.xproto")]) import xossynchronizer.modelaccessor import mock_modelaccessor reload( mock_modelaccessor) # in case nose2 loaded it in a previous test reload(xossynchronizer.modelaccessor ) # in case nose2 loaded it in a previous test from xossynchronizer.modelaccessor import model_accessor self.model_accessor = model_accessor from mock_modelaccessor import MockObjectList from kubernetes_event import KubernetesPodDetailsEventStep # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.event_step = KubernetesPodDetailsEventStep self.onos = ONOSService(name="myonos", id=1111, rest_hostname="onos-url", rest_port="8181", rest_username="******", rest_password="******", backend_code=1, backend_status="succeeded") self.fabric_service = FabricService(name="fabric", id=1112, backend_code=1, backend_status="succeeded", provider_services=[self.onos]) self.switch = Switch(name="switch1", backend_code=1, backend_status="succeeded") self.port1 = SwitchPort(name="switch1port1", switch=self.switch, backend_code=1, backend_status="succeeded") self.port2 = SwitchPort(name="switch1port2", switch=self.switch, backend_code=1, backend_status="succeeded") self.switch.ports = MockObjectList([self.port1, self.port2]) self.log = Mock()
def setUp(self): global DeferredException self.sys_path_save = sys.path # Setting up the config module from xosconfig import Config config = os.path.join(test_path, "../test_config.yaml") Config.clear() Config.init(config, "synchronizer-config-schema.yaml") # END Setting up the config module # Mock the kafka producer self.mockxoskafka = MagicMock() modules = { 'xoskafka': self.mockxoskafka, 'xoskafka.XOSKafkaProducer': self.mockxoskafka.XOSKafkaProducer, } self.module_patcher = patch.dict('sys.modules', modules) self.module_patcher.start() from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config mock_modelaccessor_config(test_path, [("fabric", "fabric.xproto"), ("onos-service", "onos.xproto")]) import xossynchronizer.modelaccessor import mock_modelaccessor imp.reload( mock_modelaccessor) # in case nose2 loaded it in a previous test imp.reload(xossynchronizer.modelaccessor ) # in case nose2 loaded it in a previous test from xossynchronizer.modelaccessor import model_accessor self.model_accessor = model_accessor from mock_modelaccessor import MockObjectList # necessary to reset XOSKafkaProducer's call_count import onos_event reload(onos_event) from onos_event import OnosPortEventStep, XOSKafkaProducer self.XOSKafkaProducer = XOSKafkaProducer # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.event_step = OnosPortEventStep self.fabric_service = FabricService(name="fabric", id=1112, backend_code=1, backend_status="succeeded") self.switch = Switch(name="switch1", ofId="of:0000000000000001", backend_code=1, backend_status="succeeded") self.port1 = SwitchPort(name="switch1port1", switch=self.switch, switch_id=self.switch.id, portId="1", oper_status=None, backend_code=1, backend_status="succeeded") self.port2 = SwitchPort(name="switch1port2", kind="access", switch=self.switch, switch_id=self.switch.id, portId="2", oper_status=None, backend_code=1, backend_status="succeeded") self.switch.ports = MockObjectList([self.port1, self.port2]) self.log = Mock()
def test_find_or_make_instance_for_s_tag(self): with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ patch.object(Node.objects, "get_items") as node_objects, \ patch.object(Flavor.objects, "get_items") as flavor_objects, \ patch.object(VEGService.objects, "get_items") as vegservice_objects, \ patch.object(VEGTenant, "volt") as volt, \ patch.object(VEGTenant, "save") as tenant_save, \ patch.object(VEGTenantPolicy, "get_image") as get_image, \ patch.object(VEGTenantPolicy, "allocate_public_service_instance") as get_psi, \ patch.object(LeastLoadedNodeScheduler, "pick") as pick, \ patch.object(Node, "site_deployment") as site_deployment, \ patch.object(Instance, "save") as instance_save, \ patch.object(Instance, "delete") as instance_delete, \ patch.object(VEGTenantPolicy, "port_set_parameter") as port_set_parameter: # setup mocks vrtenant = AddressManagerServiceInstance( public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") vegservice = VEGService( name="myvegservice", id=1, slices=MockObjectList(initial=[self.slice])) vegservice_objects.return_value = [vegservice] self.tenant.owner = vegservice volt.s_tag = 222 volt.c_tag = 111 get_image.return_value = self.image get_psi.return_value = vrtenant pick.return_value = (self.node, None) site_deployment.deployment = self.deployment flavor_objects.return_value = [self.flavor] node_objects.return_value = [self.node] npt_objects.return_value = [ self.npt_stag, self.npt_ctag, self.npt_neutron_port_name ] self.slice.networks = MockObjectList([self.priv_network]) # done setup mocks # call the function under test instance = self.policy.find_or_make_instance_for_s_tag( self.tenant, volt.s_tag) # make sure Instance was created self.assertNotEqual(instance, None) self.assertEqual(instance.creator.email, "*****@*****.**") self.assertEqual(instance.image.name, "trusty-server-multi-nic") self.assertEqual(instance.flavor.name, "m1.small") self.assertEqual(instance.isolation, "vm") self.assertEqual(instance.node.hostname, "my.node.com") self.assertEqual(instance.slice.name, "mysite_test1") self.assertEqual(instance.parent, None) instance_save.assert_called() instance_delete.assert_not_called() # Access Network Port should have tags to c-tag and s-tag port = Port.objects.first() self.assertEqual(port.instance, instance) self.assertEqual(port.network, self.priv_network) port_set_parameter.assert_has_calls([ mock.call(port, "c_tag", 111), mock.call(port, "s_tag", 222), mock.call(port, "neutron_port_name", "stag-222") ]) # The instance should be tagged with the s-tag tag = Tag.objects.get(name="s_tag") self.assertEqual(tag.value, "222") self.assertEqual(tag.object_id, instance.id) # The instance should have a tag pointing to its address_service_instance tag = Tag.objects.get(name="vm_vrouter_tenant") self.assertNotEqual(tag.value, vrtenant.id) self.assertEqual(tag.object_id, instance.id) # Allocate_public_service_instance should have been called get_psi.assert_called()
def setUp(self): global DeferredException self.sys_path_save = sys.path sys.path.append(xos_dir) sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) # Setting up the config module from xosconfig import Config config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml") Config.clear() Config.init(config, "synchronizer-config-schema.yaml") # END Setting up the config module from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor build_mock_modelaccessor(xos_dir, services_dir, [ get_models_fn("fabric-crossconnect", "fabric-crossconnect.xproto"), get_models_fn("onos-service", "onos.xproto"), ]) import synchronizers.new_base.mock_modelaccessor reload(synchronizers.new_base.mock_modelaccessor ) # in case nose2 loaded it in a previous test import synchronizers.new_base.modelaccessor reload(synchronizers.new_base.modelaccessor ) # in case nose2 loaded it in a previous test from synchronizers.new_base.modelaccessor import model_accessor from mock_modelaccessor import MockObjectList from kubernetes_event import KubernetesPodDetailsEventStep # import all class names to globals for (k, v) in model_accessor.all_model_classes.items(): globals()[k] = v self.event_step = KubernetesPodDetailsEventStep self.onos = ONOSService(name="myonos", id=1111, rest_hostname="onos-url", rest_port="8181", rest_username="******", rest_password="******", backend_code=1, backend_status="succeeded") self.fcservice = FabricCrossconnectService( name="myfcservice", id=1112, backend_code=1, backend_status="succeeded", provider_services=[self.onos]) self.fcsi1 = FabricCrossconnectServiceInstance( name="myfcsi1", owner=self.fcservice, backend_code=1, backend_status="succeeded") self.fcsi2 = FabricCrossconnectServiceInstance( name="myfcsi2", owner=self.fcservice, backend_code=1, backend_status="succeeded") self.fcservice.service_instances = MockObjectList( [self.fcsi1, self.fcsi2]) self.log = Mock()