Example #1
0
        public void AllTypesDependingOnIFileStorageServiceAreReturnedByGetStorageDependents()
        {
            // Arrange
            var config = GetConfiguration();

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var actual   = new HashSet <Type>(dependents.Select(x => x.ImplementationType));
            var allTypes = typeof(DefaultDependenciesModule).Assembly.GetTypes();
            var expected = new HashSet <Type>();

            foreach (var type in allTypes)
            {
                var constructors = type.GetConstructors();
                foreach (var constructor in constructors)
                {
                    var parameters = constructor.GetParameters();
                    if (parameters.Any(p => p.ParameterType == typeof(IFileStorageService)))
                    {
                        expected.Add(type);
                    }
                }
            }

            Assert.Subset(expected, actual);
            Assert.Subset(actual, expected);
        }
Example #2
0
        public void StorageDependentsHaveExpectedTypes()
        {
            // Arrange
            var config = GetConfiguration();

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var implementationToInterface = dependents.ToDictionary(x => x.ImplementationType, x => x.InterfaceType);

            Assert.Contains(typeof(CertificateService), implementationToInterface.Keys);
            Assert.Contains(typeof(ContentService), implementationToInterface.Keys);
            Assert.Contains(typeof(PackageFileService), implementationToInterface.Keys);
            Assert.Contains(typeof(SymbolPackageFileService), implementationToInterface.Keys);
            Assert.Contains(typeof(UploadFileService), implementationToInterface.Keys);
            Assert.Contains(typeof(CoreLicenseFileService), implementationToInterface.Keys);
            Assert.Contains(typeof(RevalidationStateService), implementationToInterface.Keys);
            Assert.Equal(7, implementationToInterface.Count);
            Assert.Equal(typeof(ICertificateService), implementationToInterface[typeof(CertificateService)]);
            Assert.Equal(typeof(IContentService), implementationToInterface[typeof(ContentService)]);
            Assert.Equal(typeof(IPackageFileService), implementationToInterface[typeof(PackageFileService)]);
            Assert.Equal(typeof(ISymbolPackageFileService), implementationToInterface[typeof(SymbolPackageFileService)]);
            Assert.Equal(typeof(IUploadFileService), implementationToInterface[typeof(UploadFileService)]);
            Assert.Equal(typeof(ICoreLicenseFileService), implementationToInterface[typeof(CoreLicenseFileService)]);
            Assert.Equal(typeof(IRevalidationStateService), implementationToInterface[typeof(RevalidationStateService)]);
        }
        private static void ConfigureForLocalFileSystem(ContainerBuilder builder, IGalleryConfigurationService configuration)
        {
            builder.RegisterType <FileSystemService>()
            .AsSelf()
            .As <IFileSystemService>()
            .SingleInstance();

            builder.RegisterType <FileSystemFileStorageService>()
            .AsSelf()
            .As <IFileStorageService>()
            .As <ICoreFileStorageService>()
            .SingleInstance();

            foreach (var dependent in StorageDependent.GetAll(configuration.Current))
            {
                var registration = builder.RegisterType(dependent.ImplementationType)
                                   .AsSelf()
                                   .As(dependent.InterfaceType);

                if (dependent.IsSingleInstance)
                {
                    registration.SingleInstance();
                }
                else
                {
                    registration.InstancePerLifetimeScope();
                }
            }

            builder.RegisterInstance(NullReportService.Instance)
            .AsSelf()
            .As <IReportService>()
            .SingleInstance();

            builder.RegisterInstance(NullStatisticsService.Instance)
            .AsSelf()
            .As <IStatisticsService>()
            .SingleInstance();

            // If we're not using azure storage, then aggregate stats comes from SQL
            builder.RegisterType <SqlAggregateStatsService>()
            .AsSelf()
            .As <IAggregateStatsService>()
            .InstancePerLifetimeScope();

            builder.RegisterInstance(new SqlErrorLog(configuration.Current.SqlConnectionString))
            .As <ErrorLog>()
            .SingleInstance();

            builder.RegisterType <GalleryContentFileMetadataService>()
            .As <IContentFileMetadataService>()
            .SingleInstance();
        }
        public void StorageDependentsUseCorrectConnectionString()
        {
            // Arrange
            var config = GetConfiguration();

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var typeToConnectionString = dependents.ToDictionary(x => x.ImplementationType, x => x.AzureStorageConnectionString);

            Assert.Equal(typeToConnectionString[typeof(ContentService)], config.AzureStorage_Content_ConnectionString);
            Assert.Equal(typeToConnectionString[typeof(PackageFileService)], config.AzureStorage_Packages_ConnectionString);
            Assert.Equal(typeToConnectionString[typeof(UploadFileService)], config.AzureStorage_Uploads_ConnectionString);
        }
        public void StorageDependentsAreGroupedByConnectionString()
        {
            // Arrange
            var mock = new Mock <IAppConfiguration>();

            mock.Setup(x => x.AzureStorage_Content_ConnectionString).Returns("Content");
            mock.Setup(x => x.AzureStorage_Packages_ConnectionString).Returns("Packages and Uploads");
            mock.Setup(x => x.AzureStorage_Uploads_ConnectionString).Returns("Packages and Uploads");
            var config = mock.Object;

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var typeToBindingKey = dependents.ToDictionary(x => x.ImplementationType, x => x.BindingKey);

            Assert.Equal(typeToBindingKey[typeof(PackageFileService)], typeToBindingKey[typeof(UploadFileService)]);
        }
