Exemplo n.º 1
0
        public void ShouldFindMavenPackageAlreadyUploaded()
        {
            var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(mavenPackageId, VersionFactory.CreateMavenVersion(packageVersion), ".jar"));

            File.Copy(mavenPackage, destinationFilePath);

            var result = FindPackages(mavenPackageId, packageVersion, mavenPackageHash, VersionFormat.Maven);

            result.AssertSuccess();
            result.AssertServiceMessage(
                ServiceMessageNames.CalamariFoundPackage.Name,
                Is.True,
                message: "Expected service message '{0}' to be True",
                args: ServiceMessageNames.CalamariFoundPackage.Name);

            result.AssertOutput(
                "Package {0} {1} hash {2} has already been uploaded",
                mavenPackageId,
                packageVersion,
                mavenPackageHash);

            var foundPackage = result.CapturedOutput.FoundPackage;

            Assert.AreEqual(VersionFactory.CreateMavenVersion(packageVersion), foundPackage.Version);
            Assert.AreEqual(mavenPackageHash, foundPackage.Hash);
            Assert.AreEqual(destinationFilePath, foundPackage.RemotePath);
            Assert.AreEqual(".jar", foundPackage.FileExtension);
            Assert.AreEqual(mavenPackageId, foundPackage.PackageId);
        }
Exemplo n.º 2
0
        private string CreateEmptyFile(string version)
        {
            var destinationPath = Path.Combine(PackagePath, PackageName.ToCachedFileName("Acme.Web", new SemanticVersion(version), ".nupkg"));

            File.WriteAllText(destinationPath, "FAKESTUFF");
            return(destinationPath);
        }
Exemplo n.º 3
0
        PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri,
                                                  ICredentials feedCredentials, string cacheDirectory)
        {
            var cred = feedCredentials.GetCredential(feedUri, "basic");

            var tempDirectory = fileSystem.CreateTemporaryDirectory();

            using (new TemporaryDirectory(tempDirectory))
            {
                var homeDir = Path.Combine(tempDirectory, "helm");
                if (!Directory.Exists(homeDir))
                {
                    Directory.CreateDirectory(homeDir);
                }
                var stagingDir = Path.Combine(tempDirectory, "staging");
                if (!Directory.Exists(stagingDir))
                {
                    Directory.CreateDirectory(stagingDir);
                }

                var log = new LogWrapper();
                Invoke($"init --home \"{homeDir}\" --client-only", tempDirectory, log);
                Invoke($"repo add --home \"{homeDir}\" {(string.IsNullOrEmpty(cred.UserName) ? "" : $"--username \"{cred.UserName}\" --password \"{cred.Password}\"")} {TempRepoName} {feedUri.ToString()}", tempDirectory, log);
                Invoke($"fetch --home \"{homeDir}\"  --version \"{version}\" --destination \"{stagingDir}\" {TempRepoName}/{packageId}", tempDirectory, log);

                var localDownloadName =
                    Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

                fileSystem.MoveFile(Directory.GetFiles(stagingDir)[0], localDownloadName);
                return(PackagePhysicalFileMetadata.Build(localDownloadName));
            }
Exemplo n.º 4
0
        public void ShouldFindPackageAlreadyUploaded()
        {
            using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion)))
            {
                var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateSemanticVersion(packageVersion), ".nupkg"));
                File.Copy(acmeWeb.FilePath, destinationFilePath);

                var result = FindPackages(packageId, packageVersion, acmeWeb.Hash);

                result.AssertSuccess();
                result.AssertServiceMessage(
                    ServiceMessageNames.CalamariFoundPackage.Name,
                    Is.True,
                    message: "Expected service message '{0}' to be True",
                    args: ServiceMessageNames.CalamariFoundPackage.Name);

                result.AssertOutput("Package {0} {1} hash {2} has already been uploaded", packageId, packageVersion,
                                    acmeWeb.Hash);

                result.AssertServiceMessage(ServiceMessageNames.FoundPackage.Name, Is.True);
                var foundPackage = result.CapturedOutput.FoundPackage;
                Assert.AreEqual(VersionFactory.CreateSemanticVersion(packageVersion), foundPackage.Version);
                Assert.AreEqual(acmeWeb.Hash, foundPackage.Hash);
                Assert.AreEqual(destinationFilePath, foundPackage.RemotePath);
                Assert.AreEqual(".nupkg", foundPackage.FileExtension);
                Assert.AreEqual(packageId, foundPackage.PackageId);
            }
        }
