def test_get_nova_flavor(self):
    self.patch_object(api_crud, 'get_nova_client')
    self.patch_object(api_crud, 'nova_client')
    self.patch_object(api_crud, 'session_from_identity_service')
    self.patch_object(api_crud, 'keystone_exceptions')
    nova = mock.MagicMock()
    self.get_nova_client.return_value = nova
    flavor = mock.MagicMock()
    flavor.id = 'fake-id'
    flavor.name = 'charm-octavia'
    nova.flavors.list.return_value = [flavor]
    self.keystone_exceptions.catalog.EndpointNotFound = Exception
    self.keystone_exceptions.connection.ConnectFailure = Exception
    self.nova_client.exceptions.ClientException = Exception
    nova.flavors.list.side_effect = Exception
    identity_service = mock.MagicMock()
    with self.assertRaises(api_crud.APIUnavailable):
        api_crud.get_nova_flavor(identity_service)
    nova.flavors.list.side_effect = None
    api_crud.get_nova_flavor(identity_service)
    nova.flavors.list.assert_called_with(is_public=False)
    self.assertFalse(nova.flavors.create.called)
    nova.flavors.list.return_value = []
    nova.flavors.create.return_value = flavor
    api_crud.get_nova_flavor(identity_service)
    nova.flavors.create.assert_called_with('charm-octavia', 1024, 1, 8, is_public=False)
def test_get_nova_flavor(self):
    self.patch_object(api_crud, 'get_nova_client')
    self.patch_object(api_crud, 'nova_client')
    self.patch_object(api_crud, 'session_from_identity_service')
    self.patch_object(api_crud, 'keystone_exceptions')
    nova = mock.MagicMock()
    self.get_nova_client.return_value = nova
    flavor = mock.MagicMock()
    flavor.id = 'fake-id'
    flavor.name = 'charm-octavia'
    nova.flavors.list.return_value = [flavor]
    self.keystone_exceptions.catalog.EndpointNotFound = Exception
    self.keystone_exceptions.connection.ConnectFailure = Exception
    self.nova_client.exceptions.ClientException = Exception
    nova.flavors.list.side_effect = Exception
    identity_service = mock.MagicMock()
    with self.assertRaises(api_crud.APIUnavailable):
        api_crud.get_nova_flavor(identity_service)
    nova.flavors.list.side_effect = None
    api_crud.get_nova_flavor(identity_service)
    nova.flavors.list.assert_called_with(is_public=False)
    self.assertFalse(nova.flavors.create.called)
    nova.flavors.list.return_value = []
    nova.flavors.create.return_value = flavor
    api_crud.get_nova_flavor(identity_service)
    nova.flavors.create.assert_called_with('charm-octavia', 1024, 1, 8, is_public=False)