Example #6
0
        public void AllTypesDependingOnIFileStorageServiceAreReturnedByGetStorageDependents()
        {
            // Arrange
            var config                 = GetConfiguration();
            var allGalleryTypes        = typeof(DefaultDependenciesModule).Assembly.GetTypes();
            var allGalleryAndCoreTypes = allGalleryTypes.Concat(typeof(ICoreFileStorageService).Assembly.GetTypes());

            Assert.True(typeof(ICoreFileStorageService).IsAssignableFrom(typeof(IFileStorageService)));

            // classes in Gallery and Gallery.Core that depend on ICoreFileStorageService (or,
            // transitively, on IFileStorageService since the latter implements the former)
            var fileStorageDependents = new HashSet <Type>(allGalleryAndCoreTypes
                                                           .Where(gct => gct
                                                                  .GetConstructors()
                                                                  .Any(c => c
                                                                       .GetParameters()
                                                                       .Any(pi => typeof(ICoreFileStorageService).IsAssignableFrom(pi.ParameterType)))));

            var fileStorageDependentsInterfaces = new HashSet <Type>(fileStorageDependents.SelectMany(t => t.GetInterfaces()));

            // file storage dependents that are actually used by Gallery types.
            var fileStorageDependentsInterfacesUsedByGallery = new HashSet <Type>(allGalleryTypes
                                                                                  .SelectMany(gt => gt
                                                                                              .GetConstructors()
                                                                                              .SelectMany(c => c
                                                                                                          .GetParameters()
                                                                                                          .Where(pi => fileStorageDependentsInterfaces.Contains(pi.ParameterType))
                                                                                                          .Select(pi => pi.ParameterType))));

            var expected = new HashSet <Type>(fileStorageDependents
                                              .Where(t => fileStorageDependentsInterfacesUsedByGallery.Any(i => i.IsAssignableFrom(t))));

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var actual = new HashSet <Type>(dependents.Select(x => x.ImplementationType));

            Assert.Subset(expected, actual);
            Assert.Subset(actual, expected);
        }
