Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSI-5277/ add identity servicer #658

Open
wants to merge 5 commits into
base: task/CSI-5277_organize_directories
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import grpc

from csi_general import identity_pb2 as pb2
from csi_general import identity_pb2_grpc as pb2_grpc

from controllers.common.config import config as common_config
from controllers.servers.csi.decorators import csi_method
from controllers.servers.csi.exception_handler import build_error_response
from controllers.common.csi_logger import get_stdout_logger

logger = get_stdout_logger()


class IdentityControllerServicer(pb2_grpc.IdentityServicer):

@csi_method(error_response_type=pb2.GetIdentityResponse)
def GetIdentity(self, request, context):
name = common_config.identity.name
version = common_config.identity.version

if not name or not version:
message = "plugin name or version cannot be empty"
return build_error_response(message, context, grpc.StatusCode.INTERNAL, pb2.GetIdentityResponse)

return pb2.GetIdentityResponse(name=name, vendor_version=version)

def GetCapabilities(self, request, context):
logger.info("GetCapabilities")
response = pb2.GetCapabilitiesResponse(
capabilities=[self._get_replication_capability(),
self._get_controller_capability()])

logger.info("finished GetCapabilities")
return response

def _get_replication_capability(self):
types = pb2.Capability.VolumeReplication.Type
capability_enum_value = types.Value("VOLUME_REPLICATION")
return pb2.Capability(
volume_replication=pb2.Capability.VolumeReplication(type=capability_enum_value))

def _get_controller_capability(self):
types = pb2.Capability.Service.Type
capability_enum_value = types.Value("CONTROLLER_SERVICE")
return pb2.Capability(
service=pb2.Capability.Service(type=capability_enum_value))

def Probe(self, request, context):
context.set_code(grpc.StatusCode.OK)
return pb2.ProbeResponse()
3 changes: 3 additions & 0 deletions controllers/servers/csi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from controllers.servers.csi.controller_server.csi_controller_server import CSIControllerServicer
from controllers.servers.csi.controller_server.volume_group_server import VolumeGroupControllerServicer
from controllers.servers.csi.csi_addons_server.replication_controller_servicer import ReplicationControllerServicer
from controllers.servers.csi.csi_addons_server.identity_controller_servicer import IdentityControllerServicer


def main():
Expand Down Expand Up @@ -54,7 +55,9 @@ def _add_csi_controller_servicers(controller_server):

def _add_csi_addons_servicers(csi_addons_server):
replication_servicer = ReplicationControllerServicer()
identity_servicer = IdentityControllerServicer()
replication_pb2_grpc.add_ControllerServicer_to_server(replication_servicer, csi_addons_server)
identity_pb2_grpc.add_IdentityServicer_to_server(identity_servicer, csi_addons_server)
return csi_addons_server


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest
import grpc
from mock import patch, Mock, call

from csi_general import identity_pb2 as pb2
from controllers.servers.csi.csi_addons_server.identity_controller_servicer import IdentityControllerServicer


class TestIdentityControllerServicer(unittest.TestCase):
def setUp(self):
self.servicer = IdentityControllerServicer()
self.request = Mock()
self.context = Mock()

@patch("controllers.common.config.config.identity")
def test_get_identity_succeeds(self, identity_config):
plugin_name = "plugin-name"
version = "1.1.0"
identity_config.name = plugin_name
identity_config.version = version
self.request.volume_capabilities = []
response = self.servicer.GetIdentity(self.request, self.context)
self.assertEqual(response, pb2.GetIdentityResponse(name=plugin_name, vendor_version=version))

@patch("controllers.common.config.config.identity")
def test_get_identity_fails_when_attributes_from_config_are_missing(self, identity_config):
identity_config.mock_add_spec(spec=["name"])
response = self.servicer.GetIdentity(self.request, self.context)
self.context.set_code.assert_called_once_with(grpc.StatusCode.INTERNAL)
self.assertEqual(response, pb2.GetIdentityResponse())

identity_config.mock_add_spec(spec=["version"])
response = self.servicer.GetIdentity(self.request, self.context)
self.assertEqual(response, pb2.GetIdentityResponse())
self.context.set_code.assert_called_with(grpc.StatusCode.INTERNAL)

@patch("controllers.common.config.config.identity")
def test_get_identity_fails_when_name_or_version_are_empty(self, identity_config):
identity_config.name = ""
identity_config.version = "1.1.0"
response = self.servicer.GetIdentity(self.request, self.context)
self.context.set_code.assert_called_once_with(grpc.StatusCode.INTERNAL)
self.assertEqual(response, pb2.GetIdentityResponse())

identity_config.name = "name"
identity_config.version = ""
response = self.servicer.GetIdentity(self.request, self.context)
self.assertEqual(response, pb2.GetIdentityResponse())
self.assertEqual(self.context.set_code.call_args_list,
[call(grpc.StatusCode.INTERNAL), call(grpc.StatusCode.INTERNAL)])

def test_get_capabilities_succeeds(self):
response = self.servicer.GetCapabilities(self.request, self.context)
supported_capabilities = 2
self.assertIn('VolumeReplication', dir(response.capabilities[0]))
self.assertIn('Service', dir(response.capabilities[1]))
self.assertEqual(len(response.capabilities), supported_capabilities)

def test_probe_succeeds(self):
response = self.servicer.Probe(self.request, self.context)
self.assertEqual(response, pb2.ProbeResponse())
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
from controllers.servers.csi.csi_addons_server.replication_controller_servicer import ReplicationControllerServicer
from controllers.tests import utils
from controllers.tests.common.test_settings import VOLUME_NAME, VOLUME_UID, OBJECT_INTERNAL_ID, \
OTHER_OBJECT_INTERNAL_ID, REPLICATION_NAME, SYSTEM_ID, COPY_TYPE, SECRET_USERNAME_VALUE, SECRET_PASSWORD_VALUE, \
SECRET_MANAGEMENT_ADDRESS_VALUE, DUMMY_VOLUME_GROUP
OTHER_OBJECT_INTERNAL_ID, REPLICATION_NAME, SYSTEM_ID, COPY_TYPE, SECRET, DUMMY_VOLUME_GROUP
from controllers.tests.controller_server.common import mock_get_agent
from controllers.tests.controller_server.controller_server.csi_controller_server_test import (CommonControllerTest)
from controllers.tests.utils import ProtoBufMock

ADDON_SERVER_PATH = "controllers.servers.csi.csi_addons_server.replication_controller_servicer"
REPLICATION_SERVICER_PATH = "controllers.servers.csi.csi_addons_server.replication_controller_servicer"


class BaseReplicationSetUp(unittest.TestCase):
Expand All @@ -27,11 +26,10 @@ def setUp(self):
self.mediator.client = Mock()

self.storage_agent = MagicMock()
mock_get_agent(self, ADDON_SERVER_PATH)
mock_get_agent(self, REPLICATION_SERVICER_PATH)

self.request = ProtoBufMock()
self.request.secrets = {"username": SECRET_USERNAME_VALUE, "password": SECRET_PASSWORD_VALUE,
"management_address": SECRET_MANAGEMENT_ADDRESS_VALUE}
self.request.secrets = SECRET
self.request.volume_id = "{0}:{1};{1}".format("A9000", OBJECT_INTERNAL_ID)
self.request.replication_id = "{}:{};{}".format("A9000", OTHER_OBJECT_INTERNAL_ID, VOLUME_UID)
self.request.replication_source.volumegroup.volume_group_id = self.request.volume_id
Expand Down