def test_get_vm_details(self):
        # Arrange
        image = self.mock_image()
        flavor = self.mock_flavor()
        vm = self.mock_vm()
        self.instance_service.get_instance_from_instance_id = Mock(return_value=vm)
        self.instance_service.get_image = Mock(return_value=image)
        self.instance_service.get_flavor = Mock(return_value=flavor)

        network = {'id': '300881', 'name': 'network_name_for_test', 'provider:segmentation_id': '777'}

        mock_client = Mock()
        test_neutron_network_service.neutron_client.Client = Mock(return_value=mock_client)
        mock_client.list_networks = Mock(return_value={'networks': [network]})

        param = Mock()
        param.name = 'ip_regex'
        param.value = 'bla.*'

        request = Mock()
        request.deployedAppJson.name = 'OpenStack App for Tests'
        request.deployedAppJson.vmdetails.uid = '240975'
        request.deployedAppJson.vmdetails.vmCustomParams = [param]
        request.appRequestJson.deploymentService.model = 'OpenStack'
        request.appRequestJson.deploymentService.attributes = [Mock()]

        cancellation_context = Mock(is_cancelled=False)
        management_vlan_id = Mock()

        # Act
        result = self.vm_details_operation.get_vm_details(openstack_session=self.openstack_session,
                                                          requests=[request],
                                                          cancellation_context=cancellation_context,
                                                          logger=Mock(),
                                                          management_vlan_id=management_vlan_id)

        # Assert
        self.assertEqual(len(result), 1)
        vm_details = result[0]
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'Image'), 'Image Name')
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'Flavour'), 'Flavor Name')
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'Availability Zone'), 'Av Zone')
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'Disk Size'), '320 GB')
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'Memory'), '256 GB')
        self.assertEqual(next(x.value for x in vm_details.vmInstanceData if x.key == 'CPU'), '2 vCPU')

        self.assertEqual(len(vm_details.vmNetworkData), 1)
        nic = vm_details.vmNetworkData[0]
        self.assertEqual(nic.isPredefined, False)
        self.assertEqual(nic.networkId, '777')

        self.assertEqual(len(nic.networkData), 3)
        self.assertEqual(nic.privateIpAddress, '192.168.1.1')
        self.assertEqual(next(x.value for x in nic.networkData if x.key == 'IP'), '192.168.1.1')
        self.assertEqual(next(x.value for x in nic.networkData if x.key == 'VLAN Name'), 'network_name_for_test')
    def test_get_network_with_segmentation_id_no_network(self):
        test_segmentation_id = '42'
        mock_client = Mock()
        test_neutron_network_service.neutron_client.Client = Mock(return_value=mock_client)

        mock_client.list_networks = Mock(return_value={'networks': []})

        result = self.network_service.get_network_with_segmentation_id(openstack_session=self.openstack_session,
                                                                       segmentation_id=test_segmentation_id,
                                                                       logger=self.mock_logger)
        self.assertEqual(result, None)
    def test_validate_mgmt_network(self):
        test_net_client = Mock()

        net_list = {'networks': [{'router:external': False}]}
        test_net_client.list_networks = Mock(return_value=net_list)

        test_mgmt_network_id = 'test_mgmt_net'
        result = self.cp_validator.validate_mgmt_network(net_client=test_net_client,
                                                         mgmt_network_id=test_mgmt_network_id,
                                                         logger=self.mock_logger)

        self.assertTrue(result)
    def test_external_network_empty(self):
        test_net_client = Mock()

        net_list = {'networks': []}
        test_net_client.list_networks = Mock(return_value=net_list)

        test_floating_ip_subnet_id = ''
        with self.assertRaises(ValueError) as context:
            result = self.cp_validator.validate_floating_ip_subnet(net_client=test_net_client,
                                                                   floating_ip_subnet_id=test_floating_ip_subnet_id,
                                                                   logger=self.mock_logger)

        self.assertTrue(context)
    def test_mgmt_network_duplicate(self):
        test_net_client = Mock()

        net_list = {'networks': [{}, {}]}
        test_net_client.list_networks = Mock(return_value=net_list)

        test_mgmt_network_id = 'test_mgmt_net'
        with self.assertRaises(ValueError) as context:
            result = self.cp_validator.validate_mgmt_network(net_client=test_net_client,
                                                             mgmt_network_id=test_mgmt_network_id,
                                                             logger=self.mock_logger)

        self.assertTrue(context)
    def test_create_or_get_network_with_segmentation_id_conflict(self):

        test_segmentation_id = '42'
        mock_client = Mock()
        test_neutron_network_service.neutron_client.Client = Mock(return_value=mock_client)
        mock_client.create_network = Mock(side_effect=test_neutron_network_service.NetCreateConflict)
        mock_client.list_networks = Mock(return_value={'networks': ['test_network']})

        result = self.network_service.create_or_get_network_with_segmentation_id(openstack_session=self.openstack_session,
                                                                                 segmentation_id=test_segmentation_id,
                                                                                 cp_resource_model=self.moc_cp_model,
                                                                                 logger=self.mock_logger)

        self.assertEqual(result, 'test_network')
    def test_external_network_not_external(self):
        test_net_client = Mock()

        mock_subnet_return_value = {'subnet': {'network_id': 'test_network_id'}}
        test_net_client.show_subnet = Mock(return_value=mock_subnet_return_value)

        net_list = {'networks': [{'router:external': False}]}
        test_net_client.list_networks = Mock(return_value=net_list)

        test_floating_ip_subnet_id = 'test_floating_ip_subnet_id'
        with self.assertRaises(ValueError) as context:
            result = self.cp_validator.validate_floating_ip_subnet(net_client=test_net_client,
                                                                   floating_ip_subnet_id=test_floating_ip_subnet_id,
                                                                   logger=self.mock_logger)

        self.assertTrue(context)
    def test_validate_floating_ip_subnet(self):
        test_net_client = Mock()

        test_network_id = 'test_network_id'
        mock_subnet_result = {'subnet': {'network_id': test_network_id}}
        test_net_client.show_subnet = Mock(return_value=mock_subnet_result)

        test_floating_ip_subnet_id = 'test_floating_ip_subnet'

        net_list = {'networks': [{'router:external': True}]}
        test_net_client.list_networks = Mock(return_value=net_list)

        result = self.cp_validator.validate_floating_ip_subnet(net_client=test_net_client,
                                                               floating_ip_subnet_id=test_floating_ip_subnet_id,
                                                               logger=self.mock_logger)

        self.assertTrue(result)