Exemplo n.º 5
0
        public void ShouldFindTheCorrectMavenPackageWhenSimilarPackageExist()
        {
            var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(mavenPackageId, VersionFactory.CreateMavenVersion(packageVersion), ".jar"));

            File.Copy(mavenPackage, destinationFilePath);

            var destination2FilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(mavenPackageId + ".Test", VersionFactory.CreateMavenVersion(packageVersion), ".jar"));

            File.Copy(mavenPackage, destination2FilePath);

            var result = FindPackages(mavenPackageId, newpackageVersion, mavenPackageHash, VersionFormat.Maven);

            result.AssertSuccess();
            result.AssertOutput("Package {0} version {1} hash {2} has not been uploaded.",
                                mavenPackageId,
                                newpackageVersion,
                                mavenPackageHash);
            result.AssertOutput("Finding earlier packages that have been uploaded to this Tentacle");
            result.AssertOutput("Found 1 earlier version of {0} on this Tentacle", mavenPackageId);
            result.AssertOutput("  - {0}: {1}", packageVersion, destinationFilePath);

            result.AssertServiceMessage(ServiceMessageNames.FoundPackage.Name, Is.True);
            var foundPackage = result.CapturedOutput.FoundPackage;

            Assert.AreEqual(VersionFactory.CreateMavenVersion(packageVersion), foundPackage.Version);
            Assert.AreEqual(mavenPackageHash, foundPackage.Hash);
            Assert.AreEqual(destinationFilePath, foundPackage.RemotePath);
            Assert.AreEqual(".jar", foundPackage.FileExtension);
            Assert.AreEqual(mavenPackageId, foundPackage.PackageId);
        }
Exemplo n.º 6
0
        public void ShouldFindTheCorrectPackageWhenSimilarPackageExist()
        {
            using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion)))
                using (var acmeWebTest = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId + ".Tests", packageVersion)))
                {
                    var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateVersion(packageVersion, VersionFormat.Semver), ".nupkg"));
                    File.Copy(acmeWeb.FilePath, destinationFilePath);

                    var destinationFilePathTest = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId + ".Tests", VersionFactory.CreateVersion(packageVersion, VersionFormat.Semver), ".nupkg"));
                    File.Copy(acmeWebTest.FilePath, destinationFilePathTest);

                    using (var newAcmeWeb =
                               new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, newpackageVersion)))
                    {
                        var result = FindPackages(packageId, newpackageVersion, newAcmeWeb.Hash);

                        result.AssertSuccess();
                        result.AssertOutput("Package {0} version {1} hash {2} has not been uploaded.", packageId,
                                            newpackageVersion,
                                            newAcmeWeb.Hash);
                        result.AssertOutput("Finding earlier packages that have been uploaded to this Tentacle");
                        result.AssertOutput("Found 1 earlier version of {0} on this Tentacle", packageId);
                        result.AssertOutput("  - {0}: {1}", packageVersion, destinationFilePath);

                        result.AssertServiceMessage(ServiceMessageNames.FoundPackage.Name, Is.True);
                        var foundPackage = result.CapturedOutput.FoundPackage;
                        Assert.AreEqual(VersionFactory.CreateSemanticVersion(packageVersion), foundPackage.Version);
                        Assert.AreEqual(acmeWeb.Hash, foundPackage.Hash);
                        Assert.AreEqual(destinationFilePath, foundPackage.RemotePath);
                        Assert.AreEqual(".nupkg", foundPackage.FileExtension);
                        Assert.AreEqual(packageId, foundPackage.PackageId);
                    }
                }
        }
Exemplo n.º 7
0
        public void ToCachedFileName_Semver()
        {
            var filename = PackageName.ToCachedFileName("My/Package", VersionFactory.CreateSemanticVersion("12.32.1-meta+data"), ".zip");

            var scrubbedFilename = Regex.Replace(filename, "[0-9A-F]{32}", "<CACHE-GUID>");

            Assert.AreEqual("My%[email protected]+data@<CACHE-GUID>.zip", scrubbedFilename);
        }
