Esempio n. 1
0
        internal void Deploy(
            [Required, Description("Location where loose source files should be drawn from.")] string sourceRoot,
            [Required, Description("Target directory for deployment.")] string targetDirectory,
            [Required, Description("Location of deployment manifest json file")] string deploymentConfigPath,
            [Required, Description("Location of drop.exe to run to download drops")] string dropExePath,
            [Required, Description("Personal access token to use to authenticate to drop service")] string dropToken,
            [Description("Maximum size of files to retain"), DefaultValue(50)] int retentionSizeGb)
        {
            try
            {
                Initialize();

                _consoleLog.CurrentSeverity = Interfaces.Logging.Severity.Debug;
                var deploymentRoot = new AbsolutePath(targetDirectory);

                var deploymentRunner = new DeploymentIngester(
                    context: new OperationContext(new Context(_logger)),
                    sourceRoot: new AbsolutePath(sourceRoot),
                    deploymentRoot: deploymentRoot,
                    deploymentConfigurationPath: new AbsolutePath(deploymentConfigPath),
                    fileSystem: _fileSystem,
                    retentionSizeGb: retentionSizeGb,
                    dropExeFilePath: new AbsolutePath(dropExePath),
                    dropToken: dropToken
                    );

                deploymentRunner.RunAsync().GetAwaiter().GetResult().ThrowIfFailure();
            }
            catch (Exception e)
            {
                _logger.Error(e.ToString());
                throw;
            }
        }
        public async Task TestFullDeployment()
        {
            var sources = new Dictionary <string, string>()
            {
                { @"Stamp3\info.txt", "" },

                { @"Env\RootFile.json", "{ 'key1': 1, 'key2': 2 }" },
                { @"Env\Subfolder\Hello.txt", "Hello world" },
                { @"Env\Foo.txt", "Baz" },

                { @"Files\Foo.txt", "Bar" },
            };

            Dictionary <string, string> getSourceDrop(string root, string prefix)
            {
                return(sources.Where(e => e.Key.StartsWith(root))
                       .ToDictionary(e => e.Key.Substring(prefix.Length), e => e.Value));
            }

            var drops = new Dictionary <string, Dictionary <string, string> >()
            {
                {
                    "https://dev.azure.com/buildxlcachetest/drop/drops/dev/testdrop1?root=release/win-x64",
                    new Dictionary <string, string>
                    {
                        { @"file1.bin", "File content 1" },
                        { @"file2.txt", "File content 2" },
                        { @"sub\file3.dll", "File content 3" }
                    }
                },
                {
                    "https://dev.azure.com/buildxlcachetest/drop/drops/dev/testdrop2?root=debug",
                    new Dictionary <string, string>
                    {
                        { @"file1.bin", "File content 1" },
                        { @"file2.txt", "File content 2 changed" },
                        { @"sub\file5.dll", "File content 5" }
                    }
                },
                {
                    DeploymentUtilities.ConfigDropUri.OriginalString,
                    new Dictionary <string, string>()
                    {
                        { DeploymentUtilities.DeploymentConfigurationFileName, ConfigString }
                    }
                },
                {
                    "file://Env", getSourceDrop(@"Env\", @"Env\")
                },
                {
                    "file://Files/Foo.txt", getSourceDrop(@"Files\Foo.txt", @"Files\")
                },
                {
                    "file://Env/Foo.txt", getSourceDrop(@"Env\Foo.txt", @"Env\")
                },
                {
                    "file://Stamp3", getSourceDrop(@"Stamp3\", @"Stamp3\")
                }
            };

            var deploymentRoot = TestRootDirectoryPath / "deploy";
            var ingester       = new DeploymentIngester(
                Context,
                sourceRoot: base.TestRootDirectoryPath / "src",
                deploymentRoot: deploymentRoot,
                deploymentConfigurationPath: base.TestRootDirectoryPath / "DeploymentConfiguration.json",
                FileSystem,
                dropExeFilePath: base.TestRootDirectoryPath / @"dropbin\drop.exe",
                retentionSizeGb: 1,
                dropToken: DropToken);

            // Write source files
            WriteFiles(ingester.SourceRoot, sources);

            FileSystem.WriteAllText(ingester.DeploymentConfigurationPath, ConfigString);

            ingester.OverrideLaunchDropProcess = t =>
            {
                var dropContents = drops[t.dropUrl];
                WriteFiles(new AbsolutePath(t.targetDirectory) / (t.relativeRoot ?? ""), dropContents);
                return(BoolResult.Success);
            };

            await ingester.RunAsync().ShouldBeSuccess();

            var manifestText       = FileSystem.ReadAllText(ingester.DeploymentManifestPath);
            var deploymentManifest = JsonSerializer.Deserialize <DeploymentManifest>(manifestText);

            foreach (var drop in drops)
            {
                var uri = new Uri(drop.Key);
                var expectedDropContents = drops[drop.Key];
                var layoutSpec           = deploymentManifest.Drops[drop.Key];
                layoutSpec.Count.Should().Be(expectedDropContents.Count);
                foreach (var fileAndContent in expectedDropContents)
                {
                    var hash         = new ContentHash(layoutSpec[fileAndContent.Key].Hash);
                    var expectedPath = ingester.DeploymentRoot / DeploymentUtilities.GetContentRelativePath(hash);

                    var text = FileSystem.ReadAllText(expectedPath);
                    text.Should().Be(fileAndContent.Value);
                }
            }

            var clock             = new MemoryClock();
            var deploymentService = new DeploymentService(new DeploymentServiceConfiguration(), deploymentRoot, _ => new TestSecretsProvider(), clock);

            BoxRef <Task> uploadFileCompletion = Task.CompletedTask;

            deploymentService.OverrideCreateCentralStorage = t => new DeploymentTestCentralStorage(t.storageSecretName)
            {
                UploadFileCompletion = uploadFileCompletion
            };

            await verifyLaunchManifestAsync(new DeploymentParameters()
            {
                Stamp = "ST_S3",
                Ring  = "Ring_1"
            },
                                            new HashSet <(string targetPath, string drop)>()
            {
                ("bin", "https://dev.azure.com/buildxlcachetest/drop/drops/dev/testdrop2?root=debug"),
                ("", "file://Env"),
                ("info", "file://Stamp3"),
            });

            async Task <LauncherManifest> verifyLaunchManifestAsync(DeploymentParameters parameters, HashSet <(string targetPath, string drop)> expectedDrops)
            {
                var launchManifest = await deploymentService.UploadFilesAndGetManifestAsync(Context, parameters, waitForCompletion : true);

                var expectedDeploymentPathToHashMap = new Dictionary <string, string>();

                launchManifest.Drops.Count.Should().Be(expectedDrops.Count);
                foreach (var drop in launchManifest.Drops)
                {
                    var targetRelativePath = drop.TargetRelativePath ?? string.Empty;
                    expectedDrops.Should().Contain((targetRelativePath, drop.Url));

                    var dropSpec = deploymentManifest.Drops[drop.Url];
                    foreach (var dropFile in drops[drop.Url])
                    {
                        expectedDeploymentPathToHashMap[Path.Combine(targetRelativePath, dropFile.Key)] = dropSpec[dropFile.Key].Hash;
                    }
                }

                launchManifest.Deployment.Count.Should().Be(expectedDeploymentPathToHashMap.Count);

                foreach (var file in launchManifest.Deployment)
                {
                    var expectedHash = expectedDeploymentPathToHashMap[file.Key];
                    file.Value.Hash.Should().Be(expectedHash);
                }

                return(launchManifest);
            }
        }