public async Task GivenACatalogVerifyPackagesShownFromList()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var beforeDate      = DateTimeOffset.UtcNow;
                        var catalogLog      = new TestLogger();
                        var log             = new TestLogger();
                        var baseUri         = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder      = Path.Combine(workingDir, "feed");
                        var nupkgsFolder    = Path.Combine(workingDir, "nupkgs");
                        var nupkgsOutFolder = Path.Combine(workingDir, "nupkgsout");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);
                        Directory.CreateDirectory(nupkgsOutFolder);

                        var packageA = new TestNupkg("a", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, catalogLog);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        var args     = new string[] { "list", feedUri.AbsoluteUri };
                        var exitCode = await NuGetMirror.Program.MainCore(args, httpSource, log);

                        exitCode.Should().Be(0);

                        log.GetMessages().Should().Contain("a 1.0.0");
                    }
        }
Exemplo n.º 2
0
        public async Task VerifyStartTimeIsExclusiveAndEndTimeIsInclusive()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log          = new TestLogger();
                        var baseUri      = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder   = Path.Combine(workingDir, "feed");
                        var nupkgsFolder = Path.Combine(workingDir, "nupkgs");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);

                        const int packageCount = 10;
                        await CatalogReaderTestHelpers.CreateCatalogAsync(
                            workingDir,
                            feedFolder,
                            nupkgsFolder,
                            baseUri,
                            catalogPageSize : 2,
                            log : log);

                        foreach (var i in Enumerable.Range(0, packageCount))
                        {
                            var nupkgFolder = Path.Combine(nupkgsFolder, i.ToString());
                            TestNupkg.Save(nupkgFolder, new TestNupkg($"Package{i}", "1.0.0"));
                            await CatalogReaderTestHelpers.PushPackagesAsync(workingDir, nupkgFolder, baseUri, log);
                        }

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var allEntries = await catalogReader.GetEntriesAsync();

                            var timestamps = allEntries
                                             .OrderBy(x => x.CommitTimeStamp)
                                             .Select(x => x.CommitTimeStamp)
                                             .ToList();

                            var start = timestamps[2];
                            var end   = timestamps[packageCount - 3];

                            // Act
                            var entries = await catalogReader.GetEntriesAsync(start, end, CancellationToken.None);

                            // Assert
                            Assert.Equal(
                                timestamps.Skip(3).Take(5),
                                entries.Select(x => x.CommitTimeStamp));
                            Assert.Equal(packageCount, timestamps.Distinct().Count());
                        }
                    }
        }
        public async Task VerifyEditsAreIgnoredInFlattenedViewAsync()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log          = new TestLogger();
                        var baseUri      = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder   = Path.Combine(workingDir, "feed");
                        var nupkgsFolder = Path.Combine(workingDir, "nupkgs");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);

                        var packageA = new TestNupkg("a", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        // Create and push
                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, log);

                        // 2nd push
                        await CatalogReaderTestHelpers.PushPackagesAsync(workingDir, nupkgsFolder, baseUri, log);

                        // 3rd push
                        await CatalogReaderTestHelpers.PushPackagesAsync(workingDir, nupkgsFolder, baseUri, log);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        // Act
                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var entries = await catalogReader.GetEntriesAsync();

                            var flatEntries = await catalogReader.GetFlattenedEntriesAsync();

                            var set = await catalogReader.GetPackageSetAsync();

                            var entry = entries.FirstOrDefault();

                            // Assert
                            // 3 adds, 2 removes
                            Assert.Equal(5, entries.Count);
                            Assert.Equal(1, flatEntries.Count);
                            Assert.Equal(1, set.Count);

                            Assert.Equal("a", entry.Id);
                            Assert.Equal("1.0.0", entry.Version.ToNormalizedString());
                        }
                    }
        }
        public async Task VerifyCatalogEntryPropertiesAsync()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log          = new TestLogger();
                        var baseUri      = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder   = Path.Combine(workingDir, "feed");
                        var nupkgsFolder = Path.Combine(workingDir, "nupkgs");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);

                        var packageA = new TestNupkg("a", "1.0.0.1-RC.1.2.b0.1+meta.blah.1");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        // Create and push
                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, log);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        // Act
                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var entries = await catalogReader.GetEntriesAsync();

                            var entry = entries.FirstOrDefault();

                            // Assert
                            Assert.Equal("a", entry.Id);
                            Assert.Equal("1.0.0.1-RC.1.2.b0.1", entry.Version.ToNormalizedString());
                            Assert.NotEmpty(entry.CommitId);
                            Assert.True(DateTimeOffset.MinValue < entry.CommitTimeStamp);
                            Assert.Equal("a.1.0.0.1-rc.1.2.b0.1", entry.FileBaseName);
                            Assert.True(entry.IsAddOrUpdate);
                            Assert.False(entry.IsDelete);
                            Assert.True(await entry.IsListedAsync());
                            Assert.Equal("https://localhost:8080/testFeed/flatcontainer/a/1.0.0.1-rc.1.2.b0.1/a.1.0.0.1-rc.1.2.b0.1.nupkg", entry.NupkgUri.AbsoluteUri);
                            Assert.Equal("https://localhost:8080/testFeed/flatcontainer/a/1.0.0.1-rc.1.2.b0.1/a.nuspec", entry.NuspecUri.AbsoluteUri);
                            Assert.Equal("https://localhost:8080/testFeed/flatcontainer/a/index.json", entry.PackageBaseAddressIndexUri.AbsoluteUri);
                            Assert.Equal("https://localhost:8080/testFeed/registration/a/1.0.0.1-rc.1.2.b0.1.json", entry.PackageRegistrationUri.AbsoluteUri);
                            Assert.Equal("https://localhost:8080/testFeed/registration/a/index.json", entry.RegistrationIndexUri.AbsoluteUri);
                            Assert.Equal("nuget:PackageDetails", string.Join("|", entry.Types));
                            Assert.StartsWith("https://localhost:8080/testFeed/catalog/data/", entry.Uri.AbsoluteUri);
                        }
                    }
        }
        public async Task GivenALargeNumberOfPackagesVerifyAllAreDownloaded()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var catalogLog      = new TestLogger();
                        var log             = new TestLogger();
                        var baseUri         = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder      = Path.Combine(workingDir, "feed");
                        var nupkgsFolder    = Path.Combine(workingDir, "nupkgs");
                        var nupkgsOutFolder = Path.Combine(workingDir, "nupkgsout");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);
                        Directory.CreateDirectory(nupkgsOutFolder);

                        var expected = new HashSet <PackageIdentity>();

                        for (var i = 0; i < 200; i++)
                        {
                            var identity = new PackageIdentity(Guid.NewGuid().ToString(), NuGetVersion.Parse($"{i}.0.0"));

                            if (expected.Add(identity))
                            {
                                var package = new TestNupkg(identity.Id, identity.Version.ToNormalizedString());
                                TestNupkg.Save(nupkgsFolder, package);
                            }
                        }

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, catalogLog);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        var args     = new string[] { "nupkgs", "-o", nupkgsOutFolder, feedUri.AbsoluteUri, "--delay", "0" };
                        var exitCode = await NuGetMirror.Program.MainCore(args, httpSource, log);

                        var errors = log.GetMessages(LogLevel.Error);
                        exitCode.Should().Be(0, errors);

                        var results = LocalFolderUtility.GetPackagesV3(nupkgsOutFolder, catalogLog).ToList();

                        results.Select(e => e.Identity).ShouldBeEquivalentTo(expected);

                        var errorLog = Path.Combine(nupkgsOutFolder, "lastRunErrors.txt");
                        File.Exists(errorLog).Should().BeFalse();
                    }
        }
        public async Task GivenLatestOnlyOptionVerifyDownloadsOnlyLatest()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var beforeDate      = DateTimeOffset.UtcNow;
                        var catalogLog      = new TestLogger();
                        var log             = new TestLogger();
                        var baseUri         = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder      = Path.Combine(workingDir, "feed");
                        var nupkgsFolder    = Path.Combine(workingDir, "nupkgs");
                        var nupkgsOutFolder = Path.Combine(workingDir, "nupkgsout");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);
                        Directory.CreateDirectory(nupkgsOutFolder);

                        var packageA1 = new TestNupkg("a", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA1);
                        var packageA2 = new TestNupkg("a", "2.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA2);

                        var packageB1 = new TestNupkg("b", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageB1);
                        var packageB2 = new TestNupkg("b", "2.0.0");
                        TestNupkg.Save(nupkgsFolder, packageB2);

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, catalogLog);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        var args     = new string[] { "nupkgs", "-o", nupkgsOutFolder, feedUri.AbsoluteUri, "--delay", "0", "--latest-only" };
                        var exitCode = await NuGetMirror.Program.MainCore(args, httpSource, log);

                        exitCode.Should().Be(0);

                        var results = LocalFolderUtility.GetPackagesV3(nupkgsOutFolder, catalogLog).ToList();

                        results.Select(e => e.Identity).ShouldBeEquivalentTo(
                            new[] {
                            new PackageIdentity("a", NuGetVersion.Parse("2.0.0")),
                            new PackageIdentity("b", NuGetVersion.Parse("2.0.0"))
                        });
                    }
        }
        public async Task VerifyPackagesAreDownloadedInV3Structure()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var beforeDate      = DateTimeOffset.UtcNow;
                        var catalogLog      = new TestLogger();
                        var log             = new TestLogger();
                        var baseUri         = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder      = Path.Combine(workingDir, "feed");
                        var nupkgsFolder    = Path.Combine(workingDir, "nupkgs");
                        var nupkgsOutFolder = Path.Combine(workingDir, "nupkgsout");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);
                        Directory.CreateDirectory(nupkgsOutFolder);

                        var packageA = new TestNupkg("a", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, catalogLog);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        var args     = new string[] { "nupkgs", "-o", nupkgsOutFolder, feedUri.AbsoluteUri, "--delay", "0" };
                        var exitCode = await NuGetMirror.Program.MainCore(args, httpSource, log);

                        exitCode.Should().Be(0);

                        var results = LocalFolderUtility.GetPackagesV3(nupkgsOutFolder, catalogLog).ToList();

                        results.Select(e => e.Identity).ShouldBeEquivalentTo(new[] { new PackageIdentity("a", NuGetVersion.Parse("1.0.0")) });

                        var afterDate = DateTimeOffset.UtcNow;
                        var cursor    = MirrorUtility.LoadCursor(new DirectoryInfo(nupkgsOutFolder));

                        (cursor <= afterDate && cursor >= beforeDate).Should().BeTrue("the cursor should match the catalog");

                        var errorLog = Path.Combine(nupkgsOutFolder, "lastRunErrors.txt");
                        File.Exists(errorLog).Should().BeFalse();
                    }
        }
        public async Task GetCatalogEntryVerifyUrlsCanBeOpenedAsJsonAsync()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log          = new TestLogger();
                        var baseUri      = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder   = Path.Combine(workingDir, "feed");
                        var nupkgsFolder = Path.Combine(workingDir, "nupkgs");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);

                        var packageA = new TestNupkg("a", "1.0.0.1-RC.1.2.b0.1+meta.blah.1");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        // Create and push
                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, log);

                        var feedUri    = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");
                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        // Act
                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var entries = await catalogReader.GetEntriesAsync();

                            var entry = entries.FirstOrDefault();

                            // Assert
                            (await entry.GetNupkgAsync()).Should().NotBeNull();
                            (await entry.GetNupkgAsync()).Should().NotBeNull();
                            (await entry.GetNuspecAsync()).Should().NotBeNull();
                            (await entry.GetPackageBaseAddressIndexUriAsync()).Should().NotBeNull();
                            (await entry.GetPackageDetailsAsync()).Should().NotBeNull();
                            (await entry.GetPackageRegistrationUriAsync()).Should().NotBeNull();
                            (await entry.GetRegistrationIndexUriAsync()).Should().NotBeNull();
                        }
                    }
        }
        public async Task VerifyNoEntriesWhenReadingAnEmptyCatalogAsync()
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log          = new TestLogger();
                        var baseUri      = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder   = Path.Combine(workingDir, "feed");
                        var nupkgsFolder = Path.Combine(workingDir, "nupkgs");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, log);

                        var feedUri = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");

                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        // Act
                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var entries = await catalogReader.GetEntriesAsync();

                            var flatEntries = await catalogReader.GetFlattenedEntriesAsync();

                            var set = await catalogReader.GetPackageSetAsync();

                            // Assert
                            Assert.Empty(entries);
                            Assert.Empty(flatEntries);
                            Assert.Empty(set);
                        }
                    }
        }
        private static async Task VerifyDownloadMode(
            Func <string, CatalogEntry, Task> actAndAssertAsync)
        {
            // Arrange
            using (var cache = new LocalCache())
                using (var cacheContext = new SourceCacheContext())
                    using (var workingDir = new TestFolder())
                    {
                        var log            = new TestLogger();
                        var baseUri        = Sleet.UriUtility.CreateUri("https://localhost:8080/testFeed/");
                        var feedFolder     = Path.Combine(workingDir, "feed");
                        var nupkgsFolder   = Path.Combine(workingDir, "nupkgs");
                        var downloadFolder = Path.Combine(workingDir, "download");
                        Directory.CreateDirectory(feedFolder);
                        Directory.CreateDirectory(nupkgsFolder);
                        Directory.CreateDirectory(downloadFolder);

                        var packageA = new TestNupkg("a", "1.0.0");
                        TestNupkg.Save(nupkgsFolder, packageA);

                        await CatalogReaderTestHelpers.CreateCatalogAsync(workingDir, feedFolder, nupkgsFolder, baseUri, log);

                        var feedUri = Sleet.UriUtility.CreateUri(baseUri.AbsoluteUri + "index.json");

                        var httpSource = CatalogReaderTestHelpers.GetHttpSource(cache, feedFolder, baseUri);

                        // Act
                        using (var catalogReader = new CatalogReader(feedUri, httpSource, cacheContext, TimeSpan.FromMinutes(1), log))
                        {
                            var entries = await catalogReader.GetEntriesAsync();

                            var entry = entries.FirstOrDefault();
                            await actAndAssertAsync(downloadFolder, entry);
                        }
                    }
        }