Exemplo n.º 8
0
        public void ToCachedFileName_MavenEncodedChars()
        {
            var filename = PackageName.ToCachedFileName("My/Package", VersionFactory.CreateMavenVersion("12:8"), ".jar");

            var scrubbedFilename = Regex.Replace(filename, "[0-9A-F]{32}", "<CACHE-GUID>");

            Assert.AreEqual("My%2FPackage@M12%3A8@<CACHE-GUID>.jar", scrubbedFilename);
        }
        /// <summary>
        /// Actually download the maven file.
        /// </summary>
        /// <returns>The path to the downloaded file</returns>
        PackagePhysicalFileMetadata DownloadArtifact(
            MavenPackageID mavenGavFirst,
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff,
            XmlDocument?snapshotMetadata)
        {
            Guard.NotNull(mavenGavFirst, "mavenGavFirst can not be null");
            Guard.NotNullOrWhiteSpace(packageId, "packageId can not be null");
            Guard.NotNull(version, "version can not be null");
            Guard.NotNullOrWhiteSpace(cacheDirectory, "cacheDirectory can not be null");
            Guard.NotNull(feedUri, "feedUri can not be null");

            var localDownloadName = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, "." + mavenGavFirst.Packaging));
            var downloadUrl       = feedUri.ToString().TrimEnd('/') +
                                    (snapshotMetadata == null
                    ? mavenGavFirst.DefaultArtifactPath
                    : mavenGavFirst.SnapshotArtifactPath(GetLatestSnapshotRelease(
                                                             snapshotMetadata,
                                                             mavenGavFirst.Packaging,
                                                             mavenGavFirst.Classifier,
                                                             mavenGavFirst.Version)));

            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    Log.Verbose($"Downloading Attempt {downloadUrl} TO {localDownloadName}");
                    using (var client = new WebClient
                    {
                        Credentials = feedCredentials
                    })
                    {
                        client.DownloadFile(downloadUrl, localDownloadName);
                        var packagePhysicalFileMetadata = PackagePhysicalFileMetadata.Build(localDownloadName);
                        return(packagePhysicalFileMetadata
                               ?? throw new CommandException($"Unable to retrieve metadata for package {packageId}, version {version}"));
                    }
                }
                catch (Exception ex)
                {
                    if ((retry + 1) == maxDownloadAttempts)
                    {
                        throw new MavenDownloadException("Failed to download the Maven artifact.\r\nLast Exception Message: " + ex.Message);
                    }
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new MavenDownloadException("Failed to download the Maven artifact");
        }
Exemplo n.º 10
0
        PackagePhysicalFileMetadata DownloadFile(string uri,
                                                 string cacheDirectory,
                                                 string packageId,
                                                 IVersion version,
                                                 string?authorizationToken,
                                                 int maxDownloadAttempts,
                                                 TimeSpan downloadAttemptBackoff)
        {
            var localDownloadName =
                Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

            var tempPath = Path.GetTempFileName();

            if (File.Exists(tempPath))
            {
                File.Delete(tempPath);
            }

            for (var retry = 0; retry < maxDownloadAttempts; ++retry)
            {
                try
                {
                    if (retry != 0)
                    {
                        Log.Verbose($"Download Attempt #{retry + 1}");
                    }

                    using (var client = new WebClient())
                    {
                        client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
                        client.Headers.Set(HttpRequestHeader.UserAgent, GetUserAgent());
                        AddAuthorization(client, authorizationToken);
                        client.DownloadFileWithProgress(uri, tempPath, (progress, total) => log.Progress(progress, $"Downloading {packageId} v{version}"));

                        DeNestContents(tempPath, localDownloadName);
                        var packagePhysicalFileMetadata = PackagePhysicalFileMetadata.Build(localDownloadName);
                        return(packagePhysicalFileMetadata
                               ?? throw new CommandException($"Unable to retrieve metadata for package {packageId}, version {version}"));
                    }
                }
                catch (WebException)
                {
                    Thread.Sleep(downloadAttemptBackoff);
                }
            }

            throw new Exception("Failed to download the package.");
        }
Exemplo n.º 11
0
        private string CreatePackage(string version, bool oldCacheFormat = false)
        {
            var sourcePackage = PackageBuilder.BuildSamplePackage("Acme.Web", version, true);

            var destinationPath = Path.Combine(PackagePath, oldCacheFormat
                ? $"Acme.Web.{version}.nupkg-fd55edc5-9b36-414b-a2d0-4a2deeb6b2ec"
                : PackageName.ToCachedFileName("Acme.Web", new SemanticVersion(version), ".nupkg"));

            if (File.Exists(destinationPath))
            {
                File.Delete(destinationPath);
            }

            File.Move(sourcePackage, destinationPath);
            return(destinationPath);
        }
Exemplo n.º 12
0
        public void ShouldNotFindEarlierPackageVersionWhenExactMatchRequested()
        {
            using (var acmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, packageVersion)))
            {
                var destinationFilePath = Path.Combine(downloadPath, PackageName.ToCachedFileName(packageId, VersionFactory.CreateSemanticVersion(packageVersion), ".nupkg"));
                File.Copy(acmeWeb.FilePath, destinationFilePath);

                using (var newAcmeWeb = new TemporaryFile(PackageBuilder.BuildSamplePackage(packageId, newpackageVersion)))
                {
                    var result = FindPackagesExact(packageId, newpackageVersion, newAcmeWeb.Hash, true);

                    result.AssertSuccess();
                    result.AssertOutput("Package {0} version {1} hash {2} has not been uploaded.", packageId, newpackageVersion,
                                        newAcmeWeb.Hash);
                    result.AssertNoOutput("Finding earlier packages that have been uploaded to this Tentacle");
                }
            }
        }