Example #7
0
        public void StorageDependentsHaveExpectedTypes()
        {
            // Arrange
            var config = GetConfiguration();

            // Act
            var dependents = StorageDependent.GetAll(config);

            // Assert
            var implementationToInterface = dependents.ToDictionary(x => x.ImplementationType, x => x.InterfaceType);

            Assert.Contains(typeof(ContentService), implementationToInterface.Keys);
            Assert.Contains(typeof(NuGetExeDownloaderService), implementationToInterface.Keys);
            Assert.Contains(typeof(PackageFileService), implementationToInterface.Keys);
            Assert.Contains(typeof(UploadFileService), implementationToInterface.Keys);
            Assert.Equal(4, implementationToInterface.Count);
            Assert.Equal(implementationToInterface[typeof(ContentService)], typeof(IContentService));
            Assert.Equal(implementationToInterface[typeof(NuGetExeDownloaderService)], typeof(INuGetExeDownloaderService));
            Assert.Equal(implementationToInterface[typeof(PackageFileService)], typeof(IPackageFileService));
            Assert.Equal(implementationToInterface[typeof(UploadFileService)], typeof(IUploadFileService));
        }
        private static void ConfigureForAzureStorage(ContainerBuilder builder, IGalleryConfigurationService configuration, ITelemetryClient telemetryClient)
        {
            /// The goal here is to initialize a <see cref="ICloudBlobClient"/> and <see cref="IFileStorageService"/>
            /// instance for each unique connection string. Each dependent of <see cref="IFileStorageService"/> (that
            /// is, each service that has a <see cref="IFileStorageService"/> constructor parameter) is registered in
            /// <see cref="StorageDependent.GetAll(IAppConfiguration)"/> and is grouped by the respective storage
            /// connection string. Each group is given a binding key which refers to the appropriate instance of the
            /// <see cref="IFileStorageService"/>.
            var completedBindingKeys = new HashSet <string>();

            foreach (var dependent in StorageDependent.GetAll(configuration.Current))
            {
                if (completedBindingKeys.Add(dependent.BindingKey))
                {
                    builder.RegisterInstance(new CloudBlobClientWrapper(dependent.AzureStorageConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
                    .AsSelf()
                    .As <ICloudBlobClient>()
                    .SingleInstance()
                    .Keyed <ICloudBlobClient>(dependent.BindingKey);

                    builder.RegisterType <CloudBlobFileStorageService>()
                    .WithParameter(new ResolvedParameter(
                                       (pi, ctx) => pi.ParameterType == typeof(ICloudBlobClient),
                                       (pi, ctx) => ctx.ResolveKeyed <ICloudBlobClient>(dependent.BindingKey)))
                    .AsSelf()
                    .As <IFileStorageService>()
                    .As <ICloudStorageStatusDependency>()
                    .SingleInstance()
                    .Keyed <IFileStorageService>(dependent.BindingKey);
                }

                var registration = builder.RegisterType(dependent.ImplementationType)
                                   .WithParameter(new ResolvedParameter(
                                                      (pi, ctx) => pi.ParameterType == typeof(IFileStorageService),
                                                      (pi, ctx) => ctx.ResolveKeyed <IFileStorageService>(dependent.BindingKey)))
                                   .AsSelf()
                                   .As(dependent.InterfaceType);

                if (dependent.IsSingleInstance)
                {
                    registration.SingleInstance();
                }
                else
                {
                    registration.InstancePerLifetimeScope();
                }
            }

            // when running on Windows Azure, we use a back-end job to calculate stats totals and store in the blobs
            builder.RegisterInstance(new JsonAggregateStatsService(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
            .AsSelf()
            .As <IAggregateStatsService>()
            .SingleInstance();

            // when running on Windows Azure, pull the statistics from the warehouse via storage
            builder.RegisterInstance(new CloudReportService(configuration.Current.AzureStorage_Statistics_ConnectionString, configuration.Current.AzureStorageReadAccessGeoRedundant))
            .AsSelf()
            .As <IReportService>()
            .As <ICloudStorageStatusDependency>()
            .SingleInstance();

            // when running on Windows Azure, download counts come from the downloads.v1.json blob
            var downloadCountService = new CloudDownloadCountService(
                telemetryClient,
                configuration.Current.AzureStorage_Statistics_ConnectionString,
                configuration.Current.AzureStorageReadAccessGeoRedundant);

            builder.RegisterInstance(downloadCountService)
            .AsSelf()
            .As <IDownloadCountService>()
            .SingleInstance();
            ObjectMaterializedInterception.AddInterceptor(new DownloadCountObjectMaterializedInterceptor(downloadCountService));

            builder.RegisterType <JsonStatisticsService>()
            .AsSelf()
            .As <IStatisticsService>()
            .SingleInstance();

            builder.RegisterInstance(new TableErrorLog(configuration.Current.AzureStorage_Errors_ConnectionString))
            .As <ErrorLog>()
            .SingleInstance();
        }