private GenerateReadmesCommand InitializeCommand(
            TempFolderContext tempFolderContext,
            string readmeTemplate       = ReadmeTemplate,
            string productFamilyReadme  = DefaultReadme,
            string repoReadme           = DefaultReadme,
            bool allowOptionalTemplates = true,
            bool validate = false)
        {
            DockerfileHelper.CreateFile(ProductFamilyReadmePath, tempFolderContext, productFamilyReadme);
            DockerfileHelper.CreateFile(RepoReadmePath, tempFolderContext, repoReadme);

            DockerfileHelper.CreateFile(AboutRepoTemplatePath, tempFolderContext, AboutRepoTemplate);

            string templatePath = null;

            if (readmeTemplate != null)
            {
                DockerfileHelper.CreateFile(ReadmeTemplatePath, tempFolderContext, readmeTemplate);
                templatePath = ReadmeTemplatePath;
            }

            Repo repo = CreateRepo("dotnet/repo");

            repo.Readmes = new[]
            {
                new Readme(RepoReadmePath, templatePath)
            };
            Manifest manifest = CreateManifest(repo);

            manifest.Registry = "mcr.microsoft.com";
            manifest.Readme   = new(ProductFamilyReadmePath, templatePath);

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            Mock <IGitService> gitServiceMock = new Mock <IGitService>();

            _environmentServiceMock = new Mock <IEnvironmentService>();
            _environmentServiceMock
            .Setup(o => o.Exit(1))
            .Throws(_exitException);

            GenerateReadmesCommand command = new GenerateReadmesCommand(_environmentServiceMock.Object, gitServiceMock.Object);

            command.Options.Manifest = manifestPath;
            command.Options.AllowOptionalTemplates = allowOptionalTemplates;
            command.Options.Validate = validate;
            command.LoadManifest();

            return(command);
        }
Exemplo n.º 2
0
        private GenerateDockerfilesCommand InitializeCommand(
            TempFolderContext tempFolderContext,
            string dockerfileTemplate = DefaultDockerfileTemplate,
            string dockerfile         = DefaultDockerfile,
            bool validate             = false)
        {
            DockerfileHelper.CreateFile(DockerfileTemplatePath, tempFolderContext, dockerfileTemplate);
            DockerfileHelper.CreateFile(DockerfilePath, tempFolderContext, dockerfile);

            Manifest manifest = CreateManifest(
                CreateRepo("repo1",
                           CreateImage(
                               CreatePlatform(
                                   DockerfilePath,
                                   new string[] { "tag1" },
                                   OS.Linux,
                                   "buster-slim",
                                   Architecture.ARM,
                                   "v7",
                                   dockerfileTemplatePath: DockerfileTemplatePath),
                               CreatePlatform(
                                   DockerfilePath,
                                   new string[] { "tag2" },
                                   OS.Windows,
                                   "nanoserver-1903"),
                               CreatePlatform(
                                   DockerfilePath,
                                   new string[] { "tag3" },
                                   OS.Linux,
                                   "alpine3.12")))
                );

            AddVariable(manifest, "Variable1", "Value1");

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            _environmentServiceMock = new Mock <IEnvironmentService>();
            _environmentServiceMock
            .Setup(o => o.Exit(1))
            .Throws(_exitException);

            GenerateDockerfilesCommand command = new GenerateDockerfilesCommand(_environmentServiceMock.Object);

            command.Options.Manifest = manifestPath;
            command.Options.Validate = validate;
            command.LoadManifest();

            return(command);
        }