Exemplo n.º 13
0
        void ValidateParameters(out string basisFilePath, out string deltaFilePath, out string newFilePath)
        {
            Guard.NotNullOrWhiteSpace(basisFileName, "No basis file was specified. Please pass --basisFileName MyPackage.1.0.0.0.nupkg");
            Guard.NotNullOrWhiteSpace(fileHash, "No file hash was specified. Please pass --fileHash MyFileHash");
            Guard.NotNullOrWhiteSpace(deltaFileName, "No delta file was specified. Please pass --deltaFileName MyPackage.1.0.0.0_to_1.0.0.1.octodelta");
            Guard.NotNullOrWhiteSpace(newFileName, "No new file name was specified. Please pass --newFileName MyPackage.1.0.0.1.nupkg");

            basisFilePath = basisFileName;
            if (!File.Exists(basisFileName))
            {
                basisFilePath = Path.GetFullPath(basisFileName);
                if (!File.Exists(basisFilePath))
                {
                    throw new CommandException("Could not find basis file: " + basisFileName);
                }
            }

            deltaFilePath = deltaFileName;
            if (!File.Exists(deltaFileName))
            {
                deltaFilePath = Path.GetFullPath(deltaFileName);
                if (!File.Exists(deltaFilePath))
                {
                    throw new CommandException("Could not find delta file: " + deltaFileName);
                }
            }

            // Probably dont need to do this since the server appends a guid in the name... maybe it was originall put here in the name of safety?
            var newPackageDetails = PackageName.FromFile(newFileName);

            newFilePath = Path.Combine(PackageStore.GetPackagesDirectory(), PackageName.ToCachedFileName(newPackageDetails.PackageId, newPackageDetails.Version, newPackageDetails.Extension));
            var hash = HashCalculator.Hash(basisFileName);

            if (hash != fileHash)
            {
                throw new CommandException($"Basis file hash `{hash}` does not match the file hash specified `{fileHash}`");
            }
        }
Exemplo n.º 14
0
        private PackagePhysicalFileMetadata DownloadChart(string packageId, IVersion version, Uri feedUri,
                                                          ICredentials feedCredentials, string cacheDirectory)
        {
            var cred = feedCredentials.GetCredential(feedUri, "basic");

            var syntax = new[] { ScriptSyntax.PowerShell, ScriptSyntax.Bash }.First(syntx =>
                                                                                    scriptEngine.GetSupportedTypes().Contains(syntx));

            var tempDirectory = fileSystem.CreateTemporaryDirectory();

            using (new TemporaryDirectory(tempDirectory))
            {
                var file   = GetFetchScript(tempDirectory, syntax);
                var result = scriptEngine.Execute(new Script(file), new CalamariVariableDictionary()
                {
                    ["Password"] = cred.Password,
                    ["Username"] = cred.UserName,
                    ["Version"]  = version.OriginalString,
                    ["Url"]      = feedUri.ToString(),
                    ["Package"]  = packageId,
                }, commandLineRunner,
                                                  new Dictionary <string, string>());
                if (!result.HasErrors)
                {
                    var localDownloadName =
                        Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, Extension));

                    var packageFile = fileSystem.EnumerateFiles(Path.Combine(tempDirectory, "staging")).First();
                    fileSystem.MoveFile(packageFile, localDownloadName);
                    return(PackagePhysicalFileMetadata.Build(localDownloadName));
                }
                else
                {
                    throw new Exception("Unable to download chart");
                }
            }
        }
Exemplo n.º 15
0
        private PackagePhysicalFileMetadata DownloadPackage(
            string packageId,
            IVersion version,
            Uri feedUri,
            ICredentials feedCredentials,
            string cacheDirectory,
            int maxDownloadAttempts,
            TimeSpan downloadAttemptBackoff)
        {
            Log.Info("Downloading NuGet package {0} v{1} from feed: '{2}'", packageId, version, feedUri);
            Log.VerboseFormat("Downloaded package will be stored in: '{0}'", cacheDirectory);
            fileSystem.EnsureDiskHasEnoughFreeSpace(cacheDirectory);

            var fullPathToDownloadTo = Path.Combine(cacheDirectory, PackageName.ToCachedFileName(packageId, version, ".nupkg"));

            var downloader = new InternalNuGetPackageDownloader(fileSystem);

            downloader.DownloadPackage(packageId, version, feedUri, feedCredentials, fullPathToDownloadTo, maxDownloadAttempts, downloadAttemptBackoff);

            var pkg = PackagePhysicalFileMetadata.Build(fullPathToDownloadTo);

            CheckWhetherThePackageHasDependencies(pkg);
            return(pkg);
        }