Example #1
0
    def test_get_service_interfaces(self, mock_get_setting, mock_list_objects_v2, mock_get_object):

        import api.service_directory as target
        # reload(target)

        # create interface objects for both hosts a and b
        object_map = {
            target.ServiceDirectory.S3_INTERFACE_KEY.format(
                deployment_name = self.TEST_DEPLOYMENT_NAME,
                interface_id = self.TEST_INTERFACE_ID(version),
                interface_url = self.TEST_INTERFACE_URL_ENCODED(host, version)
            ) : mock_aws.s3_get_object_response(self.TEST_INTERFACE_SWAGGER_STRING(host, version))
            for host, version in itertools.product(self.TEST_HOSTS, self.TEST_INTERFACE_VERSIONS)
        }
        mock_list_objects_v2.return_value = { 'Contents': [ { 'Key': key } for key in object_map.keys() ] }
        mock_get_object.side_effect = lambda **kwargs: object_map[kwargs['Key']]

        # request interfaces for host a
        result = target.get_service_interfaces(
            self.TEST_REQUEST, 
            self.TEST_DEPLOYMENT_NAME, 
            self.TEST_SERVICE_URL(self.HOST_A)
        )

        # got interfaces for host a and only host a?
        self.assertEquals(result,
            {
                "Items": AnyOrderListMatcher(
                    [ 
                        { 
                            'InterfaceId': self.TEST_INTERFACE_ID(version), 
                            'InterfaceUrl': self.TEST_INTERFACE_URL(self.HOST_A, version),
                            'InterfaceSwagger': self.TEST_INTERFACE_SWAGGER_STRING(self.HOST_A, version) 
                        } for version in self.TEST_INTERFACE_VERSIONS
                    ]
                )
            }
        )

        # used deployment name when listing objects?
        mock_list_objects_v2.assert_called_once_with(
            Bucket = self.TEST_BUCKET_NAME,
            Prefix = target.ServiceDirectory.S3_INTERFACE_PREFIX.format(deployment_name = self.TEST_DEPLOYMENT_NAME)
        )

        # requested correct interface objects?
        for version in self.TEST_INTERFACE_VERSIONS:
            mock_get_object.assert_any_call(
                Bucket = self.TEST_BUCKET_NAME, 
                Key = target.ServiceDirectory.S3_INTERFACE_KEY.format(
                    deployment_name = self.TEST_DEPLOYMENT_NAME,
                    interface_id = self.TEST_INTERFACE_ID(version),
                    interface_url = self.TEST_INTERFACE_URL_ENCODED(self.HOST_A, version)
                )
            )
        self.assertEquals(mock_get_object.call_count, len(self.TEST_INTERFACE_VERSIONS))

        # loaded bucket name from settings?
        mock_get_setting.assert_called_once_with('ProjectConfigurationBucket')
    def test_get_input_swagger_content(self, *args):
        # Setup
        Custom_ServiceApi.s3.get_object.return_value = mock_aws.s3_get_object_response(
            self.INPUT_SWAGGER_CONTENT.encode())

        # Execute
        result = Custom_ServiceApi.get_input_swagger_content(self.PROPS)

        # Verify
        Custom_ServiceApi.s3.get_object.assert_called_once_with(
            Bucket=CONFIGURATION_BUCKET, Key=INPUT_SWAGGER_KEY)

        self.assertEqual(self.INPUT_SWAGGER_CONTENT, result)
Example #3
0
    def test_get_interface_services_with_incompatible_interface(self, mock_get_setting, mock_get_object, mock_list_objects_v2):

        import api.service_directory as target
        # reload(target)

        # add keys for all interfaces all hosts
        mock_list_objects_v2.return_value = { 
            'Contents': [ 
                { 
                    'Key': target.ServiceDirectory.S3_INTERFACE_KEY.format(
                        deployment_name = self.TEST_DEPLOYMENT_NAME,
                        interface_id = self.TEST_INTERFACE_ID(version),
                        interface_url = self.TEST_INTERFACE_URL_ENCODED(host, version)
                    ) 
                }  for host, version in itertools.product(self.TEST_HOSTS, self.TEST_INTERFACE_VERSIONS)
            ]
        }

        expected_interface_versions = [self.TEST_INCOMPATIBLE_INTERFACE_VERSION]

        mock_get_object.side_effect = [ 
             mock_aws.s3_get_object_response(self.TEST_INTERFACE_SWAGGER_STRING(host, version))
                 for host, version in itertools.product(self.TEST_HOSTS, expected_interface_versions)
        ]

        # request list for incompatible interface version
        result = target.get_interface_services(
            self.TEST_REQUEST,
            self.TEST_DEPLOYMENT_NAME,
            self.TEST_INTERFACE_ID(self.TEST_INCOMPATIBLE_INTERFACE_VERSION)
        )

        # verify list includes incompatible version for both hosts
        
        expected = {
            "Items": AnyOrderListMatcher(
                [
                    {
                        "InterfaceId": self.TEST_INTERFACE_ID(version),
                        "InterfaceUrl": self.TEST_INTERFACE_URL(host, version),
                        "InterfaceSwagger": self.TEST_INTERFACE_SWAGGER_STRING(host, version)
                    } for host, version in itertools.product(self.TEST_HOSTS, expected_interface_versions)
                ]
            )
        }

        self.assertEquals(result, expected)


        # used deployment name when listing objects?
        mock_list_objects_v2.assert_called_once_with(
            Bucket = self.TEST_BUCKET_NAME,
            Prefix = target.ServiceDirectory.S3_INTERFACE_ID_PREFIX.format(
                deployment_name = self.TEST_DEPLOYMENT_NAME,
                interface_id = self.TEST_INTERFACE_ID(self.TEST_INCOMPATIBLE_INTERFACE_MAJOR_VERSION)
            )
        )

        # got swagger content from s3?
        mock_get_object.assert_has_calls(
            [
                mock.call(
                    Bucket = self.TEST_BUCKET_NAME, 
                    Key = target.ServiceDirectory.S3_INTERFACE_KEY.format(
                        deployment_name = self.TEST_DEPLOYMENT_NAME,
                        interface_id = self.TEST_INTERFACE_ID(version),
                        interface_url = self.TEST_INTERFACE_URL_ENCODED(host, version)
                    )
                )
                for host, version in itertools.product(self.TEST_HOSTS, expected_interface_versions )
            ]
        )

        # loaded bucket name from settings?
        mock_get_setting.assert_called_once_with('ProjectConfigurationBucket')