Exemplo n.º 3
0
        public async Task GenerateDockerfilesCommand_MismatchedTemplates()
        {
            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            DockerfileHelper.CreateFile(DockerfilePath, tempFolderContext, DefaultDockerfile);

            string templatePath1 = "Dockerfile.Template1";

            DockerfileHelper.CreateFile(templatePath1, tempFolderContext, DefaultDockerfileTemplate);

            string templatePath2 = "Dockerfile.Template2";

            DockerfileHelper.CreateFile(templatePath2, tempFolderContext, DefaultDockerfileTemplate);

            Manifest manifest = CreateManifest(
                CreateRepo("repo1",
                           CreateImage(
                               new Platform[]
            {
                CreatePlatform(
                    DockerfilePath,
                    new string[] { "tag1" },
                    OS.Windows,
                    "nanoserver-1903",
                    dockerfileTemplatePath: templatePath1),
                CreatePlatform(
                    DockerfilePath,
                    new string[] { "tag2" },
                    OS.Windows,
                    "windowsservercore-1903",
                    dockerfileTemplatePath: templatePath2)
            },
                               productVersion: "1.2.3"
                               )
                           )
                );

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            GenerateDockerfilesCommand command = new(Mock.Of <IEnvironmentService>());

            command.Options.Manifest = manifestPath;
            command.LoadManifest();

            InvalidOperationException exception = await Assert.ThrowsAsync <InvalidOperationException>(() => command.ExecuteAsync());

            Assert.StartsWith("Multiple unique template files are associated with the generated artifact path", exception.Message);
        }
        public async Task GenerateReadmesCommand_TemplateArgs()
        {
            const string readmeTemplate =
                @"Hello World
{{InsertTemplate(""template-with-args.md"", [ ""my-arg"": 123 ])}}";

            const string templateWithArgs =
                @"ABC-{{ARGS[""my-arg""]}}";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            DockerfileHelper.CreateFile("template-with-args.md", tempFolderContext, templateWithArgs);
            GenerateReadmesCommand command = InitializeCommand(tempFolderContext, readmeTemplate);

            await command.ExecuteAsync();

            string generatedReadme = File.ReadAllText(Path.Combine(tempFolderContext.Path, ProductFamilyReadmePath));
            string expectedReadme  =
                @"Hello World
ABC-123";

            Assert.Equal(expectedReadme, generatedReadme);
        }
        public async Task ExcludeProductFamilyReadme()
        {
            Mock <IGitHubClient> gitHubClientMock = new();

            gitHubClientMock
            .Setup(o => o.GetReferenceAsync(It.IsAny <GitHubProject>(), It.IsAny <string>()))
            .ReturnsAsync(new GitReference
            {
                Object = new GitReferenceObject()
            });

            gitHubClientMock
            .Setup(o => o.PostTreeAsync(It.IsAny <GitHubProject>(), It.IsAny <string>(), It.IsAny <GitObject[]>()))
            .ReturnsAsync(new GitTree());

            gitHubClientMock
            .Setup(o => o.PostCommitAsync(It.IsAny <GitHubProject>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string[]>()))
            .ReturnsAsync(new GitCommit());

            Mock <IGitHubClientFactory> gitHubClientFactoryMock = new();

            gitHubClientFactoryMock
            .Setup(o => o.GetClient(It.IsAny <GitHubAuth>(), false))
            .Returns(gitHubClientMock.Object);

            PublishMcrDocsCommand command = new(Mock.Of <IGitService>(), gitHubClientFactoryMock.Object, Mock.Of <ILoggerService>());

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();

            DockerfileHelper.CreateFile(ProductFamilyReadmePath, tempFolderContext, DefaultReadme);
            DockerfileHelper.CreateFile(RepoReadmePath, tempFolderContext, DefaultReadme);
            DockerfileHelper.CreateFile(AboutRepoTemplatePath, tempFolderContext, AboutRepoTemplate);
            DockerfileHelper.CreateFile(ReadmeTemplatePath, tempFolderContext, ReadmeTemplate);

            // Create MCR tags metadata template file
            StringBuilder tagsMetadataTemplateBuilder = new();

            tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:repo)");
            tagsMetadataTemplateBuilder.Append($"$(McrTagsYmlTagGroup:tag)");
            string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, TagsYamlPath);

            File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString());

            Repo     repo;
            Manifest manifest = CreateManifest(
                repo = CreateRepo("dotnet/repo", new Image[]
            {
                CreateImage(
                    CreatePlatform(CreateDockerfile("1.0/runtime/linux", tempFolderContext), new string[] { "tag" }))
            }, RepoReadmePath, ReadmeTemplatePath, Path.GetFileName(tagsMetadataTemplatePath)));

            manifest.Registry = "mcr.microsoft.com";
            manifest.Readme   = new(ProductFamilyReadmePath, ReadmeTemplatePath);
            repo.Id           = "repo";

            string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json");

            File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest));

            command.Options.Manifest = manifestPath;
            command.Options.ExcludeProductFamilyReadme = true;
            command.LoadManifest();

            await command.ExecuteAsync();

            // Verify published file list does not contain ProductFamilyReadmePath
            gitHubClientMock
            .Verify(o =>
                    o.PostTreeAsync(It.IsAny <GitHubProject>(), It.IsAny <string>(),
                                    It.Is <GitObject[]>(objs =>
                                                        objs.Length == 2 &&
                                                        Path.GetFileName(objs[0].Path) == RepoReadmePath &&
                                                        Path.GetFileName(objs[1].Path) == TagsYamlPath)));
        }